24
24
import android .os .Bundle ;
25
25
import android .window .BackEvent ;
26
26
import android .window .OnBackAnimationCallback ;
27
+ import android .window .OnBackInvokedCallback ;
27
28
import androidx .annotation .NonNull ;
28
29
import androidx .annotation .Nullable ;
29
30
import androidx .lifecycle .DefaultLifecycleObserver ;
@@ -125,8 +126,44 @@ public void itUnregistersOnBackInvokedCallbackOnRelease() {
125
126
}
126
127
127
128
@ Test
128
- @ TargetApi (34 )
129
- public void itHandlesOnBackAnimationCallbackAsExpected () {
129
+ @ Config (maxSdk = Build .VERSION_CODES .S_V2 )
130
+ public void onBackInvokedCallbackIsNullForSdk32OrLower () {
131
+ Intent intent = FlutterActivity .createDefaultIntent (ctx );
132
+ ActivityController <FlutterActivity > activityController =
133
+ Robolectric .buildActivity (FlutterActivity .class , intent );
134
+ FlutterActivity flutterActivity = activityController .get ();
135
+
136
+ flutterActivity .onCreate (null );
137
+
138
+ assertNull (
139
+ "onBackInvokedCallback should be null for SDK 32 or lower" ,
140
+ flutterActivity .onBackInvokedCallback );
141
+ }
142
+
143
+ @ Test
144
+ @ Config (sdk = Build .VERSION_CODES .TIRAMISU )
145
+ @ TargetApi (Build .VERSION_CODES .TIRAMISU )
146
+ public void onBackInvokedCallbackCallsOnBackPressedForSdk33 () {
147
+ Intent intent = FlutterActivityWithReportFullyDrawn .createDefaultIntent (ctx );
148
+ ActivityController <FlutterActivityWithReportFullyDrawn > activityController =
149
+ Robolectric .buildActivity (FlutterActivityWithReportFullyDrawn .class , intent );
150
+ FlutterActivityWithReportFullyDrawn activity = spy (activityController .get ());
151
+
152
+ activity .onCreate (null );
153
+
154
+ assertNotNull (
155
+ "onBackInvokedCallback should not be null for SDK 33" , activity .onBackInvokedCallback );
156
+
157
+ OnBackInvokedCallback callback = activity .onBackInvokedCallback ;
158
+
159
+ callback .onBackInvoked ();
160
+ verify (activity , times (1 )).onBackPressed ();
161
+ }
162
+
163
+ @ Test
164
+ @ Config (sdk = Build .VERSION_CODES .UPSIDE_DOWN_CAKE )
165
+ @ TargetApi (Build .VERSION_CODES .UPSIDE_DOWN_CAKE )
166
+ public void itHandlesOnBackAnimationCallbackAsExpectedForSdk34OrHigher () {
130
167
Intent intent = FlutterActivityWithReportFullyDrawn .createDefaultIntent (ctx );
131
168
ActivityController <FlutterActivityWithReportFullyDrawn > activityController =
132
169
Robolectric .buildActivity (FlutterActivityWithReportFullyDrawn .class , intent );
@@ -135,7 +172,7 @@ public void itHandlesOnBackAnimationCallbackAsExpected() {
135
172
activity .onCreate (null );
136
173
137
174
assertTrue (
138
- "onBackInvokedCallback should be an instance of OnBackAnimationCallback" ,
175
+ "onBackInvokedCallback should be an instance of OnBackAnimationCallback for SDK 34 or higher " ,
139
176
activity .onBackInvokedCallback instanceof OnBackAnimationCallback );
140
177
141
178
OnBackAnimationCallback callback = (OnBackAnimationCallback ) activity .onBackInvokedCallback ;
0 commit comments