• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Global.ZEN_MODE;
20 import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
21 import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
22 import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.NotificationManager;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.provider.Settings;
31 import android.support.v7.preference.ListPreference;
32 import android.support.v7.preference.PreferenceScreen;
33 
34 import com.android.settings.R;
35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.shadows.ShadowApplication;
45 import org.robolectric.util.ReflectionHelpers;
46 
47 @RunWith(SettingsRobolectricTestRunner.class)
48 public class ZenModeMessagesPreferenceControllerTest {
49 
50     private ZenModeMessagesPreferenceController mController;
51 
52     @Mock
53     private ZenModeBackend mBackend;
54     @Mock
55     private NotificationManager mNotificationManager;
56     @Mock
57     private ListPreference mockPref;
58     @Mock
59     private NotificationManager.Policy mPolicy;
60     @Mock
61     private PreferenceScreen mPreferenceScreen;
62     private ContentResolver mContentResolver;
63     private Context mContext;
64 
65     /**
66      * Array Values Key
67      * 0: anyone
68      * 1: contacts
69      * 2: starred
70      * 3: none
71      */
72     private String[] mValues;
73 
74     @Before
setup()75     public void setup() {
76         MockitoAnnotations.initMocks(this);
77         ShadowApplication shadowApplication = ShadowApplication.getInstance();
78         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
79 
80         mContext = shadowApplication.getApplicationContext();
81         mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values);
82         mContentResolver = RuntimeEnvironment.application.getContentResolver();
83         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
84 
85         when(mBackend.getPriorityMessageSenders())
86             .thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
87         when(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE))
88                 .thenCallRealMethod();
89         when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
90                 .thenCallRealMethod();
91 
92         mController = new ZenModeMessagesPreferenceController(mContext, mock(Lifecycle.class));
93         ReflectionHelpers.setField(mController, "mBackend", mBackend);
94 
95         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
96                 mockPref);
97         mController.displayPreference(mPreferenceScreen);
98     }
99 
100     @Test
updateState_TotalSilence()101     public void updateState_TotalSilence() {
102         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
103 
104         when(mBackend.isPriorityCategoryEnabled(
105                 NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
106                 .thenReturn(false);
107         final ListPreference mockPref = mock(ListPreference.class);
108         mController.updateState(mockPref);
109 
110         verify(mockPref).setEnabled(false);
111         verify(mockPref).setSummary(R.string.zen_mode_from_none);
112     }
113 
114     @Test
updateState_AlarmsOnly()115     public void updateState_AlarmsOnly() {
116         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
117 
118         final ListPreference mockPref = mock(ListPreference.class);
119         mController.updateState(mockPref);
120 
121         verify(mockPref).setEnabled(false);
122         verify(mockPref).setSummary(R.string.zen_mode_from_none);
123     }
124 
125     @Test
updateState_Priority()126     public void updateState_Priority() {
127         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
128 
129         when(mBackend.isPriorityCategoryEnabled(
130                 NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
131                 .thenReturn(true);
132 
133         mController.updateState(mockPref);
134 
135         verify(mockPref).setEnabled(true);
136         verify(mockPref).setSummary(R.string.zen_mode_from_starred);
137     }
138 
139     @Test
onPreferenceChange_setSelectedContacts_any()140     public void onPreferenceChange_setSelectedContacts_any() {
141         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
142         when(mBackend.getPriorityMessageSenders()).thenReturn(
143                 NotificationManager.Policy.PRIORITY_SENDERS_ANY);
144         mController.updateState(mockPref);
145         verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
146                 ZenModeBackend.ZEN_MODE_FROM_ANYONE)]);
147     }
148 
149     @Test
onPreferenceChange_setSelectedContacts_none()150     public void onPreferenceChange_setSelectedContacts_none() {
151         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
152         when(mBackend.getPriorityMessageSenders()).thenReturn(ZenModeBackend.SOURCE_NONE);
153         mController.updateState(mockPref);
154         verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
155                 ZenModeBackend.ZEN_MODE_FROM_NONE)]);
156     }
157 
158     @Test
onPreferenceChange_setSelectedContacts_starred()159     public void onPreferenceChange_setSelectedContacts_starred() {
160         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
161         when(mBackend.getPriorityMessageSenders()).thenReturn(
162                 NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
163         mController.updateState(mockPref);
164         verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
165                 ZenModeBackend.ZEN_MODE_FROM_STARRED)]);
166     }
167 
168     @Test
onPreferenceChange_setSelectedContacts_contacts()169     public void onPreferenceChange_setSelectedContacts_contacts() {
170         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
171         when(mBackend.getPriorityMessageSenders()).thenReturn(
172                 NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
173         mController.updateState(mockPref);
174         verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
175                 ZenModeBackend.ZEN_MODE_FROM_CONTACTS)]);
176     }
177 }