Flutter Engine
The Flutter Engine
RenderingComponentTest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.never;
6import static org.mockito.Mockito.times;
7import static org.mockito.Mockito.verify;
8
9import androidx.test.ext.junit.runners.AndroidJUnit4;
10import io.flutter.embedding.engine.renderer.FlutterRenderer;
11import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
12import java.util.concurrent.atomic.AtomicInteger;
13import org.junit.Test;
14import org.junit.runner.RunWith;
15import org.robolectric.annotation.Config;
16
17@Config(manifest = Config.NONE)
18@RunWith(AndroidJUnit4.class)
20 @Test
22 // Setup test.
23 FlutterJNI flutterJNI = new FlutterJNI();
24 FlutterRenderer flutterRenderer = new FlutterRenderer(flutterJNI);
25
26 AtomicInteger listenerInvocationCount = new AtomicInteger(0);
29 @Override
30 public void onFlutterUiDisplayed() {
31 // This is the behavior we're testing, but we also verify that this method
32 // was invoked to ensure that this test behavior executed.
33 flutterRenderer.removeIsDisplayingFlutterUiListener(this);
34
35 // Track the invocation to ensure this method is called once, and only once.
36 listenerInvocationCount.incrementAndGet();
37 }
38
39 @Override
40 public void onFlutterUiNoLongerDisplayed() {}
41 };
42 flutterRenderer.addIsDisplayingFlutterUiListener(listener);
43
44 // Execute behavior under test.
45 // Pretend we are the native side and tell FlutterJNI that Flutter has rendered a frame.
46 flutterJNI.onFirstFrame();
47
48 // Verify results.
49 // If we got to this point without an exception, and if our listener was called one time,
50 // then the behavior under test is correct.
51 assertEquals(1, listenerInvocationCount.get());
52 }
53
54 @Test
56 // Setup test.
57 FlutterJNI flutterJNI = new FlutterJNI();
58 FlutterRenderer flutterRenderer = new FlutterRenderer(flutterJNI);
59
61
62 // Pretend we are the native side and tell FlutterJNI that Flutter has rendered a frame.
63 flutterJNI.onFirstFrame();
64
65 // Execute behavior under test.
66 flutterRenderer.addIsDisplayingFlutterUiListener(listener);
67
68 // Verify results.
69 verify(listener, times(1)).onFlutterUiDisplayed();
70 }
71
72 @Test
74 // Setup test.
75 FlutterJNI flutterJNI = new FlutterJNI();
76 FlutterRenderer flutterRenderer = new FlutterRenderer(flutterJNI);
77
79
80 // Pretend we are the native side and tell FlutterJNI that Flutter has rendered a frame.
81 flutterJNI.onFirstFrame();
82
83 // Pretend that rendering has stopped.
84 flutterJNI.onRenderingStopped();
85
86 // Execute behavior under test.
87 flutterRenderer.addIsDisplayingFlutterUiListener(listener);
88
89 // Verify results.
90 verify(listener, never()).onFlutterUiDisplayed();
91 }
92}
static SkISize times(const SkISize &size, float factor)
void removeIsDisplayingFlutterUiListener(@NonNull FlutterUiDisplayListener listener)
void addIsDisplayingFlutterUiListener(@NonNull FlutterUiDisplayListener listener)