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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.provider.Settings;
25 
26 import androidx.preference.PreferenceScreen;
27 import androidx.test.core.app.ApplicationProvider;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnit;
38 import org.mockito.junit.MockitoRule;
39 import org.robolectric.RobolectricTestRunner;
40 
41 /** Tests for {@link CaptioningLocalePreferenceController}. */
42 @RunWith(RobolectricTestRunner.class)
43 public class CaptioningLocalePreferenceControllerTest {
44 
45     @Rule
46     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
47     @Mock
48     private PreferenceScreen mScreen;
49     private final Context mContext = ApplicationProvider.getApplicationContext();
50     private CaptioningLocalePreferenceController mController;
51     private LocalePreference mPreference;
52 
53     @Before
setUp()54     public void setUp() {
55         mController = new CaptioningLocalePreferenceController(mContext, "captioning_local_pref");
56         mPreference = new LocalePreference(mContext);
57         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
58     }
59 
60     @Test
getAvailabilityStatus_shouldReturnAvailable()61     public void getAvailabilityStatus_shouldReturnAvailable() {
62         assertThat(mController.getAvailabilityStatus())
63                 .isEqualTo(BasePreferenceController.AVAILABLE);
64     }
65 
66     @Test
displayPreference_byDefault_shouldReturnDefault()67     public void displayPreference_byDefault_shouldReturnDefault() {
68         mController.displayPreference(mScreen);
69 
70         assertThat(mPreference.getEntry().toString()).isEqualTo(
71                 mContext.getResources().getString(R.string.locale_default));
72     }
73 
74     @Test
displayPreference_byArabicLocale_shouldReturnArabic()75     public void displayPreference_byArabicLocale_shouldReturnArabic() {
76         Settings.Secure.putString(mContext.getContentResolver(),
77                 Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, "af_ZA");
78 
79         mController.displayPreference(mScreen);
80 
81         assertThat(mPreference.getEntry().toString()).isEqualTo("Afrikaans");
82     }
83 
84     @Test
onPreferenceChange_byArabicLocale_shouldReturnArabic()85     public void onPreferenceChange_byArabicLocale_shouldReturnArabic() {
86         mController.displayPreference(mScreen);
87 
88         mController.onPreferenceChange(mPreference, "af_ZA");
89 
90         assertThat(mPreference.getEntry().toString()).isEqualTo("Afrikaans");
91     }
92 }
93