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.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;
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;
28 import static com.google.common.truth.Truth.assertThat;
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;
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;
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;
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;
56 @RunWith(SettingsRobolectricTestRunner.class)
57 public class ZenOnboardingActivityTest {
60 MetricsLogger mMetricsLogger;
62 NotificationManager mNm;
64 ZenOnboardingActivity mActivity;
66 private Context mContext;
67 private FakeFeatureFactory mFeatureFactory;
71 MockitoAnnotations.initMocks(this);
72 ShadowApplication shadowApplication = ShadowApplication.getInstance();
73 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
75 mActivity = Robolectric.buildActivity(ZenOnboardingActivity.class)
78 mActivity.setNotificationManager(mNm);
79 mActivity.setMetricsLogger(mMetricsLogger);
83 mContext = RuntimeEnvironment.application;
84 mFeatureFactory = FakeFeatureFactory.setupForTest();
85 when(mFeatureFactory.suggestionsFeatureProvider.getSharedPrefs(any(Context.class)))
86 .thenReturn(getSharedPreferences());
90 public void loadUiRecordsEvent() {
91 verify(mMetricsLogger).visible(MetricsEvent.SETTINGS_ZEN_ONBOARDING);
95 public void saveNewSetting() {
96 Policy policy = new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON);
97 when(mNm.getNotificationPolicy()).thenReturn(policy);
99 mActivity.mNewSetting.performClick();
100 mActivity.save(null);
102 verify(mMetricsLogger).action(MetricsEvent.ACTION_ZEN_ONBOARDING_OK);
104 ArgumentCaptor<Policy> captor = ArgumentCaptor.forClass(Policy.class);
105 verify(mNm).setNotificationPolicy(captor.capture());
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());
117 public void keepCurrentSetting() {
118 Policy policy = new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON);
119 when(mNm.getNotificationPolicy()).thenReturn(policy);
121 mActivity.mKeepCurrentSetting.performClick();
122 mActivity.save(null);
124 verify(mMetricsLogger).action(MetricsEvent.ACTION_ZEN_ONBOARDING_KEEP_CURRENT_SETTINGS);
125 verify(mNm, never()).setNotificationPolicy(any());
129 public void isSuggestionComplete_zenUpdated() {
130 Policy policy = new Policy(0, 0, 0, 0);
131 when(mNm.getNotificationPolicy()).thenReturn(policy);
134 setShowSettingsSuggestion(false);
135 setWithinTimeThreshold(true);
136 assertThat(isSuggestionComplete(mContext)).isTrue();
140 public void isSuggestionComplete_withinTimeThreshold() {
141 Policy policy = new Policy(0, 0, 0, 0);
142 when(mNm.getNotificationPolicy()).thenReturn(policy);
144 setZenUpdated(false);
145 setShowSettingsSuggestion(false);
146 setWithinTimeThreshold(true);
147 assertThat(isSuggestionComplete(mContext)).isFalse();
151 public void isSuggestionComplete_showSettingsSuggestionTrue() {
152 Policy policy = new Policy(0, 0, 0, 0);
153 when(mNm.getNotificationPolicy()).thenReturn(policy);
155 setZenUpdated(false);
156 setShowSettingsSuggestion(true);
157 setWithinTimeThreshold(false);
158 assertThat(isSuggestionComplete(mContext)).isFalse();
162 public void isSuggestionComplete_showSettingsSuggestionFalse_notWithinTimeThreshold() {
163 Policy policy = new Policy(0, 0, 0, 0);
164 when(mNm.getNotificationPolicy()).thenReturn(policy);
166 setZenUpdated(false);
167 setShowSettingsSuggestion(false);
168 setWithinTimeThreshold(false);
169 assertThat(isSuggestionComplete(mContext)).isTrue();
174 public void isSuggestionComplete_visualEffectsUpdated() {
175 // all values suppressed
176 Policy policy = new Policy(0, 0, 0, 511);
177 when(mNm.getNotificationPolicy()).thenReturn(policy);
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);
188 private void setZenUpdated(boolean updated) {
189 int zenUpdated = updated ? 1 : 0;
191 Settings.Global.putInt(mContext.getContentResolver(),
192 Settings.Global.ZEN_SETTINGS_UPDATED, zenUpdated);
195 private void setWithinTimeThreshold(boolean withinTime) {
196 long firstTime = System.currentTimeMillis();
199 firstTime -= ALWAYS_SHOW_THRESHOLD / 2;
201 firstTime -= ALWAYS_SHOW_THRESHOLD * 2;
204 getSharedPreferences().edit().putLong(PREF_KEY_SUGGESTION_FIRST_DISPLAY_TIME,
208 private void setShowSettingsSuggestion(boolean show) {
209 int showZenSuggestion = 0;
211 showZenSuggestion = 1;
214 Settings.Global.putInt(mContext.getContentResolver(),
215 Settings.Global.SHOW_ZEN_SETTINGS_SUGGESTION, showZenSuggestion);
218 private SharedPreferences getSharedPreferences() {
219 return mContext.getSharedPreferences("test_zen_sugg", Context.MODE_PRIVATE);