1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16package android.content.res; 17 18import android.os.FileUtils; 19import android.os.LocaleList; 20import android.support.test.filters.SmallTest; 21import android.test.AndroidTestCase; 22import android.util.DisplayMetrics; 23import android.view.Display; 24 25import com.android.frameworks.coretests.R; 26 27import java.io.File; 28import java.io.InputStream; 29import java.util.Arrays; 30import java.util.Locale; 31 32public class ResourcesLocaleTest extends AndroidTestCase { 33 34 private String extractApkAndGetPath(int id) throws Exception { 35 final Resources resources = getContext().getResources(); 36 try (InputStream is = resources.openRawResource(id)) { 37 File path = new File(getContext().getFilesDir(), resources.getResourceEntryName(id)); 38 FileUtils.copyToFileOrThrow(is, path); 39 return path.getAbsolutePath(); 40 } 41 } 42 43 private Resources createResourcesWithApk(int rawApkId) throws Exception { 44 final AssetManager assets = new AssetManager(); 45 assertTrue(assets.addAssetPath(extractApkAndGetPath(rawApkId)) != 0); 46 47 final DisplayMetrics dm = new DisplayMetrics(); 48 dm.setToDefaults(); 49 return new Resources(assets, dm, new Configuration()); 50 } 51 52 private static void ensureNoLanguage(Resources resources, String language) { 53 final String[] supportedLocales = resources.getAssets().getNonSystemLocales(); 54 for (String languageTag : supportedLocales) { 55 if ("en-XA".equals(languageTag)) { 56 continue; 57 } 58 assertFalse( 59 "supported locales: " + Arrays.toString(supportedLocales), 60 language.equals(Locale.forLanguageTag(languageTag).getLanguage())); 61 } 62 } 63 64 @SmallTest 65 public void testEnglishIsAlwaysConsideredSupported() throws Exception { 66 final Resources resources = createResourcesWithApk(R.raw.locales); 67 ensureNoLanguage(resources, "en"); 68 69 final LocaleList preferredLocales = LocaleList.forLanguageTags("en-US,pl-PL"); 70 final Configuration config = new Configuration(); 71 config.setLocales(preferredLocales); 72 73 resources.updateConfiguration(config, null); 74 75 // The APK we loaded has default and Polish languages. If English is first in the list, 76 // always take it the default (assumed to be English). 77 assertEquals(Locale.forLanguageTag("en-US"), 78 resources.getConfiguration().getLocales().get(0)); 79 } 80 81 @SmallTest 82 public void testSelectFirstSupportedLanguage() throws Exception { 83 final Resources resources = createResourcesWithApk(R.raw.locales); 84 ensureNoLanguage(resources, "fr"); 85 86 final LocaleList preferredLocales = LocaleList.forLanguageTags("fr-FR,pl-PL"); 87 final Configuration config = new Configuration(); 88 config.setLocales(preferredLocales); 89 90 resources.updateConfiguration(config, null); 91 92 // The APK we loaded has default and Polish languages. We expect the Polish language to 93 // therefore be chosen. 94 assertEquals(Locale.forLanguageTag("pl-PL"), 95 resources.getConfiguration().getLocales().get(0)); 96 } 97} 98