NotificationCompatTest.java revision 6ff416f63548465e1c8eb73a1b469b8cf20a46ce
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 17package android.support.v4.app; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertNull; 22import static org.junit.Assert.assertSame; 23import static org.junit.Assert.assertTrue; 24 25import android.annotation.TargetApi; 26import android.app.Notification; 27import android.content.Context; 28import android.os.Bundle; 29import android.support.test.filters.SdkSuppress; 30import android.support.test.filters.SmallTest; 31import android.support.test.runner.AndroidJUnit4; 32import android.support.v4.BaseInstrumentationTestCase; 33import android.support.v4.os.BuildCompat; 34 35import org.junit.Before; 36import org.junit.Test; 37import org.junit.runner.RunWith; 38 39import java.util.ArrayList; 40 41 42@RunWith(AndroidJUnit4.class) 43@SmallTest 44public class NotificationCompatTest extends BaseInstrumentationTestCase<TestSupportActivity> { 45 private static final String TEXT_RESULT_KEY = "text"; 46 private static final String DATA_RESULT_KEY = "data"; 47 48 Context mContext; 49 50 public NotificationCompatTest() { 51 super(TestSupportActivity.class); 52 } 53 54 @Before 55 public void setup() { 56 mContext = mActivityTestRule.getActivity(); 57 } 58 59 @Test 60 public void testBadgeIcon() throws Throwable { 61 int badgeIcon = NotificationCompat.BADGE_ICON_SMALL; 62 Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity()) 63 .setBadgeIconType(badgeIcon) 64 .build(); 65 if (BuildCompat.isAtLeastO()) { 66 assertEquals(badgeIcon, NotificationCompat.getBadgeIconType(n)); 67 } else { 68 assertEquals(NotificationCompat.BADGE_ICON_NONE, 69 NotificationCompat.getBadgeIconType(n)); 70 } 71 } 72 73 @Test 74 public void testTimeout() throws Throwable { 75 long timeout = 23552; 76 Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity()) 77 .setTimeout(timeout) 78 .build(); 79 if (BuildCompat.isAtLeastO()) { 80 assertEquals(timeout, NotificationCompat.getTimeout(n)); 81 } else { 82 assertEquals(0, NotificationCompat.getTimeout(n)); 83 } 84 } 85 86 @Test 87 public void testShortcutId() throws Throwable { 88 String shortcutId = "fgdfg"; 89 Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity()) 90 .setShortcutId(shortcutId) 91 .build(); 92 if (BuildCompat.isAtLeastO()) { 93 assertEquals(shortcutId, NotificationCompat.getShortcutId(n)); 94 } else { 95 assertEquals(null, NotificationCompat.getShortcutId(n)); 96 } 97 } 98 99 @Test 100 public void testNotificationChannel() throws Throwable { 101 String channelId = "new ID"; 102 Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity()) 103 .setChannel(channelId) 104 .build(); 105 if (BuildCompat.isAtLeastO()) { 106 assertEquals(channelId, NotificationCompat.getChannel(n)); 107 } else { 108 assertNull(NotificationCompat.getChannel(n)); 109 } 110 } 111 112 @Test 113 public void testNotificationChannel_assignedFromBuilder() throws Throwable { 114 String channelId = "new ID"; 115 Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity(), channelId) 116 .build(); 117 if (BuildCompat.isAtLeastO()) { 118 assertEquals(channelId, NotificationCompat.getChannel(n)); 119 } else { 120 assertNull(NotificationCompat.getChannel(n)); 121 } 122 } 123 124 @Test 125 public void testNotificationActionBuilder_copiesRemoteInputs() throws Throwable { 126 NotificationCompat.Action a = newActionBuilder() 127 .addRemoteInput(new RemoteInput("a", "b", null, false, null, null)).build(); 128 129 NotificationCompat.Action aCopy = new NotificationCompat.Action.Builder(a).build(); 130 131 assertSame(a.getRemoteInputs()[0], aCopy.getRemoteInputs()[0]); 132 } 133 134 @Test 135 public void testNotificationActionBuilder_copiesAllowGeneratedReplies() throws Throwable { 136 NotificationCompat.Action a = newActionBuilder() 137 .setAllowGeneratedReplies(true).build(); 138 139 NotificationCompat.Action aCopy = new NotificationCompat.Action.Builder(a).build(); 140 141 assertEquals(a.getAllowGeneratedReplies(), aCopy.getAllowGeneratedReplies()); 142 } 143 144 @Test 145 public void testNotificationActionBuilder_defaultAllowGeneratedRepliesTrue() throws Throwable { 146 NotificationCompat.Action a = newActionBuilder().build(); 147 148 assertTrue(a.getAllowGeneratedReplies()); 149 } 150 151 @Test 152 public void testNotificationAction_defaultAllowGeneratedRepliesTrue() throws Throwable { 153 NotificationCompat.Action a = new NotificationCompat.Action(0, null, null); 154 155 assertTrue(a.getAllowGeneratedReplies()); 156 } 157 158 @Test 159 public void testNotificationActionBuilder_setAllowGeneratedRepliesFalse() throws Throwable { 160 NotificationCompat.Action a = newActionBuilder() 161 .setAllowGeneratedReplies(false).build(); 162 163 assertFalse(a.getAllowGeneratedReplies()); 164 } 165 166 @SdkSuppress(minSdkVersion = 17) 167 @TargetApi(17) 168 @Test 169 public void testNotificationWearableExtenderAction_setAllowGeneratedRepliesTrue() 170 throws Throwable { 171 NotificationCompat.Action a = newActionBuilder() 172 .setAllowGeneratedReplies(true).build(); 173 NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender() 174 .addAction(a); 175 Notification notification = newNotificationBuilder().extend(extender).build(); 176 assertTrue(new NotificationCompat.WearableExtender(notification).getActions().get(0) 177 .getAllowGeneratedReplies()); 178 } 179 180 @SdkSuppress(minSdkVersion = 17) 181 @TargetApi(17) 182 @Test 183 public void testNotificationWearableExtenderAction_setAllowGeneratedRepliesFalse() 184 throws Throwable { 185 NotificationCompat.Action a = newActionBuilder() 186 .setAllowGeneratedReplies(false).build(); 187 NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender() 188 .addAction(a); 189 Notification notification = newNotificationBuilder().extend(extender).build(); 190 assertFalse(new NotificationCompat.WearableExtender(notification).getActions().get(0) 191 .getAllowGeneratedReplies()); 192 } 193 194 195 @SdkSuppress(maxSdkVersion = 16) 196 @SmallTest 197 @Test 198 public void testNotificationWearableExtenderAction_noActions() 199 throws Throwable { 200 NotificationCompat.Action a = newActionBuilder() 201 .setAllowGeneratedReplies(true).build(); 202 NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender() 203 .addAction(a); 204 Notification notification = newNotificationBuilder().extend(extender).build(); 205 assertTrue(new NotificationCompat.WearableExtender(notification).getActions().size() == 0); 206 } 207 208 @Test 209 public void testNotificationActionBuilder_setDataOnlyRemoteInput() throws Throwable { 210 NotificationCompat.Action a = newActionBuilder() 211 .addRemoteInput(newDataOnlyRemoteInput()).build(); 212 RemoteInput[] textInputs = a.getRemoteInputs(); 213 assertTrue(textInputs == null || textInputs.length == 0); 214 verifyRemoteInputArrayHasSingleResult(a.getDataOnlyRemoteInputs(), DATA_RESULT_KEY); 215 } 216 217 @Test 218 public void testNotificationActionBuilder_setTextAndDataOnlyRemoteInput() throws Throwable { 219 NotificationCompat.Action a = newActionBuilder() 220 .addRemoteInput(newDataOnlyRemoteInput()) 221 .addRemoteInput(newTextRemoteInput()) 222 .build(); 223 224 verifyRemoteInputArrayHasSingleResult(a.getRemoteInputs(), TEXT_RESULT_KEY); 225 verifyRemoteInputArrayHasSingleResult(a.getDataOnlyRemoteInputs(), DATA_RESULT_KEY); 226 } 227 228 @Test 229 public void testMessage_setAndGetExtras() throws Throwable { 230 String extraKey = "extra_key"; 231 CharSequence extraValue = "extra_value"; 232 NotificationCompat.MessagingStyle.Message m = 233 new NotificationCompat.MessagingStyle.Message("text", 0 /*timestamp */, "sender"); 234 m.getExtras().putCharSequence(extraKey, extraValue); 235 assertEquals(extraValue, m.getExtras().getCharSequence(extraKey)); 236 237 ArrayList<NotificationCompat.MessagingStyle.Message> messages = new ArrayList<>(1); 238 messages.add(m); 239 Bundle[] bundleArray = 240 NotificationCompat.MessagingStyle.Message.getBundleArrayForMessages(messages); 241 assertEquals(1, bundleArray.length); 242 NotificationCompat.MessagingStyle.Message fromBundle = 243 NotificationCompat.MessagingStyle.Message.getMessageFromBundle(bundleArray[0]); 244 assertEquals(extraValue, fromBundle.getExtras().getCharSequence(extraKey)); 245 } 246 247 private static RemoteInput newDataOnlyRemoteInput() { 248 return new RemoteInput.Builder(DATA_RESULT_KEY) 249 .setAllowFreeFormInput(false) 250 .setAllowDataType("mimeType", true) 251 .build(); 252 } 253 254 private static RemoteInput newTextRemoteInput() { 255 return new RemoteInput.Builder(TEXT_RESULT_KEY).build(); // allowFreeForm defaults to true 256 } 257 258 private static void verifyRemoteInputArrayHasSingleResult( 259 RemoteInput[] remoteInputs, String expectedResultKey) { 260 assertTrue(remoteInputs != null && remoteInputs.length == 1); 261 assertEquals(expectedResultKey, remoteInputs[0].getResultKey()); 262 } 263 264 private static NotificationCompat.Action.Builder newActionBuilder() { 265 return new NotificationCompat.Action.Builder(0, "title", null); 266 } 267 268 private NotificationCompat.Builder newNotificationBuilder() { 269 return new NotificationCompat.Builder(mContext) 270 .setSmallIcon(0) 271 .setContentTitle("title") 272 .setContentText("text"); 273 } 274} 275