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