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