22
33import static junit .framework .TestCase .assertEquals ;
44import static org .mockito .Matchers .any ;
5+ import static org .mockito .Mockito .doNothing ;
56import static org .mockito .Mockito .mock ;
7+ import static org .mockito .Mockito .spy ;
68import static org .mockito .Mockito .when ;
79
810import androidx .annotation .NonNull ;
1315import io .flutter .embedding .engine .plugins .FlutterPlugin ;
1416import org .junit .Test ;
1517import org .junit .runner .RunWith ;
16- import org .mockito .invocation .InvocationOnMock ;
17- import org .mockito .stubbing .Answer ;
1818import org .robolectric .RobolectricTestRunner ;
1919import org .robolectric .RuntimeEnvironment ;
2020import org .robolectric .annotation .Config ;
@@ -30,43 +30,17 @@ public void pluginsCanAccessFlutterAssetPaths() {
3030 FlutterApplicationInfo emptyInfo =
3131 new FlutterApplicationInfo (null , null , null , null , null , null , false , false );
3232
33- // FlutterLoader is the object to which the PluginRegistry defers for obtaining
34- // the path to a Flutter asset. Ideally in this component test we would use a
35- // real FlutterLoader and directly verify the relationship between FlutterAssets
36- // and FlutterLoader. However, a real FlutterLoader cannot be used in a JVM test
37- // because it would attempt to load native libraries. Therefore, we create a fake
38- // FlutterLoader, but then we defer the corresponding asset lookup methods to the
39- // real FlutterLoader singleton. This test ends up verifying that when FlutterAssets
40- // is queried for an asset path, it returns the real expected path based on real
41- // FlutterLoader behavior.
42- FlutterLoader flutterLoader = mock (FlutterLoader .class );
43- when (flutterLoader .getLookupKeyForAsset (any (String .class )))
44- .thenAnswer (
45- new Answer <String >() {
46- @ Override
47- public String answer (InvocationOnMock invocation ) throws Throwable {
48- // Defer to a real FlutterLoader to return the asset path.
49- String fileNameOrSubpath = (String ) invocation .getArguments ()[0 ];
50- return FlutterLoader .getInstanceForTest (emptyInfo )
51- .getLookupKeyForAsset (fileNameOrSubpath );
52- }
53- });
54- when (flutterLoader .getLookupKeyForAsset (any (String .class ), any (String .class )))
55- .thenAnswer (
56- new Answer <String >() {
57- @ Override
58- public String answer (InvocationOnMock invocation ) throws Throwable {
59- // Defer to a real FlutterLoader to return the asset path.
60- String fileNameOrSubpath = (String ) invocation .getArguments ()[0 ];
61- String packageName = (String ) invocation .getArguments ()[1 ];
62- return FlutterLoader .getInstanceForTest (emptyInfo )
63- .getLookupKeyForAsset (fileNameOrSubpath , packageName );
64- }
65- });
33+ FlutterLoader realFlutterLoader = new FlutterLoader ();
34+ FlutterLoader spyFlutterLoader = spy (realFlutterLoader );
35+
36+ // Let the real startInitialization be called, but mock the rest so it doesn't try to load
37+ // the real native library.
38+ doNothing ().when (spyFlutterLoader ).loadNativeLibrary ();
39+ doNothing ().when (spyFlutterLoader ).ensureInitializationComplete (any (), any ());
6640
6741 // Execute behavior under test.
6842 FlutterEngine flutterEngine =
69- new FlutterEngine (RuntimeEnvironment .application , flutterLoader , flutterJNI );
43+ new FlutterEngine (RuntimeEnvironment .application , spyFlutterLoader , flutterJNI );
7044
7145 // As soon as our plugin is registered it will look up asset paths and store them
7246 // for our verification.
0 commit comments