yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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.statusbar.phone; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.Instrumentation; |
| 21 | import android.app.Notification; |
| 22 | import android.os.UserHandle; |
| 23 | import android.view.View; |
| 24 | import android.service.notification.StatusBarNotification; |
| 25 | import android.support.test.InstrumentationRegistry; |
| 26 | import android.support.test.filters.SmallTest; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper; |
| 29 | |
| 30 | import com.android.systemui.R; |
| 31 | import com.android.systemui.SysuiTestCase; |
| 32 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
| 33 | import com.android.systemui.statusbar.NotificationData; |
| 34 | import com.android.systemui.statusbar.StatusBarIconView; |
| 35 | import com.android.systemui.statusbar.notification.VisualStabilityManager; |
| 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Rule; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.junit.MockitoJUnit; |
| 43 | import org.mockito.junit.MockitoRule; |
| 44 | |
| 45 | import static junit.framework.Assert.assertNotNull; |
| 46 | import static junit.framework.Assert.assertNull; |
| 47 | import static junit.framework.Assert.assertTrue; |
| 48 | import static junit.framework.Assert.assertFalse; |
| 49 | |
| 50 | import static org.junit.Assert.assertEquals; |
| 51 | import static org.mockito.Mockito.mock; |
| 52 | import static org.mockito.Mockito.when; |
| 53 | |
| 54 | @SmallTest |
| 55 | @RunWith(AndroidTestingRunner.class) |
| 56 | @TestableLooper.RunWithLooper |
| 57 | public class HeadsUpManagerPhoneTest extends SysuiTestCase { |
| 58 | @Rule public MockitoRule rule = MockitoJUnit.rule(); |
| 59 | |
| 60 | private static final String TEST_PACKAGE_NAME = "test"; |
| 61 | private static final int TEST_UID = 0; |
| 62 | |
| 63 | private HeadsUpManagerPhone mHeadsUpManager; |
| 64 | |
| 65 | private NotificationData.Entry mEntry; |
| 66 | private StatusBarNotification mSbn; |
| 67 | |
| 68 | @Mock private NotificationGroupManager mGroupManager; |
| 69 | @Mock private View mStatusBarWindowView; |
| 70 | @Mock private StatusBar mBar; |
| 71 | @Mock private ExpandableNotificationRow mRow; |
| 72 | @Mock private VisualStabilityManager mVSManager; |
| 73 | |
| 74 | @Before |
| 75 | public void setUp() { |
| 76 | when(mVSManager.isReorderingAllowed()).thenReturn(true); |
| 77 | |
| 78 | mHeadsUpManager = new HeadsUpManagerPhone( |
| 79 | mContext, mStatusBarWindowView, mGroupManager, mBar, mVSManager); |
| 80 | |
| 81 | Notification.Builder n = new Notification.Builder(mContext, "") |
| 82 | .setSmallIcon(R.drawable.ic_person) |
| 83 | .setContentTitle("Title") |
| 84 | .setContentText("Text"); |
| 85 | mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, |
| 86 | 0, n.build(), new UserHandle(ActivityManager.getCurrentUser()), null, 0); |
| 87 | |
| 88 | mEntry = new NotificationData.Entry(mSbn); |
| 89 | mEntry.row = mRow; |
| 90 | mEntry.expandedIcon = mock(StatusBarIconView.class); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testBasicOperations() { |
| 95 | // Check the initial state. |
| 96 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 97 | assertNull(mHeadsUpManager.getTopEntry()); |
| 98 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 99 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 100 | |
| 101 | // Add a notification. |
| 102 | mHeadsUpManager.showNotification(mEntry); |
| 103 | |
| 104 | assertEquals(mEntry, mHeadsUpManager.getEntry(mEntry.key)); |
| 105 | assertEquals(mEntry, mHeadsUpManager.getTopEntry()); |
| 106 | assertEquals(1, mHeadsUpManager.getAllEntries().count()); |
| 107 | assertTrue(mHeadsUpManager.hasHeadsUpNotifications()); |
| 108 | |
| 109 | // Update the notification. |
| 110 | mHeadsUpManager.updateNotification(mEntry, false); |
| 111 | |
| 112 | assertEquals(mEntry, mHeadsUpManager.getEntry(mEntry.key)); |
| 113 | assertEquals(mEntry, mHeadsUpManager.getTopEntry()); |
| 114 | assertEquals(1, mHeadsUpManager.getAllEntries().count()); |
| 115 | assertTrue(mHeadsUpManager.hasHeadsUpNotifications()); |
| 116 | |
| 117 | // Try to remove but defer, since the notification is currenlt visible on display. |
| 118 | mHeadsUpManager.removeNotification(mEntry.key, false /* ignoreEarliestRemovalTime */); |
| 119 | |
| 120 | assertEquals(mEntry, mHeadsUpManager.getEntry(mEntry.key)); |
| 121 | assertEquals(mEntry, mHeadsUpManager.getTopEntry()); |
| 122 | assertEquals(1, mHeadsUpManager.getAllEntries().count()); |
| 123 | assertTrue(mHeadsUpManager.hasHeadsUpNotifications()); |
| 124 | |
| 125 | // Remove forcibly with ignoreEarliestRemovalTime = true. |
| 126 | mHeadsUpManager.removeNotification(mEntry.key, true /* ignoreEarliestRemovalTime */); |
| 127 | |
| 128 | // Check the initial state. |
| 129 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 130 | assertNull(mHeadsUpManager.getTopEntry()); |
| 131 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 132 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testsTimeoutRemoval() { |
| 137 | mHeadsUpManager.removeMinimumDisplayTimeForTesting(); |
| 138 | |
| 139 | // Check the initial state. |
| 140 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 141 | assertNull(mHeadsUpManager.getTopEntry()); |
| 142 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 143 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 144 | |
| 145 | Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 146 | |
| 147 | // Run the code on the main thready, not to run an async operations. |
| 148 | instrumentation.runOnMainSync(() -> { |
| 149 | // Add a notification. |
| 150 | mHeadsUpManager.showNotification(mEntry); |
| 151 | |
| 152 | // Ensure the head up is visible before timeout. |
| 153 | assertNotNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 154 | assertNotNull(mHeadsUpManager.getTopEntry()); |
| 155 | assertEquals(1, mHeadsUpManager.getAllEntries().count()); |
| 156 | assertTrue(mHeadsUpManager.hasHeadsUpNotifications()); |
| 157 | }); |
| 158 | // Wait for the async operations, which removes the heads up notification. |
| 159 | waitForIdleSync(); |
| 160 | |
| 161 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 162 | assertNull(mHeadsUpManager.getTopEntry()); |
| 163 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 164 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void releaseImmediately() { |
| 169 | // Check the initial state. |
| 170 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 171 | assertNull(mHeadsUpManager.getTopEntry()); |
| 172 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 173 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 174 | |
| 175 | // Add a notification. |
| 176 | mHeadsUpManager.showNotification(mEntry); |
| 177 | |
| 178 | assertEquals(mEntry, mHeadsUpManager.getEntry(mEntry.key)); |
| 179 | assertEquals(mEntry, mHeadsUpManager.getTopEntry()); |
| 180 | assertEquals(1, mHeadsUpManager.getAllEntries().count()); |
| 181 | assertTrue(mHeadsUpManager.hasHeadsUpNotifications()); |
| 182 | |
| 183 | // Remove but defer, since the notification is visible on display. |
| 184 | mHeadsUpManager.releaseImmediately(mEntry.key); |
| 185 | |
| 186 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 187 | assertNull(mHeadsUpManager.getTopEntry()); |
| 188 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 189 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void releaseAllImmediately() { |
| 194 | // Check the initial state. |
| 195 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 196 | assertNull(mHeadsUpManager.getTopEntry()); |
| 197 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 198 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 199 | |
| 200 | // Add a notification. |
| 201 | mHeadsUpManager.showNotification(mEntry); |
| 202 | |
| 203 | assertEquals(mEntry, mHeadsUpManager.getEntry(mEntry.key)); |
| 204 | assertEquals(mEntry, mHeadsUpManager.getTopEntry()); |
| 205 | assertEquals(1, mHeadsUpManager.getAllEntries().count()); |
| 206 | assertTrue(mHeadsUpManager.hasHeadsUpNotifications()); |
| 207 | |
| 208 | // Remove but defer, since the notification is visible on display. |
| 209 | mHeadsUpManager.releaseAllImmediately(); |
| 210 | |
| 211 | assertNull(mHeadsUpManager.getEntry(mEntry.key)); |
| 212 | assertNull(mHeadsUpManager.getTopEntry()); |
| 213 | assertEquals(0, mHeadsUpManager.getAllEntries().count()); |
| 214 | assertFalse(mHeadsUpManager.hasHeadsUpNotifications()); |
| 215 | } |
| 216 | } |