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.view.accessibility.CaptioningManager;
29 
30 import androidx.preference.PreferenceScreen;
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import com.android.settings.core.BasePreferenceController;
34 import com.android.settings.widget.SettingsMainSwitchPreference;
35 
36 import org.junit.After;
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.RobolectricTestRunner;
45 import org.robolectric.shadow.api.Shadow;
46 import org.robolectric.shadows.ShadowCaptioningManager;
47 
48 /** Tests for {@link CaptioningTogglePreferenceController}. */
49 @RunWith(RobolectricTestRunner.class)
50 public class CaptioningTogglePreferenceControllerTest {
51 
52     @Rule
53     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
54     @Mock
55     private PreferenceScreen mScreen;
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     private CaptioningTogglePreferenceController mController;
58     private SettingsMainSwitchPreference mSwitchPreference;
59     private ShadowCaptioningManager mShadowCaptioningManager;
60 
61     @Before
setUp()62     public void setUp() {
63         mController = new CaptioningTogglePreferenceController(mContext,
64                 "captioning_preference_switch");
65         mSwitchPreference = new SettingsMainSwitchPreference(mContext);
66         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mSwitchPreference);
67         CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
68         mShadowCaptioningManager = Shadow.extract(captioningManager);
69     }
70 
71     @After
tearDown()72     public void tearDown() {
73         Settings.Secure.putInt(mContext.getContentResolver(),
74                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF);
75     }
76 
77     @Test
getAvailabilityStatus_shouldReturnAvailable()78     public void getAvailabilityStatus_shouldReturnAvailable() {
79         assertThat(mController.getAvailabilityStatus())
80                 .isEqualTo(BasePreferenceController.AVAILABLE);
81     }
82 
83     @Test
displayPreference_captionEnabled_shouldSetChecked()84     public void displayPreference_captionEnabled_shouldSetChecked() {
85         Settings.Secure.putInt(mContext.getContentResolver(),
86                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, ON);
87 
88         mController.displayPreference(mScreen);
89 
90         assertThat(mSwitchPreference.isChecked()).isTrue();
91     }
92 
93     @Test
displayPreference_captionDisabled_shouldSetUnchecked()94     public void displayPreference_captionDisabled_shouldSetUnchecked() {
95         Settings.Secure.putInt(mContext.getContentResolver(),
96                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF);
97 
98         mController.displayPreference(mScreen);
99 
100         assertThat(mSwitchPreference.isChecked()).isFalse();
101     }
102 
103     @Test
performClick_captionEnabled_shouldSetCaptionDisabled()104     public void performClick_captionEnabled_shouldSetCaptionDisabled() {
105         Settings.Secure.putInt(mContext.getContentResolver(),
106                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, ON);
107         mController.displayPreference(mScreen);
108 
109         mSwitchPreference.performClick();
110 
111         assertThat(mSwitchPreference.isChecked()).isFalse();
112         assertThat(isCaptionEnabled()).isFalse();
113     }
114 
115     @Test
performClick_captionDisabled_shouldSetCaptionEnabled()116     public void performClick_captionDisabled_shouldSetCaptionEnabled() {
117         Settings.Secure.putInt(mContext.getContentResolver(),
118                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF);
119         mController.displayPreference(mScreen);
120 
121         mSwitchPreference.performClick();
122 
123         assertThat(mSwitchPreference.isChecked()).isTrue();
124         assertThat(isCaptionEnabled()).isTrue();
125     }
126 
127     @Test
setChecked_switchChecked_shouldSetCaptionEnabled()128     public void setChecked_switchChecked_shouldSetCaptionEnabled() {
129         mController.displayPreference(mScreen);
130 
131         mController.setChecked(/* isChecked= */ true);
132 
133         assertThat(isCaptionEnabled()).isTrue();
134     }
135 
136     @Test
setChecked_switchUnchecked_shouldSetCaptionDisabled()137     public void setChecked_switchUnchecked_shouldSetCaptionDisabled() {
138         mController.displayPreference(mScreen);
139 
140         mController.setChecked(/* isChecked= */ false);
141 
142         assertThat(isCaptionEnabled()).isFalse();
143     }
144 
145     @Test
onSwitchChanged_switchChecked_shouldSetCaptionEnabled()146     public void onSwitchChanged_switchChecked_shouldSetCaptionEnabled() {
147         mController.displayPreference(mScreen);
148 
149         mController.onCheckedChanged(/* buttonView= */ null, /* isChecked= */ true);
150 
151         assertThat(isCaptionEnabled()).isTrue();
152     }
153 
154     @Test
onSwitchChanged_switchUnchecked_shouldSetCaptionDisabled()155     public void onSwitchChanged_switchUnchecked_shouldSetCaptionDisabled() {
156         mController.displayPreference(mScreen);
157 
158         mController.onCheckedChanged(/* buttonView= */ null, /* isChecked= */ false);
159 
160         assertThat(isCaptionEnabled()).isFalse();
161     }
162 
isCaptionEnabled()163     private boolean isCaptionEnabled() {
164         return Settings.Secure.getInt(mContext.getContentResolver(),
165                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
166     }
167 }
168