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

Commit b44cfce

Browse files
committed
Tweak the code add add some unit tests
1 parent e144a6b commit b44cfce

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
// to this FlutterActivityAndFragmentDelegate.
7575
@NonNull private Host host;
7676
@Nullable private FlutterEngine flutterEngine;
77-
@Nullable private FlutterView flutterView;
77+
@VisibleForTesting @Nullable FlutterView flutterView;
7878
@Nullable private PlatformPlugin platformPlugin;
7979
@VisibleForTesting @Nullable OnPreDrawListener activePreDrawListener;
8080
private boolean isFlutterEngineFromHost;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,17 +1420,17 @@ public void autofill(SparseArray<AutofillValue> values) {
14201420
textInputPlugin.autofill(values);
14211421
}
14221422

1423-
// TODO add comment
1423+
// Invoke this from {@code FlutterActivityAndFragmentDelegate#onStart()}
14241424
/* package */ void onStart() {
1425-
if (flutterSurfaceView != null && flutterSurfaceView.getParent() == null) {
1426-
addView(flutterSurfaceView);
1425+
if (flutterSurfaceView != null) {
1426+
flutterSurfaceView.setVisibility(View.VISIBLE);
14271427
}
14281428
}
14291429

1430-
// TODO add comment
1430+
// Invoke this from {@code FlutterActivityAndFragmentDelegate#onStop()}
14311431
/* package */ void onStop() {
1432-
if (flutterSurfaceView != null && flutterSurfaceView.getParent() != null) {
1433-
removeView(flutterSurfaceView);
1432+
if (flutterSurfaceView != null) {
1433+
flutterSurfaceView.setVisibility(View.GONE);
14341434
}
14351435
}
14361436

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,24 @@ public void itThrowsWhenDelayingTheFirstDrawAndUsingATextureView() {
983983
});
984984
}
985985

986+
@Test
987+
public void itNotifiesFlutterViewWhenOnStartAndOnStop() {
988+
// ---- Test setup ----
989+
// Create the real object that we're testing.
990+
FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(mockHost);
991+
992+
// --- Execute the behavior under test ---
993+
delegate.onAttach(RuntimeEnvironment.application);
994+
delegate.onCreateView(null, null, null, 0, true);
995+
delegate.flutterView = mock(FlutterView.class);
996+
delegate.onStart();
997+
// Verify that the onStart of flutterView was called.
998+
verify(delegate.flutterView, times(1)).onStart();
999+
delegate.onStop();
1000+
// Verify that the onStop of flutterView was called.
1001+
verify(delegate.flutterView, times(1)).onStop();
1002+
}
1003+
9861004
@Test
9871005
public void itDoesNotDelayTheFirstDrawWhenRequestedAndWithAProvidedSplashScreen() {
9881006
when(mockHost.provideSplashScreen())

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,18 @@ public void itRegistersAndUnregistersToWindowManager() {
687687
verify(windowInfoRepo, times(1)).removeWindowLayoutInfoListener(any());
688688
}
689689

690+
@Test
691+
public void itChangesSurfaceViewVisibilityWhenOnStartAndOnStop() {
692+
Context context = Robolectric.setupActivity(Activity.class);
693+
FlutterView flutterView = spy(new FlutterView(context));
694+
FlutterSurfaceView flutterSurfaceView = (FlutterSurfaceView) flutterView.renderSurface;
695+
assertEquals(View.VISIBLE, flutterSurfaceView.getVisibility());
696+
flutterView.onStop();
697+
assertEquals(View.GONE, flutterSurfaceView.getVisibility());
698+
flutterView.onStart();
699+
assertEquals(View.VISIBLE, flutterSurfaceView.getVisibility());
700+
}
701+
690702
@Test
691703
public void itSendsHingeDisplayFeatureToFlutter() {
692704
Context context = Robolectric.setupActivity(Activity.class);

0 commit comments

Comments
 (0)