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.regionalpreferences; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.os.LocaleList; 23 import android.provider.Settings; 24 25 import androidx.core.text.util.LocalePreferences; 26 import androidx.test.core.app.ApplicationProvider; 27 28 import com.android.internal.app.LocalePicker; 29 import com.android.settings.testutils.ResourcesUtils; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Ignore; 34 import org.junit.Test; 35 36 import java.util.Locale; 37 38 public class RegionalPreferencesDataUtilsTest { 39 private Context mApplicationContext; 40 private String mCacheProviderContent = ""; 41 private Locale mCacheLocale; 42 private LocaleList mCacheLocaleList; 43 44 @Before setUp()45 public void setUp() throws Exception { 46 mApplicationContext = ApplicationProvider.getApplicationContext(); 47 mCacheProviderContent = Settings.System.getString( 48 mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES); 49 mCacheLocale = Locale.getDefault(Locale.Category.FORMAT); 50 mCacheLocaleList = LocaleList.getDefault(); 51 } 52 53 @After tearDown()54 public void tearDown() throws Exception { 55 RegionalPreferenceTestUtils.setSettingsProviderContent( 56 mApplicationContext, mCacheProviderContent); 57 Locale.setDefault(mCacheLocale); 58 LocalePicker.updateLocales(mCacheLocaleList); 59 } 60 61 @Test getDefaultUnicodeExtensionData_hasProviderValue_resultIsCelsius()62 public void getDefaultUnicodeExtensionData_hasProviderValue_resultIsCelsius() { 63 RegionalPreferenceTestUtils.setSettingsProviderContent( 64 mApplicationContext, "und-u-mu-celsius"); 65 66 String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( 67 mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT); 68 69 assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit); 70 } 71 72 @Test getDefaultUnicodeExtensionData_hasDefaultLocaleSubtag_resultIsCelsius()73 public void getDefaultUnicodeExtensionData_hasDefaultLocaleSubtag_resultIsCelsius() { 74 RegionalPreferenceTestUtils.setSettingsProviderContent( 75 mApplicationContext, "und"); 76 Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-celsius")); 77 78 String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( 79 mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT); 80 81 assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit); 82 } 83 84 @Test getDefaultUnicodeExtensionData_noSubtag_resultIsDefault()85 public void getDefaultUnicodeExtensionData_noSubtag_resultIsDefault() { 86 RegionalPreferenceTestUtils.setSettingsProviderContent( 87 mApplicationContext, "und"); 88 Locale.setDefault(Locale.forLanguageTag("en-US")); 89 90 String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( 91 mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT); 92 93 assertEquals(RegionalPreferencesDataUtils.DEFAULT_VALUE, unit); 94 } 95 96 @Test 97 @Ignore("b/265733270") savePreference_saveCalendarIsDangi_success()98 public void savePreference_saveCalendarIsDangi_success() { 99 RegionalPreferencesDataUtils.savePreference( 100 mApplicationContext, 101 ExtensionTypes.CALENDAR, 102 LocalePreferences.CalendarType.DANGI 103 ); 104 String providerContent = Settings.System.getString( 105 mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES); 106 Locale locale = Locale.forLanguageTag(providerContent); 107 108 109 String result1 = locale.getUnicodeLocaleType(ExtensionTypes.CALENDAR); 110 111 assertEquals(LocalePreferences.CalendarType.DANGI, result1); 112 113 String result2 = Locale.getDefault(Locale.Category.FORMAT) 114 .getUnicodeLocaleType(ExtensionTypes.CALENDAR); 115 116 assertEquals(LocalePreferences.CalendarType.DANGI, result2); 117 118 } 119 120 @Test temperatureUnitsConverter_inputFahrenheit_resultIsFahrenheitString()121 public void temperatureUnitsConverter_inputFahrenheit_resultIsFahrenheitString() { 122 String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext, 123 LocalePreferences.TemperatureUnit.FAHRENHEIT); 124 125 assertEquals(ResourcesUtils.getResourcesString( 126 mApplicationContext, "fahrenheit_temperature_unit"), result); 127 } 128 129 @Test temperatureUnitsConverter_inputDefault_resultIsDefaultString()130 public void temperatureUnitsConverter_inputDefault_resultIsDefaultString() { 131 String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext, 132 RegionalPreferencesDataUtils.DEFAULT_VALUE); 133 134 assertEquals(ResourcesUtils.getResourcesString( 135 mApplicationContext, "default_string_of_regional_preference"), result); 136 } 137 138 @Test dayConverter_inputWed_resultIsWedString()139 public void dayConverter_inputWed_resultIsWedString() { 140 String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext, 141 LocalePreferences.FirstDayOfWeek.WEDNESDAY); 142 143 assertEquals(ResourcesUtils.getResourcesString( 144 mApplicationContext, "wednesday_first_day_of_week"), result); 145 } 146 147 @Test dayConverter_inputDefault_resultIsDefaultString()148 public void dayConverter_inputDefault_resultIsDefaultString() { 149 String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext, 150 RegionalPreferencesDataUtils.DEFAULT_VALUE); 151 152 assertEquals(ResourcesUtils.getResourcesString( 153 mApplicationContext, "default_string_of_regional_preference"), result); 154 } 155 156 @Test calendarConverter_inputDefault_resultIsDefaultString()157 public void calendarConverter_inputDefault_resultIsDefaultString() { 158 String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext, 159 RegionalPreferencesDataUtils.DEFAULT_VALUE); 160 161 assertEquals(ResourcesUtils.getResourcesString( 162 mApplicationContext, "default_string_of_regional_preference"), result); 163 } 164 } 165