blob: fbb306039548bcbd91b70d0b1c33375ddb5f3eaa [file] [log] [blame]
Scott Su501c0632009-04-23 21:52:09 -07001/*
2 * Copyright (C) 2008 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.app.cts;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.content.Intent;
24import android.provider.Telephony.Threads;
Dan Sandlerf1194ca2015-04-15 11:46:29 -040025import android.service.notification.StatusBarNotification;
Scott Su501c0632009-04-23 21:52:09 -070026import android.test.AndroidTestCase;
Dan Sandlerf1194ca2015-04-15 11:46:29 -040027import android.util.Log;
Scott Su501c0632009-04-23 21:52:09 -070028
Chenjie Luo15f32be2014-09-25 11:28:03 -070029import com.android.cts.app.stub.R;
Scott Su501c0632009-04-23 21:52:09 -070030
Scott Su501c0632009-04-23 21:52:09 -070031
Scott Su501c0632009-04-23 21:52:09 -070032public class NotificationManagerTest extends AndroidTestCase {
Dan Sandlerf1194ca2015-04-15 11:46:29 -040033 final String TAG = NotificationManagerTest.class.getSimpleName();
34 final boolean DEBUG = false;
Scott Su501c0632009-04-23 21:52:09 -070035
36 private NotificationManager mNotificationManager;
37
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 mNotificationManager = (NotificationManager) mContext.getSystemService(
42 Context.NOTIFICATION_SERVICE);
Dan Sandlerf1194ca2015-04-15 11:46:29 -040043 // clear the deck so that our getActiveNotifications results are predictable
44 mNotificationManager.cancelAll();
Scott Su501c0632009-04-23 21:52:09 -070045 }
46
47 @Override
48 protected void tearDown() throws Exception {
49 super.tearDown();
50 mNotificationManager.cancelAll();
51 }
52
Scott Su501c0632009-04-23 21:52:09 -070053 public void testNotify() {
Dan Sandlerf1194ca2015-04-15 11:46:29 -040054 mNotificationManager.cancelAll();
55
Scott Su501c0632009-04-23 21:52:09 -070056 final int id = 1;
57 sendNotification(id, R.drawable.black);
Dan Sandlerf1194ca2015-04-15 11:46:29 -040058 // test updating the same notification
59 sendNotification(id, R.drawable.blue);
60 sendNotification(id, R.drawable.yellow);
61
62 // assume that sendNotification tested to make sure individual notifications were present
63 StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications();
64 for (StatusBarNotification sbn : sbns) {
65 if (sbn.getId() != id) {
66 fail("we got back other notifications besides the one we posted: "
67 + sbn.getKey());
68 }
69 }
Scott Su501c0632009-04-23 21:52:09 -070070 }
71
Scott Su501c0632009-04-23 21:52:09 -070072 public void testCancel() {
73 final int id = 9;
74 sendNotification(id, R.drawable.black);
75 mNotificationManager.cancel(id);
Dan Sandlerf1194ca2015-04-15 11:46:29 -040076
Dan Sandlerba1c89f2015-09-17 10:38:00 -040077 if (!checkNotificationExistence(id, /*shouldExist=*/ false)) {
78 fail("canceled notification was still alive, id=" + id);
Dan Sandlerf1194ca2015-04-15 11:46:29 -040079 }
Scott Su501c0632009-04-23 21:52:09 -070080 }
81
Scott Su501c0632009-04-23 21:52:09 -070082 public void testCancelAll() {
83 sendNotification(1, R.drawable.black);
84 sendNotification(2, R.drawable.blue);
85 sendNotification(3, R.drawable.yellow);
Dan Sandlerf1194ca2015-04-15 11:46:29 -040086
87 if (DEBUG) {
88 Log.d(TAG, "posted 3 notifications, here they are: ");
89 StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications();
90 for (StatusBarNotification sbn : sbns) {
91 Log.d(TAG, " " + sbn);
92 }
93 Log.d(TAG, "about to cancel...");
94 }
Scott Su501c0632009-04-23 21:52:09 -070095 mNotificationManager.cancelAll();
Dan Sandlerf1194ca2015-04-15 11:46:29 -040096
97 StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications();
98 assertTrue("notification list was not empty after cancelAll", sbns.length == 0);
Scott Su501c0632009-04-23 21:52:09 -070099 }
100
101 private void sendNotification(final int id, final int icon) {
Scott Su501c0632009-04-23 21:52:09 -0700102 final Intent intent = new Intent(Intent.ACTION_MAIN, Threads.CONTENT_URI);
103
104 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
105 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
106 intent.setAction(Intent.ACTION_MAIN);
107
108 final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Chris Wren367d72e2015-06-11 10:18:39 -0400109 final Notification notification = new Notification.Builder(mContext)
110 .setSmallIcon(icon)
111 .setWhen(System.currentTimeMillis())
112 .setContentTitle("notify#" + id)
113 .setContentText("This is #" + id + "notification ")
114 .setContentIntent(pendingIntent)
115 .build();
Scott Su501c0632009-04-23 21:52:09 -0700116 mNotificationManager.notify(id, notification);
Scott Su501c0632009-04-23 21:52:09 -0700117
Dan Sandlerba1c89f2015-09-17 10:38:00 -0400118
119 if (!checkNotificationExistence(id, /*shouldExist=*/ true)) {
120 fail("couldn't find posted notification id=" + id);
Dan Sandlerf1194ca2015-04-15 11:46:29 -0400121 }
Dan Sandlerba1c89f2015-09-17 10:38:00 -0400122 }
123
124 private boolean checkNotificationExistence(int id, boolean shouldExist) {
125 // notification is a bit asynchronous so it may take a few ms to appear in getActiveNotifications()
126 // we will check for it for up to 200ms before giving up
127 boolean found = false;
128 for (int tries=3; tries-->0;) {
129 final StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications();
130 for (StatusBarNotification sbn : sbns) {
131 if (sbn.getId() == id) {
132 found = true;
133 break;
134 }
135 }
136 if (found == shouldExist) break;
137 try {
138 Thread.sleep(100);
139 } catch (InterruptedException ex) {
140 // pass
141 }
142 }
143 return found == shouldExist;
Dan Sandlerf1194ca2015-04-15 11:46:29 -0400144 }
Scott Su501c0632009-04-23 21:52:09 -0700145}