5package io.flutter.embedding.engine;
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertTrue;
9import static org.mockito.Mockito.any;
10import static org.mockito.Mockito.doAnswer;
11import static org.mockito.Mockito.doReturn;
12import static org.mockito.Mockito.eq;
13import static org.mockito.Mockito.isNull;
14import static org.mockito.Mockito.mock;
15import static org.mockito.Mockito.nullable;
16import static org.mockito.Mockito.spy;
17import static org.mockito.Mockito.times;
18import static org.mockito.Mockito.verify;
19import static org.mockito.Mockito.when;
22import android.content.res.AssetManager;
23import androidx.test.core.app.ApplicationProvider;
24import androidx.test.ext.junit.runners.AndroidJUnit4;
25import io.flutter.FlutterInjector;
26import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint;
27import io.flutter.embedding.engine.loader.FlutterLoader;
28import io.flutter.embedding.engine.systemchannels.NavigationChannel;
29import io.flutter.plugin.platform.PlatformViewsController;
30import io.flutter.plugins.GeneratedPluginRegistrant;
31import java.util.ArrayList;
33import org.junit.After;
34import org.junit.Before;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39import org.robolectric.annotation.Config;
44@RunWith(AndroidJUnit4.class)
46 private final Context ctx = ApplicationProvider.getApplicationContext();
57 MockitoAnnotations.openMocks(
this);
59 when(mockFlutterJNI.isAttached()).thenAnswer(invocation -> jniAttached);
60 doAnswer(invocation -> jniAttached =
true).when(mockFlutterJNI).attachToNative();
63 when(mockFlutterLoader.findAppBundlePath()).thenReturn(
"some/path/to/flutter_assets");
65 FlutterJNI.Factory jniFactory =
73 return mockFlutterJNI;
83 firstEngineUnderTest =
91 engineGroupUnderTest =
97 boolean automaticallyRegisterPlugins,
98 boolean waitForRestorationData) {
99 return firstEngineUnderTest;
107 engineGroupUnderTest =
null;
108 firstEngineUnderTest =
null;
134 assertEquals(firstEngine, secondEngine);
148 nullable(String.class),
149 nullable(
List.class),
163 when(secondEngine.
spawn(
166 nullable(String.class),
167 nullable(
List.class),
185 "other entrypoint"));
187 verify(mockFlutterJNI,
times(1))
188 .runBundleAndSnapshotFromLibrary(
189 eq(
"some/path/to/flutter_assets"),
190 eq(
"other entrypoint"),
192 any(AssetManager.class),
193 nullable(
List.class));
205 when(mockFlutterJNI.isAttached()).thenReturn(
true);
208 when(secondMockFlutterJNI.
isAttached()).thenAnswer(invocation -> jniAttached);
209 doAnswer(invocation -> jniAttached =
true).when(secondMockFlutterJNI).attachToNative();
210 doReturn(secondMockFlutterJNI)
211 .when(mockFlutterJNI)
213 nullable(String.class),
214 nullable(String.class),
215 nullable(String.class),
216 nullable(
List.class));
222 verify(mockFlutterJNI,
times(1))
223 .spawn(nullable(String.class), nullable(String.class),
eq(
"/bar"), nullable(
List.class));
228 List<String> firstDartEntrypointArgs =
new ArrayList<String>();
235 verify(mockFlutterJNI,
times(1))
236 .runBundleAndSnapshotFromLibrary(
237 nullable(String.class),
238 nullable(String.class),
240 any(AssetManager.class),
241 eq(firstDartEntrypointArgs));
243 when(mockFlutterJNI.isAttached()).thenReturn(
true);
246 when(secondMockFlutterJNI.
isAttached()).thenAnswer(invocation -> jniAttached);
247 doAnswer(invocation -> jniAttached =
true).when(secondMockFlutterJNI).attachToNative();
248 doReturn(secondMockFlutterJNI)
249 .when(mockFlutterJNI)
251 nullable(String.class),
252 nullable(String.class),
253 nullable(String.class),
254 nullable(
List.class));
255 List<String> secondDartEntrypointArgs =
new ArrayList<String>();
263 verify(mockFlutterJNI,
times(1))
265 nullable(String.class),
266 nullable(String.class),
267 nullable(String.class),
268 eq(secondDartEntrypointArgs));
278 boolean waitForRestorationData =
true;
279 boolean automaticallyRegisterPlugins =
true;
306 verify(mockFlutterJNI,
times(1))
307 .runBundleAndSnapshotFromLibrary(
308 nullable(String.class),
309 nullable(String.class),
311 any(AssetManager.class),
312 nullable(
List.class));
314 when(mockFlutterJNI.isAttached()).thenReturn(
true);
317 when(secondMockFlutterJNI.
isAttached()).thenAnswer(invocation -> jniAttached);
318 doAnswer(invocation -> jniAttached =
true).when(secondMockFlutterJNI).attachToNative();
319 doReturn(secondMockFlutterJNI)
320 .when(mockFlutterJNI)
322 nullable(String.class),
323 nullable(String.class),
324 nullable(String.class),
325 nullable(
List.class));
328 boolean waitForRestorationData =
false;
329 boolean automaticallyRegisterPlugins =
false;
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Builder setFlutterLoader(@NonNull FlutterLoader flutterLoader)
Builder setFlutterJNIFactory(@NonNull FlutterJNI.Factory factory)
static void setInstance(@NonNull FlutterInjector injector)
FlutterLoader mockFlutterLoader
void canSpawnMoreEngines()
void canCreateAndRunWithCustomEntrypointArgs()
FlutterEngine firstEngineUnderTest
void canCreateAndRunWithCustomInitialRoute()
void spawnEngineSupportMoreParams()
void canRecreateEngines()
void canCreateAndRunCustomEntrypoints()
void createEngineSupportMoreParams()
FlutterEngineGroup engineGroupUnderTest
void listensToEngineDestruction()
FlutterJNI mockFlutterJNI
Options setDartEntrypointArgs(List< String > dartEntrypointArgs)
Options setPlatformViewsController( @NonNull PlatformViewsController platformViewsController)
Options setWaitForRestorationData(boolean waitForRestorationData)
Options setDartEntrypoint(DartEntrypoint dartEntrypoint)
Options setAutomaticallyRegisterPlugins(boolean automaticallyRegisterPlugins)
final List< FlutterEngine > activeEngines
FlutterEngine createAndRunEngine( @NonNull Context context, @Nullable DartEntrypoint dartEntrypoint)
PlatformViewsController getPlatformViewsController()
RestorationChannel getRestorationChannel()
NavigationChannel getNavigationChannel()
FlutterEngine spawn( @NonNull Context context, @NonNull DartEntrypoint dartEntrypoint, @Nullable String initialRoute, @Nullable List< String > dartEntrypointArgs, @Nullable PlatformViewsController platformViewsController, boolean automaticallyRegisterPlugins, boolean waitForRestorationData)
boolean automaticallyRegisterPlugins()
String findAppBundlePath()
final boolean waitForRestorationData
static void clearRegisteredEngines()
static List< FlutterEngine > getRegisteredEngines()
SIT bool any(const Vec< 1, T > &x)