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