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.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.provider.Settings; 30 import android.view.View; 31 import android.view.accessibility.CaptioningManager; 32 import android.view.accessibility.CaptioningManager.CaptionStyle; 33 34 import androidx.test.core.app.ApplicationProvider; 35 36 import com.android.internal.widget.SubtitleView; 37 import com.android.settings.R; 38 39 import org.junit.Before; 40 import org.junit.Rule; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.Spy; 45 import org.mockito.junit.MockitoJUnit; 46 import org.mockito.junit.MockitoRule; 47 import org.robolectric.RobolectricTestRunner; 48 49 import java.util.Locale; 50 51 /** Tests for {@link CaptionHelper}. */ 52 @RunWith(RobolectricTestRunner.class) 53 public class CaptionHelperTest { 54 55 @Rule 56 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 57 @Mock 58 private CaptioningManager mCaptioningManager; 59 @Mock 60 private SubtitleView mSubtitleView; 61 @Mock 62 private View mPreviewWindow; 63 @Spy 64 private final Context mContext = ApplicationProvider.getApplicationContext(); 65 private ContentResolver mContentResolver; 66 private CaptionHelper mCaptionHelper; 67 68 @Before setUp()69 public void setUp() { 70 when(mContext.getSystemService(CaptioningManager.class)).thenReturn(mCaptioningManager); 71 mCaptionHelper = new CaptionHelper(mContext); 72 mContentResolver = mContext.getContentResolver(); 73 } 74 75 @Test applyCaptionProperties_verifyAction()76 public void applyCaptionProperties_verifyAction() { 77 final float fontScale = 1.0f; 78 when(mCaptioningManager.getFontScale()).thenReturn(fontScale); 79 final int windowSize = 100; 80 when(mPreviewWindow.getWidth()).thenReturn(windowSize); 81 when(mPreviewWindow.getHeight()).thenReturn(windowSize); 82 final float textSize = CaptionHelper.LINE_HEIGHT_RATIO * windowSize * fontScale; 83 84 mCaptionHelper.applyCaptionProperties(mSubtitleView, mPreviewWindow, /* styleId= */ 0); 85 86 verify(mSubtitleView).setTextSize(textSize); 87 verify(mSubtitleView).setText(R.string.captioning_preview_characters); 88 } 89 90 @Test applyCaptionProperties_withoutPreviewWindow_verifyAction()91 public void applyCaptionProperties_withoutPreviewWindow_verifyAction() { 92 final float fontScale = 1.0f; 93 when(mCaptioningManager.getFontScale()).thenReturn(fontScale); 94 final float textSize = mContext.getResources().getDimension( 95 R.dimen.captioning_preview_text_size) * fontScale; 96 97 mCaptionHelper.applyCaptionProperties(mSubtitleView, /* PreviewWindow= */ null, 98 /* styleId= */ 0); 99 100 verify(mSubtitleView).setTextSize(textSize); 101 verify(mSubtitleView).setText(R.string.captioning_preview_characters); 102 } 103 104 @Test applyCaptionProperties_localeUS_verifyAction()105 public void applyCaptionProperties_localeUS_verifyAction() { 106 when(mCaptioningManager.getLocale()).thenReturn(Locale.US); 107 final String text = mContext.getString(R.string.captioning_preview_characters); 108 109 mCaptionHelper.applyCaptionProperties(mSubtitleView, /* PreviewWindow= */ null, 110 /* styleId= */ 0); 111 112 verify(mSubtitleView).setText(text); 113 } 114 115 @Test enableCaptioningManager_shouldSetCaptionEnabled()116 public void enableCaptioningManager_shouldSetCaptionEnabled() { 117 when(mCaptioningManager.isEnabled()).thenReturn(false); 118 119 mCaptionHelper.setEnabled(true); 120 121 final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(), 122 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON; 123 assertThat(isCaptionEnabled).isTrue(); 124 } 125 126 @Test disableCaptioningManager_shouldSetCaptionDisabled()127 public void disableCaptioningManager_shouldSetCaptionDisabled() { 128 when(mCaptioningManager.isEnabled()).thenReturn(true); 129 130 mCaptionHelper.setEnabled(false); 131 132 final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(), 133 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON; 134 assertThat(isCaptionEnabled).isFalse(); 135 } 136 137 @Test setBackgroundColor_shouldReturnSpecificColor()138 public void setBackgroundColor_shouldReturnSpecificColor() { 139 mCaptionHelper.setBackgroundColor(0xFFFF0000); 140 141 final int backgroundColor = mCaptionHelper.getBackgroundColor(); 142 assertThat(backgroundColor).isEqualTo(0xFFFF0000); 143 } 144 145 @Test setForegroundColor_shouldReturnSpecificColor()146 public void setForegroundColor_shouldReturnSpecificColor() { 147 mCaptionHelper.setForegroundColor(0xFFFF0000); 148 149 final int foregroundColor = mCaptionHelper.getForegroundColor(); 150 assertThat(foregroundColor).isEqualTo(0xFFFF0000); 151 } 152 153 @Test setWindowColor_shouldReturnSpecificColor()154 public void setWindowColor_shouldReturnSpecificColor() { 155 mCaptionHelper.setWindowColor(0xFFFF0000); 156 157 final int windowColor = mCaptionHelper.getWindowColor(); 158 assertThat(windowColor).isEqualTo(0xFFFF0000); 159 } 160 161 @Test setEdgeColor_shouldReturnSpecificColor()162 public void setEdgeColor_shouldReturnSpecificColor() { 163 mCaptionHelper.setEdgeColor(0xFFFF0000); 164 165 final int edgeColor = mCaptionHelper.getEdgeColor(); 166 assertThat(edgeColor).isEqualTo(0xFFFF0000); 167 } 168 169 @Test setEdgeType_shouldReturnSpecificType()170 public void setEdgeType_shouldReturnSpecificType() { 171 mCaptionHelper.setEdgeType(CaptionStyle.EDGE_TYPE_OUTLINE); 172 173 final int edgeType = mCaptionHelper.getEdgeType(); 174 assertThat(edgeType).isEqualTo(CaptionStyle.EDGE_TYPE_OUTLINE); 175 } 176 177 @Test setRawUserStyle_shouldReturnSpecificStyle()178 public void setRawUserStyle_shouldReturnSpecificStyle() { 179 mCaptionHelper.setRawUserStyle(CaptionStyle.PRESET_CUSTOM); 180 181 final int style = Settings.Secure.getInt(mContentResolver, 182 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0); 183 assertThat(style).isEqualTo(CaptionStyle.PRESET_CUSTOM); 184 } 185 } 186