Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ccc04d1

Browse files
committed
Tests
1 parent 13d02f1 commit ccc04d1

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterActivity.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
496496

497497
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
498498

499-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) {
500-
getOnBackInvokedDispatcher()
501-
.registerOnBackInvokedCallback(
502-
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
503-
}
499+
registerOnBackInvokedCallback();
504500

505501
configureWindowForTransparency();
506502

@@ -509,6 +505,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
509505
configureStatusBarForFullscreenFlutterExperience();
510506
}
511507

508+
@VisibleForTesting
509+
public void registerOnBackInvokedCallback() {
510+
if (Build.VERSION.SDK_INT >= 33) {
511+
getOnBackInvokedDispatcher()
512+
.registerOnBackInvokedCallback(
513+
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
514+
}
515+
}
516+
517+
@VisibleForTesting
518+
public void unregisterOnBackInvokedCallback() {
519+
if (Build.VERSION.SDK_INT >= 33) {
520+
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
521+
}
522+
}
523+
512524
private final OnBackInvokedCallback onBackInvokedCallback =
513525
new OnBackInvokedCallback() {
514526
@Override
@@ -671,9 +683,6 @@ protected void onPause() {
671683
@Override
672684
protected void onStop() {
673685
super.onStop();
674-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.T) {
675-
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
676-
}
677686
if (stillAttachedForEvent("onStop")) {
678687
delegate.onStop();
679688
}
@@ -699,6 +708,7 @@ protected void onSaveInstanceState(Bundle outState) {
699708
* <p>After calling, this activity should be disposed immediately and not be re-used.
700709
*/
701710
private void release() {
711+
unregisterOnBackInvokedCallback();
702712
if (delegate != null) {
703713
delegate.release();
704714
delegate = null;

shell/platform/android/test/io/flutter/embedding/android/FlutterActivityTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ public void flutterViewHasId() {
8888
assertTrue(activity.findViewById(FlutterActivity.FLUTTER_VIEW_ID) instanceof FlutterView);
8989
}
9090

91+
// TODO(garyq): Robolectric does not yet support android api 33 yet. Switch to a robolectric
92+
// test that directly exercises the OnBackInvoked APIs when API 33 is supported.
93+
@Test
94+
@TargetApi(33)
95+
public void itRegistersOnBackInvokedCallbackOnCreate() {
96+
Intent intent = FlutterActivityWithReportFullyDrawn.createDefaultIntent(ctx);
97+
ActivityController<FlutterActivityWithReportFullyDrawn> activityController =
98+
Robolectric.buildActivity(FlutterActivityWithReportFullyDrawn.class, intent);
99+
FlutterActivityWithReportFullyDrawn activity = spy(activityController.get());
100+
101+
activity.onCreate(null);
102+
103+
verify(activity, times(1)).registerOnBackInvokedCallback();
104+
}
105+
91106
@Test
92107
public void itCreatesDefaultIntentWithExpectedDefaults() {
93108
Intent intent = FlutterActivity.createDefaultIntent(ctx);
@@ -596,6 +611,14 @@ public void resetFullyDrawn() {
596611
}
597612
}
598613

614+
private class FlutterActivityWithMockBackInvokedHandling extends FlutterActivity {
615+
@Override
616+
public void registerOnBackInvokedCallback() {}
617+
618+
@Override
619+
public void unregisterOnBackInvokedCallback() {}
620+
}
621+
599622
private static final class FakeFlutterPlugin
600623
implements FlutterPlugin,
601624
ActivityAware,

shell/platform/android/test_runner/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ android {
3636
compileSdkVersion 33
3737

3838
defaultConfig {
39+
targetSdkVersion 33
3940
minSdkVersion 16
4041
}
4142

@@ -70,11 +71,11 @@ android {
7071
testImplementation "androidx.test:core:1.4.0"
7172
testImplementation "com.google.android.play:core:1.8.0"
7273
testImplementation "com.ibm.icu:icu4j:69.1"
73-
testImplementation "org.robolectric:robolectric:4.7.3"
74-
testImplementation "junit:junit:4.13"
75-
testImplementation "androidx.test.ext:junit:1.1.3"
74+
testImplementation "org.robolectric:robolectric:4.8.2"
75+
testImplementation "junit:junit:4.13.2"
76+
testImplementation "androidx.test.ext:junit:1.1.4-alpha07"
7677

77-
def mockitoVersion = "4.1.0"
78+
def mockitoVersion = "4.7.0"
7879
testImplementation "org.mockito:mockito-core:$mockitoVersion"
7980
testImplementation "org.mockito:mockito-inline:$mockitoVersion"
8081
testImplementation "org.mockito:mockito-android:$mockitoVersion"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
sdk=31
1+
sdk=32
22
shadows=io.flutter.CustomShadowContextImpl

0 commit comments

Comments
 (0)