Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app.cts; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.provider.Telephony.Threads; |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 25 | import android.service.notification.StatusBarNotification; |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 26 | import android.test.AndroidTestCase; |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 27 | import android.util.Log; |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 28 | |
Chenjie Luo | 15f32be | 2014-09-25 11:28:03 -0700 | [diff] [blame] | 29 | import com.android.cts.app.stub.R; |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 30 | |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 31 | |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 32 | public class NotificationManagerTest extends AndroidTestCase { |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 33 | final String TAG = NotificationManagerTest.class.getSimpleName(); |
| 34 | final boolean DEBUG = false; |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 35 | |
| 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 Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 43 | // clear the deck so that our getActiveNotifications results are predictable |
| 44 | mNotificationManager.cancelAll(); |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected void tearDown() throws Exception { |
| 49 | super.tearDown(); |
| 50 | mNotificationManager.cancelAll(); |
| 51 | } |
| 52 | |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 53 | public void testNotify() { |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 54 | mNotificationManager.cancelAll(); |
| 55 | |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 56 | final int id = 1; |
| 57 | sendNotification(id, R.drawable.black); |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 58 | // 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 Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 72 | public void testCancel() { |
| 73 | final int id = 9; |
| 74 | sendNotification(id, R.drawable.black); |
| 75 | mNotificationManager.cancel(id); |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 76 | |
Dan Sandler | ba1c89f | 2015-09-17 10:38:00 -0400 | [diff] [blame] | 77 | if (!checkNotificationExistence(id, /*shouldExist=*/ false)) { |
| 78 | fail("canceled notification was still alive, id=" + id); |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 79 | } |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 82 | public void testCancelAll() { |
| 83 | sendNotification(1, R.drawable.black); |
| 84 | sendNotification(2, R.drawable.blue); |
| 85 | sendNotification(3, R.drawable.yellow); |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 86 | |
| 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 Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 95 | mNotificationManager.cancelAll(); |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 96 | |
| 97 | StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications(); |
| 98 | assertTrue("notification list was not empty after cancelAll", sbns.length == 0); |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private void sendNotification(final int id, final int icon) { |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 102 | 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 Wren | 367d72e | 2015-06-11 10:18:39 -0400 | [diff] [blame] | 109 | 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 Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 116 | mNotificationManager.notify(id, notification); |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 117 | |
Dan Sandler | ba1c89f | 2015-09-17 10:38:00 -0400 | [diff] [blame] | 118 | |
| 119 | if (!checkNotificationExistence(id, /*shouldExist=*/ true)) { |
| 120 | fail("couldn't find posted notification id=" + id); |
Dan Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 121 | } |
Dan Sandler | ba1c89f | 2015-09-17 10:38:00 -0400 | [diff] [blame] | 122 | } |
| 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 Sandler | f1194ca | 2015-04-15 11:46:29 -0400 | [diff] [blame] | 144 | } |
Scott Su | 501c063 | 2009-04-23 21:52:09 -0700 | [diff] [blame] | 145 | } |