1 /*
2  * Copyright (C) 2019 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.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
20 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;
21 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.Mockito.when;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.Context;
29 import android.provider.Settings;
30 
31 import com.android.settings.R;
32 import com.android.settings.RestrictedListPreference;
33 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
34 import com.android.settings.testutils.shadow.ShadowUserManager;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.Robolectric;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.Shadows;
46 import org.robolectric.annotation.Config;
47 
48 import androidx.preference.PreferenceScreen;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = {ShadowUserManager.class, ShadowRestrictedLockUtilsInternal.class})
52 public class ShowOnLockscreenNotificationPreferenceControllerTest {
53 
54     private Context mContext;
55     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
56     private PreferenceScreen mScreen;
57     @Mock
58     DevicePolicyManager mDpm;
59 
60     private ShowOnLockScreenNotificationPreferenceController mController;
61     private RestrictedListPreference mPreference;
62 
63     private static final String KEY = "key";
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = RuntimeEnvironment.application;
69         mController = new ShowOnLockScreenNotificationPreferenceController(mContext, KEY);
70         mPreference = new RestrictedListPreference(
71                 mContext, Robolectric.buildAttributeSet().build());
72         mPreference.setKey(mController.getPreferenceKey());
73         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
74         mController.setDpm(mDpm);
75     }
76 
77     @Test
display_shouldDisplay()78     public void display_shouldDisplay() {
79         assertThat(mPreference.isVisible()).isTrue();
80     }
81 
82     @Test
updateState_noNotifsOnLockscreen()83     public void updateState_noNotifsOnLockscreen() {
84         Settings.Secure.putInt(mContext.getContentResolver(),
85                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
86                 0);
87         // should be ignored
88         Settings.Secure.putInt(mContext.getContentResolver(),
89                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
90                 1);
91 
92         mController.updateState(mPreference);
93 
94         assertThat(mPreference.getValue()).isEqualTo(
95                 String.valueOf(R.string.lock_screen_notifs_show_none));
96 
97         assertThat(mPreference.getSummary())
98                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_none));
99     }
100 
101     @Test
updateState_alertingNotifsOnLockscreen()102     public void updateState_alertingNotifsOnLockscreen() {
103         Settings.Secure.putInt(mContext.getContentResolver(),
104                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
105                 1);
106         Settings.Secure.putInt(mContext.getContentResolver(),
107                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
108                 0);
109 
110         mController.updateState(mPreference);
111 
112         assertThat(mPreference.getValue()).isEqualTo(
113                 String.valueOf(R.string.lock_screen_notifs_show_alerting));
114         assertThat(mPreference.getSummary())
115                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_alerting));
116     }
117 
118     @Test
updateState_allNotifsOnLockscreen()119     public void updateState_allNotifsOnLockscreen() {
120         Settings.Secure.putInt(mContext.getContentResolver(),
121                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
122                 1);
123         Settings.Secure.putInt(mContext.getContentResolver(),
124                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
125                 1);
126 
127         mController.updateState(mPreference);
128 
129         assertThat(mPreference.getValue()).isEqualTo(
130                 String.valueOf(R.string.lock_screen_notifs_show_all));
131         assertThat(mPreference.getSummary())
132                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_all));
133     }
134 
135     @Test
updateState_allNotifsOnLockscreen_isDefault()136     public void updateState_allNotifsOnLockscreen_isDefault() {
137         // settings don't exist
138 
139         mController.updateState(mPreference);
140 
141         assertThat(mPreference.getValue()).isEqualTo(
142                 String.valueOf(R.string.lock_screen_notifs_show_all));
143         assertThat(mPreference.getSummary())
144                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_all));
145     }
146 
147     @Test
updateState_dpmSaysNo()148     public void updateState_dpmSaysNo() {
149         Settings.Secure.putInt(mContext.getContentResolver(),
150                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
151                 1);
152         Settings.Secure.putInt(mContext.getContentResolver(),
153                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
154                 1);
155 
156         when(mDpm.getKeyguardDisabledFeatures(null))
157                 .thenReturn(KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
158         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
159                 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
160 
161         mController.updateState(mPreference);
162 
163         assertThat(mPreference.getValue()).isEqualTo(
164                 String.valueOf(R.string.lock_screen_notifs_show_none));
165         assertThat(mPreference.getSummary())
166                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_none));
167 
168         assertThat(mPreference.isRestrictedForEntry(
169                 mContext.getString(R.string.lock_screen_notifs_show_all))).isTrue();
170         assertThat(mPreference.isRestrictedForEntry(
171                 mContext.getString(R.string.lock_screen_notifs_show_alerting))).isTrue();
172     }
173 
174     @Test
onPreferenceChange_allToAlerting()175     public void onPreferenceChange_allToAlerting() {
176         Settings.Secure.putInt(mContext.getContentResolver(),
177                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
178                 1);
179         Settings.Secure.putInt(mContext.getContentResolver(),
180                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
181                 1);
182 
183         mController.onPreferenceChange(mPreference,
184                 Integer.toString(R.string.lock_screen_notifs_show_alerting));
185 
186         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
187                 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1)).isEqualTo(1);
188         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
189                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(0);
190 
191         assertThat(mPreference.getSummary())
192                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_alerting));
193     }
194 
195     @Test
onPreferenceChange_noneToAll()196     public void onPreferenceChange_noneToAll() {
197         Settings.Secure.putInt(mContext.getContentResolver(),
198                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
199                 0);
200         Settings.Secure.putInt(mContext.getContentResolver(),
201                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
202                 0);
203 
204         mController.onPreferenceChange(mPreference,
205                 Integer.toString(R.string.lock_screen_notifs_show_all));
206 
207         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
208                 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1)).isEqualTo(1);
209         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
210                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(1);
211 
212         assertThat(mPreference.getSummary())
213                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_all));
214     }
215 
216     @Test
onPreferenceChange_alertingToNone()217     public void onPreferenceChange_alertingToNone() {
218         Settings.Secure.putInt(mContext.getContentResolver(),
219                 LOCK_SCREEN_SHOW_NOTIFICATIONS,
220                 1);
221         Settings.Secure.putInt(mContext.getContentResolver(),
222                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS,
223                 0);
224 
225         mController.onPreferenceChange(mPreference,
226                 Integer.toString(R.string.lock_screen_notifs_show_none));
227 
228         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
229                 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1)).isEqualTo(0);
230         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
231                 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(0);
232 
233         assertThat(mPreference.getSummary())
234                 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_none));
235     }
236 }
237