1 /*
2  * Copyright (C) 2018 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 
28 import androidx.preference.PreferenceScreen;
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settingslib.widget.SelectorWithWidgetPreference;
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 AccessibilityTimeoutController}. */
45 @RunWith(RobolectricTestRunner.class)
46 public class AccessibilityTimeoutControllerTest {
47 
48     private static final String PREF_KEY = "accessibility_control_timeout_30secs";
49 
50     @Rule
51     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
52     @Mock
53     private SelectorWithWidgetPreference mMockPref;
54     @Mock
55     private PreferenceScreen mScreen;
56     @Mock
57     private AccessibilitySettingsContentObserver mAccessibilitySettingsContentObserver;
58     private Context mContext = ApplicationProvider.getApplicationContext();
59     private ContentResolver mContentResolver;
60     private AccessibilityTimeoutController mController;
61 
62     @Before
setup()63     public void setup() {
64         mContentResolver = mContext.getContentResolver();
65         mController = new AccessibilityTimeoutController(mContext, PREF_KEY,
66                 mAccessibilitySettingsContentObserver);
67         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mMockPref);
68         when(mMockPref.getKey()).thenReturn(PREF_KEY);
69         final String prefTitle =
70                 mContext.getResources().getString(R.string.accessibility_timeout_30secs);
71         when(mMockPref.getTitle()).thenReturn(prefTitle);
72         mController.displayPreference(mScreen);
73     }
74 
75     @Test
getAvailabilityStatus_shouldReturnAvailable()76     public void getAvailabilityStatus_shouldReturnAvailable() {
77         assertThat(mController.getAvailabilityStatus()).isEqualTo(
78                 BasePreferenceController.AVAILABLE);
79     }
80 
81     @Test
updateState_notChecked()82     public void updateState_notChecked() {
83         Settings.Secure.putString(mContentResolver,
84                 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS, "0");
85 
86         mController.updateState(mMockPref);
87 
88         // the first checked state is set to false by control
89         verify(mMockPref).setChecked(false);
90         verify(mMockPref).setChecked(false);
91     }
92 
93     @Test
updateState_checked()94     public void updateState_checked() {
95         Settings.Secure.putString(mContentResolver,
96                 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS, "30000");
97 
98         mController.updateState(mMockPref);
99 
100         // the first checked state is set to false by control
101         verify(mMockPref).setChecked(false);
102         verify(mMockPref).setChecked(true);
103     }
104 
105     @Test
onRadioButtonClick()106     public void onRadioButtonClick() {
107         mController.onRadioButtonClicked(mMockPref);
108 
109         String accessibilityUiTimeoutValue = Settings.Secure.getString(mContentResolver,
110                 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS);
111 
112         assertThat(accessibilityUiTimeoutValue).isEqualTo("30000");
113     }
114 
115     @Test
onStart_registerSpecificContentObserverForSpecificKeys()116     public void onStart_registerSpecificContentObserverForSpecificKeys() {
117         mController.onStart();
118 
119         verify(mAccessibilitySettingsContentObserver).register(mContentResolver);
120     }
121 
122     @Test
onStop_unregisterContentObserver()123     public void onStop_unregisterContentObserver() {
124         mController.onStop();
125 
126         verify(mAccessibilitySettingsContentObserver).unregister(mContentResolver);
127     }
128 }
129