1 /* 2 * Copyright (C) 2024 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 android.car.extendedapitest; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.app.LocaleManager; 24 import android.car.extendedapitest.testbase.CarApiTestBase; 25 import android.os.LocaleList; 26 import android.util.Log; 27 28 import com.android.bedstead.harrier.DeviceState; 29 import com.android.bedstead.multiuser.annotations.RequireRunNotOnVisibleBackgroundNonProfileUser; 30 import com.android.bedstead.multiuser.annotations.RequireRunOnVisibleBackgroundNonProfileUser; 31 import com.android.bedstead.multiuser.annotations.RequireVisibleBackgroundUsers; 32 import com.android.compatibility.common.util.ApiTest; 33 import com.android.internal.app.LocalePicker; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.ClassRule; 38 import org.junit.Rule; 39 import org.junit.Test; 40 41 42 import java.util.Locale; 43 import java.util.Objects; 44 45 public final class LocalePickerTest extends CarApiTestBase { 46 47 private static final String TAG = LocalePickerTest.class.getSimpleName(); 48 49 @Rule 50 @ClassRule 51 public static final DeviceState sDeviceState = new DeviceState(); 52 53 private final LocaleManager mLocaleManager = getContext().getSystemService(LocaleManager.class); 54 LocaleList mPreviousLocaleList; 55 56 @Before setUp()57 public void setUp() { 58 mPreviousLocaleList = mLocaleManager.getSystemLocales(); 59 } 60 61 @After cleanUp()62 public void cleanUp() { 63 try { 64 LocalePicker.updateLocales(mPreviousLocaleList); 65 } catch (Exception e) { 66 Log.e(TAG, "Failed to clean up", e); 67 } 68 } 69 70 @Test 71 @ApiTest(apis = {"android.internal.app.LocalePicker#updateLocale"}) 72 @RequireVisibleBackgroundUsers(reason = "Locale test for only MUMD devices") 73 @RequireRunNotOnVisibleBackgroundNonProfileUser testUpdateLocaleForCurrentUser()74 public void testUpdateLocaleForCurrentUser() throws Exception { 75 Locale localeToSet = getLocaleToSetInTest(); 76 77 LocalePicker.updateLocale(localeToSet); 78 79 assertSystemLocale(localeToSet); 80 } 81 82 83 @Test 84 @ApiTest(apis = {"android.internal.app.LocalePicker#updateLocale"}) 85 @RequireVisibleBackgroundUsers(reason = "Locale test for only MUMD devices") 86 @RequireRunOnVisibleBackgroundNonProfileUser testUpdateLocaleForVisibleBackgroundUserUser()87 public void testUpdateLocaleForVisibleBackgroundUserUser() throws Exception { 88 Locale localeToSet = getLocaleToSetInTest(); 89 90 assertThrows(SecurityException.class, () -> LocalePicker.updateLocale(localeToSet)); 91 } 92 getLocaleToSetInTest()93 private Locale getLocaleToSetInTest() { 94 if (Objects.equals(mPreviousLocaleList.get(0).getLanguage(), "en")) { 95 // If the current language is english, set the language to Hindi for testing. 96 return new Locale("hi", "IN"); 97 } else { 98 // If the current language is not english, set the language to english for testing. 99 return new Locale("en", "US"); 100 } 101 } 102 assertSystemLocale(Locale locale)103 private void assertSystemLocale(Locale locale) { 104 Locale systemLocale = mLocaleManager.getSystemLocales().get(0); 105 assertThat(systemLocale.getLanguage()).isEqualTo(locale.getLanguage()); 106 assertThat(systemLocale.getCountry()).isEqualTo(locale.getCountry()); 107 } 108 } 109