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_BADGE; 20 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; 21 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; 22 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; 23 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertFalse; 26 import static org.junit.Assert.assertTrue; 27 import static org.mockito.ArgumentMatchers.anyInt; 28 import static org.mockito.ArgumentMatchers.eq; 29 import static org.mockito.ArgumentMatchers.nullable; 30 import static org.mockito.Mockito.mock; 31 import static org.mockito.Mockito.never; 32 import static org.mockito.Mockito.times; 33 import static org.mockito.Mockito.verify; 34 import static org.mockito.Mockito.when; 35 36 import android.app.NotificationManager; 37 import android.content.Context; 38 import android.content.res.Resources; 39 import android.support.v7.preference.CheckBoxPreference; 40 import android.support.v7.preference.PreferenceScreen; 41 42 import com.android.settings.testutils.FakeFeatureFactory; 43 import com.android.settings.testutils.SettingsRobolectricTestRunner; 44 import com.android.settingslib.core.lifecycle.Lifecycle; 45 import com.android.settings.widget.DisabledCheckBoxPreference; 46 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.Mock; 51 import org.mockito.MockitoAnnotations; 52 import org.robolectric.shadows.ShadowApplication; 53 import org.robolectric.util.ReflectionHelpers; 54 55 @RunWith(SettingsRobolectricTestRunner.class) 56 public class ZenModeVisEffectPreferenceControllerTest { 57 private ZenModeVisEffectPreferenceController mController; 58 59 @Mock 60 private ZenModeBackend mBackend; 61 @Mock 62 private DisabledCheckBoxPreference mockPref; 63 private Context mContext; 64 private FakeFeatureFactory mFeatureFactory; 65 @Mock 66 private PreferenceScreen mScreen; 67 @Mock NotificationManager mNotificationManager; 68 69 private static final String PREF_KEY = "main_pref"; 70 private static final int PREF_METRICS = 1; 71 private static final int PARENT_EFFECT1 = SUPPRESSED_EFFECT_BADGE; 72 private static final int PARENT_EFFECT2 = SUPPRESSED_EFFECT_NOTIFICATION_LIST; 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 ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class), 84 PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, null); 85 ReflectionHelpers.setField(mController, "mBackend", mBackend); 86 87 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref); 88 mController.displayPreference(mScreen); 89 } 90 91 @Test isAvailable()92 public void isAvailable() { 93 // SUPPRESSED_EFFECT_PEEK is always available: 94 assertTrue(mController.isAvailable()); 95 96 // SUPPRESSED_EFFECT_LIGHTS is only available if the device has an LED: 97 Context mockContext = mock(Context.class); 98 mController = new ZenModeVisEffectPreferenceController(mockContext, mock(Lifecycle.class), 99 PREF_KEY, SUPPRESSED_EFFECT_LIGHTS, PREF_METRICS, null); 100 Resources mockResources = mock(Resources.class); 101 when(mockContext.getResources()).thenReturn(mockResources); 102 103 when(mockResources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) 104 .thenReturn(false); // no light 105 assertFalse(mController.isAvailable()); 106 107 when(mockResources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) 108 .thenReturn(true); // has light 109 assertTrue(mController.isAvailable()); 110 } 111 112 @Test updateState_notChecked()113 public void updateState_notChecked() { 114 when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false); 115 mController.updateState(mockPref); 116 117 verify(mockPref).setChecked(false); 118 verify(mockPref).enableCheckbox(true); 119 } 120 121 @Test updateState_checked()122 public void updateState_checked() { 123 when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(true); 124 mController.updateState(mockPref); 125 126 verify(mockPref).setChecked(true); 127 verify(mockPref).enableCheckbox(true); 128 } 129 130 @Test updateState_checkedFalse_parentChecked()131 public void updateState_checkedFalse_parentChecked() { 132 mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class), 133 PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, 134 new int[] {PARENT_EFFECT1, PARENT_EFFECT2}); 135 ReflectionHelpers.setField(mController, "mBackend", mBackend); 136 when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false); 137 when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT1)).thenReturn(false); 138 when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT2)).thenReturn(true); 139 mController.updateState(mockPref); 140 141 verify(mockPref).setChecked(true); 142 verify(mockPref).enableCheckbox(false); 143 verify(mBackend, times(1)).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true); 144 } 145 146 @Test updateState_checkedFalse_parentNotChecked()147 public void updateState_checkedFalse_parentNotChecked() { 148 mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class), 149 PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, 150 new int[] {PARENT_EFFECT1, PARENT_EFFECT2}); 151 ReflectionHelpers.setField(mController, "mBackend", mBackend); 152 when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false); 153 when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT1)).thenReturn(false); 154 when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT2)).thenReturn(false); 155 mController.updateState(mockPref); 156 157 verify(mockPref).setChecked(false); 158 verify(mockPref).enableCheckbox(true); 159 verify(mBackend, never()).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true); 160 } 161 162 @Test onPreferenceChanged_checkedFalse()163 public void onPreferenceChanged_checkedFalse() { 164 mController.onPreferenceChange(mockPref, false); 165 166 verify(mBackend).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, false); 167 verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class), 168 eq(PREF_METRICS), 169 eq(false)); 170 } 171 172 @Test onPreferenceChanged_checkedTrue()173 public void onPreferenceChanged_checkedTrue() { 174 mController.onPreferenceChange(mockPref, true); 175 verify(mBackend).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true); 176 verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class), 177 eq(PREF_METRICS), 178 eq(true)); 179 } 180 } 181