Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
io.flutter.plugin.localization.LocalizationPluginTest Class Reference

Public Member Functions

void computePlatformResolvedLocaleAPI26 ()
 
void computePlatformResolvedLocale_fromAndroidN ()
 
void computePlatformResolvedLocale_emptySupportedLocales_beforeAndroidN ()
 
void computePlatformResolvedLocale_selectFirstLocaleWhenNoUserSetting_beforeAndroidN ()
 
void computePlatformResolvedLocale_selectFirstLocaleWhenNoExactMatch_beforeAndroidN ()
 
void computePlatformResolvedLocale_selectExactMatchLocale_beforeAndroidN ()
 
void computePlatformResolvedLocale_selectOnlyLanguageLocale_beforeAndroidN ()
 
void localeFromString_languageOnly ()
 
void localeFromString_languageAndCountry ()
 
void localeFromString_languageCountryAndVariant ()
 
void localeFromString_underscore ()
 
void localeFromString_additionalVariantsAreIgnored ()
 
void getStringResource_withoutLocale () throws JSONException
 
void getStringResource_withLocale () throws JSONException
 
void getStringResource_nonExistentKey () throws JSONException
 

Detailed Description

Definition at line 32 of file LocalizationPluginTest.java.

Member Function Documentation

◆ computePlatformResolvedLocale_emptySupportedLocales_beforeAndroidN()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocale_emptySupportedLocales_beforeAndroidN ( )
inline

Definition at line 241 of file LocalizationPluginTest.java.

241 {
242 FlutterJNI flutterJNI = new FlutterJNI();
243 DartExecutor dartExecutor = mock(DartExecutor.class);
244 flutterJNI.setLocalizationPlugin(
245 new LocalizationPlugin(ctx, new LocalizationChannel(dartExecutor)));
246 String[] supportedLocales = new String[] {};
247 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
248 assertEquals(result.length, 0);
249 }
GAsyncResult * result

◆ computePlatformResolvedLocale_fromAndroidN()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocale_fromAndroidN ( )
inline

Definition at line 146 of file LocalizationPluginTest.java.

146 {
147 // --- Test Setup ---
148 FlutterJNI flutterJNI = new FlutterJNI();
149
150 Context context = mock(Context.class);
151 Resources resources = mock(Resources.class);
152 Configuration config = mock(Configuration.class);
153 DartExecutor dartExecutor = mock(DartExecutor.class);
154 LocaleList localeList =
155 new LocaleList(new Locale("es", "MX"), new Locale("zh", "CN"), new Locale("en", "US"));
156 when(context.getResources()).thenReturn(resources);
157 when(resources.getConfiguration()).thenReturn(config);
158 when(config.getLocales()).thenReturn(localeList);
159
160 flutterJNI.setLocalizationPlugin(
161 new LocalizationPlugin(context, new LocalizationChannel(dartExecutor)));
162
163 // Empty supportedLocales.
164 String[] supportedLocales = new String[] {};
165 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
166 assertEquals(result.length, 0);
167
168 // Empty preferredLocales.
169 supportedLocales =
170 new String[] {
171 "fr", "FR", "",
172 "zh", "", "",
173 "en", "CA", ""
174 };
175 localeList = new LocaleList();
176 when(config.getLocales()).thenReturn(localeList);
177 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
178 // The first locale is default.
179 assertEquals(result.length, 3);
180 assertEquals(result[0], "fr");
181 assertEquals(result[1], "FR");
182 assertEquals(result[2], "");
183
184 // Example from https://developer.android.com/guide/topics/resources/multilingual-support#postN
185 supportedLocales =
186 new String[] {
187 "en", "", "",
188 "de", "DE", "",
189 "es", "ES", "",
190 "fr", "FR", "",
191 "it", "IT", ""
192 };
193 localeList = new LocaleList(new Locale("fr", "CH"));
194 when(config.getLocales()).thenReturn(localeList);
195 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
196 // The call will use the new (> API 24) algorithm.
197 assertEquals(result.length, 3);
198 assertEquals(result[0], "fr");
199 assertEquals(result[1], "FR");
200 assertEquals(result[2], "");
201
202 supportedLocales =
203 new String[] {
204 "en", "", "",
205 "de", "DE", "",
206 "es", "ES", "",
207 "fr", "FR", "",
208 "fr", "", "",
209 "it", "IT", ""
210 };
211 localeList = new LocaleList(new Locale("fr", "CH"));
212 when(config.getLocales()).thenReturn(localeList);
213 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
214 // The call will use the new (> API 24) algorithm.
215 assertEquals(result.length, 3);
216 assertEquals(result[0], "fr");
217 assertEquals(result[1], "");
218 assertEquals(result[2], "");
219
220 // Example from https://developer.android.com/guide/topics/resources/multilingual-support#postN
221 supportedLocales =
222 new String[] {
223 "en", "", "",
224 "de", "DE", "",
225 "es", "ES", "",
226 "it", "IT", ""
227 };
228 localeList = new LocaleList(new Locale("fr", "CH"), new Locale("it", "CH"));
229 when(config.getLocales()).thenReturn(localeList);
230 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
231 // The call will use the new (> API 24) algorithm.
232 assertEquals(result.length, 3);
233 assertEquals(result[0], "it");
234 assertEquals(result[1], "IT");
235 assertEquals(result[2], "");
236 }

◆ computePlatformResolvedLocale_selectExactMatchLocale_beforeAndroidN()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocale_selectExactMatchLocale_beforeAndroidN ( )
inline

Definition at line 296 of file LocalizationPluginTest.java.

296 {
297 FlutterJNI flutterJNI = new FlutterJNI();
298 DartExecutor dartExecutor = mock(DartExecutor.class);
299 flutterJNI.setLocalizationPlugin(
300 new LocalizationPlugin(ctx, new LocalizationChannel(dartExecutor)));
301
302 String[] supportedLocales =
303 new String[] {
304 "en", "", "",
305 "de", "DE", "",
306 "es", "ES", "",
307 "fr", "FR", "",
308 "it", "IT", ""
309 };
310 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
311 assertEquals(result.length, 3);
312 assertEquals(result[0], "it");
313 assertEquals(result[1], "IT");
314 assertEquals(result[2], "");
315 }

◆ computePlatformResolvedLocale_selectFirstLocaleWhenNoExactMatch_beforeAndroidN()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocale_selectFirstLocaleWhenNoExactMatch_beforeAndroidN ( )
inline

Definition at line 273 of file LocalizationPluginTest.java.

273 {
274 FlutterJNI flutterJNI = new FlutterJNI();
275 DartExecutor dartExecutor = mock(DartExecutor.class);
276 flutterJNI.setLocalizationPlugin(
277 new LocalizationPlugin(ctx, new LocalizationChannel(dartExecutor)));
278 // Example from https://developer.android.com/guide/topics/resources/multilingual-support#postN
279 String[] supportedLocales =
280 new String[] {
281 "en", "", "",
282 "de", "DE", "",
283 "es", "ES", "",
284 "fr", "FR", "",
285 "it", "IT", ""
286 };
287 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
288 assertEquals(result.length, 3);
289 assertEquals(result[0], "en");
290 assertEquals(result[1], "");
291 assertEquals(result[2], "");
292 }

◆ computePlatformResolvedLocale_selectFirstLocaleWhenNoUserSetting_beforeAndroidN()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocale_selectFirstLocaleWhenNoUserSetting_beforeAndroidN ( )
inline

Definition at line 253 of file LocalizationPluginTest.java.

253 {
254 FlutterJNI flutterJNI = new FlutterJNI();
255 DartExecutor dartExecutor = mock(DartExecutor.class);
256 flutterJNI.setLocalizationPlugin(
257 new LocalizationPlugin(ctx, new LocalizationChannel(dartExecutor)));
258 String[] supportedLocales =
259 new String[] {
260 "fr", "FR", "",
261 "zh", "", "",
262 "en", "CA", ""
263 };
264 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
265 assertEquals(result.length, 3);
266 assertEquals(result[0], "fr");
267 assertEquals(result[1], "FR");
268 assertEquals(result[2], "");
269 }

◆ computePlatformResolvedLocale_selectOnlyLanguageLocale_beforeAndroidN()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocale_selectOnlyLanguageLocale_beforeAndroidN ( )
inline

Definition at line 319 of file LocalizationPluginTest.java.

319 {
320 FlutterJNI flutterJNI = new FlutterJNI();
321 DartExecutor dartExecutor = mock(DartExecutor.class);
322 flutterJNI.setLocalizationPlugin(
323 new LocalizationPlugin(ctx, new LocalizationChannel(dartExecutor)));
324
325 String[] supportedLocales =
326 new String[] {
327 "en", "", "",
328 "de", "DE", "",
329 "es", "ES", "",
330 "fr", "FR", "",
331 "fr", "", "",
332 "it", "IT", ""
333 };
334 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
335 assertEquals(result.length, 3);
336 assertEquals(result[0], "fr");
337 assertEquals(result[1], "");
338 assertEquals(result[2], "");
339 }

◆ computePlatformResolvedLocaleAPI26()

void io.flutter.plugin.localization.LocalizationPluginTest.computePlatformResolvedLocaleAPI26 ( )
inline

Definition at line 38 of file LocalizationPluginTest.java.

38 {
39 // --- Test Setup ---
40 FlutterJNI flutterJNI = new FlutterJNI();
41
42 Context context = mock(Context.class);
43 Resources resources = mock(Resources.class);
44 Configuration config = mock(Configuration.class);
45 DartExecutor dartExecutor = mock(DartExecutor.class);
46 LocaleList localeList =
47 new LocaleList(new Locale("es", "MX"), new Locale("zh", "CN"), new Locale("en", "US"));
48 when(context.getResources()).thenReturn(resources);
49 when(resources.getConfiguration()).thenReturn(config);
50 when(config.getLocales()).thenReturn(localeList);
51
52 flutterJNI.setLocalizationPlugin(
53 new LocalizationPlugin(context, new LocalizationChannel(dartExecutor)));
54
55 // Empty supportedLocales.
56 String[] supportedLocales = new String[] {};
57 String[] result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
58 assertEquals(result.length, 0);
59
60 // Empty preferredLocales.
61 supportedLocales =
62 new String[] {
63 "fr", "FR", "",
64 "zh", "", "",
65 "en", "CA", ""
66 };
67 localeList = new LocaleList();
68 when(config.getLocales()).thenReturn(localeList);
69 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
70 // The first locale is default.
71 assertEquals(result.length, 3);
72 assertEquals(result[0], "fr");
73 assertEquals(result[1], "FR");
74 assertEquals(result[2], "");
75
76 // Example from https://developer.android.com/guide/topics/resources/multilingual-support#postN
77 supportedLocales =
78 new String[] {
79 "en", "", "",
80 "de", "DE", "",
81 "es", "ES", "",
82 "fr", "FR", "",
83 "it", "IT", ""
84 };
85 localeList = new LocaleList(new Locale("fr", "CH"));
86 when(config.getLocales()).thenReturn(localeList);
87 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
88 // The call will use the new (> API 24) algorithm.
89 assertEquals(result.length, 3);
90 assertEquals(result[0], "fr");
91 assertEquals(result[1], "FR");
92 assertEquals(result[2], "");
93
94 supportedLocales =
95 new String[] {
96 "en", "", "",
97 "de", "DE", "",
98 "es", "ES", "",
99 "fr", "FR", "",
100 "fr", "", "",
101 "it", "IT", ""
102 };
103 localeList = new LocaleList(new Locale("fr", "CH"));
104 when(config.getLocales()).thenReturn(localeList);
105 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
106 // The call will use the new (> API 24) algorithm.
107 assertEquals(result.length, 3);
108 assertEquals(result[0], "fr");
109 assertEquals(result[1], "");
110 assertEquals(result[2], "");
111
112 // Example from https://developer.android.com/guide/topics/resources/multilingual-support#postN
113 supportedLocales =
114 new String[] {
115 "en", "", "",
116 "de", "DE", "",
117 "es", "ES", "",
118 "it", "IT", ""
119 };
120 localeList = new LocaleList(new Locale("fr", "CH"), new Locale("it", "CH"));
121 when(config.getLocales()).thenReturn(localeList);
122 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
123 // The call will use the new (> API 24) algorithm.
124 assertEquals(result.length, 3);
125 assertEquals(result[0], "it");
126 assertEquals(result[1], "IT");
127 assertEquals(result[2], "");
128
129 supportedLocales =
130 new String[] {
131 "zh", "CN", "Hans",
132 "zh", "HK", "Hant",
133 };
134 localeList = new LocaleList(new Locale("zh", "CN"));
135 when(config.getLocales()).thenReturn(localeList);
136 result = flutterJNI.computePlatformResolvedLocale(supportedLocales);
137 assertEquals(result.length, 3);
138 assertEquals(result[0], "zh");
139 assertEquals(result[1], "CN");
140 assertEquals(result[2], "Hans");
141 }

◆ getStringResource_nonExistentKey()

void io.flutter.plugin.localization.LocalizationPluginTest.getStringResource_nonExistentKey ( ) throws JSONException
inline

Definition at line 435 of file LocalizationPluginTest.java.

435 {
436 Context context = mock(Context.class);
437 Resources resources = mock(Resources.class);
438 DartExecutor dartExecutor = mock(DartExecutor.class);
439 LocalizationChannel localizationChannel = new LocalizationChannel(dartExecutor);
440 LocalizationPlugin plugin = new LocalizationPlugin(context, localizationChannel);
441
442 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
443
444 String fakePackageName = "package_name";
445 String fakeKey = "test_key";
446
447 when(context.getPackageName()).thenReturn(fakePackageName);
448 when(context.getResources()).thenReturn(resources);
449 when(resources.getIdentifier(fakeKey, "string", fakePackageName))
450 .thenReturn(0); // 0 means not exist
451
452 JSONObject param = new JSONObject();
453 param.put("key", fakeKey);
454
455 localizationChannel.handler.onMethodCall(
456 new MethodCall("Localization.getStringResource", param), mockResult);
457
458 verify(mockResult).success(null);
459 }
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)

◆ getStringResource_withLocale()

void io.flutter.plugin.localization.LocalizationPluginTest.getStringResource_withLocale ( ) throws JSONException
inline

Definition at line 400 of file LocalizationPluginTest.java.

400 {
401 Context context = mock(Context.class);
402 Context localContext = mock(Context.class);
403 Resources resources = mock(Resources.class);
404 Resources localResources = mock(Resources.class);
405 Configuration configuration = new Configuration();
406 DartExecutor dartExecutor = mock(DartExecutor.class);
407 LocalizationChannel localizationChannel = new LocalizationChannel(dartExecutor);
408 LocalizationPlugin plugin = new LocalizationPlugin(context, localizationChannel);
409
410 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
411
412 String fakePackageName = "package_name";
413 String fakeKey = "test_key";
414 int fakeId = 123;
415
416 when(context.getPackageName()).thenReturn(fakePackageName);
417 when(context.createConfigurationContext(any())).thenReturn(localContext);
418 when(context.getResources()).thenReturn(resources);
419 when(localContext.getResources()).thenReturn(localResources);
420 when(resources.getConfiguration()).thenReturn(configuration);
421 when(localResources.getIdentifier(fakeKey, "string", fakePackageName)).thenReturn(fakeId);
422 when(localResources.getString(fakeId)).thenReturn("test_value");
423
424 JSONObject param = new JSONObject();
425 param.put("key", fakeKey);
426 param.put("locale", "en-US");
427
428 localizationChannel.handler.onMethodCall(
429 new MethodCall("Localization.getStringResource", param), mockResult);
430
431 verify(mockResult).success("test_value");
432 }
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530

◆ getStringResource_withoutLocale()

void io.flutter.plugin.localization.LocalizationPluginTest.getStringResource_withoutLocale ( ) throws JSONException
inline

Definition at line 372 of file LocalizationPluginTest.java.

372 {
373 Context context = mock(Context.class);
374 Resources resources = mock(Resources.class);
375 DartExecutor dartExecutor = mock(DartExecutor.class);
376 LocalizationChannel localizationChannel = new LocalizationChannel(dartExecutor);
377 LocalizationPlugin plugin = new LocalizationPlugin(context, localizationChannel);
378
379 MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
380
381 String fakePackageName = "package_name";
382 String fakeKey = "test_key";
383 int fakeId = 123;
384
385 when(context.getPackageName()).thenReturn(fakePackageName);
386 when(context.getResources()).thenReturn(resources);
387 when(resources.getIdentifier(fakeKey, "string", fakePackageName)).thenReturn(fakeId);
388 when(resources.getString(fakeId)).thenReturn("test_value");
389
390 JSONObject param = new JSONObject();
391 param.put("key", fakeKey);
392
393 localizationChannel.handler.onMethodCall(
394 new MethodCall("Localization.getStringResource", param), mockResult);
395
396 verify(mockResult).success("test_value");
397 }

◆ localeFromString_additionalVariantsAreIgnored()

void io.flutter.plugin.localization.LocalizationPluginTest.localeFromString_additionalVariantsAreIgnored ( )
inline

Definition at line 366 of file LocalizationPluginTest.java.

366 {
367 Locale locale = LocalizationPlugin.localeFromString("de-DE-u-co-phonebk");
368 assertEquals(locale, new Locale("de", "DE"));
369 }

◆ localeFromString_languageAndCountry()

void io.flutter.plugin.localization.LocalizationPluginTest.localeFromString_languageAndCountry ( )
inline

Definition at line 348 of file LocalizationPluginTest.java.

348 {
349 Locale locale = LocalizationPlugin.localeFromString("en-US");
350 assertEquals(locale, new Locale("en", "US"));
351 }

◆ localeFromString_languageCountryAndVariant()

void io.flutter.plugin.localization.LocalizationPluginTest.localeFromString_languageCountryAndVariant ( )
inline

Definition at line 354 of file LocalizationPluginTest.java.

354 {
355 Locale locale = LocalizationPlugin.localeFromString("zh-Hans-CN");
356 assertEquals(locale, new Locale("zh", "CN", "Hans"));
357 }

◆ localeFromString_languageOnly()

void io.flutter.plugin.localization.LocalizationPluginTest.localeFromString_languageOnly ( )
inline

Definition at line 342 of file LocalizationPluginTest.java.

342 {
343 Locale locale = LocalizationPlugin.localeFromString("en");
344 assertEquals(locale, new Locale("en"));
345 }

◆ localeFromString_underscore()

void io.flutter.plugin.localization.LocalizationPluginTest.localeFromString_underscore ( )
inline

Definition at line 360 of file LocalizationPluginTest.java.

360 {
361 Locale locale = LocalizationPlugin.localeFromString("zh_Hans_CN");
362 assertEquals(locale, new Locale("zh", "CN", "Hans"));
363 }

The documentation for this class was generated from the following file: