Reorganize notification Settings classes

The notification package was getting too big.

Test: make -j64 RunSettingsRoboTests
Fixes: 145224451
Change-Id: I25ba82f42f7a137d8adcce72dcf8089d0e018bdc
This commit is contained in:
Julia Reynolds
2019-11-26 16:14:03 -05:00
parent c050807cdc
commit aceccce75c
201 changed files with 402 additions and 388 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenCustomRadioButtonPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class ZenCustomRadioButtonPreferenceTest {
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
}
@Test
public void createNewPreference_shouldSetLayout() {
final ZenCustomRadioButtonPreference preference
= new ZenCustomRadioButtonPreference(mContext);
assertThat(preference.getLayoutResource()).isEqualTo(R.layout.preference_two_target_radio);
assertThat(preference.getWidgetLayoutResource())
.isEqualTo(R.layout.preference_widget_gear);
}
@Test
public void setChecked_shouldUpdateButtonCheckedState() {
final ZenCustomRadioButtonPreference preference =
new ZenCustomRadioButtonPreference(mContext);
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(
R.layout.preference_two_target_radio, null));
final RadioButton toggle = (RadioButton) holder.findViewById(android.R.id.checkbox);
preference.onBindViewHolder(holder);
preference.setChecked(true);
assertThat(toggle.isChecked()).isTrue();
preference.setChecked(false);
assertThat(toggle.isChecked()).isFalse();
}
@Test
public void clickRadioButton_shouldNotifyRadioButtonClicked() {
final ZenCustomRadioButtonPreference preference
= new ZenCustomRadioButtonPreference(mContext);
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_two_target_radio, null));
final View toggle = holder.findViewById(R.id.checkbox_frame);
ZenCustomRadioButtonPreference.OnRadioButtonClickListener l = mock(
ZenCustomRadioButtonPreference.OnRadioButtonClickListener.class);
preference.setOnRadioButtonClickListener(l);
preference.onBindViewHolder(holder);
toggle.performClick();
verify(l).onRadioButtonClick(preference);
}
@Test
public void clickWidgetView_shouldNotifyWidgetClicked() {
final ZenCustomRadioButtonPreference preference =
new ZenCustomRadioButtonPreference(mContext);
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_two_target_radio, null));
final View widgetView = holder.findViewById(android.R.id.widget_frame);
ZenCustomRadioButtonPreference.OnGearClickListener l = mock(
ZenCustomRadioButtonPreference.OnGearClickListener.class);
preference.setOnGearClickListener(l);
preference.onBindViewHolder(holder);
widgetView.performClick();
verify(l).onGearClick(preference);
}
}

View File

@@ -0,0 +1,143 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenFooterPreferenceController;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.FooterPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenFooterPreferenceControllerTest {
private ZenFooterPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private FooterPreference mockPref;
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
NotificationManager mNotificationManager;
private static final String PREF_KEY = "main_pref";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
when(mNotificationManager.getNotificationPolicy()).thenReturn(
mock(NotificationManager.Policy.class));
mController = new ZenFooterPreferenceController(
mContext, mock(Lifecycle.class), PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
}
@Test
public void isAvailable_noVisEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_someVisEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_allVisEffects() {
int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
| SUPPRESSED_EFFECT_SCREEN_ON
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_AMBIENT
| SUPPRESSED_EFFECT_STATUS_BAR
| SUPPRESSED_EFFECT_BADGE
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK
| SUPPRESSED_EFFECT_NOTIFICATION_LIST;
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateSummary_noVisEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
mController.updateState(mockPref);
verify(mockPref).setTitle(R.string.zen_mode_restrict_notifications_mute_footer);
}
@Test
public void getSummary_someVisEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
mController.updateState(mockPref);
verify(mockPref).setTitle(null);
}
@Test
public void getSummary_allVisEffects() {
int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
| SUPPRESSED_EFFECT_SCREEN_ON
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_AMBIENT
| SUPPRESSED_EFFECT_STATUS_BAR
| SUPPRESSED_EFFECT_BADGE
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK
| SUPPRESSED_EFFECT_NOTIFICATION_LIST;
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
mController.updateState(mockPref);
verify(mockPref).setTitle(R.string.zen_mode_restrict_notifications_hide_footer);
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.notification.zen.ZenModeAlarmsPreferenceController;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
@RunWith(RobolectricTestRunner.class)
public class ZenModeAlarmsPreferenceControllerTest {
private static final boolean ALARMS_SETTINGS = true;
private ZenModeAlarmsPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private Context mContext;
private ContentResolver mContentResolver;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeAlarmsPreferenceController(mContext, mock(Lifecycle.class),
"zen_mode_behavior_alarms");
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(true);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS)).thenReturn(ALARMS_SETTINGS);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(ALARMS_SETTINGS);
}
@Test
public void onPreferenceChanged_EnableAlarms() {
boolean allowAlarms = true;
mController.onPreferenceChange(mockPref, allowAlarms);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, allowAlarms);
}
@Test
public void onPreferenceChanged_DisableAlarms() {
boolean allowAlarms = false;
mController.onPreferenceChange(mockPref, allowAlarms);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, allowAlarms);
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationChannel;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.ParceledListSlice;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.zen.ZenModeAllBypassingAppsPreferenceController;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
public class ZenModeAllBypassingAppsPreferenceControllerTest {
private ZenModeAllBypassingAppsPreferenceController mController;
private Context mContext;
@Mock
private NotificationBackend mBackend;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private ApplicationsState mApplicationState;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new ZenModeAllBypassingAppsPreferenceController(
mContext, null, mock(Fragment.class));
mController.mPreferenceScreen = mPreferenceScreen;
mController.mApplicationsState = mApplicationState;
mController.mPrefContext = mContext;
ReflectionHelpers.setField(mController, "mNotificationBackend", mBackend);
}
@Test
public void testIsAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testUpdateNotificationChannelList() {
ApplicationsState.AppEntry entry = mock(ApplicationsState.AppEntry.class);
entry.info = new ApplicationInfo();
entry.info.packageName = "test";
entry.info.uid = 0;
List<ApplicationsState.AppEntry> appEntries = new ArrayList<>();
appEntries.add(entry);
List<NotificationChannel> channelsBypassing = new ArrayList<>();
channelsBypassing.add(mock(NotificationChannel.class));
channelsBypassing.add(mock(NotificationChannel.class));
channelsBypassing.add(mock(NotificationChannel.class));
when(mBackend.getNotificationChannelsBypassingDnd(entry.info.packageName,
entry.info.uid)).thenReturn(new ParceledListSlice<>(channelsBypassing));
mController.updateNotificationChannelList(appEntries);
verify(mPreferenceScreen, times(3)).addPreference(any());
}
@Test
public void testUpdateNotificationChannelList_nullChannels() {
mController.updateNotificationChannelList(null);
verify(mPreferenceScreen, never()).addPreference(any());
}
@Test
public void testUpdateNotificationChannelList_emptyChannelsList() {
mController.updateNotificationChannelList(new ArrayList<>());
verify(mPreferenceScreen, never()).addPreference(any());
}
}

View File

@@ -0,0 +1,159 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.content.Context;
import android.provider.Settings;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenModeAutomaticRulesPreferenceController;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenRulePreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.util.reflection.FieldSetter;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
import java.util.HashMap;
import java.util.Map;
@RunWith(RobolectricTestRunner.class)
public class ZenModeAutomaticRulesPreferenceControllerTest {
private ZenModeAutomaticRulesPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private PreferenceCategory mockPref;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private ZenRulePreference mZenRulePreference;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = spy(new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
null));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mockPref);
mController.displayPreference(mPreferenceScreen);
doReturn(mZenRulePreference).when(mController).createZenRulePreference(any());
}
@Test
public void testUpdateState_clearsPreferencesWhenAddingNewPreferences() {
final int NUM_RULES = 3;
Map<String, AutomaticZenRule> rMap = new HashMap<>();
String ruleId1 = "test1_id";
String ruleId2 = "test2_id";
String ruleId3 = "test3_id";
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null, null,
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
AutomaticZenRule autoRule2 = new AutomaticZenRule("test_rule_2", null, null,
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20);
AutomaticZenRule autoRule3 = new AutomaticZenRule("test_rule_3", null, null,
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 30);
rMap.put(ruleId1, autoRule1);
rMap.put(ruleId2, autoRule2);
rMap.put(ruleId3, autoRule3);
// should add 3 new preferences to mockPref
mockGetAutomaticZenRules(NUM_RULES, rMap);
mController.updateState(mockPref);
verify(mockPref, times(1)).removeAll();
verify(mockPref, times(NUM_RULES)).addPreference(any());
}
@Test
public void testUpdateState_clearsPreferencesWhenRemovingPreferences(){
final int NUM_RULES = 2;
Map<String, AutomaticZenRule> rMap = new HashMap<>();
String ruleId1 = "test1_id";
String ruleId2 = "test2_id";
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null, null,
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
AutomaticZenRule autoRule2 = new AutomaticZenRule("test_rule_2", null, null,
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20);
rMap.put(ruleId1, autoRule1);
rMap.put(ruleId2, autoRule2);
// update state should re-add all preferences since a preference was deleted
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES + 2);
mockGetAutomaticZenRules(NUM_RULES, rMap);
mController.updateState(mockPref);
verify(mockPref, times(1)).removeAll();
verify(mockPref, times(NUM_RULES)).addPreference(any());
}
@Test
public void testUpdateState_updateEnableState() throws NoSuchFieldException {
final int NUM_RULES = 1;
Map<String, AutomaticZenRule> rMap = new HashMap<>();
String testId = "test1_id";
AutomaticZenRule rule = new AutomaticZenRule("rule_name", null, null,
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
rMap.put(testId, rule);
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES);
when(mockPref.getPreference(anyInt())).thenReturn(mZenRulePreference);
// update state should NOT re-add all the preferences, should only update enable state
rule.setEnabled(false);
rMap.put(testId, rule);
mockGetAutomaticZenRules(NUM_RULES, rMap);
FieldSetter.setField(mZenRulePreference, ZenRulePreference.class.getDeclaredField("mId"), testId);
mController.updateState(mockPref);
verify(mZenRulePreference, times(1)).updatePreference(any());
verify(mController, never()).reloadAllRules(any());
}
private void mockGetAutomaticZenRules(int numRules, Map<String, AutomaticZenRule> rules) {
Map.Entry<String, AutomaticZenRule>[] arr = new Map.Entry[numRules];
rules.entrySet().toArray(arr);
when(mBackend.getAutomaticZenRules()).thenReturn(arr);
}
}

View File

@@ -0,0 +1,176 @@
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.content.Context;
import android.database.Cursor;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import com.android.settings.notification.zen.ZenModeBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(RobolectricTestRunner.class)
public class ZenModeBackendTest {
@Mock
private NotificationManager mNotificationManager;
private static final String GENERIC_RULE_NAME = "test";
private static final String DEFAULT_ID_1 = ZenModeConfig.EVENTS_DEFAULT_RULE_ID;
private static final String DEFAULT_ID_2 = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID;
private Context mContext;
private ZenModeBackend mBackend;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mBackend = new ZenModeBackend(mContext);
}
@Test
public void updateState_checkRuleOrderingDescending() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(
NUM_RULES, false);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
// check ordering, most recent should be at the end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
}
}
@Test
public void updateState_checkRuleOrderingAscending() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesAscendingCreationTime(
NUM_RULES, false);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
// check ordering, most recent should be at the end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
}
}
@Test
public void updateState_checkRuleOrderingDescending_withDefaultRules() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(NUM_RULES,
true);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
assertEquals(rules[0].getKey(), DEFAULT_ID_1);
assertEquals(rules[1].getKey(), DEFAULT_ID_2);
// NON-DEFAULT RULES check ordering, most recent at the bottom/end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
}
}
@Test
public void updateSummary_nullCursorValues() {
Cursor testCursorWithNullValues = createMockCursor(3);
when(testCursorWithNullValues.getString(0)).thenReturn(null);
// expected - no null values
List<String> contacts = mBackend.getStarredContacts(testCursorWithNullValues);
for (String contact : contacts) {
assertThat(contact).isNotNull();
}
}
private Cursor createMockCursor(int size) {
Cursor mockCursor = mock(Cursor.class);
when(mockCursor.moveToFirst()).thenReturn(true);
doAnswer(new Answer<Boolean>() {
int count = 0;
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
if (count < size) {
count++;
return true;
}
return false;
}
}).when(mockCursor).moveToNext();
return mockCursor;
}
private Map.Entry<String, AutomaticZenRule>[] populateAutoZenRulesAscendingCreationTime(
int numRules, boolean addDefaultRules) {
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
for (int i = 0; i < numRules; i++) {
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
null, null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true,
i * 2));
}
if (addDefaultRules) {
ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null, null, null,
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_2_NAME", null, null, null,
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
}
Map.Entry<String, AutomaticZenRule>[] toReturn = new Map.Entry[ruleMap.size()];
ruleMap.entrySet().toArray(toReturn);
return toReturn;
}
private Map.Entry<String, AutomaticZenRule>[] populateAutoZenRulesDescendingCreationTime(
int numRules, boolean addDefaultRules) {
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
for (int i = 0; i < numRules; i++) {
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
null, null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true,
numRules - i));
}
if (addDefaultRules) {
ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null, null, null,
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_2_NAME", null, null, null,
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
}
Map.Entry<String, AutomaticZenRule>[] toReturn = new Map.Entry[ruleMap.size()];
ruleMap.entrySet().toArray(toReturn);
return toReturn;
}
}

View File

@@ -0,0 +1,240 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_OFF;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ZenRule;
import android.util.ArrayMap;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.zen.AbstractZenModePreferenceController.ZenModeConfigWrapper;
import com.android.settings.notification.zen.ZenModeBehaviorFooterPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeBehaviorFooterPreferenceControllerTest {
private static final String TEST_APP_NAME = "test_app";
private static final String MANUAL_RULE_FIELD = "manualRule";
private static final String AUTOMATIC_RULES_FIELD = "automaticRules";
private ZenModeBehaviorFooterPreferenceController mController;
@Mock
private NotificationManager mNotificationManager;
@Mock
private Preference mockPref;
@Mock
private ZenModeConfig mZenModeConfig;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private ZenModeConfigWrapper mConfigWrapper;
private Context mContext;
private ContentResolver mContentResolver;
private int mTitleResId = R.string.zen_sound_title;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getZenModeConfig()).thenReturn(mZenModeConfig);
mController = new ZenModeBehaviorFooterPreferenceController(
mContext, mock(Lifecycle.class), mTitleResId);
ReflectionHelpers.setField(mController, "mZenModeConfigWrapper", mConfigWrapper);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void totalSilence_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
assertTrue(mController.isAvailable());
}
@Test
public void alarmsOnly_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
assertTrue(mController.isAvailable());
}
@Test
public void priorityOnly_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
assertTrue(mController.isAvailable());
}
@Test
public void zenModeOff_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
assertTrue(mController.isAvailable());
}
@Test
public void zenModeOff_updateState_noFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(mTitleResId));
}
@Test
public void zenModeImportantInterruptions_updateState_noFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(mTitleResId));
}
@Test
public void deprecatedZenModeAlarms_qsManualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_ALARMS;
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_qs_set_behavior));
}
@Test
public void deprecatedZenModeAlarms_appManualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_ALARMS;
injectedManualRule.enabler = TEST_APP_NAME;
when(mConfigWrapper.getOwnerCaption(injectedManualRule.enabler)).thenReturn(TEST_APP_NAME);
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_app_set_behavior, TEST_APP_NAME));
}
@Test
public void deprecatedZenModeNoInterruptions_qsManualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_NO_INTERRUPTIONS;
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_qs_set_behavior));
}
@Test
public void deprecatedZenModeNoInterruptions_appManualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_NO_INTERRUPTIONS;
injectedManualRule.enabler = TEST_APP_NAME;
when(mConfigWrapper.getOwnerCaption(injectedManualRule.enabler)).thenReturn(TEST_APP_NAME);
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_app_set_behavior, TEST_APP_NAME));
}
@Test
public void deprecatedZenModeAlarms_automaticRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
ArrayMap<String, ZenRule> injectedAutomaticRules = new ArrayMap<>();
ZenRule injectedRule = spy(new ZenRule());
injectedRule.zenMode = ZEN_MODE_ALARMS;
injectedRule.component = mock(ComponentName.class);
when(injectedRule.isAutomaticActive()).thenReturn(true);
when(injectedRule.component.getPackageName()).thenReturn(TEST_APP_NAME);
injectedAutomaticRules.put("testid", injectedRule);
ReflectionHelpers.setField(mZenModeConfig, AUTOMATIC_RULES_FIELD, injectedAutomaticRules);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_app_set_behavior, TEST_APP_NAME));
}
@Test
public void deprecatedZenModeNoInterruptions_automaticRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
ArrayMap<String, ZenRule> injectedAutomaticRules = new ArrayMap<>();
ZenRule injectedRule = spy(new ZenRule());
injectedRule.zenMode = ZEN_MODE_NO_INTERRUPTIONS;
injectedRule.component = mock(ComponentName.class);
when(injectedRule.isAutomaticActive()).thenReturn(true);
when(injectedRule.component.getPackageName()).thenReturn(TEST_APP_NAME);
injectedAutomaticRules.put("testid", injectedRule);
ReflectionHelpers.setField(mZenModeConfig, AUTOMATIC_RULES_FIELD, injectedAutomaticRules);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_app_set_behavior, TEST_APP_NAME));
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeBlockedEffectsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public final class ZenModeBlockedEffectsPreferenceControllerTest {
private ZenModeBlockedEffectsPreferenceController mController;
@Mock
private NotificationManager mNotificationManager;
@Mock
private NotificationManager.Policy mPolicy;
private Context mContext;
@Mock
private ZenModeBackend mBackend;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeBlockedEffectsPreferenceController(
mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
public void testIsAvailable() {
assertTrue(mController.isAvailable());
}
@Test
public void testHasSummary() {
assertNotNull(mController.getSummary());
}
}

View File

@@ -0,0 +1,151 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_OFF;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeButtonPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeButtonPreferenceControllerTest {
private ZenModeButtonPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private Preference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private Button mZenButtonOn;
@Mock
private Button mZenButtonOff;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mController = new ZenModeButtonPreferenceController(mContext, mock(Lifecycle.class),
mock(FragmentManager.class));
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
ReflectionHelpers.setField(mController, "mZenButtonOn", mZenButtonOn);
ReflectionHelpers.setField(mController, "mZenButtonOff", mZenButtonOff);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final Preference mockPref = mock(Preference.class);
mController.updateState(mockPref);
verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final Preference mockPref = mock(Preference.class);
mController.updateState(mockPref);
verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
final Preference mockPref = mock(Preference.class);
mController.updateState(mockPref);
verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
}
@Test
public void updateState_ZenOff() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
final Preference mockPref = mock(Preference.class);
mController.updateState(mockPref);
verify(mZenButtonOn).setVisibility(View.VISIBLE);
verify(mZenButtonOff).setVisibility(View.GONE);
}
@Test
public void updateState_otherUserChangedZen() {
final Preference mockPref = mock(Preference.class);
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
mController.updateState(mockPref);
verify(mZenButtonOn).setVisibility(View.VISIBLE);
verify(mZenButtonOff).setVisibility(View.GONE);
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
final int GUEST_USER_ID = 10;
mController.mSettingObserver.onChange(false,
Settings.Global.getUriFor(Settings.Global.ZEN_MODE), GUEST_USER_ID);
verify(mZenButtonOn).setVisibility(View.GONE);
verify(mZenButtonOff).setVisibility(View.VISIBLE);
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.zen.ZenModeBypassingAppsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeBypassingAppsPreferenceControllerTest {
private ZenModeBypassingAppsPreferenceController mController;
private Context mContext;
@Mock
private NotificationBackend mBackend;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new ZenModeBypassingAppsPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mNotificationBackend", mBackend);
}
@Test
public void testIsAvailable() {
when(mBackend.getNumAppsBypassingDnd(anyInt())).thenReturn(5);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testNotAvailable() {
when(mBackend.getNumAppsBypassingDnd(anyInt())).thenReturn(0);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void testHasSummary() {
assertThat(mController.getSummary()).isNotNull();
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeCallsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
import androidx.preference.Preference;
@RunWith(RobolectricTestRunner.class)
public final class ZenModeCallsPreferenceControllerTest {
private ZenModeCallsPreferenceController mController;
@Mock
private NotificationManager mNotificationManager;
@Mock
private NotificationManager.Policy mPolicy;
private Context mContext;
@Mock
private ZenModeBackend mBackend;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeCallsPreferenceController(
mContext, mock(Lifecycle.class), "zen_mode_calls_settings");
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
public void testIsAvailable() {
assertTrue(mController.isAvailable());
}
@Test
public void testHasSummary() {
Preference pref = mock(Preference.class);
mController.updateState(pref);
verify(pref).setSummary(any());
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeDurationPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeDurationPreferenceControllerTest {
private ZenModeDurationPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private NotificationManager.Policy mPolicy;
private ContentResolver mContentResolver;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mController = new ZenModeDurationPreferenceController(mContext, mock(Lifecycle.class));
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
public void updateState_DurationForever() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
Settings.Secure.ZEN_DURATION_FOREVER);
assertEquals(mContext.getString(R.string.zen_mode_duration_summary_forever),
mController.getSummary());
}
@Test
public void updateState_DurationPrompt() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
Settings.Secure.ZEN_DURATION_PROMPT);
assertEquals(mContext.getString(R.string.zen_mode_duration_summary_always_prompt),
mController.getSummary());
}
@Test
public void updateState_DurationCustom() {
int zenDuration = 45;
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
zenDuration);
assertEquals(mContext.getString(R.string.zen_mode_duration_summary_time_minutes,
zenDuration), mController.getSummary());
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.RuntimeEnvironment.application;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeEventRuleSettings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowToast;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class ZenModeEventRuleSettingsTest {
@Mock
private FragmentActivity mActivity;
@Mock
private Intent mIntent;
@Mock
private NotificationManager mNotificationManager;
private TestFragment mFragment;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mFragment = spy(new TestFragment());
mFragment.onAttach(application);
doReturn(mActivity).when(mFragment).getActivity();
Resources res = application.getResources();
doReturn(res).when(mFragment).getResources();
when(mActivity.getTheme()).thenReturn(res.newTheme());
when(mActivity.getIntent()).thenReturn(mIntent);
when(mActivity.getResources()).thenReturn(res);
when(mFragment.getContext()).thenReturn(mContext);
}
@Test
public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
final String expected = mContext.getString(R.string.zen_mode_rule_not_found_text);
mFragment.onCreate(null);
// verify the toast
assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected);
// verify the finish
verify(mActivity).finish();
//should not crash
}
@Test
public void testNoDuplicateCalendars() {
List<ZenModeEventRuleSettings.CalendarInfo> calendarsList = new ArrayList<>();
mFragment.addCalendar(1234, "calName", 1, calendarsList);
mFragment.addCalendar(1234, "calName", 2, calendarsList);
mFragment.addCalendar(1234, "calName", 3, calendarsList);
assertThat(calendarsList.size()).isEqualTo(1);
}
private static class TestFragment extends ZenModeEventRuleSettings {
@Override
protected Object getSystemService(final String name) {
return null;
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeEventsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeEventsPreferenceControllerTest {
private static final boolean EVENTS_SETTINGS = true;
private ZenModeEventsPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeEventsPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS)).thenReturn(EVENTS_SETTINGS);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(EVENTS_SETTINGS);
}
@Test
public void onPreferenceChanged_EnableEvents() {
boolean allow = true;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS, allow);
}
@Test
public void onPreferenceChanged_DisableEvents() {
boolean allow = false;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS, allow);
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeMediaPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeMediaPreferenceControllerTest {
private ZenModeMediaPreferenceController mController;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeMediaPreferenceController(mContext,
mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(true);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA)).
thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(true);
}
@Test
public void onPreferenceChanged_EnableEvents() {
boolean allow = true;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend).saveSoundPolicy(
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
}
@Test
public void onPreferenceChanged_DisableEvents() {
boolean allow = false;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend).saveSoundPolicy(
NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import androidx.preference.Preference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeMessagesPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public final class ZenModeMessagesPreferenceControllerTest {
private ZenModeMessagesPreferenceController mController;
@Mock
private NotificationManager mNotificationManager;
@Mock
private NotificationManager.Policy mPolicy;
private Context mContext;
@Mock
private ZenModeBackend mBackend;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeMessagesPreferenceController(
mContext, mock(Lifecycle.class), "zen_mode_messages_settings");
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
public void testIsAvailable() {
assertTrue(mController.isAvailable());
}
@Test
public void testHasSummary() {
Preference pref = mock(Preference.class);
mController.updateState(pref);
verify(pref).setSummary(any());
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.Context;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModePreferenceController;
import com.android.settings.notification.zen.ZenModeSettings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModePreferenceControllerTest {
@Mock
private Preference mPreference;
@Mock
private NotificationManager mNotificationManager;
@Mock
private Policy mPolicy;
private Context mContext;
private ZenModePreferenceController mController;
private ZenModeSettings.SummaryBuilder mSummaryBuilder;
private static final String KEY_ZEN_MODE = "zen_mode";
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new ZenModePreferenceController(mContext, KEY_ZEN_MODE);
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mSummaryBuilder = spy(new ZenModeSettings.SummaryBuilder(mContext));
ReflectionHelpers.setField(mController, "mSummaryBuilder", mSummaryBuilder);
doReturn(0).when(mSummaryBuilder).getEnabledAutomaticRulesCount();
}
@Test
public void isAvailable_unsearchable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
}
@Test
public void updateState_automaticRuleEnabled_shouldSetSummary() {
when(mPreference.isEnabled()).thenReturn(true);
mController.updateState(mPreference);
verify(mPreference).setSummary(mContext.getString(R.string.zen_mode_sound_summary_off));
doReturn(1).when(mSummaryBuilder).getEnabledAutomaticRulesCount();
mController.updateState(mPreference);
verify(mPreference).setSummary(mSummaryBuilder.getSoundSummary());
}
@Test
public void updateState_preferenceDisabled_shouldNotSetSummary() {
when(mPreference.isEnabled()).thenReturn(false);
mController.updateState(mPreference);
verify(mPreference, never()).setSummary(anyString());
}
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModePriorityCallsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModePriorityCallsPreferenceControllerTest {
private ZenModePriorityCallsPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ListPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
/**
* Array Values Key
* 0: anyone
* 1: contacts
* 2: starred
* 3: none
*/
private String[] mValues;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values);
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
when(mBackend.getPriorityCallSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
when(mBackend.getAlarmsTotalSilenceCallsMessagesSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)).thenCallRealMethod();
when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenCallRealMethod();
mController = new ZenModePriorityCallsPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(false);
final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setSummary(R.string.zen_mode_from_none_calls);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setSummary(R.string.zen_mode_from_none_calls);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(R.string.zen_mode_from_starred);
}
@Test
public void onPreferenceChange_setSelectedContacts_any() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_ANY);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_ANYONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_none() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(ZenModeBackend.SOURCE_NONE);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_NONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_starred() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_STARRED)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_contacts() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_CONTACTS)]);
}
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModePriorityMessagesPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModePriorityMessagesPreferenceControllerTest {
private ZenModePriorityMessagesPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ListPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
/**
* Array Values Key
* 0: anyone
* 1: contacts
* 2: starred
* 3: none
*/
private String[] mValues;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values);
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
when(mBackend.getPriorityMessageSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
when(mBackend.getAlarmsTotalSilenceCallsMessagesSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenCallRealMethod();
when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenCallRealMethod();
mController = new ZenModePriorityMessagesPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenReturn(false);
final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setSummary(R.string.zen_mode_from_none_messages);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setSummary(R.string.zen_mode_from_none_messages);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(R.string.zen_mode_from_starred);
}
@Test
public void onPreferenceChange_setSelectedContacts_any() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_ANY);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_ANYONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_none() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(ZenModeBackend.SOURCE_NONE);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_NONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_starred() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_STARRED)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_contacts() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_CONTACTS)]);
}
}

View File

@@ -0,0 +1,142 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeRemindersPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeRemindersPreferenceControllerTest {
private static final boolean REMINDERS_SETTINGS = true;
private ZenModeRemindersPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeRemindersPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS)).
thenReturn(REMINDERS_SETTINGS);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(REMINDERS_SETTINGS);
}
@Test
public void onPreferenceChanged_EnableReminders() {
boolean allow = true;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS, allow);
}
@Test
public void onPreferenceChanged_DisableReminders() {
boolean allow = false;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS, allow);
}
}

View File

@@ -0,0 +1,159 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeRepeatCallersPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeRepeatCallersPreferenceControllerTest {
private static final boolean REPEAT_CALLERS_SETTINGS = true;
private ZenModeRepeatCallersPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeRepeatCallersPreferenceController(mContext, mock(Lifecycle.class),
15);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)).
thenReturn(REPEAT_CALLERS_SETTINGS);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(REPEAT_CALLERS_SETTINGS);
}
@Test
public void updateState_Priority_anyCallers() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(true);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_ANY);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS))
.thenReturn(false);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(true);
}
@Test
public void onPreferenceChanged_EnableRepeatCallers() {
boolean allow = true;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS,
allow);
}
@Test
public void onPreferenceChanged_DisableRepeatCallers() {
boolean allow = false;
mController.onPreferenceChange(mockPref, allow);
verify(mBackend)
.saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS,
allow);
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.RuntimeEnvironment.application;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeScheduleRuleSettings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowToast;
@RunWith(RobolectricTestRunner.class)
public class ZenModeScheduleRuleSettingsTest {
@Mock
private FragmentActivity mActivity;
@Mock
private Intent mIntent;
@Mock
private NotificationManager mNotificationManager;
private TestFragment mFragment;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mFragment = spy(new TestFragment());
mFragment.onAttach(application);
doReturn(mActivity).when(mFragment).getActivity();
Resources res = application.getResources();
doReturn(res).when(mFragment).getResources();
when(mActivity.getTheme()).thenReturn(res.newTheme());
when(mActivity.getIntent()).thenReturn(mIntent);
when(mActivity.getResources()).thenReturn(res);
when(mFragment.getContext()).thenReturn(mContext);
}
@Test
public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
final String expected = mContext.getString(R.string.zen_mode_rule_not_found_text);
mFragment.onCreate(null);
// verify the toast
assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected);
// verify the finish
verify(mActivity).finish();
//should not crash
}
public static class TestFragment extends ZenModeScheduleRuleSettings {
@Override
protected Object getSystemService(final String name) {
return null;
}
}
}

View File

@@ -0,0 +1,301 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_OFF;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ZenRule;
import android.util.ArrayMap;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.AbstractZenModePreferenceController.ZenModeConfigWrapper;
import com.android.settings.notification.zen.ZenModeSettingsFooterPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSettingsFooterPreferenceControllerTest {
private static final String TEST_APP_NAME = "test_app";
private static final String TEST_RULE_NAME = "test_rule_name";
private static final String MANUAL_RULE_FIELD = "manualRule";
private static final String AUTOMATIC_RULES_FIELD = "automaticRules";
private ZenModeSettingsFooterPreferenceController mController;
private final ArrayMap<String, ZenRule> mInjectedAutomaticRules = new ArrayMap<>();
@Mock
private NotificationManager mNotificationManager;
@Mock
private Preference mockPref;
@Mock
private ZenModeConfig mZenModeConfig;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private ZenModeConfigWrapper mConfigWrapper;
private Context mContext;
private ContentResolver mContentResolver;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getZenModeConfig()).thenReturn(mZenModeConfig);
mController = new ZenModeSettingsFooterPreferenceController(mContext, mock(Lifecycle.class),
mock(FragmentManager.class));
ReflectionHelpers.setField(mZenModeConfig, AUTOMATIC_RULES_FIELD, mInjectedAutomaticRules);
ReflectionHelpers.setField(mController, "mZenModeConfigWrapper", mConfigWrapper);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void totalSilence_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
assertTrue(mController.isAvailable());
}
@Test
public void alarmsOnly_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
assertTrue(mController.isAvailable());
}
@Test
public void priorityOnly_footerIsAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
assertTrue(mController.isAvailable());
}
@Test
public void zenModeOff_footerIsNotAvailable() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_OFF);
assertFalse(mController.isAvailable());
}
@Test
public void testDefaultNotifPolicy_app_manualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
injectManualRuleFromApp();
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_automatic_rule_app,
TEST_APP_NAME));
}
@Test
public void testDefaultNotifPolicy_time_manualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
String placeholder = "placeholder";
injectManualRuleWithTimeCountdown(1000, placeholder);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_manual_end_time, placeholder));
}
@Test
public void testDefaultNotifPolicy_forever_manualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
injectManualRuleWithIndefiniteEnd();
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_manual_indefinite));
}
@Test
public void testDefaultNotifPolicy_automaticRule_noManualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
// no manual rule
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, null);
// adding automatic rule
injectNewAutomaticRule(TEST_RULE_NAME, true, false);
mController.updateState(mockPref);
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_automatic_rule,
TEST_RULE_NAME));
}
@Test
public void testDefaultNotifPolicy_manualRuleEndsLast_hasAutomaticRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
// manual rule that ends after automatic rule ends
injectManualRuleWithIndefiniteEnd();
// automatic rule that ends before manual rule ends
injectNewAutomaticRule(TEST_RULE_NAME, true, false);
mController.updateState(mockPref);
// manual rule end time is after automatic rule end time, so it is displayed
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_manual_indefinite));
}
@Test
public void testDefaultNotifPolicy_automaticRuleEndsLast_hasManualRule_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
// manual rule that ends before automatic rule ends
injectManualRuleWithTimeCountdown(1000, "");
// automatic rule that ends after manual rule ends
ZenRule rule = injectNewAutomaticRule(TEST_RULE_NAME, true, false);
when(mConfigWrapper.parseAutomaticRuleEndTime(rule.conditionId)).thenReturn(2000L);
mController.updateState(mockPref);
// automatic rule end time is after manual rule end time, so it is displayed
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_automatic_rule,
TEST_RULE_NAME));
}
@Test
public void testDefaultNotifPolicy_multipleAutomaticRules_autoRuleApp_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
// automatic rule that ends after manual rule ends
ZenRule rule1 = injectNewAutomaticRule(TEST_RULE_NAME + "1", false, false);
when(mConfigWrapper.parseAutomaticRuleEndTime(rule1.conditionId)).thenReturn(10000L);
// automatic rule that is an app
injectNewAutomaticRule(TEST_RULE_NAME + "2", true, true);
ZenRule rule3 = injectNewAutomaticRule(TEST_RULE_NAME + "3", true, false);
when(mConfigWrapper.parseAutomaticRuleEndTime(rule3.conditionId)).thenReturn(9000L);
mController.updateState(mockPref);
// automatic rule from app is displayed
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_automatic_rule,
TEST_RULE_NAME + "2"));
}
@Test
public void testDefaultNotifPolicy_multipleAutomaticRules_setFooterTitle() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
// automatic rule that ends after manual rule ends
ZenRule rule1 = injectNewAutomaticRule(TEST_RULE_NAME + "1", true, false);
when(mConfigWrapper.parseAutomaticRuleEndTime(rule1.conditionId)).thenReturn(2000L);
ZenRule rule2 = injectNewAutomaticRule(TEST_RULE_NAME + "2", true, false);
when(mConfigWrapper.parseAutomaticRuleEndTime(rule2.conditionId)).thenReturn(8000L);
ZenRule rule3 = injectNewAutomaticRule(TEST_RULE_NAME + "3", false, false);
when(mConfigWrapper.parseAutomaticRuleEndTime(rule3.conditionId)).thenReturn(12000L);
mController.updateState(mockPref);
// active automatic rule with the latest end time will display
verify(mockPref).setTitle(mContext.getString(
com.android.settings.R.string.zen_mode_settings_dnd_automatic_rule,
TEST_RULE_NAME + "2"));
}
// manual rule that has no end condition (forever)
private void injectManualRuleWithIndefiniteEnd() {
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS;
injectedManualRule.conditionId = null;
injectedManualRule.enabler = null;
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
}
// manual rule triggered by an app
private void injectManualRuleFromApp() {
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS;
injectedManualRule.enabler = TEST_APP_NAME;
when(mConfigWrapper.getOwnerCaption(injectedManualRule.enabler)).thenReturn(TEST_APP_NAME);
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
}
// manual rule that ends in specified time
private void injectManualRuleWithTimeCountdown(long time, String timePlaceholder) {
ZenRule injectedManualRule = new ZenRule();
injectedManualRule.zenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS;
injectedManualRule.enabler = null;
injectedManualRule.conditionId = mock(Uri.class);
when(mConfigWrapper.parseManualRuleTime(injectedManualRule.conditionId)).thenReturn(time);
when(mConfigWrapper.getFormattedTime(time, mContext.getUserId()))
.thenReturn(timePlaceholder);
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, injectedManualRule);
}
// manual rule that ends in time
private ZenRule injectNewAutomaticRule(String nameAndId, boolean isActive, boolean isApp) {
ZenRule injectedRule = spy(new ZenRule());
injectedRule.zenMode = ZEN_MODE_NO_INTERRUPTIONS;
injectedRule.component = mock(ComponentName.class);
injectedRule.name = nameAndId;
injectedRule.conditionId = new Uri.Builder().authority(nameAndId).build(); // unique uri
when(injectedRule.isAutomaticActive()).thenReturn(isActive);
when(mConfigWrapper.isTimeRule(injectedRule.conditionId)).thenReturn(!isApp);
if (isApp) {
when(injectedRule.component.getPackageName()).thenReturn(TEST_APP_NAME);
}
mInjectedAutomaticRules.put(nameAndId, injectedRule);
return injectedRule;
}
}

View File

@@ -0,0 +1,136 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeSettings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSettingsTest {
private ZenModeSettings.SummaryBuilder mBuilder;
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application.getApplicationContext();
mBuilder = new ZenModeSettings.SummaryBuilder(mContext);
}
@Test
public void testBlockedEffectsSummary_none() {
Policy policy = new Policy(0, 0, 0, 0);
assertEquals(mContext.getString(R.string.zen_mode_restrict_notifications_summary_muted),
mBuilder.getBlockedEffectsSummary(policy));
}
@Test
public void testBlockedEffectsSummary_some() {
Policy policy = new Policy(0, 0, 0, NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK);
assertEquals(mContext.getString(R.string.zen_mode_restrict_notifications_summary_custom),
mBuilder.getBlockedEffectsSummary(policy));
}
@Test
public void testBlockedEffectsSummary_all() {
Policy policy = new Policy(0, 0, 0, 511);
assertEquals(mContext.getString(R.string.zen_mode_restrict_notifications_summary_hidden),
mBuilder.getBlockedEffectsSummary(policy));
}
@Test
public void testGetCallsSettingSummary_none() {
Policy policy = new Policy(0, 0, 0, 0);
assertThat(mBuilder.getCallsSettingSummary(policy)).isEqualTo("Don\u2019t allow any calls");
}
@Test
public void testGetCallsSettingSummary_contacts() {
Policy policy = new Policy(Policy.PRIORITY_CATEGORY_ALARMS | Policy.PRIORITY_CATEGORY_CALLS,
Policy.PRIORITY_SENDERS_CONTACTS, 0, 0);
assertThat(mBuilder.getCallsSettingSummary(policy)).isEqualTo("Allow from contacts");
}
@Test
public void testGetCallsSettingSummary_repeatCallers() {
Policy policy = new Policy(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0, 0);
assertThat(mBuilder.getCallsSettingSummary(policy)).isEqualTo("Allow from repeat callers");
}
@Test
public void testGetCallsSettingSummary_starredRepeatCallers() {
Policy policy = new Policy(
Policy.PRIORITY_CATEGORY_REPEAT_CALLERS | Policy.PRIORITY_CATEGORY_CALLS,
Policy.PRIORITY_SENDERS_STARRED, 0, 0);
assertThat(mBuilder.getCallsSettingSummary(policy))
.isEqualTo("Allow from starred contacts and repeat callers");
}
@Test
public void testGetSoundSettingSummary_allOff() {
Policy policy = new Policy(0, 0, 0, 0);
assertThat(mBuilder.getSoundSettingSummary(policy)).isEqualTo("Muted");
}
@Test
public void testGetSoundSettingSummary_allOn() {
Policy policy = new Policy(Policy.PRIORITY_CATEGORY_ALARMS | Policy.PRIORITY_CATEGORY_SYSTEM
| Policy.PRIORITY_CATEGORY_MEDIA, 0, 0, 0);
assertThat(mBuilder.getSoundSettingSummary(policy))
.isEqualTo("Muted, but allow alarms, media, and touch sounds");
}
@Test
public void testGetSoundSettingSummary_allOffButOne() {
Policy policy = new Policy(Policy.PRIORITY_CATEGORY_MEDIA, 0, 0, 0);
assertThat(mBuilder.getSoundSettingSummary(policy)).isEqualTo("Muted, but allow media");
}
@Test
public void testGetSoundSettingSummary_allOffButTwo() {
Policy policy = new Policy(Policy.PRIORITY_CATEGORY_SYSTEM
| Policy.PRIORITY_CATEGORY_MEDIA, 0, 0, 0);
assertThat(mBuilder.getSoundSettingSummary(policy))
.isEqualTo("Muted, but allow media and touch sounds");
}
@Test
public void searchProvider_shouldIndexDefaultXml() {
final List<SearchIndexableResource> sir = ZenModeSettings.SEARCH_INDEX_DATA_PROVIDER
.getXmlResourcesToIndex(mContext, true /* enabled */);
assertThat(sir).hasSize(1);
assertThat(sir.get(0).xmlResId).isEqualTo(R.xml.zen_mode_settings);
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.android.settings.notification.zen;
import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
import static com.google.common.truth.Truth.assertThat;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import androidx.slice.Slice;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.core.SliceAction;
import androidx.slice.widget.SliceLiveData;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeSliceBuilder;
import com.android.settings.testutils.shadow.ShadowNotificationManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.List;
@Config(shadows = ShadowNotificationManager.class)
@RunWith(RobolectricTestRunner.class)
public class ZenModeSliceBuilderTest {
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
// Set-up specs for SliceMetadata.
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
}
@Test
public void getZenModeSlice_correctSliceContent() {
final Slice dndSlice = ZenModeSliceBuilder.getSlice(mContext);
final SliceMetadata metadata = SliceMetadata.from(mContext, dndSlice);
assertThat(metadata.getTitle()).isEqualTo(
mContext.getString(R.string.zen_mode_settings_title));
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).hasSize(1);
final SliceAction primaryAction = metadata.getPrimaryAction();
assertThat(primaryAction.getIcon()).isNull();
}
@Test
public void handleUriChange_turnOn_zenModeTurnsOn() {
final Intent intent = new Intent();
intent.putExtra(EXTRA_TOGGLE_STATE, true);
NotificationManager.from(mContext).setZenMode(Settings.Global.ZEN_MODE_OFF, null, "");
ZenModeSliceBuilder.handleUriChange(mContext, intent);
final int zenMode = NotificationManager.from(mContext).getZenMode();
assertThat(zenMode).isEqualTo(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
}
@Test
public void handleUriChange_turnOff_zenModeTurnsOff() {
final Intent intent = new Intent();
intent.putExtra(EXTRA_TOGGLE_STATE, false);
NotificationManager.from(mContext).setZenMode(
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, "");
ZenModeSliceBuilder.handleUriChange(mContext, intent);
final int zenMode = NotificationManager.from(mContext).getZenMode();
assertThat(zenMode).isEqualTo(Settings.Global.ZEN_MODE_OFF);
}
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeStarredContactsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeStarredContactsPreferenceControllerTest {
private ZenModeStarredContactsPreferenceController mCallsController;
private ZenModeStarredContactsPreferenceController mMessagesController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private Preference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private Intent testIntent;
@Mock
private ComponentName mComponentName;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
when(testIntent.resolveActivity(any())).thenReturn(mComponentName);
mCallsController = new ZenModeStarredContactsPreferenceController(
mContext, mock(Lifecycle.class), PRIORITY_CATEGORY_CALLS,
"zen_mode_starred_contacts_callers");
ReflectionHelpers.setField(mCallsController, "mBackend", mBackend);
ReflectionHelpers.setField(mCallsController, "mStarredContactsIntent", testIntent);
when(mPreferenceScreen.findPreference(mCallsController.getPreferenceKey()))
.thenReturn(mockPref);
mCallsController.displayPreference(mPreferenceScreen);
mMessagesController = new ZenModeStarredContactsPreferenceController(
mContext, mock(Lifecycle.class), PRIORITY_CATEGORY_MESSAGES,
"zen_mode_starred_contacts_messages");
ReflectionHelpers.setField(mMessagesController, "mBackend", mBackend);
ReflectionHelpers.setField(mMessagesController, "mStarredContactsIntent", testIntent);
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
.thenReturn(mockPref);
mMessagesController.displayPreference(mPreferenceScreen);
}
@Test
public void isAvailable_noCallers() {
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(false);
assertThat(mCallsController.isAvailable()).isFalse();
}
@Test
public void isAvailable_anyCallers() {
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(true);
when(mBackend.getPriorityCallSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
assertThat(mCallsController.isAvailable()).isFalse();
}
@Test
public void isAvailable_starredCallers() {
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(true);
when(mBackend.getPriorityCallSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
assertThat(mCallsController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noMessages() {
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(false);
assertThat(mMessagesController.isAvailable()).isFalse();
}
@Test
public void isAvailable_anyMessages() {
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(true);
when(mBackend.getPriorityMessageSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
assertThat(mMessagesController.isAvailable()).isFalse();
}
@Test
public void isAvailable_starredMessageContacts() {
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(true);
when(mBackend.getPriorityMessageSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
assertThat(mMessagesController.isAvailable()).isTrue();
}
@Test
public void nullPreference_displayPreference() {
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
.thenReturn(null);
// should not throw a null pointer
mMessagesController.displayPreference(mPreferenceScreen);
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeSystemPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSystemPreferenceControllerTest {
private ZenModeSystemPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private Context mContext;
private ContentResolver mContentResolver;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenModeSystemPreferenceController(mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final SwitchPreference mockPref = mock(SwitchPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM)).thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setChecked(true);
}
@Test
public void onPreferenceChanged_EnableSystem() {
mController.onPreferenceChange(mockPref, true);
verify(mBackend).saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM,
true);
}
@Test
public void onPreferenceChanged_DisableSystem() {
mController.onPreferenceChange(mockPref, false);
verify(mBackend).saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM,
false);
}
}

View File

@@ -0,0 +1,182 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Resources;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeVisEffectPreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.DisabledCheckBoxPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeVisEffectPreferenceControllerTest {
private ZenModeVisEffectPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private DisabledCheckBoxPreference mockPref;
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
@Mock
private PreferenceScreen mScreen;
@Mock
NotificationManager mNotificationManager;
private static final String PREF_KEY = "main_pref";
private static final int PREF_METRICS = 1;
private static final int PARENT_EFFECT1 = SUPPRESSED_EFFECT_BADGE;
private static final int PARENT_EFFECT2 = SUPPRESSED_EFFECT_NOTIFICATION_LIST;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
when(mNotificationManager.getNotificationPolicy()).thenReturn(
mock(NotificationManager.Policy.class));
mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS, null);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable() {
// SUPPRESSED_EFFECT_PEEK is always available:
assertTrue(mController.isAvailable());
// SUPPRESSED_EFFECT_LIGHTS is only available if the device has an LED:
Context mockContext = mock(Context.class);
mController = new ZenModeVisEffectPreferenceController(mockContext, mock(Lifecycle.class),
PREF_KEY, SUPPRESSED_EFFECT_LIGHTS, PREF_METRICS, null);
Resources mockResources = mock(Resources.class);
when(mockContext.getResources()).thenReturn(mockResources);
when(mockResources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
.thenReturn(false); // no light
assertFalse(mController.isAvailable());
when(mockResources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
.thenReturn(true); // has light
assertTrue(mController.isAvailable());
}
@Test
public void updateState_notChecked() {
when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
verify(mockPref).enableCheckbox(true);
}
@Test
public void updateState_checked() {
when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
verify(mockPref).enableCheckbox(true);
}
@Test
public void updateState_checkedFalse_parentChecked() {
mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS,
new int[]{PARENT_EFFECT1, PARENT_EFFECT2});
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false);
when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT1)).thenReturn(false);
when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT2)).thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
verify(mockPref).enableCheckbox(false);
verify(mBackend, times(1)).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true);
}
@Test
public void updateState_checkedFalse_parentNotChecked() {
mController = new ZenModeVisEffectPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY, SUPPRESSED_EFFECT_PEEK, PREF_METRICS,
new int[]{PARENT_EFFECT1, PARENT_EFFECT2});
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.isVisualEffectSuppressed(SUPPRESSED_EFFECT_PEEK)).thenReturn(false);
when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT1)).thenReturn(false);
when(mBackend.isVisualEffectSuppressed(PARENT_EFFECT2)).thenReturn(false);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
verify(mockPref).enableCheckbox(true);
verify(mBackend, never()).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true);
}
@Test
public void onPreferenceChanged_checkedFalse() {
mController.onPreferenceChange(mockPref, false);
verify(mBackend).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, false);
verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
eq(PREF_METRICS),
eq(false));
}
@Test
public void onPreferenceChanged_checkedTrue() {
mController.onPreferenceChange(mockPref, true);
verify(mBackend).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true);
verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
eq(PREF_METRICS),
eq(true));
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
.ACTION_ZEN_SOUND_AND_VIS_EFFECTS;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenCustomRadioButtonPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeVisEffectsAllPreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeVisEffectsAllPreferenceControllerTest {
private ZenModeVisEffectsAllPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private ZenCustomRadioButtonPreference mockPref;
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
@Mock
private PreferenceScreen mScreen;
@Mock
NotificationManager mNotificationManager;
private static final String PREF_KEY = "main_pref";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
when(mNotificationManager.getNotificationPolicy()).thenReturn(
mock(NotificationManager.Policy.class));
mController = new ZenModeVisEffectsAllPreferenceController(
mContext, mock(Lifecycle.class), PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable() {
assertTrue(mController.isAvailable());
}
@Test
public void updateState_notChecked() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_checked() {
int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
| SUPPRESSED_EFFECT_SCREEN_ON
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_AMBIENT
| SUPPRESSED_EFFECT_STATUS_BAR
| SUPPRESSED_EFFECT_BADGE
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK
| SUPPRESSED_EFFECT_NOTIFICATION_LIST;
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
@Test
public void onPreferenceChanged_checkedTrue() {
int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
| SUPPRESSED_EFFECT_SCREEN_ON
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_AMBIENT
| SUPPRESSED_EFFECT_STATUS_BAR
| SUPPRESSED_EFFECT_BADGE
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK
| SUPPRESSED_EFFECT_NOTIFICATION_LIST;
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
mController.onRadioButtonClick(mockPref);
verify(mBackend).saveVisualEffectsPolicy(allSuppressed, true);
verify(mFeatureFactory.metricsFeatureProvider).action(eq(mContext),
eq(ACTION_ZEN_SOUND_AND_VIS_EFFECTS),
eq(true));
}
}

View File

@@ -0,0 +1,143 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenCustomRadioButtonPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeVisEffectsCustomPreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeVisEffectsCustomPreferenceControllerTest {
private ZenModeVisEffectsCustomPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private ZenCustomRadioButtonPreference mockPref;
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
NotificationManager mNotificationManager;
private static final String PREF_KEY = "main_pref";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
FakeFeatureFactory.setupForTest();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
when(mNotificationManager.getNotificationPolicy()).thenReturn(
mock(NotificationManager.Policy.class));
mController = new ZenModeVisEffectsCustomPreferenceController(
mContext, mock(Lifecycle.class), PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable_noVisEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_visEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_notChecked_noVisEffects() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_notChecked_allVisEffects() {
int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
| SUPPRESSED_EFFECT_SCREEN_ON
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_AMBIENT
| SUPPRESSED_EFFECT_STATUS_BAR
| SUPPRESSED_EFFECT_BADGE
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK
| SUPPRESSED_EFFECT_NOTIFICATION_LIST;
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, allSuppressed);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_checked() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
@Test
public void updateState_listeners() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 2);
mController.updateState(mockPref);
verify(mockPref).setOnGearClickListener(any());
verify(mockPref).setOnRadioButtonClickListener(any());
}
}

View File

@@ -0,0 +1,132 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR;
import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_ZEN_SOUND_ONLY;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.ZenCustomRadioButtonPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModeVisEffectsNonePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModeVisEffectsNonePreferenceControllerTest {
private ZenModeVisEffectsNonePreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private ZenCustomRadioButtonPreference mockPref;
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
@Mock
private PreferenceScreen mScreen;
@Mock
NotificationManager mNotificationManager;
private static final String PREF_KEY = "main_pref";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
when(mNotificationManager.getNotificationPolicy()).thenReturn(
mock(NotificationManager.Policy.class));
mController = new ZenModeVisEffectsNonePreferenceController(
mContext, mock(Lifecycle.class), PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable() {
assertTrue(mController.isAvailable());
}
@Test
public void updateState_notChecked() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_checked() {
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 0);
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
@Test
public void onRadioButtonClick() {
int allSuppressed = SUPPRESSED_EFFECT_SCREEN_OFF
| SUPPRESSED_EFFECT_SCREEN_ON
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_AMBIENT
| SUPPRESSED_EFFECT_STATUS_BAR
| SUPPRESSED_EFFECT_BADGE
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK
| SUPPRESSED_EFFECT_NOTIFICATION_LIST;
mBackend.mPolicy = new NotificationManager.Policy(0, 0, 0, 1);
mController.onRadioButtonClick(mockPref);
verify(mBackend).saveVisualEffectsPolicy(allSuppressed, false);
verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
eq(ACTION_ZEN_SOUND_ONLY),
eq(true));
}
}

View File

@@ -0,0 +1,222 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
import static com.android.settings.notification.zen.ZenOnboardingActivity.ALWAYS_SHOW_THRESHOLD;
import static com.android.settings.notification.zen.ZenOnboardingActivity
.PREF_KEY_SUGGESTION_FIRST_DISPLAY_TIME;
import static com.android.settings.notification.zen.ZenOnboardingActivity.isSuggestionComplete;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.notification.zen.ZenOnboardingActivity;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
@RunWith(RobolectricTestRunner.class)
public class ZenOnboardingActivityTest {
@Mock
private MetricsLogger mMetricsLogger;
@Mock
private NotificationManager mNm;
private ZenOnboardingActivity mActivity;
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
mActivity = Robolectric.buildActivity(ZenOnboardingActivity.class)
.create()
.get();
mActivity.setNotificationManager(mNm);
mActivity.setMetricsLogger(mMetricsLogger);
mActivity.setupUI();
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFeatureFactory.suggestionsFeatureProvider.getSharedPrefs(any(Context.class)))
.thenReturn(getSharedPreferences());
}
@Test
public void loadUiRecordsEvent() {
verify(mMetricsLogger).visible(MetricsEvent.SETTINGS_ZEN_ONBOARDING);
}
@Test
public void saveNewSetting() {
Policy policy = new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON);
when(mNm.getNotificationPolicy()).thenReturn(policy);
mActivity.mNewSetting.performClick();
mActivity.save(null);
verify(mMetricsLogger).action(MetricsEvent.ACTION_ZEN_ONBOARDING_OK);
ArgumentCaptor<Policy> captor = ArgumentCaptor.forClass(Policy.class);
verify(mNm).setNotificationPolicy(captor.capture());
Policy actual = captor.getValue();
assertThat(actual.priorityCategories).isEqualTo(PRIORITY_CATEGORY_ALARMS
| PRIORITY_CATEGORY_REPEAT_CALLERS);
assertThat(actual.priorityCallSenders).isEqualTo(Policy.PRIORITY_SENDERS_STARRED);
assertThat(actual.priorityMessageSenders).isEqualTo(Policy.PRIORITY_SENDERS_ANY);
assertThat(actual.suppressedVisualEffects).isEqualTo(
Policy.getAllSuppressedVisualEffects());
}
@Test
public void keepCurrentSetting() {
Policy policy = new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON);
when(mNm.getNotificationPolicy()).thenReturn(policy);
mActivity.mKeepCurrentSetting.performClick();
mActivity.save(null);
verify(mMetricsLogger).action(MetricsEvent.ACTION_ZEN_ONBOARDING_KEEP_CURRENT_SETTINGS);
verify(mNm, never()).setNotificationPolicy(any());
}
@Test
public void isSuggestionComplete_zenUpdated() {
Policy policy = new Policy(0, 0, 0, 0);
when(mNm.getNotificationPolicy()).thenReturn(policy);
setZenUpdated(true);
setShowSettingsSuggestion(false);
setWithinTimeThreshold(true);
assertThat(isSuggestionComplete(mContext)).isTrue();
}
@Test
public void isSuggestionComplete_withinTimeThreshold() {
Policy policy = new Policy(0, 0, 0, 0);
when(mNm.getNotificationPolicy()).thenReturn(policy);
setZenUpdated(false);
setShowSettingsSuggestion(false);
setWithinTimeThreshold(true);
assertThat(isSuggestionComplete(mContext)).isFalse();
}
@Test
public void isSuggestionComplete_showSettingsSuggestionTrue() {
Policy policy = new Policy(0, 0, 0, 0);
when(mNm.getNotificationPolicy()).thenReturn(policy);
setZenUpdated(false);
setShowSettingsSuggestion(true);
setWithinTimeThreshold(false);
assertThat(isSuggestionComplete(mContext)).isFalse();
}
@Test
public void isSuggestionComplete_showSettingsSuggestionFalse_notWithinTimeThreshold() {
Policy policy = new Policy(0, 0, 0, 0);
when(mNm.getNotificationPolicy()).thenReturn(policy);
setZenUpdated(false);
setShowSettingsSuggestion(false);
setWithinTimeThreshold(false);
assertThat(isSuggestionComplete(mContext)).isTrue();
}
@Test
public void isSuggestionComplete_visualEffectsUpdated() {
// all values suppressed
Policy policy = new Policy(0, 0, 0, 511);
when(mNm.getNotificationPolicy()).thenReturn(policy);
setZenUpdated(false);
setShowSettingsSuggestion(true);
setWithinTimeThreshold(true);
assertThat(isSuggestionComplete(mContext)).isTrue();
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ZEN_SETTINGS_UPDATED, -1)).isEqualTo(1);
}
private void setZenUpdated(boolean updated) {
int zenUpdated = updated ? 1 : 0;
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ZEN_SETTINGS_UPDATED, zenUpdated);
}
private void setWithinTimeThreshold(boolean withinTime) {
long firstTime = System.currentTimeMillis();
if (withinTime) {
firstTime -= ALWAYS_SHOW_THRESHOLD / 2;
} else {
firstTime -= ALWAYS_SHOW_THRESHOLD * 2;
}
getSharedPreferences().edit().putLong(PREF_KEY_SUGGESTION_FIRST_DISPLAY_TIME,
firstTime).commit();
}
private void setShowSettingsSuggestion(boolean show) {
int showZenSuggestion = 0;
if (show) {
showZenSuggestion = 1;
}
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.SHOW_ZEN_SETTINGS_SUGGESTION, showZenSuggestion);
}
private SharedPreferences getSharedPreferences() {
return mContext.getSharedPreferences("test_zen_sugg", Context.MODE_PRIVATE);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleCustomPolicyPreferenceControllerTest extends
ZenRuleCustomPrefContrTestBase {
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ZenCustomRadioButtonPreference mockPref;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private ZenRuleCustomPolicyPreferenceController mController;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new ZenRuleCustomPolicyPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void updateState_nullZenPolicy() {
updateControllerZenPolicy(null);
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_hasZenPolicy() {
updateControllerZenPolicy(new ZenPolicy.Builder().build());
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.service.notification.ZenPolicy;
import com.android.settings.notification.zen.AbstractZenCustomRulePreferenceController;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
abstract class ZenRuleCustomPrefContrTestBase {
public static final String RULE_ID = "test_rule_id";
public static final String PREF_KEY = "main_pref";
AutomaticZenRule mRule = new AutomaticZenRule("test", null, null, null, null,
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
abstract AbstractZenCustomRulePreferenceController getController();
void updateControllerZenPolicy(ZenPolicy policy) {
mRule.setZenPolicy(policy);
getController().onResume(mRule, RULE_ID);
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleCustomSwitchPreferenceControllerTest extends ZenRuleCustomPrefContrTestBase {
private ZenRuleCustomSwitchPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private Context mContext;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenRuleCustomSwitchPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY, ZenPolicy.PRIORITY_CATEGORY_ALARMS, 0);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void onPreferenceChanged_enable() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowMedia(false)
.allowAlarms(false)
.build());
mController.onPreferenceChange(mockPref, true);
mRule.setZenPolicy(new ZenPolicy.Builder()
.allowMedia(false)
.allowAlarms(true)
.build());
verify(mBackend).updateZenRule(RULE_ID, mRule);
}
@Test
public void onPreferenceChanged_disable() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowMedia(false)
.allowAlarms(true)
.build());
mController.onPreferenceChange(mockPref, false);
mRule.setZenPolicy(new ZenPolicy.Builder()
.allowMedia(false)
.allowAlarms(false)
.build());
verify(mBackend).updateZenRule(RULE_ID, mRule);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleDefaultPolicyPreferenceControllerTest extends
ZenRuleCustomPrefContrTestBase {
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ZenCustomRadioButtonPreference mockPref;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private ZenRuleDefaultPolicyPreferenceController mController;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new ZenRuleDefaultPolicyPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void updateState_nullZenPolicy() {
updateControllerZenPolicy(null);
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
@Test
public void updateState_hasZenPolicy() {
updateControllerZenPolicy(new ZenPolicy.Builder().build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.zen.AbstractZenCustomRulePreferenceController;
import com.android.settings.notification.zen.ZenCustomRadioButtonPreference;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRulePreferenceControllerTest {
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ZenCustomRadioButtonPreference mockPref;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private TestablePreferenceController mController;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new TestablePreferenceController(mContext,"test", mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void onResumeTest() {
final String id = "testid";
final AutomaticZenRule rule = new AutomaticZenRule("test", null, null,
null, null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
assertTrue(mController.mRule == null);
assertTrue(mController.mId == null);
mController.onResume(rule, id);
assertEquals(mController.mId, id);
assertEquals(mController.mRule, rule);
}
class TestablePreferenceController extends AbstractZenCustomRulePreferenceController {
TestablePreferenceController(Context context, String key,
Lifecycle lifecycle) {
super(context, key, lifecycle);
}
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleRepeatCallersPreferenceControllerTest extends ZenRuleCustomPrefContrTestBase {
private ZenRuleRepeatCallersPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private SwitchPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private Context mContext;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
mController = new ZenRuleRepeatCallersPreferenceController(mContext, PREF_KEY,
mock(Lifecycle.class), 15);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(anyString())).thenReturn(mRule);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_Priority_anyCallers() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE)
.allowRepeatCallers(false)
.build());
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setChecked(true);
}
@Test
public void onPreferenceChanged_EnableRepeatCallers() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
.allowRepeatCallers(false)
.build());
mController.updateState(mockPref);
mController.onPreferenceChange(mockPref, true);
mRule.setZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
.allowRepeatCallers(true)
.build());
verify(mBackend).updateZenRule(RULE_ID, mRule);
}
@Test
public void onPreferenceChanged_DisableRepeatCallers() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
.allowRepeatCallers(true)
.build());
mController.updateState(mockPref);
mController.onPreferenceChange(mockPref, false);
mRule.setZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
.allowRepeatCallers(false)
.build());
verify(mBackend).updateZenRule(RULE_ID, mRule);
}
}

View File

@@ -0,0 +1,171 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.service.notification.ZenPolicy;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleStarredContactsPreferenceControllerTest extends ZenRuleCustomPrefContrTestBase {
private ZenRuleStarredContactsPreferenceController mCallsController;
private ZenRuleStarredContactsPreferenceController mMessagesController;
private static int CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_CALLS;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private Preference mockPref;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private Intent testIntent;
@Mock
private ComponentName mComponentName;
private Context mContext;
@Override
AbstractZenCustomRulePreferenceController getController() {
if (CURR_CONTROLLER == ZenPolicy.PRIORITY_CATEGORY_MESSAGES) {
return mMessagesController;
} else {
return mCallsController;
}
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
when(testIntent.resolveActivity(any())).thenReturn(mComponentName);
mCallsController = new ZenRuleStarredContactsPreferenceController(
mContext, mock(Lifecycle.class), ZenPolicy.PRIORITY_CATEGORY_CALLS,
"zen_mode_starred_contacts_callers");
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
ReflectionHelpers.setField(mCallsController, "mBackend", mBackend);
ReflectionHelpers.setField(mCallsController, "mStarredContactsIntent", testIntent);
when(mPreferenceScreen.findPreference(mCallsController.getPreferenceKey()))
.thenReturn(mockPref);
mCallsController.displayPreference(mPreferenceScreen);
mMessagesController = new ZenRuleStarredContactsPreferenceController(
mContext, mock(Lifecycle.class), ZenPolicy.PRIORITY_CATEGORY_MESSAGES,
"zen_mode_starred_contacts_messages");
ReflectionHelpers.setField(mMessagesController, "mBackend", mBackend);
ReflectionHelpers.setField(mMessagesController, "mStarredContactsIntent", testIntent);
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
.thenReturn(mockPref);
mMessagesController.displayPreference(mPreferenceScreen);
}
@Test
public void isAvailable_noCallers() {
CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_CALLS;
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_NONE)
.build());
assertThat(mCallsController.isAvailable()).isFalse();
}
@Test
public void isAvailable_anyCallers() {
CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_CALLS;
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE)
.build());
assertThat(mCallsController.isAvailable()).isFalse();
}
@Test
public void isAvailable_starredCallers() {
CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_CALLS;
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED)
.build());
assertThat(mCallsController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noMessages() {
CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_MESSAGES;
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE)
.build());
assertThat(mCallsController.isAvailable()).isFalse();
}
@Test
public void isAvailable_anyMessages() {
CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_MESSAGES;
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE)
.build());
assertThat(mMessagesController.isAvailable()).isFalse();
}
@Test
public void isAvailable_starredMessageContacts() {
CURR_CONTROLLER = ZenPolicy.PRIORITY_CATEGORY_MESSAGES;
updateControllerZenPolicy(new ZenPolicy.Builder()
.allowMessages(ZenPolicy.PEOPLE_TYPE_STARRED)
.build());
assertThat(mMessagesController.isAvailable()).isTrue();
}
@Test
public void nullPreference_displayPreference() {
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
.thenReturn(null);
// should not throw a null pointer
mMessagesController.displayPreference(mPreferenceScreen);
}
}

View File

@@ -0,0 +1,187 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Resources;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import com.android.settings.widget.DisabledCheckBoxPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleVisEffectPreferenceControllerTest extends ZenRuleCustomPrefContrTestBase {
private static final @ZenPolicy.VisualEffect int EFFECT_PEEK = ZenPolicy.VISUAL_EFFECT_PEEK;
private static final @ZenPolicy.VisualEffect int PARENT_EFFECT1 = ZenPolicy.VISUAL_EFFECT_BADGE;
private static final @ZenPolicy.VisualEffect int PARENT_EFFECT2 =
ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST;
private static final int PREF_METRICS = 1;
@Mock
private ZenModeBackend mBackend;
@Mock
private DisabledCheckBoxPreference mockPref;
@Mock
private PreferenceScreen mScreen;
@Mock
NotificationManager mNotificationManager;
private Context mContext;
private ZenRuleVisEffectPreferenceController mController;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
when(mNotificationManager.getNotificationPolicy()).thenReturn(
mock(NotificationManager.Policy.class));
mContext = RuntimeEnvironment.application;
mController = new ZenRuleVisEffectPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY, EFFECT_PEEK, PREF_METRICS, null);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable() {
// VISUAL_EFFECT_PEEK isn't available until after onResume is called
assertFalse(mController.isAvailable());
updateControllerZenPolicy(new ZenPolicy()); // calls onResume
assertTrue(mController.isAvailable());
// VISUAL_EFFECT_LIGHTS is only available if the device has an LED:
Context mockContext = mock(Context.class);
mController = new ZenRuleVisEffectPreferenceController(mockContext, mock(Lifecycle.class),
PREF_KEY, ZenPolicy.VISUAL_EFFECT_LIGHTS, PREF_METRICS, null);
updateControllerZenPolicy(new ZenPolicy()); // calls onResume
Resources mockResources = mock(Resources.class);
when(mockContext.getResources()).thenReturn(mockResources);
when(mockResources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
.thenReturn(false); // no light
assertFalse(mController.isAvailable());
when(mockResources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
.thenReturn(true); // has light
assertTrue(mController.isAvailable());
}
@Test
public void updateState_notChecked() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(true)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
verify(mockPref).enableCheckbox(true);
}
@Test
public void updateState_checked() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(false)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
verify(mockPref).enableCheckbox(true);
}
@Test
public void updateState_checkedFalse_parentChecked() {
mController.mParentSuppressedEffects = new int[]{PARENT_EFFECT1, PARENT_EFFECT2};
updateControllerZenPolicy(new ZenPolicy.Builder()
.showVisualEffect(EFFECT_PEEK, true)
.showVisualEffect(PARENT_EFFECT1, true)
.showVisualEffect(PARENT_EFFECT2, false)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
verify(mockPref).enableCheckbox(false);
}
@Test
public void updateState_checkedFalse_parentNotChecked() {
mController.mParentSuppressedEffects = new int[]{PARENT_EFFECT1, PARENT_EFFECT2};
updateControllerZenPolicy(new ZenPolicy.Builder()
.showVisualEffect(EFFECT_PEEK, true)
.showVisualEffect(PARENT_EFFECT1, true)
.showVisualEffect(PARENT_EFFECT2, true)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
verify(mockPref).enableCheckbox(true);
}
@Test
public void onPreferenceChanged_checkedFalse() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(false)
.build());
mController.onPreferenceChange(mockPref, false);
mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
.showPeeking(true)
.build());
verify(mBackend).updateZenRule(RULE_ID, mRule);
}
@Test
public void onPreferenceChanged_checkedTrue() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(true)
.build());
mController.onPreferenceChange(mockPref, true);
mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
.showPeeking(false)
.build());
verify(mBackend).updateZenRule(RULE_ID, mRule);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleVisEffectsAllPreferenceControllerTest extends
ZenRuleCustomPrefContrTestBase {
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ZenCustomRadioButtonPreference mockPref;
@Mock
private PreferenceScreen mScreen;
private ZenRuleVisEffectsAllPreferenceController mController;
private Context mContext;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new ZenRuleVisEffectsAllPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void updateState_noVisEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.hideAllVisualEffects()
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_showAllVisualEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showAllVisualEffects()
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
@Test
public void updateState_customEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(true)
.showBadges(false)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleVisEffectsCustomPreferenceControllerTest extends
ZenRuleCustomPrefContrTestBase {
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ZenCustomRadioButtonPreference mockPref;
@Mock
private PreferenceScreen mScreen;
private ZenRuleVisEffectsCustomPreferenceController mController;
private Context mContext;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new ZenRuleVisEffectsCustomPreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void updateState_noVisEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.hideAllVisualEffects()
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_showAllVisualEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showAllVisualEffects()
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_customEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(true)
.showBadges(false)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenRuleVisEffectsNonePreferenceControllerTest extends
ZenRuleCustomPrefContrTestBase {
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ZenCustomRadioButtonPreference mockPref;
@Mock
private PreferenceScreen mScreen;
private ZenRuleVisEffectsNonePreferenceController mController;
private Context mContext;
@Override
AbstractZenCustomRulePreferenceController getController() {
return mController;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mController = new ZenRuleVisEffectsNonePreferenceController(mContext, mock(Lifecycle.class),
PREF_KEY);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mBackend.getAutomaticZenRule(RULE_ID)).thenReturn(mRule);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mScreen);
}
@Test
public void updateState_noVisEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.hideAllVisualEffects()
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(true);
}
@Test
public void updateState_showAllVisualEffects() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showAllVisualEffects()
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
@Test
public void updateState_custom() {
updateControllerZenPolicy(new ZenPolicy.Builder()
.showPeeking(true)
.showBadges(false)
.build());
mController.updateState(mockPref);
verify(mockPref).setChecked(false);
}
}