Flutter Engine
The Flutter Engine
PluginComponentTest.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package test.io.flutter.embedding.engine;
6
7import static junit.framework.TestCase.assertEquals;
8import static org.mockito.Mockito.doAnswer;
9import static org.mockito.Mockito.mock;
10import static org.mockito.Mockito.when;
11
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;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.robolectric.annotation.Config;
22
23@Config(manifest = Config.NONE)
24@RunWith(AndroidJUnit4.class)
25public class PluginComponentTest {
26 boolean jniAttached;
27
28 @Test
30 // Setup test.
31 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
32 FlutterJNI flutterJNI = mock(FlutterJNI.class);
33 jniAttached = false;
34 when(flutterJNI.isAttached()).thenAnswer(invocation -> jniAttached);
35 doAnswer(invocation -> jniAttached = true).when(flutterJNI).attachToNative();
36
37 FlutterLoader flutterLoader = new FlutterLoader(mockFlutterJNI);
38
39 // Execute behavior under test.
40 FlutterEngine flutterEngine =
41 new FlutterEngine(ApplicationProvider.getApplicationContext(), flutterLoader, flutterJNI);
42
43 // As soon as our plugin is registered it will look up asset paths and store them
44 // for our verification.
45 PluginThatAccessesAssets plugin = new PluginThatAccessesAssets();
46 flutterEngine.getPlugins().add(plugin);
47
48 // Verify results.
49 assertEquals("flutter_assets/fake_asset.jpg", plugin.getAssetPathBasedOnName());
50 assertEquals(
51 "flutter_assets/packages/fakepackage/fake_asset.jpg",
52 plugin.getAssetPathBasedOnNameAndPackage());
53 assertEquals("flutter_assets/some/path/fake_asset.jpg", plugin.getAssetPathBasedOnSubpath());
54 assertEquals(
55 "flutter_assets/packages/fakepackage/some/path/fake_asset.jpg",
56 plugin.getAssetPathBasedOnSubpathAndPackage());
57 }
58
59 private static class PluginThatAccessesAssets implements FlutterPlugin {
60 private String assetPathBasedOnName;
61 private String assetPathBasedOnNameAndPackage;
62 private String assetPathBasedOnSubpath;
63 private String assetPathBasedOnSubpathAndPackage;
64
65 public String getAssetPathBasedOnName() {
66 return assetPathBasedOnName;
67 }
68
69 public String getAssetPathBasedOnNameAndPackage() {
70 return assetPathBasedOnNameAndPackage;
71 }
72
73 public String getAssetPathBasedOnSubpath() {
74 return assetPathBasedOnSubpath;
75 }
76
77 public String getAssetPathBasedOnSubpathAndPackage() {
78 return assetPathBasedOnSubpathAndPackage;
79 }
80
81 @Override
82 public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
83 assetPathBasedOnName = binding.getFlutterAssets().getAssetFilePathByName("fake_asset.jpg");
84
85 assetPathBasedOnNameAndPackage =
86 binding.getFlutterAssets().getAssetFilePathByName("fake_asset.jpg", "fakepackage");
87
88 assetPathBasedOnSubpath =
89 binding.getFlutterAssets().getAssetFilePathByName("some/path/fake_asset.jpg");
90
91 assetPathBasedOnSubpathAndPackage =
92 binding
93 .getFlutterAssets()
94 .getAssetFilePathByName("some/path/fake_asset.jpg", "fakepackage");
95 }
96
97 @Override
98 public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {}
99 }
100}
void add(@NonNull FlutterPlugin plugin)