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 import static org.robolectric.Shadows.shadowOf;
21 
22 import android.content.Context;
23 import android.os.Looper;
24 import android.provider.Settings;
25 import android.view.accessibility.CaptioningManager;
26 
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.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.shadow.api.Shadow;
37 import org.robolectric.shadows.ShadowCaptioningManager;
38 
39 /** Tests for {@link CaptioningAppearancePreferenceController}. */
40 @RunWith(RobolectricTestRunner.class)
41 public class CaptioningAppearancePreferenceControllerTest {
42 
43     private static final String TEST_KEY = "test_key";
44     private static final int DEFAULT_PRESET_INDEX = 1;
45     private static final int DEFAULT_FONT_SCALE_INDEX = 2;
46 
47     private final Context mContext = ApplicationProvider.getApplicationContext();
48     private CaptioningAppearancePreferenceController mController;
49     private ShadowCaptioningManager mShadowCaptioningManager;
50 
51     @Before
setUp()52     public void setUp() {
53         CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
54         mShadowCaptioningManager = Shadow.extract(captioningManager);
55         mController = new CaptioningAppearancePreferenceController(mContext, TEST_KEY);
56     }
57 
58     @Test
getAvailabilityStatus_shouldReturnAvailable()59     public void getAvailabilityStatus_shouldReturnAvailable() {
60         assertThat(mController.getAvailabilityStatus()).isEqualTo(
61                 BasePreferenceController.AVAILABLE);
62     }
63 
64     @Test
getSummary_noScale_shouldReturnDefaultSummary()65     public void getSummary_noScale_shouldReturnDefaultSummary() {
66         final String expectedSummary =
67                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, DEFAULT_PRESET_INDEX);
68         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
69     }
70 
71     @Test
getSummary_smallestScale_shouldReturnExpectedSummary()72     public void getSummary_smallestScale_shouldReturnExpectedSummary() {
73         Settings.Secure.putFloat(mContext.getContentResolver(),
74             Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 0.25f);
75         shadowOf(Looper.getMainLooper()).idle();
76 
77         final String expectedSummary =
78                 getSummaryCombo(/* fontScaleIndex= */ 0, DEFAULT_PRESET_INDEX);
79         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
80     }
81 
82     @Test
getSummary_smallScale_shouldReturnExpectedSummary()83     public void getSummary_smallScale_shouldReturnExpectedSummary() {
84         Settings.Secure.putFloat(mContext.getContentResolver(),
85             Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 0.5f);
86         shadowOf(Looper.getMainLooper()).idle();
87 
88         final String expectedSummary =
89                 getSummaryCombo(/* fontScaleIndex= */ 1, DEFAULT_PRESET_INDEX);
90         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
91     }
92 
93     @Test
getSummary_mediumScale_shouldReturnExpectedSummary()94     public void getSummary_mediumScale_shouldReturnExpectedSummary() {
95         Settings.Secure.putFloat(mContext.getContentResolver(),
96             Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 1.0f);
97         shadowOf(Looper.getMainLooper()).idle();
98 
99         final String expectedSummary =
100                 getSummaryCombo(/* fontScaleIndex= */ 2, DEFAULT_PRESET_INDEX);
101         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
102     }
103 
104     @Test
getSummary_largeScale_shouldReturnExpectedSummary()105     public void getSummary_largeScale_shouldReturnExpectedSummary() {
106         Settings.Secure.putFloat(mContext.getContentResolver(),
107             Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 1.5f);
108         shadowOf(Looper.getMainLooper()).idle();
109 
110         final String expectedSummary =
111                 getSummaryCombo(/* fontScaleIndex= */ 3, DEFAULT_PRESET_INDEX);
112         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
113     }
114 
115     @Test
getSummary_largestScale_shouldReturnExpectedSummary()116     public void getSummary_largestScale_shouldReturnExpectedSummary() {
117         Settings.Secure.putFloat(mContext.getContentResolver(),
118             Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 2.0f);
119         shadowOf(Looper.getMainLooper()).idle();
120 
121         final String expectedSummary =
122                 getSummaryCombo(/* fontScaleIndex= */ 4, DEFAULT_PRESET_INDEX);
123         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
124     }
125 
126     @Test
getSummary_setByAppPreset_shouldReturnExpectedSummary()127     public void getSummary_setByAppPreset_shouldReturnExpectedSummary() {
128         Settings.Secure.putInt(mContext.getContentResolver(),
129                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 4);
130 
131         final String expectedSummary =
132                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 0);
133         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
134     }
135 
136     @Test
getSummary_whiteOnBlackPreset_shouldReturnExpectedSummary()137     public void getSummary_whiteOnBlackPreset_shouldReturnExpectedSummary() {
138         Settings.Secure.putInt(mContext.getContentResolver(),
139                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0);
140 
141         final String expectedSummary =
142                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 1);
143         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
144     }
145 
146     @Test
getSummary_blackOnWhitePreset_shouldReturnExpectedSummary()147     public void getSummary_blackOnWhitePreset_shouldReturnExpectedSummary() {
148         Settings.Secure.putInt(mContext.getContentResolver(),
149                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 1);
150 
151         final String expectedSummary =
152                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 2);
153         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
154     }
155 
156     @Test
getSummary_yellowOnBlackPreset_shouldReturnExpectedSummary()157     public void getSummary_yellowOnBlackPreset_shouldReturnExpectedSummary() {
158         Settings.Secure.putInt(mContext.getContentResolver(),
159                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 2);
160 
161         final String expectedSummary =
162                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 3);
163         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
164     }
165 
166     @Test
getSummary_yellowOnBluePreset_shouldReturnExpectedSummary()167     public void getSummary_yellowOnBluePreset_shouldReturnExpectedSummary() {
168         Settings.Secure.putInt(mContext.getContentResolver(),
169                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 3);
170 
171         final String expectedSummary =
172                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 4);
173         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
174     }
175 
176     @Test
getSummary_customPreset_shouldReturnExpectedSummary()177     public void getSummary_customPreset_shouldReturnExpectedSummary() {
178         Settings.Secure.putInt(mContext.getContentResolver(),
179                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, -1);
180 
181         final String expectedSummary =
182                 getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 5);
183         assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
184     }
185 
getSummaryCombo(int fontScaleIndex, int presetIndex)186     private String getSummaryCombo(int fontScaleIndex, int presetIndex) {
187         final String[] fontScaleArray = mContext.getResources().getStringArray(
188                 R.array.captioning_font_size_selector_titles);
189         final String[] presetArray = mContext.getResources().getStringArray(
190                 R.array.captioning_preset_selector_titles);
191         return mContext.getString(R.string.preference_summary_default_combination,
192                 fontScaleArray[fontScaleIndex], presetArray[presetIndex]);
193     }
194 }
195