Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distriZenbuted on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.notification; |
| 18 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 19 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 20 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 21 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 22 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 23 | |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 24 | import static junit.framework.Assert.assertEquals; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 25 | import static junit.framework.Assert.assertFalse; |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 26 | import static junit.framework.TestCase.assertTrue; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 27 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 28 | import static org.junit.Assert.assertNotEquals; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.any; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 31 | import static org.mockito.ArgumentMatchers.anyInt; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 32 | import static org.mockito.ArgumentMatchers.anyString; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 33 | import static org.mockito.ArgumentMatchers.eq; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 34 | import static org.mockito.Mockito.atLeastOnce; |
| 35 | import static org.mockito.Mockito.doNothing; |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 36 | import static org.mockito.Mockito.mock; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 37 | import static org.mockito.Mockito.never; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 38 | import static org.mockito.Mockito.spy; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 39 | import static org.mockito.Mockito.times; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 40 | import static org.mockito.Mockito.verify; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 41 | import static org.mockito.Mockito.when; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 42 | |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 43 | import android.app.AppGlobals; |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 44 | import android.app.AppOpsManager; |
Beverly | a0d07ab | 2018-11-28 16:49:23 -0500 | [diff] [blame] | 45 | import android.app.AutomaticZenRule; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 46 | import android.app.NotificationManager; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 47 | import android.app.NotificationManager.Policy; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 48 | import android.content.ComponentName; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 49 | import android.content.ContentResolver; |
| 50 | import android.content.Context; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 51 | import android.content.pm.PackageManager; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 52 | import android.content.res.Resources; |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 53 | import android.content.res.XmlResourceParser; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 54 | import android.media.AudioAttributes; |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 55 | import android.media.AudioManager; |
| 56 | import android.media.AudioManagerInternal; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 57 | import android.media.AudioSystem; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 58 | import android.media.VolumePolicy; |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 59 | import android.net.Uri; |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 60 | import android.os.UserHandle; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 61 | import android.provider.Settings; |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 62 | import android.provider.Settings.Global; |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 63 | import android.service.notification.Condition; |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 64 | import android.service.notification.ZenModeConfig; |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 65 | import android.service.notification.ZenModeConfig.ScheduleInfo; |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 66 | import android.service.notification.ZenPolicy; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 67 | import android.test.suitebuilder.annotation.SmallTest; |
| 68 | import android.testing.AndroidTestingRunner; |
| 69 | import android.testing.TestableLooper; |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 70 | import android.util.ArrayMap; |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 71 | import android.util.Log; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 72 | import android.util.Xml; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 73 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 74 | import com.android.internal.R; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 75 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 76 | import com.android.internal.util.FastXmlSerializer; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 77 | import com.android.server.UiServiceTestCase; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 78 | import com.android.server.notification.ManagedServices.UserProfiles; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 79 | |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 80 | import org.junit.Before; |
| 81 | import org.junit.Test; |
| 82 | import org.junit.runner.RunWith; |
| 83 | import org.mockito.Mock; |
| 84 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 85 | import org.xmlpull.v1.XmlPullParser; |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 86 | import org.xmlpull.v1.XmlPullParserException; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 87 | import org.xmlpull.v1.XmlSerializer; |
| 88 | |
| 89 | import java.io.BufferedInputStream; |
| 90 | import java.io.BufferedOutputStream; |
| 91 | import java.io.ByteArrayInputStream; |
| 92 | import java.io.ByteArrayOutputStream; |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 93 | import java.io.IOException; |
| 94 | import java.io.InputStream; |
| 95 | import java.io.Reader; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 96 | import java.util.Objects; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 97 | |
| 98 | @SmallTest |
| 99 | @RunWith(AndroidTestingRunner.class) |
| 100 | @TestableLooper.RunWithLooper |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 101 | public class ZenModeHelperTest extends UiServiceTestCase { |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 102 | |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 103 | private static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE"; |
| 104 | private static final String SCHEDULE_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE"; |
| 105 | |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 106 | ConditionProviders mConditionProviders; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 107 | @Mock NotificationManager mNotificationManager; |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 108 | private Resources mResources; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 109 | private TestableLooper mTestableLooper; |
| 110 | private ZenModeHelper mZenModeHelperSpy; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 111 | private Context mContext; |
| 112 | private ContentResolver mContentResolver; |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 113 | @Mock AppOpsManager mAppOps; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 114 | |
| 115 | @Before |
| 116 | public void setUp() { |
| 117 | MockitoAnnotations.initMocks(this); |
| 118 | |
| 119 | mTestableLooper = TestableLooper.get(this); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 120 | mContext = spy(getContext()); |
| 121 | mContentResolver = mContext.getContentResolver(); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 122 | mResources = spy(mContext.getResources()); |
| 123 | try { |
| 124 | when(mResources.getXml(R.xml.default_zen_mode_config)).thenReturn( |
| 125 | getDefaultConfigParser()); |
| 126 | } catch (Exception e) { |
| 127 | Log.d("ZenModeHelperTest", "Couldn't mock default zen mode config xml file err=" + |
| 128 | e.toString()); |
| 129 | } |
| 130 | |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 131 | when(mContext.getSystemService(AppOpsManager.class)).thenReturn(mAppOps); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 132 | when(mContext.getSystemService(NotificationManager.class)).thenReturn(mNotificationManager); |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 133 | mConditionProviders = new ConditionProviders(mContext, new UserProfiles(), |
| 134 | AppGlobals.getPackageManager()); |
| 135 | mConditionProviders.addSystemProvider(new CountdownConditionProvider()); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 136 | mZenModeHelperSpy = spy(new ZenModeHelper(mContext, mTestableLooper.getLooper(), |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 137 | mConditionProviders)); |
| 138 | } |
| 139 | |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 140 | private XmlResourceParser getDefaultConfigParser() throws IOException, XmlPullParserException { |
| 141 | String xml = "<zen version=\"8\" user=\"0\">\n" |
| 142 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 143 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
| 144 | + "visualScreenOff=\"true\" alarms=\"true\" " |
| 145 | + "media=\"true\" system=\"false\" />\n" |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 146 | + "<automatic ruleId=\"" + EVENTS_DEFAULT_RULE_ID |
| 147 | + "\" enabled=\"false\" snoozing=\"false\"" |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 148 | + " name=\"Event\" zen=\"1\"" |
| 149 | + " component=\"android/com.android.server.notification.EventConditionProvider\"" |
| 150 | + " conditionId=\"condition://android/event?userId=-10000&calendar=&" |
| 151 | + "reply=1\"/>\n" |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 152 | + "<automatic ruleId=\"" + SCHEDULE_DEFAULT_RULE_ID + "\" enabled=\"false\"" |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 153 | + " snoozing=\"false\" name=\"Sleeping\" zen=\"1\"" |
| 154 | + " component=\"android/com.android.server.notification.ScheduleConditionProvider\"" |
| 155 | + " conditionId=\"condition://android/schedule?days=1.2.3.4.5.6.7 &start=22.0" |
| 156 | + "&end=7.0&exitAtAlarm=true\"/>" |
| 157 | + "<disallow visualEffects=\"511\" />" |
| 158 | + "</zen>"; |
| 159 | XmlPullParser parser = Xml.newPullParser(); |
| 160 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())), null); |
| 161 | parser.nextTag(); |
| 162 | return new XmlResourceParserImpl(parser); |
| 163 | } |
| 164 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 165 | private ByteArrayOutputStream writeXmlAndPurge(Integer version) throws Exception { |
| 166 | XmlSerializer serializer = new FastXmlSerializer(); |
| 167 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 168 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 169 | serializer.startDocument(null, true); |
| 170 | mZenModeHelperSpy.writeXml(serializer, false, version, UserHandle.USER_ALL); |
| 171 | serializer.endDocument(); |
| 172 | serializer.flush(); |
| 173 | mZenModeHelperSpy.setConfig(new ZenModeConfig(), null, "writing xml"); |
| 174 | return baos; |
| 175 | } |
| 176 | |
| 177 | private ByteArrayOutputStream writeXmlAndPurgeForUser(Integer version, int userId) |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 178 | throws Exception { |
| 179 | XmlSerializer serializer = new FastXmlSerializer(); |
| 180 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 181 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 182 | serializer.startDocument(null, true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 183 | mZenModeHelperSpy.writeXml(serializer, true, version, userId); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 184 | serializer.endDocument(); |
| 185 | serializer.flush(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 186 | ZenModeConfig newConfig = new ZenModeConfig(); |
| 187 | newConfig.user = userId; |
| 188 | mZenModeHelperSpy.setConfig(newConfig, null, "writing xml"); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 189 | return baos; |
| 190 | } |
| 191 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 192 | private XmlPullParser getParserForByteStream(ByteArrayOutputStream baos) throws Exception { |
| 193 | XmlPullParser parser = Xml.newPullParser(); |
| 194 | parser.setInput( |
| 195 | new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), null); |
| 196 | parser.nextTag(); |
| 197 | return parser; |
| 198 | } |
| 199 | |
| 200 | private ArrayMap<String, ZenModeConfig.ZenRule> getCustomAutomaticRules() { |
| 201 | ArrayMap<String, ZenModeConfig.ZenRule> automaticRules = new ArrayMap<>(); |
| 202 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 203 | final ScheduleInfo customRuleInfo = new ScheduleInfo(); |
| 204 | customRule.enabled = true; |
| 205 | customRule.creationTime = 0; |
| 206 | customRule.id = "customRule"; |
| 207 | customRule.name = "Custom Rule"; |
| 208 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 209 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(customRuleInfo); |
| 210 | customRule.configurationActivity = |
| 211 | new ComponentName("android", "ScheduleConditionProvider"); |
| 212 | customRule.pkg = customRule.configurationActivity.getPackageName(); |
| 213 | automaticRules.put("customRule", customRule); |
| 214 | return automaticRules; |
| 215 | } |
| 216 | |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 217 | @Test |
| 218 | public void testZenOff_NoMuteApplied() { |
| 219 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_OFF; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 220 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(Policy.PRIORITY_CATEGORY_ALARMS | |
| 221 | Policy.PRIORITY_CATEGORY_MEDIA, 0, 0, 0, 0); |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 222 | mZenModeHelperSpy.applyRestrictions(); |
| 223 | |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 224 | doNothing().when(mZenModeHelperSpy).applyRestrictions(eq(false), anyBoolean(), anyInt()); |
| 225 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 226 | AudioAttributes.USAGE_ALARM); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 227 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 228 | AudioAttributes.USAGE_MEDIA); |
| 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void testZenOn_AllowAlarmsMedia_NoAlarmMediaMuteApplied() { |
| 233 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 234 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(Policy.PRIORITY_CATEGORY_ALARMS | |
| 235 | Policy.PRIORITY_CATEGORY_MEDIA, 0, 0, 0, 0); |
| 236 | |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 237 | mZenModeHelperSpy.applyRestrictions(); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 238 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, false, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 239 | AudioAttributes.USAGE_ALARM); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 240 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, false, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 241 | AudioAttributes.USAGE_MEDIA); |
| 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void testZenOn_DisallowAlarmsMedia_AlarmMediaMuteApplied() { |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 246 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 247 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 248 | mZenModeHelperSpy.applyRestrictions(); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 249 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, true, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 250 | AudioAttributes.USAGE_ALARM); |
| 251 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 252 | // Media is a catch-all that includes games |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 253 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, true, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 254 | AudioAttributes.USAGE_MEDIA); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 255 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, true, |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 256 | AudioAttributes.USAGE_GAME); |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Test |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 260 | public void testTotalSilence() { |
| 261 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 262 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(Policy.PRIORITY_CATEGORY_ALARMS | |
| 263 | Policy.PRIORITY_CATEGORY_MEDIA, 0, 0, 0, 0); |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 264 | mZenModeHelperSpy.applyRestrictions(); |
| 265 | |
| 266 | // Total silence will silence alarms, media and system noises (but not vibrations) |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 267 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 268 | AudioAttributes.USAGE_ALARM); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 269 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 270 | AudioAttributes.USAGE_MEDIA); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 271 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 272 | AudioAttributes.USAGE_GAME); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 273 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 274 | AudioAttributes.USAGE_ASSISTANCE_SONIFICATION, AppOpsManager.OP_PLAY_AUDIO); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 275 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 276 | AudioAttributes.USAGE_ASSISTANCE_SONIFICATION, AppOpsManager.OP_VIBRATE); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 277 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 278 | AudioAttributes.USAGE_UNKNOWN); |
| 279 | } |
| 280 | |
| 281 | @Test |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 282 | public void testAlarmsOnly_alarmMediaMuteNotApplied() { |
| 283 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_ALARMS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 284 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 285 | mZenModeHelperSpy.applyRestrictions(); |
| 286 | |
| 287 | // Alarms only mode will not silence alarms |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 288 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 289 | AudioAttributes.USAGE_ALARM); |
| 290 | |
| 291 | // Alarms only mode will not silence media |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 292 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 293 | AudioAttributes.USAGE_MEDIA); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 294 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 295 | AudioAttributes.USAGE_GAME); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 296 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Jean-Michel Trivi | 6d38a1b | 2018-04-09 10:54:36 -0700 | [diff] [blame] | 297 | AudioAttributes.USAGE_UNKNOWN); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 298 | |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 299 | // Alarms only will silence system noises (but not vibrations) |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 300 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 301 | AudioAttributes.USAGE_ASSISTANCE_SONIFICATION, AppOpsManager.OP_PLAY_AUDIO); |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void testAlarmsOnly_callsMuteApplied() { |
| 306 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_ALARMS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 307 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 308 | mZenModeHelperSpy.applyRestrictions(); |
| 309 | |
| 310 | // Alarms only mode will silence calls despite priority-mode config |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 311 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 312 | AudioAttributes.USAGE_NOTIFICATION_RINGTONE); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 313 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, true, |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 314 | AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST); |
| 315 | } |
| 316 | |
| 317 | @Test |
| 318 | public void testAlarmsOnly_allZenConfigToggledCannotBypass_alarmMuteNotApplied() { |
| 319 | // Only audio attributes with SUPPRESIBLE_NEVER can bypass |
| 320 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_ALARMS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 321 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 322 | mZenModeHelperSpy.applyRestrictions(); |
| 323 | |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 324 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(false, false, |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 325 | AudioAttributes.USAGE_ALARM); |
| 326 | } |
| 327 | |
| 328 | @Test |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 329 | public void testZenAllCannotBypass() { |
| 330 | // Only audio attributes with SUPPRESIBLE_NEVER can bypass |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 331 | // with special case USAGE_ASSISTANCE_SONIFICATION |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 332 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 333 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 334 | mZenModeHelperSpy.applyRestrictions(); |
| 335 | |
| 336 | for (int usage : AudioAttributes.SDK_USAGES) { |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 337 | if (usage == AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) { |
| 338 | // only mute audio, not vibrations |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 339 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, true, usage, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 340 | AppOpsManager.OP_PLAY_AUDIO); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 341 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, false, usage, |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 342 | AppOpsManager.OP_VIBRATE); |
| 343 | } else { |
| 344 | boolean shouldMute = AudioAttributes.SUPPRESSIBLE_USAGES.get(usage) |
| 345 | != AudioAttributes.SUPPRESSIBLE_NEVER; |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 346 | verify(mZenModeHelperSpy, atLeastOnce()).applyRestrictions(true, shouldMute, usage); |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 347 | } |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 348 | } |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 349 | } |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 350 | |
| 351 | @Test |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 352 | public void testApplyRestrictions_whitelist_priorityOnlyMode() { |
| 353 | mZenModeHelperSpy.setPriorityOnlyDndExemptPackages(new String[] {PKG_O}); |
| 354 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 355 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
| 356 | mZenModeHelperSpy.applyRestrictions(); |
| 357 | |
| 358 | for (int usage : AudioAttributes.SDK_USAGES) { |
| 359 | verify(mAppOps).setRestriction( |
| 360 | eq(AppOpsManager.OP_PLAY_AUDIO), eq(usage), anyInt(), eq(new String[]{PKG_O})); |
| 361 | verify(mAppOps).setRestriction( |
| 362 | eq(AppOpsManager.OP_VIBRATE), eq(usage), anyInt(), eq(new String[]{PKG_O})); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | @Test |
| 367 | public void testApplyRestrictions_whitelist_alarmsOnlyMode() { |
| 368 | mZenModeHelperSpy.setPriorityOnlyDndExemptPackages(new String[] {PKG_O}); |
| 369 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_ALARMS; |
| 370 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
| 371 | mZenModeHelperSpy.applyRestrictions(); |
| 372 | |
| 373 | for (int usage : AudioAttributes.SDK_USAGES) { |
| 374 | verify(mAppOps).setRestriction( |
| 375 | eq(AppOpsManager.OP_PLAY_AUDIO), eq(usage), anyInt(), eq(null)); |
| 376 | verify(mAppOps).setRestriction( |
| 377 | eq(AppOpsManager.OP_VIBRATE), eq(usage), anyInt(), eq(null)); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | @Test |
| 382 | public void testApplyRestrictions_whitelist_totalSilenceMode() { |
| 383 | mZenModeHelperSpy.setPriorityOnlyDndExemptPackages(new String[] {PKG_O}); |
| 384 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_NO_INTERRUPTIONS; |
| 385 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
| 386 | mZenModeHelperSpy.applyRestrictions(); |
| 387 | |
| 388 | for (int usage : AudioAttributes.SDK_USAGES) { |
| 389 | verify(mAppOps).setRestriction( |
| 390 | eq(AppOpsManager.OP_PLAY_AUDIO), eq(usage), anyInt(), eq(null)); |
| 391 | verify(mAppOps).setRestriction( |
| 392 | eq(AppOpsManager.OP_VIBRATE), eq(usage), anyInt(), eq(null)); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | @Test |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 397 | public void testZenUpgradeNotification() { |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 398 | // shows zen upgrade notification if stored settings says to shows, |
| 399 | // zen has not been updated, boot is completed |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 400 | // and we're setting zen mode on |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 401 | Settings.Secure.putInt(mContentResolver, Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 1); |
| 402 | Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_SETTINGS_UPDATED, 0); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 403 | mZenModeHelperSpy.mIsBootComplete = true; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 404 | mZenModeHelperSpy.mConsolidatedPolicy = new Policy(0, 0, 0, 0, 0); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 405 | mZenModeHelperSpy.setZenModeSetting(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); |
| 406 | |
| 407 | verify(mZenModeHelperSpy, times(1)).createZenUpgradeNotification(); |
| 408 | verify(mNotificationManager, times(1)).notify(eq(ZenModeHelper.TAG), |
| 409 | eq(SystemMessage.NOTE_ZEN_UPGRADE), any()); |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 410 | assertEquals(0, Settings.Secure.getInt(mContentResolver, |
| 411 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, -1)); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | @Test |
| 415 | public void testNoZenUpgradeNotification() { |
| 416 | // doesn't show upgrade notification if stored settings says don't show |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 417 | Settings.Secure.putInt(mContentResolver, Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0); |
| 418 | Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_SETTINGS_UPDATED, 0); |
| 419 | mZenModeHelperSpy.mIsBootComplete = true; |
| 420 | mZenModeHelperSpy.setZenModeSetting(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); |
| 421 | |
| 422 | verify(mZenModeHelperSpy, never()).createZenUpgradeNotification(); |
| 423 | verify(mNotificationManager, never()).notify(eq(ZenModeHelper.TAG), |
| 424 | eq(SystemMessage.NOTE_ZEN_UPGRADE), any()); |
| 425 | } |
| 426 | |
| 427 | @Test |
| 428 | public void testNoZenUpgradeNotificationZenUpdated() { |
| 429 | // doesn't show upgrade notification since zen was already updated |
| 430 | Settings.Secure.putInt(mContentResolver, Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0); |
| 431 | Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_SETTINGS_UPDATED, 1); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 432 | mZenModeHelperSpy.mIsBootComplete = true; |
| 433 | mZenModeHelperSpy.setZenModeSetting(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); |
| 434 | |
| 435 | verify(mZenModeHelperSpy, never()).createZenUpgradeNotification(); |
| 436 | verify(mNotificationManager, never()).notify(eq(ZenModeHelper.TAG), |
| 437 | eq(SystemMessage.NOTE_ZEN_UPGRADE), any()); |
| 438 | } |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 439 | |
| 440 | @Test |
| 441 | public void testZenSetInternalRinger_AllPriorityNotificationSoundsMuted() { |
| 442 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
| 443 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 444 | Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_RINGER_LEVEL, |
| 445 | Integer.toString(AudioManager.RINGER_MODE_NORMAL)); |
| 446 | |
| 447 | // 1. Current ringer is normal |
| 448 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
| 449 | // Set zen to priority-only with all notification sounds muted (so ringer will be muted) |
| 450 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 451 | mZenModeHelperSpy.mConfig.allowReminders = false; |
| 452 | mZenModeHelperSpy.mConfig.allowCalls = false; |
| 453 | mZenModeHelperSpy.mConfig.allowMessages = false; |
| 454 | mZenModeHelperSpy.mConfig.allowEvents = false; |
| 455 | mZenModeHelperSpy.mConfig.allowRepeatCallers= false; |
| 456 | |
Beverly | 9e4214d | 2018-03-26 11:10:10 -0400 | [diff] [blame] | 457 | // 2. apply priority only zen - verify ringer is unchanged |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 458 | mZenModeHelperSpy.applyZenToRingerMode(); |
Beverly | 9e4214d | 2018-03-26 11:10:10 -0400 | [diff] [blame] | 459 | verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_SILENT, |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 460 | mZenModeHelperSpy.TAG); |
| 461 | |
Beverly | 9e4214d | 2018-03-26 11:10:10 -0400 | [diff] [blame] | 462 | // 3. apply zen off - verify zen is set to previous ringer (normal) |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 463 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); |
| 464 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
| 465 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 466 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 467 | mZenModeHelperSpy.TAG); |
| 468 | } |
| 469 | |
| 470 | @Test |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 471 | public void testRingerAffectedStreamsTotalSilence() { |
| 472 | // in total silence: |
| 473 | // ringtone, notification, system, alarm, streams, music are affected by ringer mode |
| 474 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; |
| 475 | ZenModeHelper.RingerModeDelegate ringerModeDelegate = |
| 476 | mZenModeHelperSpy.new RingerModeDelegate(); |
| 477 | int ringerModeAffectedStreams = ringerModeDelegate.getRingerModeAffectedStreams(0); |
| 478 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_RING)) != 0); |
| 479 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_NOTIFICATION)) |
| 480 | != 0); |
| 481 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_SYSTEM)) != 0); |
| 482 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_ALARM)) != 0); |
| 483 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_MUSIC)) != 0); |
| 484 | } |
| 485 | |
| 486 | @Test |
| 487 | public void testRingerAffectedStreamsPriorityOnly() { |
| 488 | // in priority only mode: |
| 489 | // ringtone, notification and system streams are affected by ringer mode |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 490 | mZenModeHelperSpy.mConfig.allowAlarms = true; |
| 491 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 492 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 493 | ZenModeHelper.RingerModeDelegate ringerModeDelegateRingerMuted = |
| 494 | mZenModeHelperSpy.new RingerModeDelegate(); |
| 495 | |
| 496 | int ringerModeAffectedStreams = |
| 497 | ringerModeDelegateRingerMuted.getRingerModeAffectedStreams(0); |
| 498 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_RING)) != 0); |
| 499 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_NOTIFICATION)) |
| 500 | != 0); |
| 501 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_SYSTEM)) != 0); |
| 502 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_ALARM)) == 0); |
| 503 | assertTrue((ringerModeAffectedStreams & (1 << AudioSystem.STREAM_MUSIC)) == 0); |
| 504 | |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 505 | // even when ringer is muted (since all ringer sounds cannot bypass DND), |
| 506 | // system stream is still affected by ringer mode |
| 507 | mZenModeHelperSpy.mConfig.allowSystem = false; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 508 | mZenModeHelperSpy.mConfig.allowReminders = false; |
| 509 | mZenModeHelperSpy.mConfig.allowCalls = false; |
| 510 | mZenModeHelperSpy.mConfig.allowMessages = false; |
| 511 | mZenModeHelperSpy.mConfig.allowEvents = false; |
| 512 | mZenModeHelperSpy.mConfig.allowRepeatCallers= false; |
| 513 | ZenModeHelper.RingerModeDelegate ringerModeDelegateRingerNotMuted = |
| 514 | mZenModeHelperSpy.new RingerModeDelegate(); |
| 515 | |
| 516 | int ringerMutedRingerModeAffectedStreams = |
| 517 | ringerModeDelegateRingerNotMuted.getRingerModeAffectedStreams(0); |
| 518 | assertTrue((ringerMutedRingerModeAffectedStreams & (1 << AudioSystem.STREAM_RING)) != 0); |
| 519 | assertTrue((ringerMutedRingerModeAffectedStreams & (1 << AudioSystem.STREAM_NOTIFICATION)) |
| 520 | != 0); |
| 521 | assertTrue((ringerMutedRingerModeAffectedStreams & (1 << AudioSystem.STREAM_SYSTEM)) |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 522 | != 0); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 523 | assertTrue((ringerMutedRingerModeAffectedStreams & (1 << AudioSystem.STREAM_ALARM)) == 0); |
| 524 | assertTrue((ringerMutedRingerModeAffectedStreams & (1 << AudioSystem.STREAM_MUSIC)) == 0); |
| 525 | } |
| 526 | |
| 527 | @Test |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 528 | public void testZenSetInternalRinger_NotAllPriorityNotificationSoundsMuted_StartNormal() { |
| 529 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
| 530 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 531 | Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_RINGER_LEVEL, |
| 532 | Integer.toString(AudioManager.RINGER_MODE_NORMAL)); |
| 533 | |
| 534 | // 1. Current ringer is normal |
| 535 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
| 536 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 537 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 538 | |
| 539 | // 2. apply priority only zen - verify ringer is normal |
| 540 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 541 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 542 | mZenModeHelperSpy.TAG); |
| 543 | |
| 544 | // 3. apply zen off - verify ringer remains normal |
| 545 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
| 546 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
| 547 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 548 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 549 | mZenModeHelperSpy.TAG); |
| 550 | } |
| 551 | |
| 552 | @Test |
| 553 | public void testZenSetInternalRinger_NotAllPriorityNotificationSoundsMuted_StartSilent() { |
| 554 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
| 555 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 556 | Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_RINGER_LEVEL, |
| 557 | Integer.toString(AudioManager.RINGER_MODE_SILENT)); |
| 558 | |
| 559 | // 1. Current ringer is silent |
| 560 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); |
| 561 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 562 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 563 | |
| 564 | // 2. apply priority only zen - verify ringer is silent |
| 565 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 566 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_SILENT, |
| 567 | mZenModeHelperSpy.TAG); |
| 568 | |
| 569 | // 3. apply zen-off - verify ringer is still silent |
| 570 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); |
| 571 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
| 572 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 573 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_SILENT, |
| 574 | mZenModeHelperSpy.TAG); |
| 575 | } |
| 576 | |
| 577 | @Test |
| 578 | public void testZenSetInternalRinger_NotAllPriorityNotificationSoundsMuted_RingerChanges() { |
| 579 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
| 580 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 581 | Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_RINGER_LEVEL, |
| 582 | Integer.toString(AudioManager.RINGER_MODE_NORMAL)); |
| 583 | |
| 584 | // 1. Current ringer is normal |
| 585 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
| 586 | // Set zen to priority-only with all notification sounds muted (so ringer will be muted) |
| 587 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 588 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 589 | |
| 590 | // 2. apply priority only zen - verify zen will still be normal |
| 591 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 592 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 593 | mZenModeHelperSpy.TAG); |
| 594 | |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 595 | // 3. change ringer from normal to silent, verify previous ringer set to new ringer (silent) |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 596 | ZenModeHelper.RingerModeDelegate ringerModeDelegate = |
| 597 | mZenModeHelperSpy.new RingerModeDelegate(); |
| 598 | ringerModeDelegate.onSetRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 599 | AudioManager.RINGER_MODE_SILENT, "test", AudioManager.RINGER_MODE_NORMAL, |
| 600 | VolumePolicy.DEFAULT); |
| 601 | assertEquals(AudioManager.RINGER_MODE_SILENT, Global.getInt(mContext.getContentResolver(), |
| 602 | Global.ZEN_MODE_RINGER_LEVEL, AudioManager.RINGER_MODE_NORMAL)); |
| 603 | |
| 604 | // 4. apply zen off - verify ringer still silenced |
| 605 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); |
| 606 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
| 607 | mZenModeHelperSpy.applyZenToRingerMode(); |
| 608 | verify(mAudioManager, atLeastOnce()).setRingerModeInternal(AudioManager.RINGER_MODE_SILENT, |
| 609 | mZenModeHelperSpy.TAG); |
| 610 | } |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 611 | |
| 612 | @Test |
| 613 | public void testSilentRingerSavedInZenOff_startsZenOff() { |
| 614 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 615 | mZenModeHelperSpy.mConfig = new ZenModeConfig(); |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 616 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 617 | |
| 618 | // apply zen off multiple times - verify ringer is not set to normal |
| 619 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); |
| 620 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
| 621 | mZenModeHelperSpy.mConfig = null; // will evaluate config to zen mode off |
| 622 | for (int i = 0; i < 3; i++) { |
| 623 | // if zen doesn't change, zen should not reapply itself to the ringer |
| 624 | mZenModeHelperSpy.evaluateZenMode("test", true); |
| 625 | } |
| 626 | verify(mZenModeHelperSpy, never()).applyZenToRingerMode(); |
| 627 | verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 628 | mZenModeHelperSpy.TAG); |
| 629 | } |
| 630 | |
| 631 | @Test |
| 632 | public void testSilentRingerSavedOnZenOff_startsZenOn() { |
| 633 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
| 634 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 635 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 636 | mZenModeHelperSpy.mConfig = new ZenModeConfig(); |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 637 | |
| 638 | // previously set silent ringer |
| 639 | ZenModeHelper.RingerModeDelegate ringerModeDelegate = |
| 640 | mZenModeHelperSpy.new RingerModeDelegate(); |
| 641 | ringerModeDelegate.onSetRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 642 | AudioManager.RINGER_MODE_SILENT, "test", AudioManager.RINGER_MODE_NORMAL, |
| 643 | VolumePolicy.DEFAULT); |
| 644 | assertEquals(AudioManager.RINGER_MODE_SILENT, Global.getInt(mContext.getContentResolver(), |
| 645 | Global.ZEN_MODE_RINGER_LEVEL, AudioManager.RINGER_MODE_NORMAL)); |
| 646 | |
| 647 | // apply zen off multiple times - verify ringer is not set to normal |
| 648 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); |
| 649 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 650 | for (int i = 0; i < 3; i++) { |
| 651 | // if zen doesn't change, zen should not reapply itself to the ringer |
| 652 | mZenModeHelperSpy.evaluateZenMode("test", true); |
| 653 | } |
| 654 | verify(mZenModeHelperSpy, times(1)).applyZenToRingerMode(); |
| 655 | verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 656 | mZenModeHelperSpy.TAG); |
| 657 | } |
| 658 | |
| 659 | @Test |
| 660 | public void testVibrateRingerSavedOnZenOff_startsZenOn() { |
| 661 | AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class); |
| 662 | mZenModeHelperSpy.mAudioManager = mAudioManager; |
| 663 | mZenModeHelperSpy.mZenMode = Global.ZEN_MODE_OFF; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 664 | mZenModeHelperSpy.mConfig = new ZenModeConfig(); |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 665 | |
| 666 | // previously set silent ringer |
| 667 | ZenModeHelper.RingerModeDelegate ringerModeDelegate = |
| 668 | mZenModeHelperSpy.new RingerModeDelegate(); |
| 669 | ringerModeDelegate.onSetRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 670 | AudioManager.RINGER_MODE_VIBRATE, "test", AudioManager.RINGER_MODE_NORMAL, |
| 671 | VolumePolicy.DEFAULT); |
| 672 | assertEquals(AudioManager.RINGER_MODE_VIBRATE, Global.getInt(mContext.getContentResolver(), |
| 673 | Global.ZEN_MODE_RINGER_LEVEL, AudioManager.RINGER_MODE_NORMAL)); |
| 674 | |
| 675 | // apply zen off multiple times - verify ringer is not set to normal |
| 676 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 677 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 678 | for (int i = 0; i < 3; i++) { |
| 679 | // if zen doesn't change, zen should not reapply itself to the ringer |
| 680 | mZenModeHelperSpy.evaluateZenMode("test", true); |
| 681 | } |
| 682 | verify(mZenModeHelperSpy, times(1)).applyZenToRingerMode(); |
| 683 | verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, |
| 684 | mZenModeHelperSpy.TAG); |
| 685 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 686 | |
| 687 | @Test |
| 688 | public void testParcelConfig() { |
| 689 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 690 | mZenModeHelperSpy.mConfig.allowAlarms = false; |
| 691 | mZenModeHelperSpy.mConfig.allowMedia = false; |
| 692 | mZenModeHelperSpy.mConfig.allowSystem = false; |
| 693 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 694 | mZenModeHelperSpy.mConfig.allowCalls = true; |
| 695 | mZenModeHelperSpy.mConfig.allowMessages = true; |
| 696 | mZenModeHelperSpy.mConfig.allowEvents = true; |
| 697 | mZenModeHelperSpy.mConfig.allowRepeatCallers= true; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 698 | mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE; |
| 699 | mZenModeHelperSpy.mConfig.manualRule = new ZenModeConfig.ZenRule(); |
| 700 | mZenModeHelperSpy.mConfig.manualRule.component = new ComponentName("a", "a"); |
| 701 | mZenModeHelperSpy.mConfig.manualRule.enabled = true; |
| 702 | mZenModeHelperSpy.mConfig.manualRule.snoozing = true; |
| 703 | |
| 704 | ZenModeConfig actual = mZenModeHelperSpy.mConfig.copy(); |
| 705 | |
| 706 | assertEquals(mZenModeHelperSpy.mConfig, actual); |
| 707 | } |
| 708 | |
| 709 | @Test |
| 710 | public void testWriteXml() throws Exception { |
| 711 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 712 | mZenModeHelperSpy.mConfig.allowAlarms = false; |
| 713 | mZenModeHelperSpy.mConfig.allowMedia = false; |
| 714 | mZenModeHelperSpy.mConfig.allowSystem = false; |
| 715 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 716 | mZenModeHelperSpy.mConfig.allowCalls = true; |
| 717 | mZenModeHelperSpy.mConfig.allowMessages = true; |
| 718 | mZenModeHelperSpy.mConfig.allowEvents = true; |
| 719 | mZenModeHelperSpy.mConfig.allowRepeatCallers= true; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 720 | mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE; |
| 721 | mZenModeHelperSpy.mConfig.manualRule = new ZenModeConfig.ZenRule(); |
| 722 | mZenModeHelperSpy.mConfig.manualRule.zenMode = |
| 723 | Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 724 | mZenModeHelperSpy.mConfig.manualRule.component = new ComponentName("a", "a"); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 725 | mZenModeHelperSpy.mConfig.manualRule.pkg = "a"; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 726 | mZenModeHelperSpy.mConfig.manualRule.enabled = true; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 727 | |
| 728 | ZenModeConfig expected = mZenModeHelperSpy.mConfig.copy(); |
| 729 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 730 | ByteArrayOutputStream baos = writeXmlAndPurge(null); |
| 731 | XmlPullParser parser = getParserForByteStream(baos); |
| 732 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 733 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 734 | assertEquals("Config mismatch: current vs expected: " |
| 735 | + mZenModeHelperSpy.mConfig.diff(expected), expected, mZenModeHelperSpy.mConfig); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | @Test |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 739 | public void testWriteXml_onlyBackupsTargetUser() throws Exception { |
| 740 | // Setup configs for user 10 and 11. |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 741 | setupZenConfig(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 742 | ZenModeConfig config10 = mZenModeHelperSpy.mConfig.copy(); |
| 743 | config10.user = 10; |
| 744 | config10.allowAlarms = true; |
| 745 | config10.allowMedia = true; |
| 746 | mZenModeHelperSpy.setConfig(config10, null, "writeXml"); |
| 747 | ZenModeConfig config11 = mZenModeHelperSpy.mConfig.copy(); |
| 748 | config11.user = 11; |
| 749 | config11.allowAlarms = false; |
| 750 | config11.allowMedia = false; |
| 751 | mZenModeHelperSpy.setConfig(config11, null, "writeXml"); |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 752 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 753 | // Backup user 10 and reset values. |
| 754 | ByteArrayOutputStream baos = writeXmlAndPurgeForUser(null, 10); |
| 755 | ZenModeConfig newConfig11 = new ZenModeConfig(); |
| 756 | newConfig11.user = 11; |
| 757 | mZenModeHelperSpy.mConfigs.put(11, newConfig11); |
| 758 | |
| 759 | // Parse backup data. |
| 760 | XmlPullParser parser = getParserForByteStream(baos); |
| 761 | mZenModeHelperSpy.readXml(parser, true, 10); |
| 762 | mZenModeHelperSpy.readXml(parser, true, 11); |
| 763 | |
| 764 | ZenModeConfig actual = mZenModeHelperSpy.mConfigs.get(10); |
| 765 | assertEquals( |
| 766 | "Config mismatch: current vs expected: " + actual.diff(config10), config10, actual); |
| 767 | assertNotEquals("Expected config mismatch", config11, mZenModeHelperSpy.mConfigs.get(11)); |
| 768 | } |
| 769 | |
| 770 | @Test |
| 771 | public void testReadXmlRestore_forSystemUser() throws Exception { |
| 772 | setupZenConfig(); |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 773 | // one enabled automatic rule |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 774 | mZenModeHelperSpy.mConfig.automaticRules = getCustomAutomaticRules(); |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 775 | ZenModeConfig original = mZenModeHelperSpy.mConfig.copy(); |
| 776 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 777 | ByteArrayOutputStream baos = writeXmlAndPurgeForUser(null, UserHandle.USER_SYSTEM); |
| 778 | XmlPullParser parser = getParserForByteStream(baos); |
| 779 | mZenModeHelperSpy.readXml(parser, true, UserHandle.USER_SYSTEM); |
| 780 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 781 | assertEquals("Config mismatch: current vs original: " |
| 782 | + mZenModeHelperSpy.mConfig.diff(original), original, mZenModeHelperSpy.mConfig); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 783 | assertEquals(original.hashCode(), mZenModeHelperSpy.mConfig.hashCode()); |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 784 | } |
| 785 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 786 | /** Restore should ignore the data's user id and restore for the target user. */ |
| 787 | @Test |
| 788 | public void testReadXmlRestore_forNonSystemUser() throws Exception { |
| 789 | // Setup config. |
| 790 | setupZenConfig(); |
| 791 | mZenModeHelperSpy.mConfig.automaticRules = getCustomAutomaticRules(); |
| 792 | ZenModeConfig expected = mZenModeHelperSpy.mConfig.copy(); |
| 793 | |
| 794 | // Backup data for user 0. |
| 795 | ByteArrayOutputStream baos = writeXmlAndPurgeForUser(null, UserHandle.USER_SYSTEM); |
| 796 | |
| 797 | // Restore data for user 10. |
| 798 | XmlPullParser parser = getParserForByteStream(baos); |
| 799 | mZenModeHelperSpy.readXml(parser, true, 10); |
| 800 | |
| 801 | ZenModeConfig actual = mZenModeHelperSpy.mConfigs.get(10); |
| 802 | expected.user = 10; |
| 803 | assertEquals( |
| 804 | "Config mismatch: current vs original: " + actual.diff(expected), expected, actual); |
| 805 | assertEquals(expected.hashCode(), actual.hashCode()); |
| 806 | expected.user = 0; |
| 807 | assertNotEquals(expected, mZenModeHelperSpy.mConfig); |
| 808 | } |
| 809 | |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 810 | @Test |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 811 | public void testWriteXmlWithZenPolicy() throws Exception { |
| 812 | final String ruleId = "customRule"; |
| 813 | setupZenConfig(); |
| 814 | |
| 815 | // one enabled automatic rule with zen policy |
| 816 | ArrayMap<String, ZenModeConfig.ZenRule> automaticRules = new ArrayMap<>(); |
| 817 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 818 | final ScheduleInfo customRuleInfo = new ScheduleInfo(); |
| 819 | customRule.enabled = true; |
| 820 | customRule.creationTime = 0; |
| 821 | customRule.id = "customRule"; |
| 822 | customRule.name = "Custom Rule"; |
| 823 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 824 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(customRuleInfo); |
| 825 | customRule.configurationActivity = |
| 826 | new ComponentName("android", "ScheduleConditionProvider"); |
| 827 | customRule.pkg = customRule.configurationActivity.getPackageName(); |
| 828 | customRule.zenPolicy = new ZenPolicy.Builder() |
| 829 | .allowAlarms(false) |
| 830 | .allowMedia(false) |
| 831 | .allowRepeatCallers(false) |
| 832 | .allowCalls(ZenPolicy.PEOPLE_TYPE_NONE) |
| 833 | .allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS) |
| 834 | .allowEvents(true) |
| 835 | .allowReminders(false) |
| 836 | .build(); |
| 837 | automaticRules.put("customRule", customRule); |
| 838 | mZenModeHelperSpy.mConfig.automaticRules = automaticRules; |
| 839 | |
| 840 | ZenModeConfig expected = mZenModeHelperSpy.mConfig.copy(); |
| 841 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 842 | ByteArrayOutputStream baos = writeXmlAndPurge(null); |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 843 | XmlPullParser parser = Xml.newPullParser(); |
| 844 | parser.setInput(new BufferedInputStream( |
| 845 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 846 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 847 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 848 | |
| 849 | ZenModeConfig.ZenRule original = expected.automaticRules.get(ruleId); |
| 850 | ZenModeConfig.ZenRule current = mZenModeHelperSpy.mConfig.automaticRules.get(ruleId); |
| 851 | |
| 852 | assertEquals("Automatic rules mismatch", original, current); |
| 853 | } |
| 854 | |
| 855 | @Test |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 856 | public void testReadXmlRestoreWithZenPolicy_forSystemUser() throws Exception { |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 857 | final String ruleId = "customRule"; |
| 858 | setupZenConfig(); |
| 859 | |
| 860 | // one enabled automatic rule with zen policy |
| 861 | ArrayMap<String, ZenModeConfig.ZenRule> automaticRules = new ArrayMap<>(); |
| 862 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 863 | final ScheduleInfo customRuleInfo = new ScheduleInfo(); |
| 864 | customRule.enabled = true; |
| 865 | customRule.creationTime = 0; |
| 866 | customRule.id = ruleId; |
| 867 | customRule.name = "Custom Rule"; |
| 868 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 869 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(customRuleInfo); |
| 870 | customRule.configurationActivity = |
| 871 | new ComponentName("android", "ScheduleConditionProvider"); |
| 872 | customRule.pkg = customRule.configurationActivity.getPackageName(); |
| 873 | customRule.zenPolicy = new ZenPolicy.Builder() |
| 874 | .allowSystem(true) |
| 875 | .allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE) |
| 876 | .allowReminders(true) |
| 877 | .build(); |
| 878 | automaticRules.put(ruleId, customRule); |
| 879 | mZenModeHelperSpy.mConfig.automaticRules = automaticRules; |
| 880 | |
| 881 | ZenModeConfig expected = mZenModeHelperSpy.mConfig.copy(); |
| 882 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 883 | ByteArrayOutputStream baos = writeXmlAndPurgeForUser(null, UserHandle.USER_SYSTEM); |
| 884 | XmlPullParser parser = getParserForByteStream(baos); |
| 885 | mZenModeHelperSpy.readXml(parser, true, UserHandle.USER_SYSTEM); |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 886 | |
| 887 | ZenModeConfig.ZenRule original = expected.automaticRules.get(ruleId); |
| 888 | ZenModeConfig.ZenRule current = mZenModeHelperSpy.mConfig.automaticRules.get(ruleId); |
| 889 | |
| 890 | assertEquals("Automatic rules mismatch", original, current); |
| 891 | } |
| 892 | |
| 893 | @Test |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 894 | public void testReadXmlRulesNotOverriden() throws Exception { |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 895 | setupZenConfig(); |
| 896 | |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 897 | // automatic zen rule is enabled on upgrade so rules should not be overriden to default |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 898 | ArrayMap<String, ZenModeConfig.ZenRule> enabledAutoRule = new ArrayMap<>(); |
| 899 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 900 | final ScheduleInfo weeknights = new ScheduleInfo(); |
| 901 | customRule.enabled = true; |
| 902 | customRule.name = "Custom Rule"; |
| 903 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 904 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(weeknights); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 905 | customRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 906 | enabledAutoRule.put("customRule", customRule); |
| 907 | mZenModeHelperSpy.mConfig.automaticRules = enabledAutoRule; |
| 908 | |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 909 | // set previous version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 910 | ByteArrayOutputStream baos = writeXmlAndPurge(5); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 911 | XmlPullParser parser = Xml.newPullParser(); |
| 912 | parser.setInput(new BufferedInputStream( |
| 913 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 914 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 915 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 916 | |
| 917 | assertTrue(mZenModeHelperSpy.mConfig.automaticRules.containsKey("customRule")); |
| 918 | setupZenConfigMaintained(); |
| 919 | } |
| 920 | |
| 921 | @Test |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 922 | public void testMigrateSuppressedVisualEffects_oneExistsButOff() throws Exception { |
| 923 | String xml = "<zen version=\"6\" user=\"0\">\n" |
| 924 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 925 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
Julia Reynolds | 4501387 | 2018-07-12 14:24:56 -0400 | [diff] [blame] | 926 | + "visualScreenOff=\"true\" alarms=\"true\" " |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 927 | + "media=\"true\" system=\"false\" />\n" |
| 928 | + "<disallow visualEffects=\"511\" />" |
| 929 | + "</zen>"; |
| 930 | |
| 931 | XmlPullParser parser = Xml.newPullParser(); |
| 932 | parser.setInput(new BufferedInputStream( |
| 933 | new ByteArrayInputStream(xml.getBytes())), null); |
| 934 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 935 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 936 | |
| 937 | assertEquals(0, mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 938 | |
| 939 | xml = "<zen version=\"6\" user=\"0\">\n" |
| 940 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 941 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
Julia Reynolds | 4501387 | 2018-07-12 14:24:56 -0400 | [diff] [blame] | 942 | + "visualScreenOn=\"true\" alarms=\"true\" " |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 943 | + "media=\"true\" system=\"false\" />\n" |
| 944 | + "<disallow visualEffects=\"511\" />" |
| 945 | + "</zen>"; |
| 946 | |
| 947 | parser = Xml.newPullParser(); |
| 948 | parser.setInput(new BufferedInputStream( |
| 949 | new ByteArrayInputStream(xml.getBytes())), null); |
| 950 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 951 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 952 | |
| 953 | assertEquals(0, mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 954 | } |
| 955 | |
| 956 | @Test |
| 957 | public void testMigrateSuppressedVisualEffects_bothExistButOff() throws Exception { |
| 958 | String xml = "<zen version=\"6\" user=\"0\">\n" |
| 959 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 960 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
Julia Reynolds | 4501387 | 2018-07-12 14:24:56 -0400 | [diff] [blame] | 961 | + "visualScreenOff=\"true\" visualScreenOn=\"true\" alarms=\"true\" " |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 962 | + "media=\"true\" system=\"false\" />\n" |
| 963 | + "<disallow visualEffects=\"511\" />" |
| 964 | + "</zen>"; |
| 965 | |
| 966 | XmlPullParser parser = Xml.newPullParser(); |
| 967 | parser.setInput(new BufferedInputStream( |
| 968 | new ByteArrayInputStream(xml.getBytes())), null); |
| 969 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 970 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 971 | |
| 972 | assertEquals(0, mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 973 | } |
| 974 | |
| 975 | @Test |
| 976 | public void testMigrateSuppressedVisualEffects_bothExistButOn() throws Exception { |
| 977 | String xml = "<zen version=\"6\" user=\"0\">\n" |
| 978 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 979 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
Julia Reynolds | 4501387 | 2018-07-12 14:24:56 -0400 | [diff] [blame] | 980 | + "visualScreenOff=\"false\" visualScreenOn=\"false\" alarms=\"true\" " |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 981 | + "media=\"true\" system=\"false\" />\n" |
| 982 | + "<disallow visualEffects=\"511\" />" |
| 983 | + "</zen>"; |
| 984 | |
| 985 | XmlPullParser parser = Xml.newPullParser(); |
| 986 | parser.setInput(new BufferedInputStream( |
| 987 | new ByteArrayInputStream(xml.getBytes())), null); |
| 988 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 989 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 990 | |
| 991 | assertEquals(SUPPRESSED_EFFECT_FULL_SCREEN_INTENT |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 992 | | SUPPRESSED_EFFECT_LIGHTS |
| 993 | | SUPPRESSED_EFFECT_PEEK, |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 994 | mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 995 | |
| 996 | xml = "<zen version=\"6\" user=\"0\">\n" |
| 997 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 998 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
Julia Reynolds | 4501387 | 2018-07-12 14:24:56 -0400 | [diff] [blame] | 999 | + "visualScreenOff=\"true\" visualScreenOn=\"false\" alarms=\"true\" " |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 1000 | + "media=\"true\" system=\"false\" />\n" |
| 1001 | + "<disallow visualEffects=\"511\" />" |
| 1002 | + "</zen>"; |
| 1003 | |
| 1004 | parser = Xml.newPullParser(); |
| 1005 | parser.setInput(new BufferedInputStream( |
| 1006 | new ByteArrayInputStream(xml.getBytes())), null); |
| 1007 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1008 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 1009 | |
| 1010 | assertEquals(SUPPRESSED_EFFECT_PEEK, mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 1011 | |
| 1012 | xml = "<zen version=\"6\" user=\"0\">\n" |
| 1013 | + "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" " |
| 1014 | + "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" " |
Julia Reynolds | 4501387 | 2018-07-12 14:24:56 -0400 | [diff] [blame] | 1015 | + "visualScreenOff=\"false\" visualScreenOn=\"true\" alarms=\"true\" " |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 1016 | + "media=\"true\" system=\"false\" />\n" |
| 1017 | + "<disallow visualEffects=\"511\" />" |
| 1018 | + "</zen>"; |
| 1019 | |
| 1020 | parser = Xml.newPullParser(); |
| 1021 | parser.setInput(new BufferedInputStream( |
| 1022 | new ByteArrayInputStream(xml.getBytes())), null); |
| 1023 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1024 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 1025 | |
| 1026 | assertEquals(SUPPRESSED_EFFECT_FULL_SCREEN_INTENT | SUPPRESSED_EFFECT_LIGHTS, |
| 1027 | mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 1028 | } |
| 1029 | |
| 1030 | @Test |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1031 | public void testReadXmlResetDefaultRules() throws Exception { |
| 1032 | setupZenConfig(); |
| 1033 | |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1034 | // no enabled automatic zen rules and no default rules |
| 1035 | // so rules should be overriden by default rules |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1036 | mZenModeHelperSpy.mConfig.automaticRules = new ArrayMap<>(); |
| 1037 | |
| 1038 | // set previous version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1039 | ByteArrayOutputStream baos = writeXmlAndPurge(5); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1040 | XmlPullParser parser = Xml.newPullParser(); |
| 1041 | parser.setInput(new BufferedInputStream( |
| 1042 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 1043 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1044 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1045 | |
| 1046 | // check default rules |
| 1047 | ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelperSpy.mConfig.automaticRules; |
| 1048 | assertTrue(rules.size() != 0); |
| 1049 | for (String defaultId : ZenModeConfig.DEFAULT_RULE_IDS) { |
| 1050 | assertTrue(rules.containsKey(defaultId)); |
| 1051 | } |
| 1052 | |
| 1053 | setupZenConfigMaintained(); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | @Test |
| 1058 | public void testReadXmlAllDisabledRulesResetDefaultRules() throws Exception { |
| 1059 | setupZenConfig(); |
| 1060 | |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 1061 | // all automatic zen rules are disabled on upgrade (and default rules don't already exist) |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1062 | // so rules should be overriden by default rules |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 1063 | ArrayMap<String, ZenModeConfig.ZenRule> disabledAutoRule = new ArrayMap<>(); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1064 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 1065 | final ScheduleInfo weeknights = new ScheduleInfo(); |
| 1066 | customRule.enabled = false; |
| 1067 | customRule.name = "Custom Rule"; |
| 1068 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1069 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(weeknights); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 1070 | customRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 1071 | disabledAutoRule.put("customRule", customRule); |
| 1072 | mZenModeHelperSpy.mConfig.automaticRules = disabledAutoRule; |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1073 | |
| 1074 | // set previous version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1075 | ByteArrayOutputStream baos = writeXmlAndPurge(5); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1076 | XmlPullParser parser = Xml.newPullParser(); |
| 1077 | parser.setInput(new BufferedInputStream( |
| 1078 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 1079 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1080 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1081 | |
| 1082 | // check default rules |
| 1083 | ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelperSpy.mConfig.automaticRules; |
| 1084 | assertTrue(rules.size() != 0); |
| 1085 | for (String defaultId : ZenModeConfig.DEFAULT_RULE_IDS) { |
| 1086 | assertTrue(rules.containsKey(defaultId)); |
| 1087 | } |
| 1088 | assertFalse(rules.containsKey("customRule")); |
| 1089 | |
| 1090 | setupZenConfigMaintained(); |
| 1091 | } |
| 1092 | |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1093 | @Test |
| 1094 | public void testReadXmlOnlyOneDefaultRuleExists() throws Exception { |
| 1095 | setupZenConfig(); |
| 1096 | |
| 1097 | // all automatic zen rules are disabled on upgrade and only one default rule exists |
| 1098 | // so rules should be overriden to the default rules |
| 1099 | ArrayMap<String, ZenModeConfig.ZenRule> automaticRules = new ArrayMap<>(); |
| 1100 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 1101 | final ScheduleInfo customRuleInfo = new ScheduleInfo(); |
| 1102 | customRule.enabled = false; |
| 1103 | customRule.name = "Custom Rule"; |
| 1104 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1105 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(customRuleInfo); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 1106 | customRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 1107 | customRule.zenPolicy = new ZenPolicy.Builder() |
| 1108 | .allowReminders(true) |
| 1109 | .allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE) |
| 1110 | .build(); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1111 | automaticRules.put("customRule", customRule); |
| 1112 | |
| 1113 | ZenModeConfig.ZenRule defaultScheduleRule = new ZenModeConfig.ZenRule(); |
| 1114 | final ScheduleInfo defaultScheduleRuleInfo = new ScheduleInfo(); |
| 1115 | defaultScheduleRule.enabled = false; |
| 1116 | defaultScheduleRule.name = "Default Schedule Rule"; |
| 1117 | defaultScheduleRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1118 | defaultScheduleRule.conditionId = ZenModeConfig.toScheduleConditionId( |
| 1119 | defaultScheduleRuleInfo); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 1120 | customRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1121 | defaultScheduleRule.id = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID; |
| 1122 | automaticRules.put(ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, defaultScheduleRule); |
| 1123 | |
| 1124 | mZenModeHelperSpy.mConfig.automaticRules = automaticRules; |
| 1125 | |
| 1126 | // set previous version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1127 | ByteArrayOutputStream baos = writeXmlAndPurge(5); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1128 | XmlPullParser parser = Xml.newPullParser(); |
| 1129 | parser.setInput(new BufferedInputStream( |
| 1130 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 1131 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1132 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1133 | |
| 1134 | // check default rules |
| 1135 | ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelperSpy.mConfig.automaticRules; |
| 1136 | assertTrue(rules.size() != 0); |
| 1137 | for (String defaultId : ZenModeConfig.DEFAULT_RULE_IDS) { |
| 1138 | assertTrue(rules.containsKey(defaultId)); |
| 1139 | } |
| 1140 | assertFalse(rules.containsKey("customRule")); |
| 1141 | |
| 1142 | setupZenConfigMaintained(); |
| 1143 | } |
| 1144 | |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1145 | @Test |
| 1146 | public void testReadXmlDefaultRulesExist() throws Exception { |
| 1147 | setupZenConfig(); |
| 1148 | |
| 1149 | // Default rules exist so rules should not be overridden by defaults |
| 1150 | ArrayMap<String, ZenModeConfig.ZenRule> automaticRules = new ArrayMap<>(); |
| 1151 | ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); |
| 1152 | final ScheduleInfo customRuleInfo = new ScheduleInfo(); |
| 1153 | customRule.enabled = false; |
| 1154 | customRule.name = "Custom Rule"; |
| 1155 | customRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1156 | customRule.conditionId = ZenModeConfig.toScheduleConditionId(customRuleInfo); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 1157 | customRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 1158 | customRule.zenPolicy = new ZenPolicy.Builder() |
| 1159 | .allowReminders(true) |
| 1160 | .allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE) |
| 1161 | .build(); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1162 | automaticRules.put("customRule", customRule); |
| 1163 | |
| 1164 | ZenModeConfig.ZenRule defaultScheduleRule = new ZenModeConfig.ZenRule(); |
| 1165 | final ScheduleInfo defaultScheduleRuleInfo = new ScheduleInfo(); |
| 1166 | defaultScheduleRule.enabled = false; |
| 1167 | defaultScheduleRule.name = "Default Schedule Rule"; |
| 1168 | defaultScheduleRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1169 | defaultScheduleRule.conditionId = ZenModeConfig.toScheduleConditionId( |
| 1170 | defaultScheduleRuleInfo); |
| 1171 | defaultScheduleRule.id = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID; |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 1172 | defaultScheduleRule.zenPolicy = new ZenPolicy.Builder() |
| 1173 | .allowEvents(true) |
| 1174 | .allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE) |
| 1175 | .build(); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1176 | automaticRules.put(ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, defaultScheduleRule); |
| 1177 | |
| 1178 | ZenModeConfig.ZenRule defaultEventRule = new ZenModeConfig.ZenRule(); |
| 1179 | final ScheduleInfo defaultEventRuleInfo = new ScheduleInfo(); |
| 1180 | defaultEventRule.enabled = false; |
| 1181 | defaultEventRule.name = "Default Event Rule"; |
| 1182 | defaultEventRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1183 | defaultEventRule.conditionId = ZenModeConfig.toScheduleConditionId( |
| 1184 | defaultEventRuleInfo); |
| 1185 | defaultEventRule.id = ZenModeConfig.EVENTS_DEFAULT_RULE_ID; |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 1186 | defaultScheduleRule.zenPolicy = new ZenPolicy.Builder() |
| 1187 | .allowAlarms(false) |
| 1188 | .allowMedia(false) |
| 1189 | .allowRepeatCallers(false) |
| 1190 | .build(); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1191 | automaticRules.put(ZenModeConfig.EVENTS_DEFAULT_RULE_ID, defaultEventRule); |
| 1192 | |
| 1193 | mZenModeHelperSpy.mConfig.automaticRules = automaticRules; |
| 1194 | |
| 1195 | // set previous version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1196 | ByteArrayOutputStream baos = writeXmlAndPurge(5); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1197 | XmlPullParser parser = Xml.newPullParser(); |
| 1198 | parser.setInput(new BufferedInputStream( |
| 1199 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 1200 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 1201 | mZenModeHelperSpy.readXml(parser, false, UserHandle.USER_ALL); |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 1202 | |
| 1203 | // check default rules |
| 1204 | ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelperSpy.mConfig.automaticRules; |
| 1205 | assertTrue(rules.size() != 0); |
| 1206 | for (String defaultId : ZenModeConfig.DEFAULT_RULE_IDS) { |
| 1207 | assertTrue(rules.containsKey(defaultId)); |
| 1208 | } |
| 1209 | assertTrue(rules.containsKey("customRule")); |
| 1210 | |
| 1211 | setupZenConfigMaintained(); |
| 1212 | } |
| 1213 | |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 1214 | @Test |
| 1215 | public void testCountdownConditionSubscription() throws Exception { |
| 1216 | ZenModeConfig config = new ZenModeConfig(); |
| 1217 | mZenModeHelperSpy.mConfig = config; |
| 1218 | mZenModeHelperSpy.mConditions.evaluateConfig(mZenModeHelperSpy.mConfig, null, true); |
| 1219 | assertEquals(0, mZenModeHelperSpy.mConditions.mSubscriptions.size()); |
| 1220 | |
| 1221 | mZenModeHelperSpy.mConfig.manualRule = new ZenModeConfig.ZenRule(); |
| 1222 | Uri conditionId = ZenModeConfig.toCountdownConditionId(9000000, false); |
| 1223 | mZenModeHelperSpy.mConfig.manualRule.conditionId = conditionId; |
| 1224 | mZenModeHelperSpy.mConfig.manualRule.component = new ComponentName("android", |
| 1225 | CountdownConditionProvider.class.getName()); |
| 1226 | mZenModeHelperSpy.mConfig.manualRule.condition = new Condition(conditionId, "", "", "", 0, |
| 1227 | Condition.STATE_TRUE, Condition.FLAG_RELEVANT_NOW); |
| 1228 | mZenModeHelperSpy.mConfig.manualRule.enabled = true; |
| 1229 | ZenModeConfig originalConfig = mZenModeHelperSpy.mConfig.copy(); |
| 1230 | |
| 1231 | mZenModeHelperSpy.mConditions.evaluateConfig(mZenModeHelperSpy.mConfig, null, true); |
| 1232 | |
| 1233 | assertEquals(true, ZenModeConfig.isValidCountdownConditionId(conditionId)); |
| 1234 | assertEquals(originalConfig, mZenModeHelperSpy.mConfig); |
| 1235 | assertEquals(1, mZenModeHelperSpy.mConditions.mSubscriptions.size()); |
| 1236 | } |
| 1237 | |
Beverly | ac7733c | 2018-10-01 11:40:06 -0400 | [diff] [blame] | 1238 | @Test |
| 1239 | public void testEmptyDefaultRulesMap() { |
| 1240 | ZenModeConfig config = new ZenModeConfig(); |
| 1241 | config.automaticRules = new ArrayMap<>(); |
| 1242 | mZenModeHelperSpy.mConfig = config; |
| 1243 | mZenModeHelperSpy.updateDefaultZenRules(); // shouldn't throw null pointer |
| 1244 | } |
| 1245 | |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 1246 | @Test |
| 1247 | public void testDoNotUpdateModifiedDefaultAutoRule() { |
| 1248 | // mDefaultConfig is set to default config in setup by getDefaultConfigParser |
| 1249 | when(mContext.checkCallingPermission(anyString())) |
| 1250 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 1251 | |
| 1252 | // shouldn't update rule that's been modified |
| 1253 | ZenModeConfig.ZenRule updatedDefaultRule = new ZenModeConfig.ZenRule(); |
| 1254 | updatedDefaultRule.modified = true; |
| 1255 | updatedDefaultRule.enabled = false; |
| 1256 | updatedDefaultRule.creationTime = 0; |
| 1257 | updatedDefaultRule.id = SCHEDULE_DEFAULT_RULE_ID; |
| 1258 | updatedDefaultRule.name = "Schedule Default Rule"; |
| 1259 | updatedDefaultRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1260 | updatedDefaultRule.conditionId = ZenModeConfig.toScheduleConditionId(new ScheduleInfo()); |
| 1261 | updatedDefaultRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
| 1262 | |
| 1263 | ArrayMap<String, ZenModeConfig.ZenRule> autoRules = new ArrayMap<>(); |
| 1264 | autoRules.put(SCHEDULE_DEFAULT_RULE_ID, updatedDefaultRule); |
| 1265 | mZenModeHelperSpy.mConfig.automaticRules = autoRules; |
| 1266 | |
| 1267 | mZenModeHelperSpy.updateDefaultZenRules(); |
| 1268 | assertEquals(updatedDefaultRule, |
| 1269 | mZenModeHelperSpy.mConfig.automaticRules.get(SCHEDULE_DEFAULT_RULE_ID)); |
| 1270 | } |
| 1271 | |
| 1272 | @Test |
| 1273 | public void testDoNotUpdateEnabledDefaultAutoRule() { |
| 1274 | // mDefaultConfig is set to default config in setup by getDefaultConfigParser |
| 1275 | when(mContext.checkCallingPermission(anyString())) |
| 1276 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 1277 | |
| 1278 | // shouldn't update the rule that's enabled |
| 1279 | ZenModeConfig.ZenRule updatedDefaultRule = new ZenModeConfig.ZenRule(); |
| 1280 | updatedDefaultRule.enabled = true; |
| 1281 | updatedDefaultRule.modified = false; |
| 1282 | updatedDefaultRule.creationTime = 0; |
| 1283 | updatedDefaultRule.id = SCHEDULE_DEFAULT_RULE_ID; |
| 1284 | updatedDefaultRule.name = "Schedule Default Rule"; |
| 1285 | updatedDefaultRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1286 | updatedDefaultRule.conditionId = ZenModeConfig.toScheduleConditionId(new ScheduleInfo()); |
| 1287 | updatedDefaultRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
| 1288 | |
| 1289 | ArrayMap<String, ZenModeConfig.ZenRule> autoRules = new ArrayMap<>(); |
| 1290 | autoRules.put(SCHEDULE_DEFAULT_RULE_ID, updatedDefaultRule); |
| 1291 | mZenModeHelperSpy.mConfig.automaticRules = autoRules; |
| 1292 | |
| 1293 | mZenModeHelperSpy.updateDefaultZenRules(); |
| 1294 | assertEquals(updatedDefaultRule, |
| 1295 | mZenModeHelperSpy.mConfig.automaticRules.get(SCHEDULE_DEFAULT_RULE_ID)); |
| 1296 | } |
| 1297 | |
| 1298 | @Test |
| 1299 | public void testUpdateDefaultAutoRule() { |
| 1300 | // mDefaultConfig is set to default config in setup by getDefaultConfigParser |
| 1301 | final String defaultRuleName = "rule name test"; |
| 1302 | when(mContext.checkCallingPermission(anyString())) |
| 1303 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 1304 | |
| 1305 | // will update rule that is not enabled and modified |
| 1306 | ZenModeConfig.ZenRule customDefaultRule = new ZenModeConfig.ZenRule(); |
| 1307 | customDefaultRule.enabled = false; |
| 1308 | customDefaultRule.modified = false; |
| 1309 | customDefaultRule.creationTime = 0; |
| 1310 | customDefaultRule.id = SCHEDULE_DEFAULT_RULE_ID; |
| 1311 | customDefaultRule.name = "Schedule Default Rule"; |
| 1312 | customDefaultRule.zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1313 | customDefaultRule.conditionId = ZenModeConfig.toScheduleConditionId(new ScheduleInfo()); |
| 1314 | customDefaultRule.component = new ComponentName("android", "ScheduleConditionProvider"); |
| 1315 | |
| 1316 | ArrayMap<String, ZenModeConfig.ZenRule> autoRules = new ArrayMap<>(); |
| 1317 | autoRules.put(SCHEDULE_DEFAULT_RULE_ID, customDefaultRule); |
| 1318 | mZenModeHelperSpy.mConfig.automaticRules = autoRules; |
| 1319 | |
| 1320 | mZenModeHelperSpy.updateDefaultZenRules(); |
| 1321 | ZenModeConfig.ZenRule ruleAfterUpdating = |
| 1322 | mZenModeHelperSpy.mConfig.automaticRules.get(SCHEDULE_DEFAULT_RULE_ID); |
| 1323 | assertEquals(customDefaultRule.enabled, ruleAfterUpdating.enabled); |
| 1324 | assertEquals(customDefaultRule.modified, ruleAfterUpdating.modified); |
| 1325 | assertEquals(customDefaultRule.id, ruleAfterUpdating.id); |
| 1326 | assertEquals(customDefaultRule.conditionId, ruleAfterUpdating.conditionId); |
| 1327 | assertFalse(Objects.equals(defaultRuleName, ruleAfterUpdating.name)); // update name |
| 1328 | } |
| 1329 | |
Beverly | a0d07ab | 2018-11-28 16:49:23 -0500 | [diff] [blame] | 1330 | @Test |
| 1331 | public void testAddAutomaticZenRule() { |
| 1332 | AutomaticZenRule zenRule = new AutomaticZenRule("name", |
| 1333 | new ComponentName("android", "ScheduleConditionProvider"), |
| 1334 | ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), |
| 1335 | NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); |
| 1336 | String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test"); |
| 1337 | |
| 1338 | assertTrue(id != null); |
| 1339 | ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id); |
| 1340 | assertTrue(ruleInConfig != null); |
| 1341 | assertEquals(zenRule.isEnabled(), ruleInConfig.enabled); |
| 1342 | assertEquals(zenRule.isModified(), ruleInConfig.modified); |
| 1343 | assertEquals(zenRule.getConditionId(), ruleInConfig.conditionId); |
| 1344 | assertEquals(NotificationManager.zenModeFromInterruptionFilter( |
| 1345 | zenRule.getInterruptionFilter(), -1), ruleInConfig.zenMode); |
| 1346 | assertEquals(zenRule.getName(), ruleInConfig.name); |
| 1347 | } |
| 1348 | |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1349 | private void setupZenConfig() { |
| 1350 | mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 1351 | mZenModeHelperSpy.mConfig.allowAlarms = false; |
| 1352 | mZenModeHelperSpy.mConfig.allowMedia = false; |
| 1353 | mZenModeHelperSpy.mConfig.allowSystem = false; |
| 1354 | mZenModeHelperSpy.mConfig.allowReminders = true; |
| 1355 | mZenModeHelperSpy.mConfig.allowCalls = true; |
| 1356 | mZenModeHelperSpy.mConfig.allowMessages = true; |
| 1357 | mZenModeHelperSpy.mConfig.allowEvents = true; |
| 1358 | mZenModeHelperSpy.mConfig.allowRepeatCallers= true; |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1359 | mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE; |
Beverly | aa5ca96 | 2018-08-10 15:42:56 -0400 | [diff] [blame] | 1360 | mZenModeHelperSpy.mConfig.manualRule = null; |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | private void setupZenConfigMaintained() { |
| 1364 | // config is still the same as when it was setup (setupZenConfig) |
| 1365 | assertFalse(mZenModeHelperSpy.mConfig.allowAlarms); |
| 1366 | assertFalse(mZenModeHelperSpy.mConfig.allowMedia); |
| 1367 | assertFalse(mZenModeHelperSpy.mConfig.allowSystem); |
| 1368 | assertTrue(mZenModeHelperSpy.mConfig.allowReminders); |
| 1369 | assertTrue(mZenModeHelperSpy.mConfig.allowCalls); |
| 1370 | assertTrue(mZenModeHelperSpy.mConfig.allowMessages); |
| 1371 | assertTrue(mZenModeHelperSpy.mConfig.allowEvents); |
| 1372 | assertTrue(mZenModeHelperSpy.mConfig.allowRepeatCallers); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 1373 | assertEquals(SUPPRESSED_EFFECT_BADGE, mZenModeHelperSpy.mConfig.suppressedVisualEffects); |
| 1374 | } |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 1375 | |
| 1376 | /** |
| 1377 | * Wrapper to use XmlPullParser as XmlResourceParser for Resources.getXml() |
| 1378 | */ |
| 1379 | final class XmlResourceParserImpl implements XmlResourceParser { |
| 1380 | private XmlPullParser parser; |
| 1381 | |
| 1382 | public XmlResourceParserImpl(XmlPullParser parser) { |
| 1383 | this.parser = parser; |
| 1384 | } |
| 1385 | |
| 1386 | public int getEventType() throws XmlPullParserException { |
| 1387 | return parser.getEventType(); |
| 1388 | } |
| 1389 | |
| 1390 | @Override |
| 1391 | public void setFeature(String name, boolean state) throws XmlPullParserException { |
| 1392 | parser.setFeature(name, state); |
| 1393 | } |
| 1394 | |
| 1395 | @Override |
| 1396 | public boolean getFeature(String name) { |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | @Override |
| 1401 | public void setProperty(String name, Object value) throws XmlPullParserException { |
| 1402 | parser.setProperty(name, value); |
| 1403 | } |
| 1404 | |
| 1405 | @Override |
| 1406 | public Object getProperty(String name) { |
| 1407 | return parser.getProperty(name); |
| 1408 | } |
| 1409 | |
| 1410 | @Override |
| 1411 | public void setInput(Reader in) throws XmlPullParserException { |
| 1412 | parser.setInput(in); |
| 1413 | } |
| 1414 | |
| 1415 | @Override |
| 1416 | public void setInput(InputStream inputStream, String inputEncoding) |
| 1417 | throws XmlPullParserException { |
| 1418 | parser.setInput(inputStream, inputEncoding); |
| 1419 | } |
| 1420 | |
| 1421 | @Override |
| 1422 | public String getInputEncoding() { |
| 1423 | return parser.getInputEncoding(); |
| 1424 | } |
| 1425 | |
| 1426 | @Override |
| 1427 | public void defineEntityReplacementText(String entityName, String replacementText) |
| 1428 | throws XmlPullParserException { |
| 1429 | parser.defineEntityReplacementText(entityName, replacementText); |
| 1430 | } |
| 1431 | |
| 1432 | @Override |
| 1433 | public int getNamespaceCount(int depth) throws XmlPullParserException { |
| 1434 | return parser.getNamespaceCount(depth); |
| 1435 | } |
| 1436 | |
| 1437 | @Override |
| 1438 | public String getNamespacePrefix(int pos) throws XmlPullParserException { |
| 1439 | return parser.getNamespacePrefix(pos); |
| 1440 | } |
| 1441 | |
| 1442 | @Override |
| 1443 | public String getNamespaceUri(int pos) throws XmlPullParserException { |
| 1444 | return parser.getNamespaceUri(pos); |
| 1445 | } |
| 1446 | |
| 1447 | @Override |
| 1448 | public String getNamespace(String prefix) { |
| 1449 | return parser.getNamespace(prefix); |
| 1450 | } |
| 1451 | |
| 1452 | @Override |
| 1453 | public int getDepth() { |
| 1454 | return parser.getDepth(); |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | public String getPositionDescription() { |
| 1459 | return parser.getPositionDescription(); |
| 1460 | } |
| 1461 | |
| 1462 | @Override |
| 1463 | public int getLineNumber() { |
| 1464 | return parser.getLineNumber(); |
| 1465 | } |
| 1466 | |
| 1467 | @Override |
| 1468 | public int getColumnNumber() { |
| 1469 | return parser.getColumnNumber(); |
| 1470 | } |
| 1471 | |
| 1472 | @Override |
| 1473 | public boolean isWhitespace() throws XmlPullParserException { |
| 1474 | return parser.isWhitespace(); |
| 1475 | } |
| 1476 | |
| 1477 | @Override |
| 1478 | public String getText() { |
| 1479 | return parser.getText(); |
| 1480 | } |
| 1481 | |
| 1482 | @Override |
| 1483 | public char[] getTextCharacters(int[] holderForStartAndLength) { |
| 1484 | return parser.getTextCharacters(holderForStartAndLength); |
| 1485 | } |
| 1486 | |
| 1487 | @Override |
| 1488 | public String getNamespace() { |
| 1489 | return parser.getNamespace(); |
| 1490 | } |
| 1491 | |
| 1492 | @Override |
| 1493 | public String getName() { |
| 1494 | return parser.getName(); |
| 1495 | } |
| 1496 | |
| 1497 | @Override |
| 1498 | public String getPrefix() { |
| 1499 | return parser.getPrefix(); |
| 1500 | } |
| 1501 | |
| 1502 | @Override |
| 1503 | public boolean isEmptyElementTag() throws XmlPullParserException { |
| 1504 | return false; |
| 1505 | } |
| 1506 | |
| 1507 | @Override |
| 1508 | public int getAttributeCount() { |
| 1509 | return parser.getAttributeCount(); |
| 1510 | } |
| 1511 | |
| 1512 | public int next() throws IOException, XmlPullParserException { |
| 1513 | return parser.next(); |
| 1514 | } |
| 1515 | |
| 1516 | @Override |
| 1517 | public int nextToken() throws XmlPullParserException, IOException { |
| 1518 | return parser.next(); |
| 1519 | } |
| 1520 | |
| 1521 | @Override |
| 1522 | public void require(int type, String namespace, String name) |
| 1523 | throws XmlPullParserException, IOException { |
| 1524 | parser.require(type, namespace, name); |
| 1525 | } |
| 1526 | |
| 1527 | @Override |
| 1528 | public String nextText() throws XmlPullParserException, IOException { |
| 1529 | return parser.nextText(); |
| 1530 | } |
| 1531 | |
| 1532 | @Override |
| 1533 | public String getAttributeNamespace(int index) { |
| 1534 | return ""; |
| 1535 | } |
| 1536 | |
| 1537 | @Override |
| 1538 | public String getAttributeName(int index) { |
| 1539 | return parser.getAttributeName(index); |
| 1540 | } |
| 1541 | |
| 1542 | @Override |
| 1543 | public String getAttributePrefix(int index) { |
| 1544 | return parser.getAttributePrefix(index); |
| 1545 | } |
| 1546 | |
| 1547 | @Override |
| 1548 | public String getAttributeType(int index) { |
| 1549 | return parser.getAttributeType(index); |
| 1550 | } |
| 1551 | |
| 1552 | @Override |
| 1553 | public boolean isAttributeDefault(int index) { |
| 1554 | return parser.isAttributeDefault(index); |
| 1555 | } |
| 1556 | |
| 1557 | @Override |
| 1558 | public String getAttributeValue(int index) { |
| 1559 | return parser.getAttributeValue(index); |
| 1560 | } |
| 1561 | |
| 1562 | @Override |
| 1563 | public String getAttributeValue(String namespace, String name) { |
| 1564 | return parser.getAttributeValue(namespace, name); |
| 1565 | } |
| 1566 | |
| 1567 | @Override |
| 1568 | public int getAttributeNameResource(int index) { |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | @Override |
| 1573 | public int getAttributeListValue(String namespace, String attribute, String[] options, |
| 1574 | int defaultValue) { |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
| 1578 | @Override |
| 1579 | public boolean getAttributeBooleanValue(String namespace, String attribute, |
| 1580 | boolean defaultValue) { |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | @Override |
| 1585 | public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) { |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
| 1589 | @Override |
| 1590 | public int getAttributeIntValue(String namespace, String attribute, int defaultValue) { |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | @Override |
| 1595 | public int getAttributeUnsignedIntValue(String namespace, String attribute, |
| 1596 | int defaultValue) { |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | @Override |
| 1601 | public float getAttributeFloatValue(String namespace, String attribute, |
| 1602 | float defaultValue) { |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | @Override |
| 1607 | public int getAttributeListValue(int index, String[] options, int defaultValue) { |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | @Override |
| 1612 | public boolean getAttributeBooleanValue(int index, boolean defaultValue) { |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
| 1616 | @Override |
| 1617 | public int getAttributeResourceValue(int index, int defaultValue) { |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | @Override |
| 1622 | public int getAttributeIntValue(int index, int defaultValue) { |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | @Override |
| 1627 | public int getAttributeUnsignedIntValue(int index, int defaultValue) { |
| 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | @Override |
| 1632 | public float getAttributeFloatValue(int index, float defaultValue) { |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | @Override |
| 1637 | public String getIdAttribute() { |
| 1638 | return null; |
| 1639 | } |
| 1640 | |
| 1641 | @Override |
| 1642 | public String getClassAttribute() { |
| 1643 | return null; |
| 1644 | } |
| 1645 | |
| 1646 | @Override |
| 1647 | public int getIdAttributeResourceValue(int defaultValue) { |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | @Override |
| 1652 | public int getStyleAttribute() { |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | @Override |
| 1657 | public void close() { |
| 1658 | } |
| 1659 | |
| 1660 | @Override |
| 1661 | public int nextTag() throws IOException, XmlPullParserException { |
| 1662 | return parser.nextTag(); |
| 1663 | } |
| 1664 | } |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 1665 | } |