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.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.provider.Settings;
27 import android.view.accessibility.CaptioningManager.CaptionStyle;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import com.android.settings.core.BasePreferenceController;
34 
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.junit.MockitoJUnit;
41 import org.mockito.junit.MockitoRule;
42 import org.robolectric.RobolectricTestRunner;
43 
44 /** Tests for {@link CaptioningCustomController}. */
45 @RunWith(RobolectricTestRunner.class)
46 public class CaptioningCustomControllerTest {
47 
48     private static final String PREF_KEY = "custom";
49 
50     @Rule
51     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
52     @Mock
53     private PreferenceScreen mScreen;
54     @Mock
55     private AccessibilitySettingsContentObserver mAccessibilitySettingsContentObserver;
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     private ContentResolver mContentResolver;
58     private CaptioningCustomController mController;
59     private Preference mPreference;
60 
61     @Before
setUp()62     public void setUp() {
63         mContentResolver = mContext.getContentResolver();
64         mController = new CaptioningCustomController(mContext, PREF_KEY,
65                 mAccessibilitySettingsContentObserver);
66         mPreference = new Preference(mContext);
67         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
68     }
69 
70     @Test
getAvailabilityStatus_shouldReturnAvailable()71     public void getAvailabilityStatus_shouldReturnAvailable() {
72         assertThat(mController.getAvailabilityStatus())
73                 .isEqualTo(BasePreferenceController.AVAILABLE);
74     }
75 
76 
77     @Test
displayPreference_byDefault_shouldIsInvisible()78     public void displayPreference_byDefault_shouldIsInvisible() {
79         mController.displayPreference(mScreen);
80 
81         assertThat(mPreference.isVisible()).isFalse();
82     }
83 
84     @Test
displayPreference_customValue_shouldIsVisible()85     public void displayPreference_customValue_shouldIsVisible() {
86         Settings.Secure.putInt(mContext.getContentResolver(),
87                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
88 
89         mController.displayPreference(mScreen);
90 
91         assertThat(mPreference.isVisible()).isTrue();
92     }
93 
94     @Test
onStart_registerSpecificContentObserverForSpecificKeys()95     public void onStart_registerSpecificContentObserverForSpecificKeys() {
96         mController.onStart();
97 
98         verify(mAccessibilitySettingsContentObserver).register(mContentResolver);
99     }
100 
101     @Test
onStop_unregisterContentObserver()102     public void onStop_unregisterContentObserver() {
103         mController.onStop();
104 
105         verify(mAccessibilitySettingsContentObserver).unregister(mContentResolver);
106     }
107 }
108