Flutter Engine
The Flutter Engine
FlutterSurfaceViewTest.java
Go to the documentation of this file.
1package io.flutter.embedding.android;
2
3import static io.flutter.Build.API_LEVELS;
4import static org.mockito.ArgumentMatchers.any;
5import static org.mockito.Mockito.mock;
6import static org.mockito.Mockito.never;
7import static org.mockito.Mockito.spy;
8import static org.mockito.Mockito.times;
9import static org.mockito.Mockito.verify;
10import static org.mockito.Mockito.when;
11
12import android.annotation.TargetApi;
13import android.view.Surface;
14import android.view.SurfaceHolder;
15import androidx.test.core.app.ApplicationProvider;
16import androidx.test.ext.junit.runners.AndroidJUnit4;
17import io.flutter.embedding.engine.FlutterJNI;
18import io.flutter.embedding.engine.renderer.FlutterRenderer;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.robolectric.annotation.Config;
22
23@Config(manifest = Config.NONE)
24@RunWith(AndroidJUnit4.class)
25@TargetApi(API_LEVELS.API_30)
27 @Test
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}
static SkISize times(const SkISize &size, float factor)
void attachToRenderer(@NonNull FlutterRenderer renderer)
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530