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.notification;
18 
19 import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
20 
21 import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF;
22 import static com.android.settings.notification.BadgingNotificationPreferenceController.ON;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 
31 import android.app.admin.DevicePolicyManager;
32 import android.content.Context;
33 import android.provider.Settings;
34 
35 import androidx.preference.Preference;
36 import androidx.preference.PreferenceScreen;
37 import androidx.preference.TwoStatePreference;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Answers;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class BadgingNotificationPreferenceControllerTest {
50 
51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52     private Context mContext;
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private PreferenceScreen mScreen;
55 
56     private BadgingNotificationPreferenceController mController;
57     private Preference mPreference;
58 
59     private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         doReturn(mock(DevicePolicyManager.class)).when(mContext)
65                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
66         mController = new BadgingNotificationPreferenceController(mContext,
67                 KEY_NOTIFICATION_BADGING);
68         mPreference = new Preference(RuntimeEnvironment.application);
69         mPreference.setKey(mController.getPreferenceKey());
70         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
71     }
72 
73     @Test
display_configIsTrue_shouldDisplay()74     public void display_configIsTrue_shouldDisplay() {
75         when(mContext.getResources().
76                 getBoolean(com.android.internal.R.bool.config_notificationBadging))
77                 .thenReturn(true);
78         mController.displayPreference(mScreen);
79 
80         assertThat(mPreference.isVisible()).isTrue();
81     }
82 
83     @Test
display_configIsFalse_shouldNotDisplay()84     public void display_configIsFalse_shouldNotDisplay() {
85         when(mContext.getResources().
86                 getBoolean(com.android.internal.R.bool.config_notificationBadging))
87                 .thenReturn(false);
88 
89         mController.displayPreference(mScreen);
90 
91         assertThat(mPreference.isVisible()).isFalse();
92     }
93 
94     @Test
updateState_preferenceSetCheckedWhenSettingIsOn()95     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
96         final TwoStatePreference preference = mock(TwoStatePreference.class);
97         final Context context = RuntimeEnvironment.application;
98         Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, ON);
99 
100         mController = new BadgingNotificationPreferenceController(context,
101                 KEY_NOTIFICATION_BADGING);
102         mController.updateState(preference);
103 
104         verify(preference).setChecked(true);
105     }
106 
107     @Test
updateState_preferenceSetUncheckedWhenSettingIsOff()108     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
109         final TwoStatePreference preference = mock(TwoStatePreference.class);
110         final Context context = RuntimeEnvironment.application;
111         Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, OFF);
112 
113         mController = new BadgingNotificationPreferenceController(context,
114                 KEY_NOTIFICATION_BADGING);
115         mController.updateState(preference);
116 
117         verify(preference).setChecked(false);
118     }
119 
120     @Test
isChecked_settingIsOff_shouldReturnFalse()121     public void isChecked_settingIsOff_shouldReturnFalse() {
122         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF);
123 
124         assertThat(mController.isChecked()).isFalse();
125     }
126 
127     @Test
isChecked_settingIsOn_shouldReturnTrue()128     public void isChecked_settingIsOn_shouldReturnTrue() {
129         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON);
130 
131         assertThat(mController.isChecked()).isTrue();
132     }
133 
134     @Test
setChecked_setFalse_disablesSetting()135     public void setChecked_setFalse_disablesSetting() {
136         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON);
137 
138         mController.setChecked(false);
139         int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
140                 NOTIFICATION_BADGING, -1);
141 
142         assertThat(updatedValue).isEqualTo(OFF);
143     }
144 
145     @Test
setChecked_setTrue_enablesSetting()146     public void setChecked_setTrue_enablesSetting() {
147         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF);
148 
149         mController.setChecked(true);
150         int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
151                 NOTIFICATION_BADGING, -1);
152 
153         assertThat(updatedValue).isEqualTo(ON);
154     }
155 
156     @Test
isSliceableCorrectKey_returnsTrue()157     public void isSliceableCorrectKey_returnsTrue() {
158         final BadgingNotificationPreferenceController controller =
159                 new BadgingNotificationPreferenceController(mContext,
160                         "notification_badging");
161         assertThat(controller.isSliceable()).isTrue();
162     }
163 
164     @Test
isSliceableIncorrectKey_returnsFalse()165     public void isSliceableIncorrectKey_returnsFalse() {
166         final BadgingNotificationPreferenceController controller =
167                 new BadgingNotificationPreferenceController(mContext, "bad_key");
168         assertThat(controller.isSliceable()).isFalse();
169     }
170 }
171