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_SOUND_ONLY; 30 31 import static org.junit.Assert.assertTrue; 32 import static org.mockito.ArgumentMatchers.eq; 33 import static org.mockito.ArgumentMatchers.nullable; 34 import static org.mockito.Mockito.mock; 35 import static org.mockito.Mockito.verify; 36 import static org.mockito.Mockito.when; 37 38 import android.app.NotificationManager; 39 import android.content.Context; 40 41 import androidx.preference.PreferenceScreen; 42 43 import com.android.settings.testutils.FakeFeatureFactory; 44 import com.android.settingslib.core.lifecycle.Lifecycle; 45 46 import org.junit.Before; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 import org.robolectric.RobolectricTestRunner; 52 import org.robolectric.RuntimeEnvironment; 53 import org.robolectric.shadows.ShadowApplication; 54 import org.robolectric.util.ReflectionHelpers; 55 56 @RunWith(RobolectricTestRunner.class) 57 public class ZenModeVisEffectsNonePreferenceControllerTest { 58 private ZenModeVisEffectsNonePreferenceController mController; 59 60 @Mock 61 private ZenModeBackend mBackend; 62 @Mock 63 private ZenCustomRadioButtonPreference mockPref; 64 private Context mContext; 65 private FakeFeatureFactory mFeatureFactory; 66 @Mock 67 private PreferenceScreen mScreen; 68 @Mock 69 NotificationManager mNotificationManager; 70 71 private static final String PREF_KEY = "main_pref"; 72 73 @Before setup()74 public void setup() { 75 MockitoAnnotations.initMocks(this); 76 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 77 mContext = RuntimeEnvironment.application; 78 mFeatureFactory = FakeFeatureFactory.setupForTest(); 79 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 80 when(mNotificationManager.getNotificationPolicy()).thenReturn( 81 mock(NotificationManager.Policy.class)); 82 mController = new ZenModeVisEffectsNonePreferenceController( 83 mContext, mock(Lifecycle.class), PREF_KEY); 84 ReflectionHelpers.setField(mController, "mBackend", mBackend); 85 86 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref); 87 mController.displayPreference(mScreen); 88 } 89 90 @Test isAvailable()91 public void isAvailable() { 92 assertTrue(mController.isAvailable()); 93 } 94 95 @Test updateState_notChecked()96 public void updateState_notChecked() { 97 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1); 98 mController.updateState(mockPref); 99 100 verify(mockPref).setChecked(false); 101 } 102 103 @Test updateState_checked()104 public void updateState_checked() { 105 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0); 106 mController.updateState(mockPref); 107 108 verify(mockPref).setChecked(true); 109 } 110 111 @Test onRadioButtonClick()112 public void onRadioButtonClick() { 113 int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF 114 | SUPPRESSED_EFFECT_SCREEN_ON 115 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT 116 | SUPPRESSED_EFFECT_AMBIENT 117 | SUPPRESSED_EFFECT_STATUS_BAR 118 | SUPPRESSED_EFFECT_BADGE 119 | SUPPRESSED_EFFECT_LIGHTS 120 | SUPPRESSED_EFFECT_PEEK 121 | SUPPRESSED_EFFECT_NOTIFICATION_LIST; 122 mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1); 123 mController.onRadioButtonClick(mockPref); 124 verify(mBackend).saveVisualEffectsPolicy(allSuppressed, false); 125 verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class), 126 eq(ACTION_ZEN_SOUND_ONLY), 127 eq(true)); 128 } 129 } 130