1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.app.cts; 18 19 import android.app.AutomaticZenRule; 20 import android.app.NotificationManager; 21 import android.content.ComponentName; 22 import android.net.Uri; 23 import android.os.Parcel; 24 import android.service.notification.ZenPolicy; 25 import android.test.AndroidTestCase; 26 27 public class AutomaticZenRuleTest extends AndroidTestCase { 28 29 private final String mName = "name"; 30 private final ComponentName mOwner = new ComponentName("pkg", "cls"); 31 private final ComponentName mConfigActivity = new ComponentName("pkg", "act"); 32 private final ZenPolicy mPolicy = new ZenPolicy.Builder().allowAlarms(true).build(); 33 private final Uri mConditionId = new Uri.Builder().scheme("scheme") 34 .authority("authority") 35 .appendPath("path") 36 .appendPath("test") 37 .build(); 38 private final int mInterruptionFilter = NotificationManager.INTERRUPTION_FILTER_NONE; 39 private final boolean mEnabled = true; 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 } 45 46 public void testDescribeContents() { 47 final int expected = 0; 48 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConditionId, 49 mInterruptionFilter, mEnabled); 50 assertEquals(expected, rule.describeContents()); 51 } 52 53 public void testWriteToParcel() { 54 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 55 mPolicy, mInterruptionFilter, mEnabled); 56 Parcel parcel = Parcel.obtain(); 57 rule.writeToParcel(parcel, 0); 58 parcel.setDataPosition(0); 59 AutomaticZenRule rule1 = new AutomaticZenRule(parcel); 60 assertEquals(mName, rule1.getName()); 61 assertEquals(mOwner, rule1.getOwner()); 62 assertEquals(mConditionId, rule1.getConditionId()); 63 assertEquals(mInterruptionFilter, rule1.getInterruptionFilter()); 64 assertEquals(mEnabled, rule1.isEnabled()); 65 assertEquals(mPolicy, rule1.getZenPolicy()); 66 assertEquals(mConfigActivity, rule1.getConfigurationActivity()); 67 68 rule.setName(null); 69 parcel = Parcel.obtain(); 70 rule.writeToParcel(parcel, 0); 71 parcel.setDataPosition(0); 72 rule1 = new AutomaticZenRule(parcel); 73 assertNull(rule1.getName()); 74 } 75 76 public void testSetConditionId() { 77 final Uri newConditionId = new Uri.Builder().scheme("scheme") 78 .authority("authority2") 79 .appendPath("3path") 80 .appendPath("test4") 81 .build(); 82 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 83 mPolicy, mInterruptionFilter, mEnabled); 84 rule.setConditionId(newConditionId); 85 assertEquals(newConditionId, rule.getConditionId()); 86 } 87 88 public void testSetEnabled() { 89 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 90 mPolicy, mInterruptionFilter, mEnabled); 91 rule.setEnabled(!mEnabled); 92 assertEquals(!mEnabled, rule.isEnabled()); 93 } 94 95 public void testSetInterruptionFilter() { 96 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 97 mPolicy, mInterruptionFilter, mEnabled); 98 for (int i = NotificationManager.INTERRUPTION_FILTER_UNKNOWN; 99 i <= NotificationManager.INTERRUPTION_FILTER_ALARMS; i++) { 100 rule.setInterruptionFilter(i); 101 assertEquals(i, rule.getInterruptionFilter()); 102 } 103 } 104 105 public void testSetName() { 106 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 107 mPolicy, mInterruptionFilter, mEnabled); 108 rule.setName(mName + "new"); 109 assertEquals(mName + "new", rule.getName()); 110 } 111 112 public void testSetConfigurationActivity() { 113 ComponentName newConfigActivity = new ComponentName("pkg", "new!"); 114 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 115 mPolicy, mInterruptionFilter, mEnabled); 116 rule.setConfigurationActivity(newConfigActivity); 117 assertEquals(newConfigActivity, rule.getConfigurationActivity()); 118 } 119 120 public void testCreateRuleWithZenPolicy() { 121 ZenPolicy.Builder builder = new ZenPolicy.Builder(); 122 ZenPolicy policy = builder.build(); 123 builder.allowAlarms(true); 124 AutomaticZenRule rule = new AutomaticZenRule(mName, mOwner, mConfigActivity, mConditionId, 125 policy, mInterruptionFilter, mEnabled); 126 assertEquals(mInterruptionFilter, rule.getInterruptionFilter()); 127 assertEquals(rule.getZenPolicy(), policy); 128 } 129 } 130