|
| 1 | +package io.flutter.embedding.android; |
| 2 | + |
| 3 | +import static io.flutter.Build.API_LEVELS; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.spy; |
| 8 | +import static org.mockito.Mockito.times; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import android.annotation.TargetApi; |
| 13 | +import android.view.Surface; |
| 14 | +import android.view.SurfaceHolder; |
| 15 | +import androidx.test.core.app.ApplicationProvider; |
| 16 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 17 | +import io.flutter.embedding.engine.FlutterJNI; |
| 18 | +import io.flutter.embedding.engine.renderer.FlutterRenderer; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.robolectric.annotation.Config; |
| 22 | + |
| 23 | +@Config(manifest = Config.NONE) |
| 24 | +@RunWith(AndroidJUnit4.class) |
| 25 | +@TargetApi(API_LEVELS.API_30) |
| 26 | +public class FlutterSurfaceViewTest { |
| 27 | + @Test |
| 28 | + public void itShouldCreateANewSurfaceWhenReattachedAfterDetachingFromRenderer() { |
| 29 | + // Consider this scenario: In an add-to-app context, where multiple Flutter activities share the |
| 30 | + // same engine, a situation occurs. When navigating from FlutterActivity1 to FlutterActivity2, |
| 31 | + // the Flutter view associated with FlutterActivity1 is detached from the engine. Then, the |
| 32 | + // Flutter view of FlutterActivity2 is attached. Upon navigating back to FlutterActivity1, its |
| 33 | + // Flutter view is re-attached to the shared engine. |
| 34 | + // |
| 35 | + // The expected behavior is: When a Flutter view detaches from the shared engine, the associated |
| 36 | + // surface should be released. When the Flutter view re-attaches, a new surface should be |
| 37 | + // created. |
| 38 | + |
| 39 | + // Setup the test. |
| 40 | + final FlutterSurfaceView surfaceView = |
| 41 | + spy(new FlutterSurfaceView(ApplicationProvider.getApplicationContext())); |
| 42 | + |
| 43 | + FlutterJNI fakeFlutterJNI = mock(FlutterJNI.class); |
| 44 | + FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI); |
| 45 | + |
| 46 | + SurfaceHolder fakeSurfaceHolder = mock(SurfaceHolder.class); |
| 47 | + Surface fakeSurface = mock(Surface.class); |
| 48 | + when(surfaceView.getHolder()).thenReturn(fakeSurfaceHolder); |
| 49 | + when(fakeSurfaceHolder.getSurface()).thenReturn(fakeSurface); |
| 50 | + when(surfaceView.isSurfaceAvailableForRendering()).thenReturn(true); |
| 51 | + when(surfaceView.getWindowToken()).thenReturn(mock(android.os.IBinder.class)); |
| 52 | + |
| 53 | + // Execute the behavior under test. |
| 54 | + surfaceView.attachToRenderer(flutterRenderer); |
| 55 | + |
| 56 | + // Verify the behavior under test. |
| 57 | + verify(fakeFlutterJNI, times(1)).onSurfaceCreated(any(Surface.class)); |
| 58 | + |
| 59 | + // Execute the behavior under test. |
| 60 | + surfaceView.detachFromRenderer(); |
| 61 | + |
| 62 | + // Verify the behavior under test. |
| 63 | + verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed(); |
| 64 | + |
| 65 | + // Execute the behavior under test. |
| 66 | + surfaceView.attachToRenderer(flutterRenderer); |
| 67 | + |
| 68 | + // Verify the behavior under test. |
| 69 | + verify(fakeFlutterJNI, never()).onSurfaceWindowChanged(any(Surface.class)); |
| 70 | + verify(fakeFlutterJNI, times(2)).onSurfaceCreated(any(Surface.class)); |
| 71 | + } |
| 72 | +} |
0 commit comments