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.View;
28 
29 import androidx.preference.PreferenceScreen;
30 import androidx.test.core.app.ApplicationProvider;
31 
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settingslib.widget.LayoutPreference;
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.Spy;
41 import org.mockito.junit.MockitoJUnit;
42 import org.mockito.junit.MockitoRule;
43 import org.robolectric.RobolectricTestRunner;
44 
45 /** Tests for {@link CaptioningPreviewPreferenceController}. */
46 @RunWith(RobolectricTestRunner.class)
47 public class CaptioningPreviewPreferenceControllerTest {
48 
49     @Rule
50     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
51     @Mock
52     private PreferenceScreen mScreen;
53     @Mock
54     private ContentResolver mContentResolver;
55     @Spy
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     private CaptioningPreviewPreferenceController mController;
58     private LayoutPreference mLayoutPreference;
59 
60     @Before
setUp()61     public void setUp() {
62         when(mContext.getContentResolver()).thenReturn(mContentResolver);
63         mController = new CaptioningPreviewPreferenceController(mContext,
64                 "captioning_preference_switch");
65         final View view = new View(mContext);
66         mLayoutPreference = new LayoutPreference(mContext, view);
67         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPreference);
68     }
69 
70     @Test
getAvailabilityStatus_shouldReturnAvailable()71     public void getAvailabilityStatus_shouldReturnAvailable() {
72         assertThat(mController.getAvailabilityStatus())
73                 .isEqualTo(BasePreferenceController.AVAILABLE);
74     }
75 
76     @Test
onStart_registerSpecificContentObserverForSpecificKeys()77     public void onStart_registerSpecificContentObserverForSpecificKeys() {
78         mController.onStart();
79 
80         for (String key : mController.CAPTIONING_FEATURE_KEYS) {
81             verify(mContentResolver).registerContentObserver(Settings.Secure.getUriFor(key),
82                     /* notifyForDescendants= */ false, mController.mSettingsContentObserver);
83         }
84     }
85 
86     @Test
onStop_unregisterContentObserver()87     public void onStop_unregisterContentObserver() {
88         mController.onStop();
89 
90         verify(mContentResolver).unregisterContentObserver(mController.mSettingsContentObserver);
91     }
92 }
93