1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.localepicker;
18 
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.app.ActivityManager;
23 import android.app.ActivityThread;
24 import android.content.res.Configuration;
25 import android.os.LocaleList;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 
31 import java.util.Locale;
32 
33 public class LocaleFeatureProviderImplTest {
34     private LocaleFeatureProviderImpl mLocaleFeatureProviderImpl;
35     private Configuration mCacheConfig;
36 
37     @Before
setUp()38     public void setUp() throws Exception {
39         mLocaleFeatureProviderImpl = new LocaleFeatureProviderImpl();
40         // Cache current configuration.
41         mCacheConfig = ActivityManager.getService().getConfiguration();
42     }
43 
44     @After
tearDown()45     public void tearDown() throws Exception {
46         // Recovery the configuration to the current device.
47         ActivityManager.getService().updatePersistentConfigurationWithAttribution(mCacheConfig,
48                 ActivityThread.currentOpPackageName(), null);
49     }
50 
51     @Test
getLocaleNames_hasEnAndZh_resultIsEnglishAndChinese()52     public void getLocaleNames_hasEnAndZh_resultIsEnglishAndChinese() throws Exception {
53         LocaleList locales = LocaleList.forLanguageTags("en-US-u-mu-celsius,zh-TW");
54         final Configuration config = new Configuration();
55         config.setLocales(locales);
56         ActivityManager.getService().updatePersistentConfigurationWithAttribution(config,
57                 ActivityThread.currentOpPackageName(), null);
58 
59         String result = mLocaleFeatureProviderImpl.getLocaleNames().trim();
60 
61         String expected1 =
62                 Locale.forLanguageTag("en-US-u-mu-celsius").stripExtensions().getDisplayName();
63         String expected2 = Locale.forLanguageTag("zh-TW").getDisplayName();
64         assertTrue(result.contains(expected1));
65         assertTrue(result.contains(expected2));
66     }
67 
68     @Test
getLocaleNames_hasExtension_resultWithoutExtensionInfo()69     public void getLocaleNames_hasExtension_resultWithoutExtensionInfo() throws Exception {
70         LocaleList locales = LocaleList.forLanguageTags("en-US-u-mu-celsius,zh-TW");
71         final Configuration config = new Configuration();
72         config.setLocales(locales);
73         ActivityManager.getService().updatePersistentConfigurationWithAttribution(config,
74                 ActivityThread.currentOpPackageName(), null);
75 
76         String result = mLocaleFeatureProviderImpl.getLocaleNames().toLowerCase(Locale.ROOT);
77 
78         assertFalse(result.contains("celsius"));
79     }
80 }
81