1 /*
2  * Copyright (C) 2017 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.development;
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.Context;
25 import android.provider.Settings;
26 
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreference;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class ShowTapsPreferenceControllerTest {
40 
41     @Mock
42     private PreferenceScreen mScreen;
43     @Mock
44     private SwitchPreference mPreference;
45 
46     private Context mContext;
47 
48     private ShowTapsPreferenceController mController;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         mContext = RuntimeEnvironment.application;
54         mController = new ShowTapsPreferenceController(mContext);
55         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
56         mController.displayPreference(mScreen);
57     }
58 
59     @Test
updateState_showTapsEnabled_shouldCheckedPreference()60     public void updateState_showTapsEnabled_shouldCheckedPreference() {
61         Settings.System.putInt(mContext.getContentResolver(),
62                 Settings.System.SHOW_TOUCHES, ShowTapsPreferenceController.SETTING_VALUE_ON);
63 
64         mController.updateState(mPreference);
65 
66         verify(mPreference).setChecked(true);
67     }
68 
69     @Test
updateState_showTapsDisabled_shouldUncheckedPreference()70     public void updateState_showTapsDisabled_shouldUncheckedPreference() {
71         Settings.System.putInt(mContext.getContentResolver(),
72                 Settings.System.SHOW_TOUCHES, ShowTapsPreferenceController.SETTING_VALUE_OFF);
73 
74         mController.updateState(mPreference);
75 
76         verify(mPreference).setChecked(false);
77     }
78 
79     @Test
onPreferenceChange_preferenceChecked_shouldEnableShowTaps()80     public void onPreferenceChange_preferenceChecked_shouldEnableShowTaps() {
81         mController.onPreferenceChange(mPreference, true /* new value */);
82 
83         final int showTapsMode = Settings.System.getInt(mContext.getContentResolver(),
84                 Settings.System.SHOW_TOUCHES, -1 /* default */);
85 
86         assertThat(showTapsMode).isEqualTo(ShowTapsPreferenceController.SETTING_VALUE_ON);
87     }
88 
89     @Test
onPreferenceChange__preferenceUnchecked_shouldDisableShowTaps()90     public void onPreferenceChange__preferenceUnchecked_shouldDisableShowTaps() {
91         mController.onPreferenceChange(mPreference, false /* new value */);
92 
93         final int showTapsMode = Settings.System.getInt(mContext.getContentResolver(),
94                 Settings.System.SHOW_TOUCHES, -1 /* default */);
95 
96         assertThat(showTapsMode).isEqualTo(ShowTapsPreferenceController.SETTING_VALUE_OFF);
97     }
98 
99     @Test
onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled()100     public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled() {
101         mController.onDeveloperOptionsSwitchDisabled();
102 
103         final int showTapsMode = Settings.System.getInt(mContext.getContentResolver(),
104                 Settings.System.SHOW_TOUCHES, -1 /* default */);
105 
106         assertThat(showTapsMode).isEqualTo(ShowTapsPreferenceController.SETTING_VALUE_OFF);
107         verify(mPreference).setEnabled(false);
108         verify(mPreference).setChecked(false);
109     }
110 }
111