Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 com.android.systemui; |
| 18 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertNull; |
| 21 | |
| 22 | import static org.junit.Assert.assertFalse; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.mockito.Mockito.mock; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 25 | import static org.mockito.Mockito.verify; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 26 | import static org.mockito.Mockito.when; |
| 27 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 28 | import android.annotation.UserIdInt; |
| 29 | import android.app.Notification; |
| 30 | import android.app.NotificationManager; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.UserHandle; |
| 33 | import android.service.notification.StatusBarNotification; |
| 34 | import android.support.test.filters.SmallTest; |
| 35 | import android.support.test.runner.AndroidJUnit4; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 36 | import android.widget.RemoteViews; |
| 37 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 38 | import com.android.internal.messages.nano.SystemMessageProto; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 39 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 40 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 41 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 46 | import org.mockito.ArgumentCaptor; |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 47 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 48 | @SmallTest |
| 49 | @RunWith(AndroidJUnit4.class) |
| 50 | public class ForegroundServiceControllerTest extends SysuiTestCase { |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 51 | @UserIdInt private static final int USERID_ONE = 10; // UserManagerService.MIN_USER_ID; |
| 52 | @UserIdInt private static final int USERID_TWO = USERID_ONE + 1; |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 53 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 54 | private ForegroundServiceController mFsc; |
| 55 | private ForegroundServiceNotificationListener mListener; |
| 56 | private NotificationEntryListener mEntryListener; |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 60 | mFsc = new ForegroundServiceController(); |
| 61 | NotificationEntryManager notificationEntryManager = mock(NotificationEntryManager.class); |
| 62 | mListener = new ForegroundServiceNotificationListener( |
| 63 | mContext, mFsc, notificationEntryManager); |
| 64 | ArgumentCaptor<NotificationEntryListener> entryListenerCaptor = |
| 65 | ArgumentCaptor.forClass(NotificationEntryListener.class); |
| 66 | verify(notificationEntryManager).addNotificationEntryListener( |
| 67 | entryListenerCaptor.capture()); |
| 68 | mEntryListener = entryListenerCaptor.getValue(); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testAppOpsCRUD() { |
| 73 | // no crash on remove that doesn't exist |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 74 | mFsc.onAppOpChanged(9, 1000, "pkg1", false); |
| 75 | assertNull(mFsc.getAppOps(0, "pkg1")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 76 | |
| 77 | // multiuser & multipackage |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 78 | mFsc.onAppOpChanged(8, 50, "pkg1", true); |
| 79 | mFsc.onAppOpChanged(1, 60, "pkg3", true); |
| 80 | mFsc.onAppOpChanged(7, 500000, "pkg2", true); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 81 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 82 | assertEquals(1, mFsc.getAppOps(0, "pkg1").size()); |
| 83 | assertTrue(mFsc.getAppOps(0, "pkg1").contains(8)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 84 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 85 | assertEquals(1, mFsc.getAppOps(UserHandle.getUserId(500000), "pkg2").size()); |
| 86 | assertTrue(mFsc.getAppOps(UserHandle.getUserId(500000), "pkg2").contains(7)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 87 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 88 | assertEquals(1, mFsc.getAppOps(0, "pkg3").size()); |
| 89 | assertTrue(mFsc.getAppOps(0, "pkg3").contains(1)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 90 | |
| 91 | // multiple ops for the same package |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 92 | mFsc.onAppOpChanged(9, 50, "pkg1", true); |
| 93 | mFsc.onAppOpChanged(5, 50, "pkg1", true); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 94 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 95 | assertEquals(3, mFsc.getAppOps(0, "pkg1").size()); |
| 96 | assertTrue(mFsc.getAppOps(0, "pkg1").contains(8)); |
| 97 | assertTrue(mFsc.getAppOps(0, "pkg1").contains(9)); |
| 98 | assertTrue(mFsc.getAppOps(0, "pkg1").contains(5)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 99 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 100 | assertEquals(1, mFsc.getAppOps(UserHandle.getUserId(500000), "pkg2").size()); |
| 101 | assertTrue(mFsc.getAppOps(UserHandle.getUserId(500000), "pkg2").contains(7)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 102 | |
| 103 | // remove one of the multiples |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 104 | mFsc.onAppOpChanged(9, 50, "pkg1", false); |
| 105 | assertEquals(2, mFsc.getAppOps(0, "pkg1").size()); |
| 106 | assertTrue(mFsc.getAppOps(0, "pkg1").contains(8)); |
| 107 | assertTrue(mFsc.getAppOps(0, "pkg1").contains(5)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 108 | |
| 109 | // remove last op |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 110 | mFsc.onAppOpChanged(1, 60, "pkg3", false); |
| 111 | assertNull(mFsc.getAppOps(0, "pkg3")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 115 | public void testDisclosurePredicate() { |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 116 | StatusBarNotification sbn_user1_app1 = makeMockSBN(USERID_ONE, "com.example.app1", |
| 117 | 5000, "monkeys", Notification.FLAG_AUTO_CANCEL); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 118 | StatusBarNotification sbn_user1_disclosure = makeMockSBN(USERID_ONE, "android", |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 119 | SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICES, |
| 120 | null, Notification.FLAG_NO_CLEAR); |
| 121 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 122 | assertTrue(mFsc.isDisclosureNotification(sbn_user1_disclosure)); |
| 123 | assertFalse(mFsc.isDisclosureNotification(sbn_user1_app1)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Test |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 127 | public void testNeedsDisclosureAfterRemovingUnrelatedNotification() { |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 128 | final String PKG1 = "com.example.app100"; |
| 129 | |
| 130 | StatusBarNotification sbn_user1_app1 = makeMockSBN(USERID_ONE, PKG1, |
| 131 | 5000, "monkeys", Notification.FLAG_AUTO_CANCEL); |
| 132 | StatusBarNotification sbn_user1_app1_fg = makeMockFgSBN(USERID_ONE, PKG1); |
| 133 | |
| 134 | // first add a normal notification |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 135 | entryAdded(sbn_user1_app1, NotificationManager.IMPORTANCE_DEFAULT); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 136 | // nothing required yet |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 137 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 138 | // now the app starts a fg service |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 139 | entryAdded(makeMockDisclosure(USERID_ONE, new String[]{PKG1}), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 140 | NotificationManager.IMPORTANCE_DEFAULT); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 141 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); // should be required! |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 142 | // add the fg notification |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 143 | entryAdded(sbn_user1_app1_fg, NotificationManager.IMPORTANCE_DEFAULT); |
| 144 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); // app1 has got it covered |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 145 | // remove the boring notification |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 146 | entryRemoved(sbn_user1_app1); |
| 147 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); // app1 has STILL got it covered |
| 148 | entryRemoved(sbn_user1_app1_fg); |
| 149 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); // should be required! |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testSimpleAddRemove() { |
| 154 | final String PKG1 = "com.example.app1"; |
| 155 | final String PKG2 = "com.example.app2"; |
| 156 | |
| 157 | StatusBarNotification sbn_user1_app1 = makeMockSBN(USERID_ONE, PKG1, |
| 158 | 5000, "monkeys", Notification.FLAG_AUTO_CANCEL); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 159 | entryAdded(sbn_user1_app1, NotificationManager.IMPORTANCE_DEFAULT); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 160 | |
| 161 | // no services are "running" |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 162 | entryAdded(makeMockDisclosure(USERID_ONE, null), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 163 | NotificationManager.IMPORTANCE_DEFAULT); |
| 164 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 165 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 166 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 167 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 168 | entryUpdated(makeMockDisclosure(USERID_ONE, new String[]{PKG1}), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 169 | NotificationManager.IMPORTANCE_DEFAULT); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 170 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); // should be required! |
| 171 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 172 | |
| 173 | // switch to different package |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 174 | entryUpdated(makeMockDisclosure(USERID_ONE, new String[]{PKG2}), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 175 | NotificationManager.IMPORTANCE_DEFAULT); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 176 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 177 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 178 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 179 | entryUpdated(makeMockDisclosure(USERID_TWO, new String[]{PKG1}), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 180 | NotificationManager.IMPORTANCE_DEFAULT); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 181 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 182 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_TWO)); // finally user2 needs one too |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 183 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 184 | entryUpdated(makeMockDisclosure(USERID_ONE, new String[]{PKG2, PKG1}), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 185 | NotificationManager.IMPORTANCE_DEFAULT); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 186 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 187 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 188 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 189 | entryRemoved(makeMockDisclosure(USERID_ONE, null /*unused*/)); |
| 190 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 191 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 192 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 193 | entryRemoved(makeMockDisclosure(USERID_TWO, null /*unused*/)); |
| 194 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 195 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Test |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 199 | public void testDisclosureBasic() { |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 200 | final String PKG1 = "com.example.app0"; |
| 201 | |
| 202 | StatusBarNotification sbn_user1_app1 = makeMockSBN(USERID_ONE, PKG1, |
| 203 | 5000, "monkeys", Notification.FLAG_AUTO_CANCEL); |
| 204 | StatusBarNotification sbn_user1_app1_fg = makeMockFgSBN(USERID_ONE, PKG1); |
| 205 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 206 | entryAdded(sbn_user1_app1, NotificationManager.IMPORTANCE_DEFAULT); // not fg |
| 207 | entryAdded(makeMockDisclosure(USERID_ONE, new String[]{PKG1}), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 208 | NotificationManager.IMPORTANCE_DEFAULT); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 209 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); // should be required! |
| 210 | entryAdded(sbn_user1_app1_fg, NotificationManager.IMPORTANCE_DEFAULT); |
| 211 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); // app1 has got it covered |
| 212 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 213 | |
| 214 | // let's take out the other notification and see what happens. |
| 215 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 216 | entryRemoved(sbn_user1_app1); |
| 217 | assertFalse( |
| 218 | mFsc.isDisclosureNeededForUser(USERID_ONE)); // still covered by sbn_user1_app1_fg |
| 219 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 220 | |
| 221 | // let's attempt to downgrade the notification from FLAG_FOREGROUND and see what we get |
| 222 | StatusBarNotification sbn_user1_app1_fg_sneaky = makeMockFgSBN(USERID_ONE, PKG1); |
| 223 | sbn_user1_app1_fg_sneaky.getNotification().flags = 0; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 224 | entryUpdated(sbn_user1_app1_fg_sneaky, |
| 225 | NotificationManager.IMPORTANCE_DEFAULT); |
| 226 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); // should be required! |
| 227 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 228 | |
| 229 | // ok, ok, we'll put it back |
| 230 | sbn_user1_app1_fg_sneaky.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 231 | entryUpdated(sbn_user1_app1_fg, NotificationManager.IMPORTANCE_DEFAULT); |
| 232 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 233 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 234 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 235 | entryRemoved(sbn_user1_app1_fg_sneaky); |
| 236 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); // should be required! |
| 237 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 238 | |
| 239 | // now let's test an upgrade |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 240 | entryAdded(sbn_user1_app1, NotificationManager.IMPORTANCE_DEFAULT); |
| 241 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 242 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 243 | sbn_user1_app1.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 244 | entryUpdated(sbn_user1_app1, |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 245 | NotificationManager.IMPORTANCE_DEFAULT); // this is now a fg notification |
| 246 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 247 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
| 248 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 249 | |
| 250 | // remove it, make sure we're out of compliance again |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 251 | entryRemoved(sbn_user1_app1); // was fg, should return true |
| 252 | entryRemoved(sbn_user1_app1); |
| 253 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
| 254 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 255 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 256 | // importance upgrade |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 257 | entryAdded(sbn_user1_app1_fg, NotificationManager.IMPORTANCE_MIN); |
| 258 | assertTrue(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 259 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 260 | sbn_user1_app1.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 261 | entryUpdated(sbn_user1_app1_fg, |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 262 | NotificationManager.IMPORTANCE_DEFAULT); // this is now a fg notification |
| 263 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 264 | // finally, let's turn off the service |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 265 | entryAdded(makeMockDisclosure(USERID_ONE, null), |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 266 | NotificationManager.IMPORTANCE_DEFAULT); |
| 267 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 268 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_ONE)); |
| 269 | assertFalse(mFsc.isDisclosureNeededForUser(USERID_TWO)); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 272 | @Test |
Julia Reynolds | af78a79 | 2018-03-06 14:27:00 -0500 | [diff] [blame] | 273 | public void testOverlayPredicate() { |
| 274 | StatusBarNotification sbn_user1_app1 = makeMockSBN(USERID_ONE, "com.example.app1", |
| 275 | 5000, "monkeys", Notification.FLAG_AUTO_CANCEL); |
| 276 | StatusBarNotification sbn_user1_overlay = makeMockSBN(USERID_ONE, "android", |
| 277 | 0, "AlertWindowNotification", Notification.FLAG_NO_CLEAR); |
| 278 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 279 | assertTrue(mFsc.isSystemAlertNotification(sbn_user1_overlay)); |
| 280 | assertFalse(mFsc.isSystemAlertNotification(sbn_user1_app1)); |
Julia Reynolds | af78a79 | 2018-03-06 14:27:00 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Test |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 284 | public void testStdLayoutBasic() { |
| 285 | final String PKG1 = "com.example.app0"; |
| 286 | |
| 287 | StatusBarNotification sbn_user1_app1 = makeMockFgSBN(USERID_ONE, PKG1, 0, true); |
| 288 | sbn_user1_app1.getNotification().flags = 0; |
| 289 | StatusBarNotification sbn_user1_app1_fg = makeMockFgSBN(USERID_ONE, PKG1, 1, true); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 290 | entryAdded(sbn_user1_app1, NotificationManager.IMPORTANCE_MIN); // not fg |
| 291 | assertTrue(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); // should be required! |
| 292 | entryAdded(sbn_user1_app1_fg, NotificationManager.IMPORTANCE_MIN); |
| 293 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); // app1 has got it covered |
| 294 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "otherpkg")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 295 | // let's take out the non-fg notification and see what happens. |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 296 | entryRemoved(sbn_user1_app1); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 297 | // still covered by sbn_user1_app1_fg |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 298 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); |
| 299 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "anyPkg")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 300 | |
| 301 | // let's attempt to downgrade the notification from FLAG_FOREGROUND and see what we get |
| 302 | StatusBarNotification sbn_user1_app1_fg_sneaky = makeMockFgSBN(USERID_ONE, PKG1, 1, true); |
| 303 | sbn_user1_app1_fg_sneaky.getNotification().flags = 0; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 304 | entryUpdated(sbn_user1_app1_fg_sneaky, NotificationManager.IMPORTANCE_MIN); |
| 305 | assertTrue(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); // should be required! |
| 306 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "anything")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 307 | // ok, ok, we'll put it back |
| 308 | sbn_user1_app1_fg_sneaky.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 309 | entryUpdated(sbn_user1_app1_fg, NotificationManager.IMPORTANCE_MIN); |
| 310 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); |
| 311 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "whatever")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 312 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 313 | entryRemoved(sbn_user1_app1_fg_sneaky); |
| 314 | assertTrue(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); // should be required! |
| 315 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "a")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 316 | |
| 317 | // let's try a custom layout |
| 318 | sbn_user1_app1_fg_sneaky = makeMockFgSBN(USERID_ONE, PKG1, 1, false); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 319 | entryUpdated(sbn_user1_app1_fg_sneaky, NotificationManager.IMPORTANCE_MIN); |
| 320 | assertTrue(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); // should be required! |
| 321 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "anything")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 322 | // now let's test an upgrade (non fg to fg) |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 323 | entryAdded(sbn_user1_app1, NotificationManager.IMPORTANCE_MIN); |
| 324 | assertTrue(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); |
| 325 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, "b")); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 326 | sbn_user1_app1.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 327 | entryUpdated(sbn_user1_app1, |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 328 | NotificationManager.IMPORTANCE_MIN); // this is now a fg notification |
| 329 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 330 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, PKG1)); |
| 331 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 332 | |
| 333 | // remove it, make sure we're out of compliance again |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 334 | entryRemoved(sbn_user1_app1); // was fg, should return true |
| 335 | entryRemoved(sbn_user1_app1); |
| 336 | assertFalse(mFsc.isSystemAlertWarningNeeded(USERID_TWO, PKG1)); |
| 337 | assertTrue(mFsc.isSystemAlertWarningNeeded(USERID_ONE, PKG1)); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 338 | } |
| 339 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 340 | private StatusBarNotification makeMockSBN(int userid, String pkg, int id, String tag, |
| 341 | int flags) { |
| 342 | final Notification n = mock(Notification.class); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 343 | n.extras = new Bundle(); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 344 | n.flags = flags; |
| 345 | return makeMockSBN(userid, pkg, id, tag, n); |
| 346 | } |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 347 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 348 | private StatusBarNotification makeMockSBN(int userid, String pkg, int id, String tag, |
| 349 | Notification n) { |
| 350 | final StatusBarNotification sbn = mock(StatusBarNotification.class); |
| 351 | when(sbn.getNotification()).thenReturn(n); |
| 352 | when(sbn.getId()).thenReturn(id); |
| 353 | when(sbn.getPackageName()).thenReturn(pkg); |
Julia Reynolds | af78a79 | 2018-03-06 14:27:00 -0500 | [diff] [blame] | 354 | when(sbn.getTag()).thenReturn(tag); |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 355 | when(sbn.getUserId()).thenReturn(userid); |
| 356 | when(sbn.getUser()).thenReturn(new UserHandle(userid)); |
| 357 | when(sbn.getKey()).thenReturn("MOCK:"+userid+"|"+pkg+"|"+id+"|"+tag); |
| 358 | return sbn; |
| 359 | } |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 360 | |
| 361 | private StatusBarNotification makeMockFgSBN(int userid, String pkg, int id, |
| 362 | boolean usesStdLayout) { |
| 363 | StatusBarNotification sbn = |
| 364 | makeMockSBN(userid, pkg, id, "foo", Notification.FLAG_FOREGROUND_SERVICE); |
| 365 | if (usesStdLayout) { |
| 366 | sbn.getNotification().contentView = null; |
| 367 | sbn.getNotification().headsUpContentView = null; |
| 368 | sbn.getNotification().bigContentView = null; |
| 369 | } else { |
| 370 | sbn.getNotification().contentView = mock(RemoteViews.class); |
| 371 | } |
| 372 | return sbn; |
| 373 | } |
| 374 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 375 | private StatusBarNotification makeMockFgSBN(int userid, String pkg) { |
| 376 | return makeMockSBN(userid, pkg, 1000, "foo", Notification.FLAG_FOREGROUND_SERVICE); |
| 377 | } |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 378 | |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 379 | private StatusBarNotification makeMockDisclosure(int userid, String[] pkgs) { |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 380 | final Notification n = mock(Notification.class); |
| 381 | n.flags = Notification.FLAG_ONGOING_EVENT; |
| 382 | final Bundle extras = new Bundle(); |
| 383 | if (pkgs != null) extras.putStringArray(Notification.EXTRA_FOREGROUND_APPS, pkgs); |
| 384 | n.extras = extras; |
Dan Sandler | 9830b5a | 2017-10-26 23:27:57 -0400 | [diff] [blame] | 385 | n.when = System.currentTimeMillis() - 10000; // ten seconds ago |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 386 | final StatusBarNotification sbn = makeMockSBN(userid, "android", |
| 387 | SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICES, |
| 388 | null, n); |
| 389 | sbn.getNotification().extras = extras; |
| 390 | return sbn; |
| 391 | } |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 392 | |
| 393 | private void entryRemoved(StatusBarNotification notification) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 394 | mEntryListener.onEntryRemoved(new NotificationEntry(notification), |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 395 | null, false); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | private void entryAdded(StatusBarNotification notification, int importance) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 399 | NotificationEntry entry = new NotificationEntry(notification); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 400 | entry.importance = importance; |
| 401 | mEntryListener.onPendingEntryAdded(entry); |
| 402 | } |
| 403 | |
| 404 | private void entryUpdated(StatusBarNotification notification, int importance) { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 405 | NotificationEntry entry = new NotificationEntry(notification); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 406 | entry.importance = importance; |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 407 | mEntryListener.onPostEntryUpdated(entry); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 408 | } |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 409 | } |