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 FirstDayOfWeekControllerTest {
35     private Context mApplicationContext;
36     private FirstDayOfWeekController 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 FirstDayOfWeekController(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_resultIsWed()57     public void getSummary_hasProviderValue_resultIsWed() {
58         RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-wed");
59 
60         String summary = mController.getSummary().toString();
61 
62         assertEquals(ResourcesUtils.getResourcesString(
63                 mApplicationContext, "wednesday_first_day_of_week"), summary);
64     }
65 
66     @Test
getSummary_hasProviderValue_resultIsSat()67     public void getSummary_hasProviderValue_resultIsSat() {
68         RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-sat");
69 
70         String summary = mController.getSummary().toString();
71 
72         assertEquals(ResourcesUtils.getResourcesString(
73                 mApplicationContext, "saturday_first_day_of_week"), summary);
74     }
75 
76     @Test
getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat()77     public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat() {
78         RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "");
79         Locale.setDefault(Locale.forLanguageTag("en-US-u-fw-sat"));
80 
81         String summary = mController.getSummary().toString();
82 
83         assertEquals(ResourcesUtils.getResourcesString(
84                 mApplicationContext, "saturday_first_day_of_week"), summary);
85     }
86 
87     @Test
getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsdefault()88     public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsdefault() {
89         RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "");
90         Locale.setDefault(Locale.forLanguageTag("en-US"));
91 
92         String summary = mController.getSummary().toString();
93 
94         assertEquals(ResourcesUtils.getResourcesString(
95                 mApplicationContext, "default_string_of_regional_preference"), summary);
96     }
97 }
98