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.when;
25 
26 import android.content.Context;
27 import android.provider.Settings;
28 import android.util.AttributeSet;
29 import android.view.accessibility.CaptioningManager;
30 import android.view.accessibility.CaptioningManager.CaptionStyle;
31 
32 import androidx.preference.PreferenceScreen;
33 import androidx.test.core.app.ApplicationProvider;
34 
35 import com.android.settings.core.BasePreferenceController;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnit;
43 import org.mockito.junit.MockitoRule;
44 import org.robolectric.Robolectric;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.shadow.api.Shadow;
47 import org.robolectric.shadows.ShadowCaptioningManager;
48 
49 /** Tests for {@link CaptioningPresetController}. */
50 @RunWith(RobolectricTestRunner.class)
51 public class CaptioningPresetControllerTest {
52 
53     @Rule
54     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
55     @Mock
56     private PreferenceScreen mScreen;
57     private final Context mContext = ApplicationProvider.getApplicationContext();
58     private CaptioningPresetController mController;
59     private PresetPreference mPreference;
60     private ShadowCaptioningManager mShadowCaptioningManager;
61 
62     @Before
setUp()63     public void setUp() {
64         mController = new CaptioningPresetController(mContext, "captioning_preset");
65         final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
66         mPreference = new PresetPreference(mContext, attributeSet);
67         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
68         CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
69         mShadowCaptioningManager = Shadow.extract(captioningManager);
70     }
71 
72     @Test
getAvailabilityStatus_shouldReturnAvailable()73     public void getAvailabilityStatus_shouldReturnAvailable() {
74         assertThat(mController.getAvailabilityStatus())
75                 .isEqualTo(BasePreferenceController.AVAILABLE);
76     }
77 
78     @Test
getSummary_defaultValue_shouldReturnWhiteOnBlack()79     public void getSummary_defaultValue_shouldReturnWhiteOnBlack() {
80         mController.displayPreference(mScreen);
81 
82         assertThat(mPreference.getSummary().toString()).isEqualTo("White on black");
83     }
84 
85     @Test
getSummary_customValue_shouldReturnCustom()86     public void getSummary_customValue_shouldReturnCustom() {
87         Settings.Secure.putInt(mContext.getContentResolver(),
88                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
89 
90         mController.displayPreference(mScreen);
91 
92         assertThat(mPreference.getSummary().toString()).isEqualTo("Custom");
93     }
94 
95     @Test
setCustomValue_shouldReturnCustom()96     public void setCustomValue_shouldReturnCustom() {
97         mController.displayPreference(mScreen);
98 
99         mPreference.setValue(CaptionStyle.PRESET_CUSTOM);
100 
101         assertThat(mPreference.getSummary().toString()).isEqualTo("Custom");
102     }
103 
104     @Test
onValueChanged_shouldSetCaptionEnabled()105     public void onValueChanged_shouldSetCaptionEnabled() {
106         Settings.Secure.putInt(
107             mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF);
108         mController.displayPreference(mScreen);
109 
110         mController.onValueChanged(mPreference, CaptionStyle.PRESET_CUSTOM);
111 
112         final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
113                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
114         assertThat(isCaptionEnabled).isTrue();
115     }
116 }
117