|
| 1 | +package test.io.flutter.embedding.engine; |
| 2 | + |
| 3 | +import android.support.annotation.NonNull; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.mockito.invocation.InvocationOnMock; |
| 8 | +import org.mockito.stubbing.Answer; |
| 9 | +import org.robolectric.RobolectricTestRunner; |
| 10 | +import org.robolectric.RuntimeEnvironment; |
| 11 | +import org.robolectric.annotation.Config; |
| 12 | + |
| 13 | +import io.flutter.embedding.engine.FlutterEngine; |
| 14 | +import io.flutter.embedding.engine.FlutterJNI; |
| 15 | +import io.flutter.embedding.engine.loader.FlutterLoader; |
| 16 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 17 | + |
| 18 | +import static junit.framework.TestCase.assertEquals; |
| 19 | +import static org.mockito.Matchers.any; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +@Config(manifest=Config.NONE) |
| 24 | +@RunWith(RobolectricTestRunner.class) |
| 25 | +public class PluginComponentTest { |
| 26 | + @Test |
| 27 | + public void pluginsCanAccessFlutterAssetPaths() { |
| 28 | + // Setup test. |
| 29 | + FlutterJNI flutterJNI = mock(FlutterJNI.class); |
| 30 | + when(flutterJNI.isAttached()).thenReturn(true); |
| 31 | + |
| 32 | + // FlutterLoader is the object to which the PluginRegistry defers for obtaining |
| 33 | + // the path to a Flutter asset. Ideally in this component test we would use a |
| 34 | + // real FlutterLoader and directly verify the relationship between FlutterAssets |
| 35 | + // and FlutterLoader. However, a real FlutterLoader cannot be used in a JVM test |
| 36 | + // because it would attempt to load native libraries. Therefore, we create a fake |
| 37 | + // FlutterLoader, but then we defer the corresponding asset lookup methods to the |
| 38 | + // real FlutterLoader singleton. This test ends up verifying that when FlutterAssets |
| 39 | + // is queried for an asset path, it returns the real expected path based on real |
| 40 | + // FlutterLoader behavior. |
| 41 | + FlutterLoader flutterLoader = mock(FlutterLoader.class); |
| 42 | + when(flutterLoader.getLookupKeyForAsset(any(String.class))).thenAnswer(new Answer<String>() { |
| 43 | + @Override |
| 44 | + public String answer(InvocationOnMock invocation) throws Throwable { |
| 45 | + // Defer to a real FlutterLoader to return the asset path. |
| 46 | + String fileNameOrSubpath = (String) invocation.getArguments()[0]; |
| 47 | + return FlutterLoader.getInstance().getLookupKeyForAsset(fileNameOrSubpath); |
| 48 | + } |
| 49 | + }); |
| 50 | + when(flutterLoader.getLookupKeyForAsset(any(String.class), any(String.class))).thenAnswer(new Answer<String>() { |
| 51 | + @Override |
| 52 | + public String answer(InvocationOnMock invocation) throws Throwable { |
| 53 | + // Defer to a real FlutterLoader to return the asset path. |
| 54 | + String fileNameOrSubpath = (String) invocation.getArguments()[0]; |
| 55 | + String packageName = (String) invocation.getArguments()[1]; |
| 56 | + return FlutterLoader.getInstance().getLookupKeyForAsset(fileNameOrSubpath, packageName); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // Execute behavior under test. |
| 61 | + FlutterEngine flutterEngine = new FlutterEngine( |
| 62 | + RuntimeEnvironment.application, |
| 63 | + flutterLoader, |
| 64 | + flutterJNI |
| 65 | + ); |
| 66 | + |
| 67 | + // As soon as our plugin is registered it will look up asset paths and store them |
| 68 | + // for our verification. |
| 69 | + PluginThatAccessesAssets plugin = new PluginThatAccessesAssets(); |
| 70 | + flutterEngine.getPlugins().add(plugin); |
| 71 | + |
| 72 | + // Verify results. |
| 73 | + assertEquals("flutter_assets/fake_asset.jpg", plugin.getAssetPathBasedOnName()); |
| 74 | + assertEquals("flutter_assets/packages/fakepackage/fake_asset.jpg", plugin.getAssetPathBasedOnNameAndPackage()); |
| 75 | + assertEquals("flutter_assets/some/path/fake_asset.jpg", plugin.getAssetPathBasedOnSubpath()); |
| 76 | + assertEquals("flutter_assets/packages/fakepackage/some/path/fake_asset.jpg", plugin.getAssetPathBasedOnSubpathAndPackage()); |
| 77 | + } |
| 78 | + |
| 79 | + private static class PluginThatAccessesAssets implements FlutterPlugin { |
| 80 | + private String assetPathBasedOnName; |
| 81 | + private String assetPathBasedOnNameAndPackage; |
| 82 | + private String assetPathBasedOnSubpath; |
| 83 | + private String assetPathBasedOnSubpathAndPackage; |
| 84 | + |
| 85 | + public String getAssetPathBasedOnName() { |
| 86 | + return assetPathBasedOnName; |
| 87 | + } |
| 88 | + |
| 89 | + public String getAssetPathBasedOnNameAndPackage() { |
| 90 | + return assetPathBasedOnNameAndPackage; |
| 91 | + } |
| 92 | + |
| 93 | + public String getAssetPathBasedOnSubpath() { |
| 94 | + return assetPathBasedOnSubpath; |
| 95 | + } |
| 96 | + |
| 97 | + public String getAssetPathBasedOnSubpathAndPackage() { |
| 98 | + return assetPathBasedOnSubpathAndPackage; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { |
| 103 | + assetPathBasedOnName = binding |
| 104 | + .getFlutterAssets() |
| 105 | + .getAssetFilePathByName("fake_asset.jpg"); |
| 106 | + |
| 107 | + assetPathBasedOnNameAndPackage = binding |
| 108 | + .getFlutterAssets() |
| 109 | + .getAssetFilePathByName("fake_asset.jpg", "fakepackage"); |
| 110 | + |
| 111 | + assetPathBasedOnSubpath = binding |
| 112 | + .getFlutterAssets() |
| 113 | + .getAssetFilePathByName("some/path/fake_asset.jpg"); |
| 114 | + |
| 115 | + assetPathBasedOnSubpathAndPackage = binding |
| 116 | + .getFlutterAssets() |
| 117 | + .getAssetFilePathByName("some/path/fake_asset.jpg", "fakepackage"); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {} |
| 122 | + } |
| 123 | +} |
0 commit comments