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.development;
18 
19 import static com.android.settings.development.StylusHandwritingPreferenceController.SETTING_VALUE_OFF;
20 import static com.android.settings.development.StylusHandwritingPreferenceController.SETTING_VALUE_ON;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.provider.Settings;
29 
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class StylusHandwritingPreferenceControllerTest {
43 
44     @Mock
45     private SwitchPreference mPreference;
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48 
49     private Context mContext;
50     private StylusHandwritingPreferenceController mController;
51 
52     @Before
setup()53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mController = new StylusHandwritingPreferenceController(mContext);
57         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
58             .thenReturn(mPreference);
59         mController.displayPreference(mPreferenceScreen);
60     }
61 
62     @Test
onPreferenceChange_settingEnabled_stylusHandwritingShouldBeOn()63     public void onPreferenceChange_settingEnabled_stylusHandwritingShouldBeOn() {
64         mController.onPreferenceChange(mPreference, true /* new value */);
65 
66         final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
67                 Settings.Secure.STYLUS_HANDWRITING_ENABLED, -1 /* default */);
68 
69         assertThat(mode).isEqualTo(SETTING_VALUE_ON);
70     }
71 
72     @Test
onPreferenceChange_settingEnabled_stylusHandwritingShouldBeOff()73     public void onPreferenceChange_settingEnabled_stylusHandwritingShouldBeOff() {
74         mController.onPreferenceChange(mPreference, false /* new value */);
75 
76         final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
77                 Settings.Secure.STYLUS_HANDWRITING_ENABLED, -1 /* default */);
78 
79         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
80     }
81 
82     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()83     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
84         Settings.Secure.putInt(mContext.getContentResolver(),
85                 Settings.Secure.STYLUS_HANDWRITING_ENABLED, SETTING_VALUE_OFF);
86         mController.updateState(mPreference);
87 
88         verify(mPreference).setChecked(false);
89     }
90 
91     @Test
updateState_settingEnabled_preferenceShouldBeChecked()92     public void updateState_settingEnabled_preferenceShouldBeChecked() {
93         Settings.Secure.putInt(mContext.getContentResolver(),
94                 Settings.Secure.STYLUS_HANDWRITING_ENABLED, SETTING_VALUE_ON);
95         mController.updateState(mPreference);
96 
97         verify(mPreference).setChecked(true);
98     }
99 
100 }
101