Add custom dnd pages for each dnd auto rule

- Fix strings
- Add zen custom settings dialog when custom settings are being applied
Test: make RunSettingsRoboTests -j40
Bug: 111475013
Fixes: 120787133
Fixes: 120796642
Fixes: 120865472
Change-Id: I34d6b4b23d36277e3704416d65e2418418c124e1
This commit is contained in:
Beverly
2018-12-11 16:19:33 -05:00
parent f30fb4b20b
commit b9f38af689
50 changed files with 3582 additions and 159 deletions

View File

@@ -1,26 +1,58 @@
package com.android.settings.notification;
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 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;
import org.robolectric.RobolectricTestRunner;
@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;
@@ -63,6 +95,39 @@ public class ZenModeBackendTest {
}
}
@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<>();

View File

@@ -17,12 +17,16 @@
package com.android.settings.notification;
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.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
@@ -67,11 +71,10 @@ public final class ZenModeMessagesPreferenceControllerTest {
assertTrue(mController.isAvailable());
}
// TODO: (b/111475013 - beverlyt) set messages summary
// @Test
// public void testHasSummary() {
// Preference pref = mock(Preference.class);
// mController.updateState(pref);
// verify(pref).setSummary(any());
// }
@Test
public void testHasSummary() {
Preference pref = mock(Preference.class);
mController.updateState(pref);
verify(pref).setSummary(any());
}
}

View File

@@ -22,9 +22,8 @@ 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.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;
@@ -40,6 +39,7 @@ 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;
@@ -92,8 +92,8 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getZenModeConfig()).thenReturn(mZenModeConfig);
mController =
new ZenModeSettingsFooterPreferenceController(mContext, mock(Lifecycle.class));
mController = new ZenModeSettingsFooterPreferenceController(mContext, mock(Lifecycle.class),
mock(FragmentManager.class));
ReflectionHelpers.setField(mZenModeConfig, AUTOMATIC_RULES_FIELD, mInjectedAutomaticRules);
ReflectionHelpers.setField(mController, "mZenModeConfigWrapper", mConfigWrapper);

View File

@@ -30,7 +30,6 @@ import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -49,7 +48,6 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class ZenModeStarredContactsPreferenceControllerTest {
@@ -156,18 +154,6 @@ public class ZenModeStarredContactsPreferenceControllerTest {
assertThat(mMessagesController.isAvailable()).isTrue();
}
@Test
public void updateSummary_nullCursorValues() {
Cursor testCursorWithNullValues = createMockCursor(3);
when(testCursorWithNullValues.getString(0)).thenReturn(null);
// expected - no null values
List<String> contacts = mMessagesController.getStarredContacts(testCursorWithNullValues);
for (String contact : contacts) {
assertThat(contact).isNotNull();
}
}
@Test
public void nullPreference_displayPreference() {
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
@@ -176,25 +162,4 @@ public class ZenModeStarredContactsPreferenceControllerTest {
// should not throw a null pointer
mMessagesController.displayPreference(mPreferenceScreen);
}
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) {
if (count < size) {
count++;
return true;
}
return false;
}
}).when(mockCursor).moveToNext();
return mockCursor;
}
}

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;
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,40 @@
/*
* 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;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.service.notification.ZenPolicy;
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;
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;
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,92 @@
/*
* 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;
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.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;
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;
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;
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;
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;
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;
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);
}
}