1 package com.android.settings.development;
2 
3 /*
4  * Copyright (C) 2017 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.provider.Settings;
28 
29 import androidx.preference.PreferenceScreen;
30 import androidx.preference.SwitchPreference;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class NotificationChannelWarningsPreferenceControllerTest {
42 
43     @Mock
44     private SwitchPreference mPreference;
45     @Mock
46     private PreferenceScreen mScreen;
47 
48     private Context mContext;
49     private NotificationChannelWarningsPreferenceController mController;
50 
51     @Before
setup()52     public void setup() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         mController = new NotificationChannelWarningsPreferenceController(mContext);
56         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
57         mController.displayPreference(mScreen);
58     }
59 
60     @Test
onPreferenceChange_settingEnabled_shouldEnableNotificationChannelWarnings()61     public void onPreferenceChange_settingEnabled_shouldEnableNotificationChannelWarnings() {
62         mController.onPreferenceChange(mPreference, true /* new value */);
63 
64         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
65                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, -1 /* default */);
66 
67         assertThat(mode).isEqualTo(
68                 NotificationChannelWarningsPreferenceController.SETTING_VALUE_ON);
69     }
70 
71     @Test
onPreferenceChange_settingDisabled_shouldDisableNotificationChannelWarnings()72     public void onPreferenceChange_settingDisabled_shouldDisableNotificationChannelWarnings() {
73         mController.onPreferenceChange(mPreference, false /* new value */);
74 
75         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
76                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, -1 /* default */);
77 
78         assertThat(mode).isEqualTo(
79                 NotificationChannelWarningsPreferenceController.SETTING_VALUE_OFF);
80     }
81 
82     @Test
updateState_settingEnabled_preferenceShouldBeChecked()83     public void updateState_settingEnabled_preferenceShouldBeChecked() {
84         Settings.Global.putInt(mContext.getContentResolver(),
85                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS,
86                 NotificationChannelWarningsPreferenceController.SETTING_VALUE_ON);
87 
88         mController.updateState(mPreference);
89 
90         verify(mPreference).setChecked(true);
91     }
92 
93     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()94     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
95         Settings.Global.putInt(mContext.getContentResolver(),
96                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS,
97                 NotificationChannelWarningsPreferenceController.SETTING_VALUE_OFF);
98 
99         mController.updateState(mPreference);
100 
101         verify(mPreference).setChecked(false);
102     }
103 
104     @Test
updateState_settingUndefinedDebuggingEnabled_preferenceShouldBeChecked()105     public void updateState_settingUndefinedDebuggingEnabled_preferenceShouldBeChecked() {
106         mController = spy(mController);
107         doReturn(true).when(mController).isDebuggable();
108         Settings.Global.putString(mContext.getContentResolver(),
109                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, "NotAnInteger");
110 
111         mController.updateState(mPreference);
112 
113         verify(mPreference).setChecked(true);
114     }
115 
116     @Test
updateState_settingUndefinedDebuggingDisabled_preferenceShouldNotBeChecked()117     public void updateState_settingUndefinedDebuggingDisabled_preferenceShouldNotBeChecked() {
118         mController = spy(mController);
119         doReturn(false).when(mController).isDebuggable();
120         Settings.Global.putString(mContext.getContentResolver(),
121                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, "NotAnInteger");
122 
123         mController.updateState(mPreference);
124 
125         verify(mPreference).setChecked(false);
126     }
127 
128     @Test
onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled()129     public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled() {
130         mController.onDeveloperOptionsSwitchDisabled();
131         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
132                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, -1 /* default */);
133 
134         assertThat(mode).isEqualTo(
135                 NotificationChannelWarningsPreferenceController.SETTING_VALUE_OFF);
136         verify(mPreference).setChecked(false);
137         verify(mPreference).setEnabled(false);
138     }
139 }
140