Flutter Engine
The Flutter Engine
FlutterJNITest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine;
2
3import static io.flutter.Build.API_LEVELS;
4import static org.junit.Assert.assertEquals;
5import static org.mockito.Mockito.mock;
6import static org.mockito.Mockito.spy;
7import static org.mockito.Mockito.times;
8import static org.mockito.Mockito.verify;
9import static org.mockito.Mockito.when;
10
11import android.annotation.TargetApi;
12import android.content.Context;
13import android.content.res.Configuration;
14import android.content.res.Resources;
15import android.os.LocaleList;
16import androidx.test.ext.junit.runners.AndroidJUnit4;
17import io.flutter.embedding.engine.dart.DartExecutor;
18import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorsStack;
19import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
20import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
21import io.flutter.plugin.localization.LocalizationPlugin;
22import io.flutter.plugin.platform.PlatformViewsController;
23import java.nio.ByteBuffer;
24import java.util.Locale;
25import java.util.concurrent.atomic.AtomicInteger;
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.robolectric.annotation.Config;
29
30@Config(manifest = Config.NONE)
31@RunWith(AndroidJUnit4.class)
32@TargetApi(API_LEVELS.API_24) // LocaleList and scriptCode are API 24+.
33public class FlutterJNITest {
34 @Test
36 // --- Test Setup ---
37 FlutterJNI flutterJNI = new FlutterJNI();
38
39 AtomicInteger callbackInvocationCount = new AtomicInteger(0);
42 @Override
43 public void onFlutterUiDisplayed() {
44 callbackInvocationCount.incrementAndGet();
46 }
47
48 @Override
49 public void onFlutterUiNoLongerDisplayed() {}
50 };
52
53 // --- Execute Test ---
54 flutterJNI.onFirstFrame();
55
56 // --- Verify Results ---
57 assertEquals(1, callbackInvocationCount.get());
58
59 // --- Execute Test ---
60 // The callback removed itself from the listener list. A second call doesn't call the callback.
61 flutterJNI.onFirstFrame();
62
63 // --- Verify Results ---
64 assertEquals(1, callbackInvocationCount.get());
65 }
66
67 @Test
69 // --- Test Setup ---
70 FlutterJNI flutterJNI = new FlutterJNI();
71
72 Context context = mock(Context.class);
73 Resources resources = mock(Resources.class);
74 Configuration config = mock(Configuration.class);
75 DartExecutor dartExecutor = mock(DartExecutor.class);
76 LocaleList localeList =
77 new LocaleList(new Locale("es", "MX"), new Locale("zh", "CN"), new Locale("en", "US"));
78 when(context.getResources()).thenReturn(resources);
79 when(resources.getConfiguration()).thenReturn(config);
80 when(config.getLocales()).thenReturn(localeList);
81
82 flutterJNI.setLocalizationPlugin(
83 new LocalizationPlugin(context, new LocalizationChannel(dartExecutor)));
84 String[] supportedLocales =
85 new String[] {
86 "fr", "FR", "",
87 "zh", "", "",
88 "en", "CA", ""
89 };
90 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
91 assertEquals(result.length, 3);
92 assertEquals(result[0], "zh");
93 assertEquals(result[1], "");
94 assertEquals(result[2], "");
95
96 supportedLocales =
97 new String[] {
98 "fr", "FR", "",
99 "ar", "", "",
100 "en", "CA", ""
101 };
102 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
103 assertEquals(result.length, 3);
104 assertEquals(result[0], "en");
105 assertEquals(result[1], "CA");
106 assertEquals(result[2], "");
107
108 supportedLocales =
109 new String[] {
110 "fr", "FR", "",
111 "ar", "", "",
112 "en", "US", ""
113 };
114 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
115 assertEquals(result.length, 3);
116 assertEquals(result[0], "en");
117 assertEquals(result[1], "US");
118 assertEquals(result[2], "");
119
120 supportedLocales =
121 new String[] {
122 "ar", "", "",
123 "es", "MX", "",
124 "en", "US", ""
125 };
126 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
127 assertEquals(result.length, 3);
128 assertEquals(result[0], "es");
129 assertEquals(result[1], "MX");
130 assertEquals(result[2], "");
131
132 // Empty supportedLocales.
133 supportedLocales = new String[] {};
134 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
135 assertEquals(result.length, 0);
136
137 // Empty preferredLocales.
138 supportedLocales =
139 new String[] {
140 "fr", "FR", "",
141 "zh", "", "",
142 "en", "CA", ""
143 };
144 localeList = new LocaleList();
145 when(config.getLocales()).thenReturn(localeList);
146 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
147 // The first locale is default.
148 assertEquals(result.length, 3);
149 assertEquals(result[0], "fr");
150 assertEquals(result[1], "FR");
151 assertEquals(result[2], "");
152 }
153
155 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
156
157 FlutterJNI flutterJNI = new FlutterJNI();
158 flutterJNI.setPlatformViewsController(platformViewsController);
160 // --- Execute Test ---
161 flutterJNI.onDisplayPlatformView(
162 /*viewId=*/ 1,
163 /*x=*/ 10,
164 /*y=*/ 20,
165 /*width=*/ 100,
166 /*height=*/ 200,
167 /*viewWidth=*/ 100,
168 /*viewHeight=*/ 200,
169 /*mutatorsStack=*/ stack);
170
171 // --- Verify Results ---
172 verify(platformViewsController, times(1))
173 .onDisplayPlatformView(
174 /*viewId=*/ 1,
175 /*x=*/ 10,
176 /*y=*/ 20,
177 /*width=*/ 100,
178 /*height=*/ 200,
179 /*viewWidth=*/ 100,
180 /*viewHeight=*/ 200,
181 /*mutatorsStack=*/ stack);
182 }
183
184 @Test
186 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
187
188 FlutterJNI flutterJNI = new FlutterJNI();
189 flutterJNI.setPlatformViewsController(platformViewsController);
190
191 // --- Execute Test ---
192 flutterJNI.onDisplayOverlaySurface(
193 /*id=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200);
194
195 // --- Verify Results ---
196 verify(platformViewsController, times(1))
197 .onDisplayOverlaySurface(/*id=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200);
198 }
199
200 @Test
202 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
203
204 // --- Test Setup ---
205 FlutterJNI flutterJNI = new FlutterJNI();
206 flutterJNI.setPlatformViewsController(platformViewsController);
207
208 // --- Execute Test ---
209 flutterJNI.onBeginFrame();
210
211 // --- Verify Results ---
212 verify(platformViewsController, times(1)).onBeginFrame();
213 }
214
215 @Test
217 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
218
219 // --- Test Setup ---
220 FlutterJNI flutterJNI = new FlutterJNI();
221 flutterJNI.setPlatformViewsController(platformViewsController);
222
223 // --- Execute Test ---
224 flutterJNI.onEndFrame();
225
226 // --- Verify Results ---
227 verify(platformViewsController, times(1)).onEndFrame();
228 }
229
230 @Test
232 PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
233
234 FlutterJNI flutterJNI = new FlutterJNI();
235 flutterJNI.setPlatformViewsController(platformViewsController);
236
237 // --- Execute Test ---
238 flutterJNI.createOverlaySurface();
239
240 // --- Verify Results ---
241 verify(platformViewsController, times(1)).createOverlaySurface();
242 }
243
244 @Test(expected = IllegalArgumentException.class)
245 public void invokePlatformMessageResponseCallback_wantsDirectBuffer() {
246 FlutterJNI flutterJNI = new FlutterJNI();
247 ByteBuffer buffer = ByteBuffer.allocate(4);
248 flutterJNI.invokePlatformMessageResponseCallback(0, buffer, buffer.position());
249 }
250
251 @Test
253 FlutterJNI flutterJNI = spy(new FlutterJNI());
254 // --- Execute Test ---
255 flutterJNI.setRefreshRateFPS(120.0f);
256 // --- Verify Results ---
257 verify(flutterJNI, times(1)).updateRefreshRate();
258 }
259}
static SkISize times(const SkISize &size, float factor)
void addIsDisplayingFlutterUiListener(@NonNull FlutterUiDisplayListener listener)
void setPlatformViewsController(@NonNull PlatformViewsController platformViewsController)
void setLocalizationPlugin(@Nullable LocalizationPlugin localizationPlugin)
FlutterOverlaySurface createOverlaySurface()
void setRefreshRateFPS(float refreshRateFPS)
void removeIsDisplayingFlutterUiListener(@NonNull FlutterUiDisplayListener listener)
void onDisplayOverlaySurface(int id, int x, int y, int width, int height)
void invokePlatformMessageResponseCallback(int responseId, @NonNull ByteBuffer message, int position)
String[] computePlatformResolvedLocale(@NonNull String[] strings)
void onDisplayPlatformView(int viewId, int x, int y, int width, int height, int viewWidth, int viewHeight, FlutterMutatorsStack mutatorsStack)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126