Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Package Attributes | List of all members
io.flutter.embedding.android.FlutterFragmentTest Class Reference

Classes

class  TestDelegateFactory
 

Public Member Functions

void itCreatesDefaultFragmentWithExpectedDefaults ()
 
void itCreatesNewEngineFragmentWithRequestedSettings ()
 
void itCreatesNewEngineInGroupFragmentWithRequestedSettings ()
 
void itCreatesNewEngineFragmentThatDelaysFirstDrawWhenRequested ()
 
void itCreatesCachedEngineFragmentWithExpectedDefaults ()
 
void itCreatesCachedEngineFragmentThatDestroysTheEngine ()
 
void itCreatesCachedEngineFragmentThatDelaysFirstDrawWhenRequested ()
 
void itCanBeDetachedFromTheEngineAndStopSendingFurtherEvents ()
 
void itDoesNotReleaseEnginewhenDetachFromFlutterEngine ()
 
void itReleaseEngineWhenOnDetach ()
 
void itReturnsExclusiveAppComponent ()
 
void itDelegatesOnBackPressedWithSetFrameworkHandlesBack ()
 
void itHandlesPopSystemNavigationAutomaticallyWhenEnabled ()
 
void itRegistersComponentCallbacks ()
 

Package Attributes

boolean isDelegateAttached
 

Detailed Description

Definition at line 37 of file FlutterFragmentTest.java.

Member Function Documentation

◆ itCanBeDetachedFromTheEngineAndStopSendingFurtherEvents()

void io.flutter.embedding.android.FlutterFragmentTest.itCanBeDetachedFromTheEngineAndStopSendingFurtherEvents ( )
inline

Definition at line 175 of file FlutterFragmentTest.java.

175 {
176 FlutterActivityAndFragmentDelegate mockDelegate =
177 mock(FlutterActivityAndFragmentDelegate.class);
178 TestDelegateFactory delegateFactory = new TestDelegateFactory(mockDelegate);
179 FlutterFragment fragment =
180 FlutterFragment.withCachedEngine("my_cached_engine")
181 .destroyEngineWithFragment(true)
182 .build();
183
184 isDelegateAttached = true;
185 when(mockDelegate.isAttached()).thenAnswer(invocation -> isDelegateAttached);
186 doAnswer(invocation -> isDelegateAttached = false).when(mockDelegate).onDetach();
187
188 fragment.setDelegateFactory(delegateFactory);
189 fragment.onStart();
190 fragment.onResume();
191 fragment.onPostResume();
192
193 verify(mockDelegate, times(1)).onStart();
194 verify(mockDelegate, times(1)).onResume();
195 verify(mockDelegate, times(1)).onPostResume();
196
197 fragment.onPause();
198 fragment.detachFromFlutterEngine();
199 verify(mockDelegate, times(1)).onPause();
200 verify(mockDelegate, times(1)).onDestroyView();
201 verify(mockDelegate, times(1)).onDetach();
202
203 fragment.onStop();
204 verify(mockDelegate, never()).onStop();
205
206 fragment.onStart();
207 fragment.onResume();
208 fragment.onPostResume();
209 // No more events through to the delegate.
210 verify(mockDelegate, times(1)).onStart();
211 verify(mockDelegate, times(1)).onResume();
212 verify(mockDelegate, times(1)).onPostResume();
213
214 fragment.onDestroy();
215 // 1 time same as before.
216 verify(mockDelegate, times(1)).onDestroyView();
217 verify(mockDelegate, times(1)).onDetach();
218 }
static SkISize times(const SkISize &size, float factor)

◆ itCreatesCachedEngineFragmentThatDelaysFirstDrawWhenRequested()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesCachedEngineFragmentThatDelaysFirstDrawWhenRequested ( )
inline

Definition at line 165 of file FlutterFragmentTest.java.

165 {
166 FlutterFragment fragment =
167 FlutterFragment.withCachedEngine("my_cached_engine")
168 .shouldDelayFirstAndroidViewDraw(true)
169 .build();
170
171 assertNotNull(fragment.shouldDelayFirstAndroidViewDraw());
172 }

◆ itCreatesCachedEngineFragmentThatDestroysTheEngine()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesCachedEngineFragmentThatDestroysTheEngine ( )
inline

Definition at line 153 of file FlutterFragmentTest.java.

153 {
154 FlutterFragment fragment =
155 FlutterFragment.withCachedEngine("my_cached_engine")
156 .destroyEngineWithFragment(true)
157 .build();
158
159 assertTrue(fragment.shouldAttachEngineToActivity());
160 assertEquals("my_cached_engine", fragment.getCachedEngineId());
161 assertTrue(fragment.shouldDestroyEngineWithHost());
162 }

◆ itCreatesCachedEngineFragmentWithExpectedDefaults()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesCachedEngineFragmentWithExpectedDefaults ( )
inline

Definition at line 143 of file FlutterFragmentTest.java.

143 {
144 FlutterFragment fragment = FlutterFragment.withCachedEngine("my_cached_engine").build();
145
146 assertTrue(fragment.shouldAttachEngineToActivity());
147 assertEquals("my_cached_engine", fragment.getCachedEngineId());
148 assertFalse(fragment.shouldDestroyEngineWithHost());
149 assertFalse(fragment.shouldDelayFirstAndroidViewDraw());
150 }

◆ itCreatesDefaultFragmentWithExpectedDefaults()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesDefaultFragmentWithExpectedDefaults ( )
inline

Definition at line 55 of file FlutterFragmentTest.java.

55 {
56 FlutterFragment fragment = FlutterFragment.createDefault();
57 TestDelegateFactory delegateFactory =
58 new TestDelegateFactory(new FlutterActivityAndFragmentDelegate(fragment));
59 fragment.setDelegateFactory(delegateFactory);
60
61 assertEquals("main", fragment.getDartEntrypointFunctionName());
62 assertNull(fragment.getDartEntrypointLibraryUri());
63 assertNull(fragment.getDartEntrypointArgs());
64 assertEquals("/", fragment.getInitialRoute());
65 assertArrayEquals(new String[] {}, fragment.getFlutterShellArgs().toArray());
66 assertTrue(fragment.shouldAttachEngineToActivity());
67 assertFalse(fragment.shouldHandleDeeplinking());
68 assertNull(fragment.getCachedEngineId());
69 assertTrue(fragment.shouldDestroyEngineWithHost());
70 assertEquals(RenderMode.surface, fragment.getRenderMode());
71 assertEquals(TransparencyMode.transparent, fragment.getTransparencyMode());
72 assertFalse(fragment.shouldDelayFirstAndroidViewDraw());
73 }

◆ itCreatesNewEngineFragmentThatDelaysFirstDrawWhenRequested()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesNewEngineFragmentThatDelaysFirstDrawWhenRequested ( )
inline

Definition at line 135 of file FlutterFragmentTest.java.

135 {
136 FlutterFragment fragment =
137 FlutterFragment.withNewEngine().shouldDelayFirstAndroidViewDraw(true).build();
138
139 assertNotNull(fragment.shouldDelayFirstAndroidViewDraw());
140 }

◆ itCreatesNewEngineFragmentWithRequestedSettings()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesNewEngineFragmentWithRequestedSettings ( )
inline

Definition at line 76 of file FlutterFragmentTest.java.

76 {
77 FlutterFragment fragment =
78 FlutterFragment.withNewEngine()
79 .dartEntrypoint("custom_entrypoint")
80 .dartLibraryUri("package:foo/bar.dart")
81 .dartEntrypointArgs(new ArrayList<String>(Arrays.asList("foo", "bar")))
82 .initialRoute("/custom/route")
83 .shouldAttachEngineToActivity(false)
84 .handleDeeplinking(true)
85 .renderMode(RenderMode.texture)
86 .transparencyMode(TransparencyMode.opaque)
87 .build();
88 TestDelegateFactory delegateFactory =
89 new TestDelegateFactory(new FlutterActivityAndFragmentDelegate(fragment));
90 fragment.setDelegateFactory(delegateFactory);
91
92 assertEquals("custom_entrypoint", fragment.getDartEntrypointFunctionName());
93 assertEquals("package:foo/bar.dart", fragment.getDartEntrypointLibraryUri());
94 assertEquals("/custom/route", fragment.getInitialRoute());
95 assertArrayEquals(new String[] {"foo", "bar"}, fragment.getDartEntrypointArgs().toArray());
96 assertArrayEquals(new String[] {}, fragment.getFlutterShellArgs().toArray());
97 assertFalse(fragment.shouldAttachEngineToActivity());
98 assertTrue(fragment.shouldHandleDeeplinking());
99 assertNull(fragment.getCachedEngineId());
100 assertTrue(fragment.shouldDestroyEngineWithHost());
101 assertEquals(RenderMode.texture, fragment.getRenderMode());
102 assertEquals(TransparencyMode.opaque, fragment.getTransparencyMode());
103 }

◆ itCreatesNewEngineInGroupFragmentWithRequestedSettings()

void io.flutter.embedding.android.FlutterFragmentTest.itCreatesNewEngineInGroupFragmentWithRequestedSettings ( )
inline

Definition at line 106 of file FlutterFragmentTest.java.

106 {
107 FlutterFragment fragment =
108 FlutterFragment.withNewEngineInGroup("my_cached_engine_group")
109 .dartEntrypoint("custom_entrypoint")
110 .initialRoute("/custom/route")
111 .shouldAttachEngineToActivity(false)
112 .handleDeeplinking(true)
113 .renderMode(RenderMode.texture)
114 .transparencyMode(TransparencyMode.opaque)
115 .build();
116
117 TestDelegateFactory delegateFactory =
118 new TestDelegateFactory(new FlutterActivityAndFragmentDelegate(fragment));
119
120 fragment.setDelegateFactory(delegateFactory);
121
122 assertEquals("my_cached_engine_group", fragment.getCachedEngineGroupId());
123 assertEquals("custom_entrypoint", fragment.getDartEntrypointFunctionName());
124 assertEquals("/custom/route", fragment.getInitialRoute());
125 assertArrayEquals(new String[] {}, fragment.getFlutterShellArgs().toArray());
126 assertFalse(fragment.shouldAttachEngineToActivity());
127 assertTrue(fragment.shouldHandleDeeplinking());
128 assertNull(fragment.getCachedEngineId());
129 assertTrue(fragment.shouldDestroyEngineWithHost());
130 assertEquals(RenderMode.texture, fragment.getRenderMode());
131 assertEquals(TransparencyMode.opaque, fragment.getTransparencyMode());
132 }

◆ itDelegatesOnBackPressedWithSetFrameworkHandlesBack()

void io.flutter.embedding.android.FlutterFragmentTest.itDelegatesOnBackPressedWithSetFrameworkHandlesBack ( )
inline

Definition at line 293 of file FlutterFragmentTest.java.

293 {
294 // We need to mock FlutterJNI to avoid triggering native code.
295 FlutterJNI flutterJNI = mock(FlutterJNI.class);
296 when(flutterJNI.isAttached()).thenReturn(true);
297
298 FlutterEngine flutterEngine =
299 new FlutterEngine(ctx, new FlutterLoader(), flutterJNI, null, false);
300 FlutterEngineCache.getInstance().put("my_cached_engine", flutterEngine);
301
302 FlutterFragment fragment =
303 FlutterFragment.withCachedEngine("my_cached_engine")
304 // This enables the use of onBackPressedCallback, which is what
305 // sends backs to the framework if setFrameworkHandlesBack is true.
306 .shouldAutomaticallyHandleOnBackPressed(true)
307 .build();
308 FragmentActivity activity = getMockFragmentActivity();
309 activity
310 .getSupportFragmentManager()
311 .beginTransaction()
312 .add(android.R.id.content, fragment)
313 .commitNow();
314
315 FlutterActivityAndFragmentDelegate mockDelegate =
316 mock(FlutterActivityAndFragmentDelegate.class);
317 isDelegateAttached = true;
318 when(mockDelegate.isAttached()).thenAnswer(invocation -> isDelegateAttached);
319 doAnswer(invocation -> isDelegateAttached = false).when(mockDelegate).onDetach();
320 TestDelegateFactory delegateFactory = new TestDelegateFactory(mockDelegate);
321 fragment.setDelegateFactory(delegateFactory);
322
323 // Calling onBackPressed now will still be handled by Android (the default),
324 // until setFrameworkHandlesBack is set to true.
325 activity.onBackPressed();
326 verify(mockDelegate, times(0)).onBackPressed();
327
328 // Setting setFrameworkHandlesBack to true means the delegate will receive
329 // the back and Android won't handle it.
330 fragment.setFrameworkHandlesBack(true);
331 activity.getOnBackPressedDispatcher().onBackPressed();
332 verify(mockDelegate, times(1)).onBackPressed();
333 }

◆ itDoesNotReleaseEnginewhenDetachFromFlutterEngine()

void io.flutter.embedding.android.FlutterFragmentTest.itDoesNotReleaseEnginewhenDetachFromFlutterEngine ( )
inline

Definition at line 221 of file FlutterFragmentTest.java.

221 {
222 FlutterActivityAndFragmentDelegate mockDelegate =
223 mock(FlutterActivityAndFragmentDelegate.class);
224 isDelegateAttached = true;
225 when(mockDelegate.isAttached()).thenAnswer(invocation -> isDelegateAttached);
226 doAnswer(invocation -> isDelegateAttached = false).when(mockDelegate).onDetach();
227 TestDelegateFactory delegateFactory = new TestDelegateFactory(mockDelegate);
228
229 FlutterFragment fragment =
230 FlutterFragment.withCachedEngine("my_cached_engine")
231 .destroyEngineWithFragment(true)
232 .build();
233
234 fragment.setDelegateFactory(delegateFactory);
235 fragment.onStart();
236 fragment.onResume();
237 fragment.onPostResume();
238 fragment.onPause();
239
240 assertTrue(mockDelegate.isAttached());
241 fragment.detachFromFlutterEngine();
242 verify(mockDelegate, times(1)).onDetach();
243 verify(mockDelegate, never()).release();
244 assertFalse(mockDelegate.isAttached());
245 }

◆ itHandlesPopSystemNavigationAutomaticallyWhenEnabled()

void io.flutter.embedding.android.FlutterFragmentTest.itHandlesPopSystemNavigationAutomaticallyWhenEnabled ( )
inline

Definition at line 339 of file FlutterFragmentTest.java.

339 {
340 // We need to mock FlutterJNI to avoid triggering native code.
341 FlutterJNI flutterJNI = mock(FlutterJNI.class);
342 when(flutterJNI.isAttached()).thenReturn(true);
343
344 FlutterEngine flutterEngine =
345 new FlutterEngine(ctx, new FlutterLoader(), flutterJNI, null, false);
346 FlutterEngineCache.getInstance().put("my_cached_engine", flutterEngine);
347
348 FlutterFragment fragment =
349 FlutterFragment.withCachedEngine("my_cached_engine")
350 .shouldAutomaticallyHandleOnBackPressed(true)
351 .build();
352 FragmentActivity activity = getMockFragmentActivity();
353 activity
354 .getSupportFragmentManager()
355 .beginTransaction()
356 .add(android.R.id.content, fragment)
357 .commitNow();
358 final AtomicBoolean onBackPressedCalled = new AtomicBoolean(false);
359 OnBackPressedCallback callback =
360 new OnBackPressedCallback(true) {
361 @Override
362 public void handleOnBackPressed() {
363 onBackPressedCalled.set(true);
364 }
365 };
366 activity.getOnBackPressedDispatcher().addCallback(callback);
367
368 FlutterActivityAndFragmentDelegate mockDelegate =
369 mock(FlutterActivityAndFragmentDelegate.class);
370 TestDelegateFactory delegateFactory = new TestDelegateFactory(mockDelegate);
371 fragment.setDelegateFactory(delegateFactory);
372
373 assertTrue(callback.isEnabled());
374
375 assertTrue(fragment.popSystemNavigator());
376
377 verify(mockDelegate, never()).onBackPressed();
378 assertTrue(onBackPressedCalled.get());
379 assertTrue(callback.isEnabled());
380
381 callback.setEnabled(false);
382 assertFalse(callback.isEnabled());
383 assertTrue(fragment.popSystemNavigator());
384
385 verify(mockDelegate, never()).onBackPressed();
386 assertFalse(callback.isEnabled());
387 }
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

◆ itRegistersComponentCallbacks()

void io.flutter.embedding.android.FlutterFragmentTest.itRegistersComponentCallbacks ( )
inline

Definition at line 390 of file FlutterFragmentTest.java.

390 {
391 FlutterActivityAndFragmentDelegate mockDelegate =
392 mock(FlutterActivityAndFragmentDelegate.class);
393 isDelegateAttached = true;
394 when(mockDelegate.isAttached()).thenAnswer(invocation -> isDelegateAttached);
395 doAnswer(invocation -> isDelegateAttached = false).when(mockDelegate).onDetach();
396 TestDelegateFactory delegateFactory = new TestDelegateFactory(mockDelegate);
397
398 Context spyCtx = spy(ctx);
399 // We need to mock FlutterJNI to avoid triggering native code.
400 FlutterJNI flutterJNI = mock(FlutterJNI.class);
401 when(flutterJNI.isAttached()).thenReturn(true);
402
403 FlutterEngine flutterEngine =
404 new FlutterEngine(spyCtx, new FlutterLoader(), flutterJNI, null, false);
405 FlutterEngineCache.getInstance().put("my_cached_engine", flutterEngine);
406
407 FlutterFragment fragment = spy(FlutterFragment.withCachedEngine("my_cached_engine").build());
408 when(fragment.getContext()).thenReturn(spyCtx);
409 fragment.setDelegateFactory(delegateFactory);
410
411 fragment.onAttach(spyCtx);
412 verify(spyCtx, times(1)).registerComponentCallbacks(any());
413 verify(spyCtx, never()).unregisterComponentCallbacks(any());
414
415 fragment.onDetach();
416 verify(spyCtx, times(1)).registerComponentCallbacks(any());
417 verify(spyCtx, times(1)).unregisterComponentCallbacks(any());
418 }
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530

◆ itReleaseEngineWhenOnDetach()

void io.flutter.embedding.android.FlutterFragmentTest.itReleaseEngineWhenOnDetach ( )
inline

Definition at line 248 of file FlutterFragmentTest.java.

248 {
249 FlutterActivityAndFragmentDelegate mockDelegate =
250 mock(FlutterActivityAndFragmentDelegate.class);
251 isDelegateAttached = true;
252 when(mockDelegate.isAttached()).thenAnswer(invocation -> isDelegateAttached);
253 doAnswer(invocation -> isDelegateAttached = false).when(mockDelegate).onDetach();
254 TestDelegateFactory delegateFactory = new TestDelegateFactory(mockDelegate);
255
256 FlutterFragment fragment =
257 spy(
258 FlutterFragment.withCachedEngine("my_cached_engine")
259 .destroyEngineWithFragment(true)
260 .build());
261 when(fragment.getContext()).thenReturn(mock(Context.class));
262
263 fragment.setDelegateFactory(delegateFactory);
264 fragment.onStart();
265 fragment.onResume();
266 fragment.onPostResume();
267 fragment.onPause();
268
269 assertTrue(mockDelegate.isAttached());
270 fragment.onDetach();
271 verify(mockDelegate, times(1)).onDetach();
272 verify(mockDelegate, times(1)).release();
273 assertFalse(mockDelegate.isAttached());
274 }

◆ itReturnsExclusiveAppComponent()

void io.flutter.embedding.android.FlutterFragmentTest.itReturnsExclusiveAppComponent ( )
inline

Definition at line 277 of file FlutterFragmentTest.java.

277 {
278 FlutterFragment fragment = FlutterFragment.createDefault();
279 FlutterActivityAndFragmentDelegate delegate = new FlutterActivityAndFragmentDelegate(fragment);
280 TestDelegateFactory delegateFactory = new TestDelegateFactory(delegate);
281 fragment.setDelegateFactory(delegateFactory);
282
283 assertEquals(fragment.getExclusiveAppComponent(), delegate);
284 }

Member Data Documentation

◆ isDelegateAttached

boolean io.flutter.embedding.android.FlutterFragmentTest.isDelegateAttached
package

Definition at line 39 of file FlutterFragmentTest.java.


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