1package io.flutter.plugin.localization;
3import static io.flutter.Build.API_LEVELS;
4import static org.junit.Assert.assertEquals;
5import static org.mockito.Mockito.any;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.verify;
8import static org.mockito.Mockito.when;
10import android.annotation.TargetApi;
12import android.content.res.Configuration;
13import android.content.res.Resources;
15import androidx.test.core.app.ApplicationProvider;
16import androidx.test.ext.junit.runners.AndroidJUnit4;
17import io.flutter.embedding.engine.FlutterJNI;
18import io.flutter.embedding.engine.dart.DartExecutor;
19import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
20import io.flutter.plugin.common.MethodCall;
21import io.flutter.plugin.common.MethodChannel;
22import java.util.Locale;
23import org.json.JSONException;
24import org.json.JSONObject;
26import org.junit.runner.RunWith;
27import org.robolectric.annotation.Config;
30@RunWith(AndroidJUnit4.class)
31@TargetApi(API_LEVELS.API_24)
33 private final Context ctx = ApplicationProvider.getApplicationContext();
38 public
void computePlatformResolvedLocaleAPI26() {
42 Context context = mock(Context.class);
43 Resources resources = mock(Resources.class);
44 Configuration config = mock(Configuration.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);
56 String[] supportedLocales =
new String[] {};
58 assertEquals(
result.length, 0);
67 localeList =
new LocaleList();
68 when(config.getLocales()).thenReturn(localeList);
71 assertEquals(
result.length, 3);
72 assertEquals(
result[0],
"fr");
73 assertEquals(
result[1],
"FR");
74 assertEquals(
result[2],
"");
85 localeList =
new LocaleList(
new Locale(
"fr",
"CH"));
86 when(config.getLocales()).thenReturn(localeList);
89 assertEquals(
result.length, 3);
90 assertEquals(
result[0],
"fr");
91 assertEquals(
result[1],
"FR");
92 assertEquals(
result[2],
"");
103 localeList =
new LocaleList(
new Locale(
"fr",
"CH"));
104 when(config.getLocales()).thenReturn(localeList);
107 assertEquals(
result.length, 3);
108 assertEquals(
result[0],
"fr");
109 assertEquals(
result[1],
"");
110 assertEquals(
result[2],
"");
120 localeList =
new LocaleList(
new Locale(
"fr",
"CH"),
new Locale(
"it",
"CH"));
121 when(config.getLocales()).thenReturn(localeList);
124 assertEquals(
result.length, 3);
125 assertEquals(
result[0],
"it");
126 assertEquals(
result[1],
"IT");
127 assertEquals(
result[2],
"");
134 localeList =
new LocaleList(
new Locale(
"zh",
"CN"));
135 when(config.getLocales()).thenReturn(localeList);
137 assertEquals(
result.length, 3);
138 assertEquals(
result[0],
"zh");
139 assertEquals(
result[1],
"CN");
140 assertEquals(
result[2],
"Hans");
146 public
void computePlatformResolvedLocale_fromAndroidN() {
150 Context context = mock(Context.class);
151 Resources resources = mock(Resources.class);
152 Configuration config = mock(Configuration.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);
164 String[] supportedLocales =
new String[] {};
166 assertEquals(
result.length, 0);
175 localeList =
new LocaleList();
176 when(config.getLocales()).thenReturn(localeList);
179 assertEquals(
result.length, 3);
180 assertEquals(
result[0],
"fr");
181 assertEquals(
result[1],
"FR");
182 assertEquals(
result[2],
"");
193 localeList =
new LocaleList(
new Locale(
"fr",
"CH"));
194 when(config.getLocales()).thenReturn(localeList);
197 assertEquals(
result.length, 3);
198 assertEquals(
result[0],
"fr");
199 assertEquals(
result[1],
"FR");
200 assertEquals(
result[2],
"");
211 localeList =
new LocaleList(
new Locale(
"fr",
"CH"));
212 when(config.getLocales()).thenReturn(localeList);
215 assertEquals(
result.length, 3);
216 assertEquals(
result[0],
"fr");
217 assertEquals(
result[1],
"");
218 assertEquals(
result[2],
"");
228 localeList =
new LocaleList(
new Locale(
"fr",
"CH"),
new Locale(
"it",
"CH"));
229 when(config.getLocales()).thenReturn(localeList);
232 assertEquals(
result.length, 3);
233 assertEquals(
result[0],
"it");
234 assertEquals(
result[1],
"IT");
235 assertEquals(
result[2],
"");
241 public
void computePlatformResolvedLocale_emptySupportedLocales_beforeAndroidN() {
246 String[] supportedLocales =
new String[] {};
248 assertEquals(
result.length, 0);
253 public
void computePlatformResolvedLocale_selectFirstLocaleWhenNoUserSetting_beforeAndroidN() {
258 String[] supportedLocales =
265 assertEquals(
result.length, 3);
266 assertEquals(
result[0],
"fr");
267 assertEquals(
result[1],
"FR");
268 assertEquals(
result[2],
"");
273 public
void computePlatformResolvedLocale_selectFirstLocaleWhenNoExactMatch_beforeAndroidN() {
279 String[] supportedLocales =
288 assertEquals(
result.length, 3);
289 assertEquals(
result[0],
"en");
290 assertEquals(
result[1],
"");
291 assertEquals(
result[2],
"");
296 public
void computePlatformResolvedLocale_selectExactMatchLocale_beforeAndroidN() {
302 String[] supportedLocales =
311 assertEquals(
result.length, 3);
312 assertEquals(
result[0],
"it");
313 assertEquals(
result[1],
"IT");
314 assertEquals(
result[2],
"");
319 public
void computePlatformResolvedLocale_selectOnlyLanguageLocale_beforeAndroidN() {
325 String[] supportedLocales =
335 assertEquals(
result.length, 3);
336 assertEquals(
result[0],
"fr");
337 assertEquals(
result[1],
"");
338 assertEquals(
result[2],
"");
344 assertEquals(locale,
new Locale(
"en"));
350 assertEquals(locale,
new Locale(
"en",
"US"));
356 assertEquals(locale,
new Locale(
"zh",
"CN",
"Hans"));
362 assertEquals(locale,
new Locale(
"zh",
"CN",
"Hans"));
368 assertEquals(locale,
new Locale(
"de",
"DE"));
373 Context context = mock(Context.class);
374 Resources resources = mock(Resources.class);
381 String fakePackageName =
"package_name";
382 String fakeKey =
"test_key";
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");
390 JSONObject param =
new JSONObject();
391 param.put(
"key", fakeKey);
393 localizationChannel.
handler.onMethodCall(
394 new MethodCall(
"Localization.getStringResource", param), mockResult);
396 verify(mockResult).success(
"test_value");
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();
412 String fakePackageName =
"package_name";
413 String fakeKey =
"test_key";
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");
424 JSONObject param =
new JSONObject();
425 param.put(
"key", fakeKey);
426 param.put(
"locale",
"en-US");
428 localizationChannel.
handler.onMethodCall(
429 new MethodCall(
"Localization.getStringResource", param), mockResult);
431 verify(mockResult).success(
"test_value");
436 Context context = mock(Context.class);
437 Resources resources = mock(Resources.class);
444 String fakePackageName =
"package_name";
445 String fakeKey =
"test_key";
447 when(context.getPackageName()).thenReturn(fakePackageName);
448 when(context.getResources()).thenReturn(resources);
449 when(resources.getIdentifier(fakeKey,
"string", fakePackageName))
452 JSONObject param =
new JSONObject();
453 param.put(
"key", fakeKey);
455 localizationChannel.
handler.onMethodCall(
456 new MethodCall(
"Localization.getStringResource", param), mockResult);
458 verify(mockResult).success(
null);
void setLocalizationPlugin(@Nullable LocalizationPlugin localizationPlugin)
String[] computePlatformResolvedLocale(@NonNull String[] strings)
final MethodChannel.MethodCallHandler handler
void getStringResource_nonExistentKey()
void getStringResource_withoutLocale()
void localeFromString_languageOnly()
void localeFromString_underscore()
void getStringResource_withLocale()
void localeFromString_additionalVariantsAreIgnored()
void localeFromString_languageCountryAndVariant()
void localeFromString_languageAndCountry()
static Locale localeFromString(@NonNull String localeString)
SIT bool any(const Vec< 1, T > &x)