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 static android.app.NotificationManager.IMPORTANCE_DEFAULT; 20 21 import android.app.Notification; 22 import android.app.NotificationChannel; 23 import android.app.NotificationManager; 24 import android.graphics.Color; 25 import android.media.AudioAttributes; 26 import android.net.Uri; 27 import android.os.Parcel; 28 import android.provider.Settings; 29 import android.test.AndroidTestCase; 30 31 public class NotificationChannelTest extends AndroidTestCase { 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 } 37 38 public void testDescribeContents() { 39 final int expected = 0; 40 NotificationChannel channel = 41 new NotificationChannel("1", "1", IMPORTANCE_DEFAULT); 42 assertEquals(expected, channel.describeContents()); 43 } 44 45 public void testConstructor() { 46 NotificationChannel channel = 47 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 48 assertEquals("1", channel.getId()); 49 assertEquals("one", channel.getName()); 50 assertEquals(null, channel.getDescription()); 51 assertEquals(false, channel.canBypassDnd()); 52 assertEquals(false, channel.shouldShowLights()); 53 assertEquals(false, channel.shouldVibrate()); 54 assertEquals(null, channel.getVibrationPattern()); 55 assertEquals(IMPORTANCE_DEFAULT, channel.getImportance()); 56 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, channel.getSound()); 57 assertTrue(channel.canShowBadge()); 58 assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, channel.getAudioAttributes()); 59 assertEquals(null, channel.getGroup()); 60 assertTrue(channel.getLightColor() == 0); 61 } 62 63 public void testWriteToParcel() { 64 NotificationChannel channel = 65 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 66 Parcel parcel = Parcel.obtain(); 67 channel.writeToParcel(parcel, 0); 68 parcel.setDataPosition(0); 69 NotificationChannel channel1 = NotificationChannel.CREATOR.createFromParcel(parcel); 70 assertEquals(channel, channel1); 71 } 72 73 public void testName() { 74 NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); 75 channel.setName("new name"); 76 assertEquals("new name", channel.getName()); 77 } 78 79 public void testDescription() { 80 NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); 81 channel.setDescription("success"); 82 assertEquals("success", channel.getDescription()); 83 } 84 85 public void testLights() { 86 NotificationChannel channel = 87 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 88 channel.enableLights(true); 89 assertTrue(channel.shouldShowLights()); 90 channel.enableLights(false); 91 assertFalse(channel.shouldShowLights()); 92 } 93 94 public void testLightColor() { 95 NotificationChannel channel = 96 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 97 channel.setLightColor(Color.RED); 98 assertFalse(channel.shouldShowLights()); 99 assertEquals(Color.RED, channel.getLightColor()); 100 } 101 102 public void testVibration() { 103 NotificationChannel channel = 104 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 105 channel.enableVibration(true); 106 assertTrue(channel.shouldVibrate()); 107 channel.enableVibration(false); 108 assertFalse(channel.shouldVibrate()); 109 } 110 111 public void testVibrationPattern() { 112 final long[] pattern = new long[] {1, 7, 1, 7, 3}; 113 NotificationChannel channel = 114 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 115 assertNull(channel.getVibrationPattern()); 116 channel.setVibrationPattern(pattern); 117 assertEquals(pattern, channel.getVibrationPattern()); 118 assertTrue(channel.shouldVibrate()); 119 120 channel.setVibrationPattern(new long[]{}); 121 assertEquals(false, channel.shouldVibrate()); 122 123 channel.setVibrationPattern(null); 124 assertEquals(false, channel.shouldVibrate()); 125 } 126 127 public void testSound() { 128 Uri expected = new Uri.Builder().scheme("fruit").appendQueryParameter("favorite", "bananas") 129 .build(); 130 AudioAttributes attributes = new AudioAttributes.Builder() 131 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) 132 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) 133 .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) 134 .build(); 135 NotificationChannel channel = 136 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 137 channel.setSound(expected, attributes); 138 assertEquals(expected, channel.getSound()); 139 assertEquals(attributes, channel.getAudioAttributes()); 140 } 141 142 public void testShowBadge() { 143 NotificationChannel channel = 144 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 145 channel.setShowBadge(true); 146 assertTrue(channel.canShowBadge()); 147 } 148 149 public void testGroup() { 150 NotificationChannel channel = 151 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT); 152 channel.setGroup("banana"); 153 assertEquals("banana", channel.getGroup()); 154 } 155 } 156