• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
20 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
21 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
22 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
23 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
24 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
25 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
26 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
27 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
28 
29 import static com.google.common.truth.Truth.assertThat;
30 
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 
35 import android.app.NotificationManager;
36 import android.content.Context;
37 import android.support.v7.preference.PreferenceScreen;
38 
39 import com.android.settings.R;
40 import com.android.settings.testutils.SettingsRobolectricTestRunner;
41 import com.android.settingslib.core.lifecycle.Lifecycle;
42 import com.android.settingslib.widget.FooterPreference;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.robolectric.shadows.ShadowApplication;
50 import org.robolectric.util.ReflectionHelpers;
51 
52 @RunWith(SettingsRobolectricTestRunner.class)
53 public class ZenFooterPreferenceControllerTest {
54     private ZenFooterPreferenceController mController;
55 
56     @Mock
57     private ZenModeBackend mBackend;
58     @Mock
59     private FooterPreference mockPref;
60     private Context mContext;
61     @Mock
62     private PreferenceScreen mScreen;
63     @Mock NotificationManager mNotificationManager;
64 
65     private static final String PREF_KEY = "main_pref";
66 
67     @Before
setup()68     public void setup() {
69         MockitoAnnotations.initMocks(this);
70         ShadowApplication shadowApplication = ShadowApplication.getInstance();
71         mContext = shadowApplication.getApplicationContext();
72         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
73         when(mNotificationManager.getNotificationPolicy()).thenReturn(
74                 mock(NotificationManager.Policy.class));
75         mController = new ZenFooterPreferenceController(
76                 mContext, mock(Lifecycle.class), PREF_KEY);
77         ReflectionHelpers.setField(mController, "mBackend", mBackend);
78 
79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
80     }
81 
82     @Test
isAvailable_noVisEffects()83     public void isAvailable_noVisEffects() {
84         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
85         assertThat(mController.isAvailable()).isTrue();
86     }
87 
88     @Test
isAvailable_someVisEffects()89     public void isAvailable_someVisEffects() {
90         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
91         assertThat(mController.isAvailable()).isFalse();
92     }
93 
94     @Test
isAvailable_allVisEffects()95     public void isAvailable_allVisEffects() {
96         int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
97                 | SUPPRESSED_EFFECT_SCREEN_ON
98                 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
99                 | SUPPRESSED_EFFECT_AMBIENT
100                 | SUPPRESSED_EFFECT_STATUS_BAR
101                 | SUPPRESSED_EFFECT_BADGE
102                 | SUPPRESSED_EFFECT_LIGHTS
103                 | SUPPRESSED_EFFECT_PEEK
104                 | SUPPRESSED_EFFECT_NOTIFICATION_LIST;
105         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
106         assertThat(mController.isAvailable()).isTrue();
107     }
108 
109     @Test
updateSummary_noVisEffects()110     public void updateSummary_noVisEffects() {
111         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
112         mController.updateState(mockPref);
113         verify(mockPref).setTitle(R.string.zen_mode_restrict_notifications_mute_footer);
114     }
115 
116     @Test
getSummary_someVisEffects()117     public void getSummary_someVisEffects() {
118         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
119         mController.updateState(mockPref);
120         verify(mockPref).setTitle(null);
121     }
122 
123     @Test
getSummary_allVisEffects()124     public void getSummary_allVisEffects() {
125         int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
126                 | SUPPRESSED_EFFECT_SCREEN_ON
127                 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
128                 | SUPPRESSED_EFFECT_AMBIENT
129                 | SUPPRESSED_EFFECT_STATUS_BAR
130                 | SUPPRESSED_EFFECT_BADGE
131                 | SUPPRESSED_EFFECT_LIGHTS
132                 | SUPPRESSED_EFFECT_PEEK
133                 | SUPPRESSED_EFFECT_NOTIFICATION_LIST;
134         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
135         mController.updateState(mockPref);
136         verify(mockPref).setTitle(R.string.zen_mode_restrict_notifications_hide_footer);
137     }
138 }
139