Flutter Engine
The Flutter Engine
Public Member Functions | Package Attributes | List of all members
test.io.flutter.embedding.engine.FlutterEngineTest Class Reference

Public Member Functions

void setUp ()
 
void tearDown ()
 
void itAutomaticallyRegistersPluginsByDefault ()
 
void itUpdatesDisplayMetricsOnConstructionWithActivityContext ()
 
void itSendLocalesOnEngineInit ()
 
void itCatchesAndDisplaysRegistrationExceptions ()
 
void itDoesNotAutomaticallyRegistersPluginsWhenFlutterLoaderDisablesIt ()
 
void itDoesNotAutomaticallyRegistersPluginsWhenFlutterEngineDisablesIt ()
 
void itNotifiesPlatformViewsControllerWhenDevHotRestart ()
 
void itNotifiesPlatformViewsControllerAboutJNILifecycle ()
 
void itUsesApplicationContext () throws NameNotFoundException
 
void itUsesPackageContextForAssetManager () throws NameNotFoundException
 
void itCanUseFlutterLoaderInjectionViaFlutterInjector () throws NameNotFoundException
 
void itNotifiesListenersForDestruction () throws NameNotFoundException
 
void itDoesNotAttachAgainWhenBuiltWithAnAttachedJNI () throws NameNotFoundException
 
void itComesWithARunningDartExecutorIfJNIIsAlreadyAttached () throws NameNotFoundException
 
void passesEngineGroupToPlugins () throws NameNotFoundException
 

Package Attributes

FlutterJNI flutterJNI
 
boolean jniAttached
 

Detailed Description

Definition at line 53 of file FlutterEngineTest.java.

Member Function Documentation

◆ itAutomaticallyRegistersPluginsByDefault()

void test.io.flutter.embedding.engine.FlutterEngineTest.itAutomaticallyRegistersPluginsByDefault ( )
inline

Definition at line 93 of file FlutterEngineTest.java.

93 {
94 assertTrue(GeneratedPluginRegistrant.getRegisteredEngines().isEmpty());
95 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
96 when(mockFlutterLoader.automaticallyRegisterPlugins()).thenReturn(true);
97 FlutterEngine flutterEngine = new FlutterEngine(ctx, mockFlutterLoader, flutterJNI);
98
99 List<FlutterEngine> registeredEngines = GeneratedPluginRegistrant.getRegisteredEngines();
100 assertEquals(1, registeredEngines.size());
101 assertEquals(flutterEngine, registeredEngines.get(0));
102 }

◆ itCanUseFlutterLoaderInjectionViaFlutterInjector()

void test.io.flutter.embedding.engine.FlutterEngineTest.itCanUseFlutterLoaderInjectionViaFlutterInjector ( ) throws NameNotFoundException
inline

Definition at line 276 of file FlutterEngineTest.java.

276 {
277 FlutterInjector.reset();
278 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
279 FlutterInjector.setInstance(
280 new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
281 Context packageContext = mock(Context.class);
282
283 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
284
285 new FlutterEngine(mockContext, null, flutterJNI);
286
287 verify(mockFlutterLoader, times(1)).startInitialization(any());
288 verify(mockFlutterLoader, times(1)).ensureInitializationComplete(any(), any());
289 FlutterInjector.reset();
290 }
static SkISize times(const SkISize &size, float factor)
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530

◆ itCatchesAndDisplaysRegistrationExceptions()

void test.io.flutter.embedding.engine.FlutterEngineTest.itCatchesAndDisplaysRegistrationExceptions ( )
inline

Definition at line 136 of file FlutterEngineTest.java.

136 {
137 assertTrue(GeneratedPluginRegistrant.getRegisteredEngines().isEmpty());
138 GeneratedPluginRegistrant.pluginRegistrationException =
139 new RuntimeException("I'm a bug in the plugin");
140 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
141 when(mockFlutterLoader.automaticallyRegisterPlugins()).thenReturn(true);
142 FlutterEngine flutterEngine = new FlutterEngine(ctx, mockFlutterLoader, flutterJNI);
143
144 List<FlutterEngine> registeredEngines = GeneratedPluginRegistrant.getRegisteredEngines();
145 // When it crashes, it doesn't end up registering anything.
146 assertEquals(0, registeredEngines.size());
147
148 // Check the logs actually says registration failed, so a subsequent MissingPluginException
149 // isn't mysterious.
150 assertTrue(
151 ShadowLog.getLogsForTag("GeneratedPluginsRegister")
152 .get(0)
153 .msg
154 .contains("Tried to automatically register plugins"));
155 assertEquals(
156 GeneratedPluginRegistrant.pluginRegistrationException,
157 ShadowLog.getLogsForTag("GeneratedPluginsRegister").get(1).throwable.getCause());
158
159 GeneratedPluginRegistrant.pluginRegistrationException = null;
160 }

◆ itComesWithARunningDartExecutorIfJNIIsAlreadyAttached()

void test.io.flutter.embedding.engine.FlutterEngineTest.itComesWithARunningDartExecutorIfJNIIsAlreadyAttached ( ) throws NameNotFoundException
inline

Definition at line 331 of file FlutterEngineTest.java.

331 {
332 Context packageContext = mock(Context.class);
333
334 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
335 when(flutterJNI.isAttached()).thenReturn(true);
336
337 FlutterEngine engineUnderTest =
338 new FlutterEngine(
339 mockContext,
340 mock(FlutterLoader.class),
342 /*dartVmArgs=*/ new String[] {},
343 /*automaticallyRegisterPlugins=*/ false);
344
345 assertTrue(engineUnderTest.getDartExecutor().isExecutingDart());
346 }

◆ itDoesNotAttachAgainWhenBuiltWithAnAttachedJNI()

void test.io.flutter.embedding.engine.FlutterEngineTest.itDoesNotAttachAgainWhenBuiltWithAnAttachedJNI ( ) throws NameNotFoundException
inline

Definition at line 313 of file FlutterEngineTest.java.

313 {
314 Context packageContext = mock(Context.class);
315
316 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
317 when(flutterJNI.isAttached()).thenReturn(true);
318
319 FlutterEngine engineUnderTest =
320 new FlutterEngine(
321 mockContext,
322 mock(FlutterLoader.class),
324 /*dartVmArgs=*/ new String[] {},
325 /*automaticallyRegisterPlugins=*/ false);
326
327 verify(flutterJNI, never()).attachToNative();
328 }

◆ itDoesNotAutomaticallyRegistersPluginsWhenFlutterEngineDisablesIt()

void test.io.flutter.embedding.engine.FlutterEngineTest.itDoesNotAutomaticallyRegistersPluginsWhenFlutterEngineDisablesIt ( )
inline

Definition at line 174 of file FlutterEngineTest.java.

174 {
175 assertTrue(GeneratedPluginRegistrant.getRegisteredEngines().isEmpty());
176 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
177 when(mockFlutterLoader.automaticallyRegisterPlugins()).thenReturn(true);
178 new FlutterEngine(
179 ctx,
180 mockFlutterLoader,
182 /*dartVmArgs=*/ new String[] {},
183 /*automaticallyRegisterPlugins=*/ false);
184
185 List<FlutterEngine> registeredEngines = GeneratedPluginRegistrant.getRegisteredEngines();
186 assertTrue(registeredEngines.isEmpty());
187 }

◆ itDoesNotAutomaticallyRegistersPluginsWhenFlutterLoaderDisablesIt()

void test.io.flutter.embedding.engine.FlutterEngineTest.itDoesNotAutomaticallyRegistersPluginsWhenFlutterLoaderDisablesIt ( )
inline

Definition at line 163 of file FlutterEngineTest.java.

163 {
164 assertTrue(GeneratedPluginRegistrant.getRegisteredEngines().isEmpty());
165 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
166 when(mockFlutterLoader.automaticallyRegisterPlugins()).thenReturn(false);
167 new FlutterEngine(ctx, mockFlutterLoader, flutterJNI);
168
169 List<FlutterEngine> registeredEngines = GeneratedPluginRegistrant.getRegisteredEngines();
170 assertTrue(registeredEngines.isEmpty());
171 }

◆ itNotifiesListenersForDestruction()

void test.io.flutter.embedding.engine.FlutterEngineTest.itNotifiesListenersForDestruction ( ) throws NameNotFoundException
inline

Definition at line 293 of file FlutterEngineTest.java.

293 {
294 Context packageContext = mock(Context.class);
295
296 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
297
298 FlutterEngine engineUnderTest =
299 new FlutterEngine(
300 mockContext,
301 mock(FlutterLoader.class),
303 /*dartVmArgs=*/ new String[] {},
304 /*automaticallyRegisterPlugins=*/ false);
305
306 EngineLifecycleListener listener = mock(EngineLifecycleListener.class);
307 engineUnderTest.addEngineLifecycleListener(listener);
308 engineUnderTest.destroy();
309 verify(listener, times(1)).onEngineWillDestroy();
310 }

◆ itNotifiesPlatformViewsControllerAboutJNILifecycle()

void test.io.flutter.embedding.engine.FlutterEngineTest.itNotifiesPlatformViewsControllerAboutJNILifecycle ( )
inline

Definition at line 225 of file FlutterEngineTest.java.

225 {
226 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
227
228 // Execute behavior under test.
230 new FlutterEngine(
231 ctx,
232 mock(FlutterLoader.class),
234 platformViewsController,
235 /*dartVmArgs=*/ new String[] {},
236 /*automaticallyRegisterPlugins=*/ false);
237 verify(platformViewsController, times(1)).onAttachedToJNI();
238
239 engine.destroy();
240 verify(platformViewsController, times(1)).onDetachedFromJNI();
241 }
FlutterEngine engine
Definition: main.cc:68

◆ itNotifiesPlatformViewsControllerWhenDevHotRestart()

void test.io.flutter.embedding.engine.FlutterEngineTest.itNotifiesPlatformViewsControllerWhenDevHotRestart ( )
inline

Definition at line 190 of file FlutterEngineTest.java.

190 {
191 // Setup test.
192 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
193 when(mockFlutterJNI.isAttached()).thenReturn(true);
194
195 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
196
197 ArgumentCaptor<FlutterEngine.EngineLifecycleListener> engineLifecycleListenerArgumentCaptor =
198 ArgumentCaptor.forClass(FlutterEngine.EngineLifecycleListener.class);
199
200 // Execute behavior under test.
201 new FlutterEngine(
202 ctx,
203 mock(FlutterLoader.class),
204 mockFlutterJNI,
205 platformViewsController,
206 /*dartVmArgs=*/ new String[] {},
207 /*automaticallyRegisterPlugins=*/ false);
208
209 // Obtain the EngineLifecycleListener within FlutterEngine that was given to FlutterJNI.
210 verify(mockFlutterJNI)
211 .addEngineLifecycleListener(engineLifecycleListenerArgumentCaptor.capture());
212 FlutterEngine.EngineLifecycleListener engineLifecycleListener =
213 engineLifecycleListenerArgumentCaptor.getValue();
214 assertNotNull(engineLifecycleListener);
215
216 // Simulate a pre-engine restart, AKA hot restart.
217 engineLifecycleListener.onPreEngineRestart();
218
219 // Verify that FlutterEngine notified PlatformViewsController of the pre-engine restart,
220 // AKA hot restart.
221 verify(platformViewsController, times(1)).onPreEngineRestart();
222 }

◆ itSendLocalesOnEngineInit()

void test.io.flutter.embedding.engine.FlutterEngineTest.itSendLocalesOnEngineInit ( )
inline

Definition at line 120 of file FlutterEngineTest.java.

120 {
121 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
122 when(mockFlutterJNI.isAttached()).thenReturn(true);
123
124 assertTrue(GeneratedPluginRegistrant.getRegisteredEngines().isEmpty());
125 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
126 when(mockFlutterLoader.automaticallyRegisterPlugins()).thenReturn(true);
127 FlutterEngine flutterEngine = new FlutterEngine(ctx, mockFlutterLoader, mockFlutterJNI);
128
129 verify(mockFlutterJNI, times(1))
130 .dispatchPlatformMessage(eq("flutter/localization"), any(), anyInt(), anyInt());
131 }
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition: M44Test.cpp:18

◆ itUpdatesDisplayMetricsOnConstructionWithActivityContext()

void test.io.flutter.embedding.engine.FlutterEngineTest.itUpdatesDisplayMetricsOnConstructionWithActivityContext ( )
inline

Definition at line 105 of file FlutterEngineTest.java.

105 {
106 // Needs an activity. ApplicationContext won't work for this.
107 ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class);
108 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
109 when(mockFlutterJNI.isAttached()).thenReturn(true);
110
111 FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
112 FlutterEngine flutterEngine =
113 new FlutterEngine(activityController.get(), mockFlutterLoader, mockFlutterJNI);
114
115 verify(mockFlutterJNI, times(1))
116 .updateDisplayMetrics(eq(0), any(Float.class), any(Float.class), any(Float.class));
117 }

◆ itUsesApplicationContext()

void test.io.flutter.embedding.engine.FlutterEngineTest.itUsesApplicationContext ( ) throws NameNotFoundException
inline

Definition at line 244 of file FlutterEngineTest.java.

244 {
245 Context packageContext = mock(Context.class);
246
247 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
248
249 new FlutterEngine(
250 mockContext,
251 mock(FlutterLoader.class),
253 /*dartVmArgs=*/ new String[] {},
254 /*automaticallyRegisterPlugins=*/ false);
255
256 verify(mockContext, atLeast(1)).getApplicationContext();
257 }

◆ itUsesPackageContextForAssetManager()

void test.io.flutter.embedding.engine.FlutterEngineTest.itUsesPackageContextForAssetManager ( ) throws NameNotFoundException
inline

Definition at line 260 of file FlutterEngineTest.java.

260 {
261 Context packageContext = mock(Context.class);
262 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
263
264 new FlutterEngine(
265 mockContext,
266 mock(FlutterLoader.class),
268 /*dartVmArgs=*/ new String[] {},
269 /*automaticallyRegisterPlugins=*/ false);
270
271 verify(packageContext, atLeast(1)).getAssets();
272 verify(mockContext, times(0)).getAssets();
273 }

◆ passesEngineGroupToPlugins()

void test.io.flutter.embedding.engine.FlutterEngineTest.passesEngineGroupToPlugins ( ) throws NameNotFoundException
inline

Definition at line 349 of file FlutterEngineTest.java.

349 {
350 Context packageContext = mock(Context.class);
351
352 when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);
353 when(flutterJNI.isAttached()).thenReturn(true);
354
355 FlutterEngineGroup mockGroup = mock(FlutterEngineGroup.class);
356
357 FlutterEngine engineUnderTest =
358 new FlutterEngine(
359 mockContext,
360 mock(FlutterLoader.class),
362 new PlatformViewsController(),
363 /*dartVmArgs=*/ new String[] {},
364 /*automaticallyRegisterPlugins=*/ false,
365 /*waitForRestorationData=*/ false,
366 mockGroup);
367
368 PluginRegistry registry = engineUnderTest.getPlugins();
369 FlutterPlugin mockPlugin = mock(FlutterPlugin.class);
370 ArgumentCaptor<FlutterPlugin.FlutterPluginBinding> pluginBindingCaptor =
371 ArgumentCaptor.forClass(FlutterPlugin.FlutterPluginBinding.class);
372 registry.add(mockPlugin);
373 verify(mockPlugin).onAttachedToEngine(pluginBindingCaptor.capture());
374 assertNotNull(pluginBindingCaptor.getValue());
375 assertEquals(mockGroup, pluginBindingCaptor.getValue().getEngineGroup());
376 }

◆ setUp()

void test.io.flutter.embedding.engine.FlutterEngineTest.setUp ( )
inline

Definition at line 60 of file FlutterEngineTest.java.

60 {
61 MockitoAnnotations.openMocks(this);
62
63 Resources mockResources = mock(Resources.class);
64 Configuration mockConfiguration = mock(Configuration.class);
65 doReturn(mockResources).when(mockContext).getResources();
66 doReturn(mockConfiguration).when(mockResources).getConfiguration();
67 doReturn(LocaleList.getEmptyLocaleList()).when(mockConfiguration).getLocales();
68
69 jniAttached = false;
70 when(flutterJNI.isAttached()).thenAnswer(invocation -> jniAttached);
71 doAnswer(
72 new Answer() {
73 @Override
74 public Object answer(InvocationOnMock invocation) throws Throwable {
75 jniAttached = true;
76 return null;
77 }
78 })
79 .when(flutterJNI)
80 .attachToNative();
81 GeneratedPluginRegistrant.clearRegisteredEngines();
82 }

◆ tearDown()

void test.io.flutter.embedding.engine.FlutterEngineTest.tearDown ( )
inline

Definition at line 85 of file FlutterEngineTest.java.

85 {
86 GeneratedPluginRegistrant.clearRegisteredEngines();
87 // Make sure to not forget to remove the mock exception in the generated plugin registration
88 // mock, or everything subsequent will break.
89 GeneratedPluginRegistrant.pluginRegistrationException = null;
90 }

Member Data Documentation

◆ flutterJNI

FlutterJNI test.io.flutter.embedding.engine.FlutterEngineTest.flutterJNI
package

Definition at line 56 of file FlutterEngineTest.java.

◆ jniAttached

boolean test.io.flutter.embedding.engine.FlutterEngineTest.jniAttached
package

Definition at line 57 of file FlutterEngineTest.java.


The documentation for this class was generated from the following file: