NotificationCompatTest.java revision 159a5dfb07b6836ffdbf4099b8ca55b890b5109f
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.app.Notification; 26import android.support.test.filters.SmallTest; 27import android.support.test.runner.AndroidJUnit4; 28import android.support.v4.BaseInstrumentationTestCase; 29import android.support.v4.os.BuildCompat; 30 31import org.junit.Test; 32import org.junit.runner.RunWith; 33 34@RunWith(AndroidJUnit4.class) 35@SmallTest 36public class NotificationCompatTest extends BaseInstrumentationTestCase<TestSupportActivity> { 37 38 public NotificationCompatTest() { 39 super(TestSupportActivity.class); 40 } 41 42 @Test 43 public void testNotificationChannel() throws Throwable { 44 String channelId = "new ID"; 45 Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity()) 46 .setChannel(channelId) 47 .build(); 48 if (BuildCompat.isAtLeastO()) { 49 assertEquals(channelId, NotificationCompat.getChannel(n)); 50 } else { 51 assertNull(NotificationCompat.getChannel(n)); 52 } 53 } 54 55 @Test 56 public void testNotificationActionBuilder_copiesRemoteInputs() throws Throwable { 57 NotificationCompat.Action a = newActionBuilder() 58 .addRemoteInput(new RemoteInput("a", "b", null, false, null)).build(); 59 60 NotificationCompat.Action aCopy = new NotificationCompat.Action.Builder(a).build(); 61 62 assertSame(a.getRemoteInputs()[0], aCopy.getRemoteInputs()[0]); 63 } 64 65 @Test 66 public void testNotificationActionBuilder_copiesAllowGeneratedReplies() throws Throwable { 67 NotificationCompat.Action a = newActionBuilder() 68 .setAllowGeneratedReplies(true).build(); 69 70 NotificationCompat.Action aCopy = new NotificationCompat.Action.Builder(a).build(); 71 72 assertEquals(a.getAllowGeneratedReplies(), aCopy.getAllowGeneratedReplies()); 73 } 74 75 @Test 76 public void testNotificationActionBuilder_defaultAllowGeneratedRepliesTrue() throws Throwable { 77 NotificationCompat.Action a = newActionBuilder().build(); 78 79 assertTrue(a.getAllowGeneratedReplies()); 80 } 81 82 @Test 83 public void testNotificationAction_defaultAllowGeneratedRepliesTrue() throws Throwable { 84 NotificationCompat.Action a = new NotificationCompat.Action(0, null, null); 85 86 assertTrue(a.getAllowGeneratedReplies()); 87 } 88 89 @Test 90 public void testNotificationActionBuilder_setAllowGeneratedRepliesFalse() throws Throwable { 91 NotificationCompat.Action a = newActionBuilder() 92 .setAllowGeneratedReplies(false).build(); 93 94 assertFalse(a.getAllowGeneratedReplies()); 95 } 96 97 private NotificationCompat.Action.Builder newActionBuilder() { 98 return new NotificationCompat.Action.Builder(0, "title", null); 99 } 100} 101