Flutter Engine
The Flutter Engine
FlutterTextureViewTest.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.graphics.SurfaceTexture;
14import android.view.Surface;
15import android.view.TextureView;
16import androidx.test.core.app.ApplicationProvider;
17import androidx.test.ext.junit.runners.AndroidJUnit4;
18import io.flutter.embedding.engine.FlutterJNI;
19import io.flutter.embedding.engine.renderer.FlutterRenderer;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.robolectric.annotation.Config;
23
24@Config(manifest = Config.NONE)
25@RunWith(AndroidJUnit4.class)
26@TargetApi(API_LEVELS.API_30)
28 @Test
30 final FlutterTextureView textureView =
31 new FlutterTextureView(ApplicationProvider.getApplicationContext());
32 final Surface mockRenderSurface = mock(Surface.class);
33
34 textureView.setRenderSurface(mockRenderSurface);
35
36 final TextureView.SurfaceTextureListener listener = textureView.getSurfaceTextureListener();
37 listener.onSurfaceTextureDestroyed(mock(SurfaceTexture.class));
38
39 verify(mockRenderSurface).release();
40 }
41
42 @Test
44 // Consider this scenario: In an add-to-app context, where multiple Flutter activities share the
45 // same engine, a situation occurs. When navigating from FlutterActivity1 to FlutterActivity2,
46 // the Flutter view associated with FlutterActivity1 is detached from the engine. Then, the
47 // Flutter view of FlutterActivity2 is attached. Upon navigating back to FlutterActivity1, its
48 // Flutter view is re-attached to the shared engine.
49 //
50 // The expected behavior is: When a Flutter view detaches from the shared engine, the associated
51 // surface should be released. When the Flutter view re-attaches, a new surface should be
52 // created.
53
54 // Setup the test.
55 final FlutterTextureView textureView =
56 spy(new FlutterTextureView(ApplicationProvider.getApplicationContext()));
57
58 FlutterJNI fakeFlutterJNI = mock(FlutterJNI.class);
59 FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
60
61 when(textureView.isSurfaceAvailableForRendering()).thenReturn(true);
62 when(textureView.getSurfaceTexture()).thenReturn(mock(SurfaceTexture.class));
63 when(textureView.getWindowToken()).thenReturn(mock(android.os.IBinder.class));
64
65 // Execute the behavior under test.
66 textureView.attachToRenderer(flutterRenderer);
67
68 // Verify the behavior under test.
69 verify(fakeFlutterJNI, times(1)).onSurfaceCreated(any(Surface.class));
70
71 // Execute the behavior under test.
72 textureView.detachFromRenderer();
73
74 // Verify the behavior under test.
75 verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed();
76
77 // Execute the behavior under test.
78 textureView.attachToRenderer(flutterRenderer);
79
80 // Verify the behavior under test.
81 verify(fakeFlutterJNI, never()).onSurfaceWindowChanged(any(Surface.class));
82 verify(fakeFlutterJNI, times(2)).onSurfaceCreated(any(Surface.class));
83 }
84}
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