|
8 | 8 | import static org.mockito.Mockito.any; |
9 | 9 | import static org.mockito.Mockito.doAnswer; |
10 | 10 | import static org.mockito.Mockito.doReturn; |
| 11 | +import static org.mockito.Mockito.eq; |
| 12 | +import static org.mockito.Mockito.isNull; |
11 | 13 | import static org.mockito.Mockito.mock; |
12 | 14 | import static org.mockito.Mockito.spy; |
| 15 | +import static org.mockito.Mockito.times; |
| 16 | +import static org.mockito.Mockito.verify; |
13 | 17 | import static org.mockito.Mockito.when; |
14 | 18 |
|
15 | 19 | import android.content.Context; |
16 | | -import io.flutter.embedding.engine.dart.DartExecutor; |
| 20 | +import android.content.res.AssetManager; |
| 21 | +import io.flutter.FlutterInjector; |
17 | 22 | import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint; |
18 | 23 | import io.flutter.embedding.engine.loader.FlutterLoader; |
19 | 24 | import io.flutter.plugins.GeneratedPluginRegistrant; |
|
27 | 32 | import org.robolectric.RuntimeEnvironment; |
28 | 33 | import org.robolectric.annotation.Config; |
29 | 34 |
|
30 | | -// It's a component test because it tests both FlutterEngineGroup and FlutterEngine. |
| 35 | +// It's a component test because it tests the FlutterEngineGroup its components such as the |
| 36 | +// FlutterEngine and the DartExecutor. |
31 | 37 | @Config(manifest = Config.NONE) |
32 | 38 | @RunWith(RobolectricTestRunner.class) |
33 | 39 | public class FlutterEngineGroupComponentTest { |
34 | | - @Mock FlutterJNI flutterJNI; |
| 40 | + @Mock FlutterJNI mockflutterJNI; |
| 41 | + @Mock FlutterLoader mockFlutterLoader; |
35 | 42 | FlutterEngineGroup engineGroupUnderTest; |
36 | 43 | FlutterEngine firstEngineUnderTest; |
37 | 44 | boolean jniAttached; |
38 | 45 |
|
39 | 46 | @Before |
40 | 47 | public void setUp() { |
| 48 | + FlutterInjector.reset(); |
| 49 | + |
41 | 50 | MockitoAnnotations.initMocks(this); |
42 | 51 | jniAttached = false; |
43 | | - when(flutterJNI.isAttached()).thenAnswer(invocation -> jniAttached); |
44 | | - doAnswer(invocation -> jniAttached = true).when(flutterJNI).attachToNative(false); |
| 52 | + when(mockflutterJNI.isAttached()).thenAnswer(invocation -> jniAttached); |
| 53 | + doAnswer(invocation -> jniAttached = true).when(mockflutterJNI).attachToNative(false); |
45 | 54 | GeneratedPluginRegistrant.clearRegisteredEngines(); |
46 | 55 |
|
| 56 | + when(mockFlutterLoader.findAppBundlePath()).thenReturn("some/path/to/flutter_assets"); |
| 57 | + FlutterInjector.setInstance( |
| 58 | + new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build()); |
| 59 | + |
47 | 60 | firstEngineUnderTest = |
48 | 61 | spy( |
49 | 62 | new FlutterEngine( |
50 | 63 | RuntimeEnvironment.application, |
51 | 64 | mock(FlutterLoader.class), |
52 | | - flutterJNI, |
| 65 | + mockflutterJNI, |
53 | 66 | /*dartVmArgs=*/ new String[] {}, |
54 | 67 | /*automaticallyRegisterPlugins=*/ false)); |
55 | | - when(firstEngineUnderTest.getDartExecutor()).thenReturn(mock(DartExecutor.class)); |
56 | 68 | engineGroupUnderTest = |
57 | | - new FlutterEngineGroup() { |
| 69 | + new FlutterEngineGroup(RuntimeEnvironment.application) { |
58 | 70 | @Override |
59 | 71 | FlutterEngine createEngine(Context context) { |
60 | 72 | return firstEngineUnderTest; |
@@ -127,4 +139,21 @@ public void canSpawnMoreEngines() { |
127 | 139 | RuntimeEnvironment.application, mock(DartEntrypoint.class)); |
128 | 140 | assertEquals(2, engineGroupUnderTest.activeEngines.size()); |
129 | 141 | } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void canCreateAndRunCustomEntrypoints() { |
| 145 | + FlutterEngine firstEngine = |
| 146 | + engineGroupUnderTest.createAndRunEngine( |
| 147 | + RuntimeEnvironment.application, |
| 148 | + new DartEntrypoint( |
| 149 | + FlutterInjector.instance().flutterLoader().findAppBundlePath(), |
| 150 | + "other entrypoint")); |
| 151 | + assertEquals(1, engineGroupUnderTest.activeEngines.size()); |
| 152 | + verify(mockflutterJNI, times(1)) |
| 153 | + .runBundleAndSnapshotFromLibrary( |
| 154 | + eq("some/path/to/flutter_assets"), |
| 155 | + eq("other entrypoint"), |
| 156 | + isNull(String.class), |
| 157 | + any(AssetManager.class)); |
| 158 | + } |
130 | 159 | } |
0 commit comments