5package test.io.flutter.embedding.engine;
7import static junit.framework.TestCase.assertEquals;
8import static org.mockito.Mockito.doAnswer;
9import static org.mockito.Mockito.mock;
10import static org.mockito.Mockito.when;
12import androidx.annotation.NonNull;
13import androidx.test.core.app.ApplicationProvider;
14import androidx.test.ext.junit.runners.AndroidJUnit4;
15import io.flutter.embedding.engine.FlutterEngine;
16import io.flutter.embedding.engine.FlutterJNI;
17import io.flutter.embedding.engine.loader.FlutterLoader;
18import io.flutter.embedding.engine.plugins.FlutterPlugin;
20import org.junit.runner.RunWith;
21import org.robolectric.annotation.Config;
24@RunWith(AndroidJUnit4.class)
34 when(flutterJNI.
isAttached()).thenAnswer(invocation -> jniAttached);
35 doAnswer(invocation -> jniAttached =
true).when(flutterJNI).attachToNative();
41 new FlutterEngine(ApplicationProvider.getApplicationContext(), flutterLoader, flutterJNI);
45 PluginThatAccessesAssets plugin =
new PluginThatAccessesAssets();
49 assertEquals(
"flutter_assets/fake_asset.jpg", plugin.getAssetPathBasedOnName());
51 "flutter_assets/packages/fakepackage/fake_asset.jpg",
52 plugin.getAssetPathBasedOnNameAndPackage());
53 assertEquals(
"flutter_assets/some/path/fake_asset.jpg", plugin.getAssetPathBasedOnSubpath());
55 "flutter_assets/packages/fakepackage/some/path/fake_asset.jpg",
56 plugin.getAssetPathBasedOnSubpathAndPackage());
59 private static class PluginThatAccessesAssets
implements FlutterPlugin {
60 private String assetPathBasedOnName;
61 private String assetPathBasedOnNameAndPackage;
62 private String assetPathBasedOnSubpath;
63 private String assetPathBasedOnSubpathAndPackage;
65 public String getAssetPathBasedOnName() {
66 return assetPathBasedOnName;
69 public String getAssetPathBasedOnNameAndPackage() {
70 return assetPathBasedOnNameAndPackage;
73 public String getAssetPathBasedOnSubpath() {
74 return assetPathBasedOnSubpath;
77 public String getAssetPathBasedOnSubpathAndPackage() {
78 return assetPathBasedOnSubpathAndPackage;
82 public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
83 assetPathBasedOnName = binding.getFlutterAssets().getAssetFilePathByName(
"fake_asset.jpg");
85 assetPathBasedOnNameAndPackage =
86 binding.getFlutterAssets().getAssetFilePathByName(
"fake_asset.jpg",
"fakepackage");
88 assetPathBasedOnSubpath =
89 binding.getFlutterAssets().getAssetFilePathByName(
"some/path/fake_asset.jpg");
91 assetPathBasedOnSubpathAndPackage =
94 .getAssetFilePathByName(
"some/path/fake_asset.jpg",
"fakepackage");
98 public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {}
PluginRegistry getPlugins()
void pluginsCanAccessFlutterAssetPaths()
void add(@NonNull FlutterPlugin plugin)