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.PRIORITY_CATEGORY_ALARMS; 20 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS; 21 import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; 22 23 import static com.android.settings.notification.ZenOnboardingActivity.ALWAYS_SHOW_THRESHOLD; 24 import static com.android.settings.notification.ZenOnboardingActivity 25 .PREF_KEY_SUGGESTION_FIRST_DISPLAY_TIME; 26 import static com.android.settings.notification.ZenOnboardingActivity.isSuggestionComplete; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 import static org.mockito.ArgumentMatchers.any; 31 import static org.mockito.Mockito.never; 32 import static org.mockito.Mockito.verify; 33 import static org.mockito.Mockito.when; 34 35 import android.app.NotificationManager; 36 import android.app.NotificationManager.Policy; 37 import android.content.Context; 38 import android.content.SharedPreferences; 39 import android.provider.Settings; 40 41 import com.android.internal.logging.MetricsLogger; 42 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 43 import com.android.settings.testutils.FakeFeatureFactory; 44 import com.android.settings.testutils.SettingsRobolectricTestRunner; 45 46 import org.junit.Before; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.ArgumentCaptor; 50 import org.mockito.Mock; 51 import org.mockito.MockitoAnnotations; 52 import org.robolectric.Robolectric; 53 import org.robolectric.RuntimeEnvironment; 54 import org.robolectric.shadows.ShadowApplication; 55 56 @RunWith(SettingsRobolectricTestRunner.class) 57 public class ZenOnboardingActivityTest { 58 59 @Mock 60 MetricsLogger mMetricsLogger; 61 @Mock 62 NotificationManager mNm; 63 64 ZenOnboardingActivity mActivity; 65 66 private Context mContext; 67 private FakeFeatureFactory mFeatureFactory; 68 69 @Before setUp()70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 73 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm); 74 75 mActivity = Robolectric.buildActivity(ZenOnboardingActivity.class) 76 .create() 77 .get(); 78 mActivity.setNotificationManager(mNm); 79 mActivity.setMetricsLogger(mMetricsLogger); 80 81 mActivity.setupUI(); 82 83 mContext = RuntimeEnvironment.application; 84 mFeatureFactory = FakeFeatureFactory.setupForTest(); 85 when(mFeatureFactory.suggestionsFeatureProvider.getSharedPrefs(any(Context.class))) 86 .thenReturn(getSharedPreferences()); 87 } 88 89 @Test loadUiRecordsEvent()90 public void loadUiRecordsEvent() { 91 verify(mMetricsLogger).visible(MetricsEvent.SETTINGS_ZEN_ONBOARDING); 92 } 93 94 @Test saveNewSetting()95 public void saveNewSetting() { 96 Policy policy = new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON); 97 when(mNm.getNotificationPolicy()).thenReturn(policy); 98 99 mActivity.mNewSetting.performClick(); 100 mActivity.save(null); 101 102 verify(mMetricsLogger).action(MetricsEvent.ACTION_ZEN_ONBOARDING_OK); 103 104 ArgumentCaptor<Policy> captor = ArgumentCaptor.forClass(Policy.class); 105 verify(mNm).setNotificationPolicy(captor.capture()); 106 107 Policy actual = captor.getValue(); 108 assertThat(actual.priorityCategories).isEqualTo(PRIORITY_CATEGORY_ALARMS 109 | PRIORITY_CATEGORY_REPEAT_CALLERS); 110 assertThat(actual.priorityCallSenders).isEqualTo(Policy.PRIORITY_SENDERS_STARRED); 111 assertThat(actual.priorityMessageSenders).isEqualTo(Policy.PRIORITY_SENDERS_ANY); 112 assertThat(actual.suppressedVisualEffects).isEqualTo( 113 Policy.getAllSuppressedVisualEffects()); 114 } 115 116 @Test keepCurrentSetting()117 public void keepCurrentSetting() { 118 Policy policy = new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON); 119 when(mNm.getNotificationPolicy()).thenReturn(policy); 120 121 mActivity.mKeepCurrentSetting.performClick(); 122 mActivity.save(null); 123 124 verify(mMetricsLogger).action(MetricsEvent.ACTION_ZEN_ONBOARDING_KEEP_CURRENT_SETTINGS); 125 verify(mNm, never()).setNotificationPolicy(any()); 126 } 127 128 @Test isSuggestionComplete_zenUpdated()129 public void isSuggestionComplete_zenUpdated() { 130 Policy policy = new Policy(0, 0, 0, 0); 131 when(mNm.getNotificationPolicy()).thenReturn(policy); 132 133 setZenUpdated(true); 134 setShowSettingsSuggestion(false); 135 setWithinTimeThreshold(true); 136 assertThat(isSuggestionComplete(mContext)).isTrue(); 137 } 138 139 @Test isSuggestionComplete_withinTimeThreshold()140 public void isSuggestionComplete_withinTimeThreshold() { 141 Policy policy = new Policy(0, 0, 0, 0); 142 when(mNm.getNotificationPolicy()).thenReturn(policy); 143 144 setZenUpdated(false); 145 setShowSettingsSuggestion(false); 146 setWithinTimeThreshold(true); 147 assertThat(isSuggestionComplete(mContext)).isFalse(); 148 } 149 150 @Test isSuggestionComplete_showSettingsSuggestionTrue()151 public void isSuggestionComplete_showSettingsSuggestionTrue() { 152 Policy policy = new Policy(0, 0, 0, 0); 153 when(mNm.getNotificationPolicy()).thenReturn(policy); 154 155 setZenUpdated(false); 156 setShowSettingsSuggestion(true); 157 setWithinTimeThreshold(false); 158 assertThat(isSuggestionComplete(mContext)).isFalse(); 159 } 160 161 @Test isSuggestionComplete_showSettingsSuggestionFalse_notWithinTimeThreshold()162 public void isSuggestionComplete_showSettingsSuggestionFalse_notWithinTimeThreshold() { 163 Policy policy = new Policy(0, 0, 0, 0); 164 when(mNm.getNotificationPolicy()).thenReturn(policy); 165 166 setZenUpdated(false); 167 setShowSettingsSuggestion(false); 168 setWithinTimeThreshold(false); 169 assertThat(isSuggestionComplete(mContext)).isTrue(); 170 } 171 172 173 @Test isSuggestionComplete_visualEffectsUpdated()174 public void isSuggestionComplete_visualEffectsUpdated() { 175 // all values suppressed 176 Policy policy = new Policy(0, 0, 0, 511); 177 when(mNm.getNotificationPolicy()).thenReturn(policy); 178 179 setZenUpdated(false); 180 setShowSettingsSuggestion(true); 181 setWithinTimeThreshold(true); 182 assertThat(isSuggestionComplete(mContext)).isTrue(); 183 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 184 Settings.Global.ZEN_SETTINGS_UPDATED, -1)).isEqualTo(1); 185 } 186 187 setZenUpdated(boolean updated)188 private void setZenUpdated(boolean updated) { 189 int zenUpdated = updated ? 1 : 0; 190 191 Settings.Global.putInt(mContext.getContentResolver(), 192 Settings.Global.ZEN_SETTINGS_UPDATED, zenUpdated); 193 } 194 setWithinTimeThreshold(boolean withinTime)195 private void setWithinTimeThreshold(boolean withinTime) { 196 long firstTime = System.currentTimeMillis(); 197 198 if (withinTime) { 199 firstTime -= ALWAYS_SHOW_THRESHOLD / 2; 200 } else { 201 firstTime -= ALWAYS_SHOW_THRESHOLD * 2; 202 } 203 204 getSharedPreferences().edit().putLong(PREF_KEY_SUGGESTION_FIRST_DISPLAY_TIME, 205 firstTime).commit(); 206 } 207 setShowSettingsSuggestion(boolean show)208 private void setShowSettingsSuggestion(boolean show) { 209 int showZenSuggestion = 0; 210 if (show) { 211 showZenSuggestion = 1; 212 } 213 214 Settings.Global.putInt(mContext.getContentResolver(), 215 Settings.Global.SHOW_ZEN_SETTINGS_SUGGESTION, showZenSuggestion); 216 } 217 getSharedPreferences()218 private SharedPreferences getSharedPreferences() { 219 return mContext.getSharedPreferences("test_zen_sugg", Context.MODE_PRIVATE); 220 } 221 } 222