2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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;
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;
33 import static com.google.common.truth.Truth.assertThat;
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;
42 import android.app.NotificationManager;
43 import android.content.Context;
44 import androidx.preference.PreferenceScreen;
46 import com.android.settings.testutils.FakeFeatureFactory;
47 import com.android.settings.testutils.SettingsRobolectricTestRunner;
48 import com.android.settingslib.core.lifecycle.Lifecycle;
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;
58 @RunWith(SettingsRobolectricTestRunner.class)
59 public class ZenModeVisEffectsCustomPreferenceControllerTest {
60 private ZenModeVisEffectsCustomPreferenceController mController;
63 private ZenModeBackend mBackend;
65 private ZenCustomRadioButtonPreference mockPref;
66 private Context mContext;
67 private FakeFeatureFactory mFeatureFactory;
69 private PreferenceScreen mScreen;
70 @Mock NotificationManager mNotificationManager;
72 private static final String PREF_KEY = "main_pref";
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);
87 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
88 mController.displayPreference(mScreen);
92 public void isAvailable_noVisEffects() {
93 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
94 assertThat(mController.isAvailable()).isTrue();
98 public void isAvailable_visEffects() {
99 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
100 assertThat(mController.isAvailable()).isTrue();
105 public void updateState_notChecked_noVisEffects() {
106 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
107 mController.updateState(mockPref);
109 verify(mockPref).setChecked(false);
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);
126 verify(mockPref).setChecked(false);
130 public void updateState_checked() {
131 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
132 mController.updateState(mockPref);
134 verify(mockPref).setChecked(true);
138 public void updateState_listeners() {
139 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
140 mController.updateState(mockPref);
142 verify(mockPref).setOnGearClickListener(any());
143 verify(mockPref).setOnRadioButtonClickListener(any());