• 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.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_ZEN_CUSTOM;
30 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
31         .ACTION_ZEN_SOUND_AND_VIS_EFFECTS;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.anyInt;
37 import static org.mockito.ArgumentMatchers.eq;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41 
42 import android.app.NotificationManager;
43 import android.content.Context;
44 import android.support.v7.preference.PreferenceScreen;
45 
46 import com.android.settings.testutils.FakeFeatureFactory;
47 import com.android.settings.testutils.SettingsRobolectricTestRunner;
48 import com.android.settingslib.core.lifecycle.Lifecycle;
49 
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Mock;
54 import org.mockito.MockitoAnnotations;
55 import org.robolectric.shadows.ShadowApplication;
56 import org.robolectric.util.ReflectionHelpers;
57 
58 @RunWith(SettingsRobolectricTestRunner.class)
59 public class ZenModeVisEffectsCustomPreferenceControllerTest {
60     private ZenModeVisEffectsCustomPreferenceController mController;
61 
62     @Mock
63     private ZenModeBackend mBackend;
64     @Mock
65     private ZenCustomRadioButtonPreference mockPref;
66     private Context mContext;
67     private FakeFeatureFactory mFeatureFactory;
68     @Mock
69     private PreferenceScreen mScreen;
70     @Mock NotificationManager mNotificationManager;
71 
72     private static final String PREF_KEY = "main_pref";
73 
74     @Before
setup()75     public void setup() {
76         MockitoAnnotations.initMocks(this);
77         ShadowApplication shadowApplication = ShadowApplication.getInstance();
78         mContext = shadowApplication.getApplicationContext();
79         mFeatureFactory = FakeFeatureFactory.setupForTest();
80         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
81         when(mNotificationManager.getNotificationPolicy()).thenReturn(
82                 mock(NotificationManager.Policy.class));
83         mController = new ZenModeVisEffectsCustomPreferenceController(
84                 mContext, mock(Lifecycle.class), PREF_KEY);
85         ReflectionHelpers.setField(mController, "mBackend", mBackend);
86 
87         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
88         mController.displayPreference(mScreen);
89     }
90 
91     @Test
isAvailable_noVisEffects()92     public void isAvailable_noVisEffects() {
93         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
94         assertThat(mController.isAvailable()).isTrue();
95     }
96 
97     @Test
isAvailable_visEffects()98     public void isAvailable_visEffects() {
99         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
100         assertThat(mController.isAvailable()).isTrue();
101     }
102 
103 
104     @Test
updateState_notChecked_noVisEffects()105     public void updateState_notChecked_noVisEffects() {
106         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
107         mController.updateState(mockPref);
108 
109         verify(mockPref).setChecked(false);
110     }
111 
112     @Test
updateState_notChecked_allVisEffects()113     public void updateState_notChecked_allVisEffects() {
114         int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
115                 | SUPPRESSED_EFFECT_SCREEN_ON
116                 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
117                 | SUPPRESSED_EFFECT_AMBIENT
118                 | SUPPRESSED_EFFECT_STATUS_BAR
119                 | SUPPRESSED_EFFECT_BADGE
120                 | SUPPRESSED_EFFECT_LIGHTS
121                 | SUPPRESSED_EFFECT_PEEK
122                 | SUPPRESSED_EFFECT_NOTIFICATION_LIST;
123         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
124         mController.updateState(mockPref);
125 
126         verify(mockPref).setChecked(false);
127     }
128 
129     @Test
updateState_checked()130     public void updateState_checked() {
131         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
132         mController.updateState(mockPref);
133 
134         verify(mockPref).setChecked(true);
135     }
136 
137     @Test
updateState_listeners()138     public void updateState_listeners() {
139         mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
140         mController.updateState(mockPref);
141 
142         verify(mockPref).setOnGearClickListener(any());
143         verify(mockPref).setOnRadioButtonClickListener(any());
144     }
145 }
146