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