1 /* 2 * Copyright (C) 2022 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.platform.test.annotations.DisableFlags; 23 import android.platform.test.flag.junit.SetFlagsRule; 24 import android.provider.Settings; 25 26 import androidx.test.core.app.ApplicationProvider; 27 28 import com.android.settings.flags.Flags; 29 import com.android.settings.testutils.ResourcesUtils; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Rule; 34 import org.junit.Test; 35 36 import java.util.Locale; 37 38 public class TemperatureUnitControllerTest { 39 private Context mApplicationContext; 40 private TemperatureUnitController mController; 41 private String mCacheProviderContent = ""; 42 private Locale mCacheLocale; 43 44 @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 45 46 @Before setUp()47 public void setUp() throws Exception { 48 mApplicationContext = ApplicationProvider.getApplicationContext(); 49 mController = new TemperatureUnitController(mApplicationContext, "key"); 50 mCacheProviderContent = Settings.System.getString( 51 mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES); 52 mCacheLocale = Locale.getDefault(Locale.Category.FORMAT); 53 } 54 55 @After tearDown()56 public void tearDown() throws Exception { 57 RegionalPreferenceTestUtils.setSettingsProviderContent( 58 mApplicationContext, mCacheProviderContent); 59 Locale.setDefault(mCacheLocale); 60 } 61 62 @Test 63 @DisableFlags(Flags.FLAG_REGIONAL_PREFERENCES_API_ENABLED) getSummary_hasProviderValue_resultIsCelsius()64 public void getSummary_hasProviderValue_resultIsCelsius() { 65 RegionalPreferenceTestUtils.setSettingsProviderContent( 66 mApplicationContext, "und-u-mu-celsius"); 67 68 String summary = mController.getSummary().toString(); 69 70 assertEquals(ResourcesUtils.getResourcesString( 71 mApplicationContext, "celsius_temperature_unit"), summary); 72 } 73 74 @Test 75 @DisableFlags(Flags.FLAG_REGIONAL_PREFERENCES_API_ENABLED) getSummary_hasProviderValue_resultIsFahrenheit()76 public void getSummary_hasProviderValue_resultIsFahrenheit() { 77 RegionalPreferenceTestUtils.setSettingsProviderContent( 78 mApplicationContext, "und-u-mu-fahrenhe"); 79 80 String summary = mController.getSummary().toString(); 81 82 assertEquals(ResourcesUtils.getResourcesString( 83 mApplicationContext, "fahrenheit_temperature_unit"), summary); 84 } 85 86 @Test 87 @DisableFlags(Flags.FLAG_REGIONAL_PREFERENCES_API_ENABLED) getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsFahrenheit()88 public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsFahrenheit() { 89 RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); 90 Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-fahrenhe")); 91 92 String summary = mController.getSummary().toString(); 93 94 assertEquals(ResourcesUtils.getResourcesString( 95 mApplicationContext, "fahrenheit_temperature_unit"), summary); 96 } 97 98 @Test 99 @DisableFlags(Flags.FLAG_REGIONAL_PREFERENCES_API_ENABLED) getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsDefault()100 public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsDefault() { 101 RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); 102 Locale.setDefault(Locale.forLanguageTag("en-US")); 103 104 String summary = mController.getSummary().toString(); 105 106 assertEquals(ResourcesUtils.getResourcesString( 107 mApplicationContext, "default_string_of_regional_preference"), summary); 108 } 109 } 110