Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 20 | import static android.view.WindowInsetsController.APPEARANCE_LOW_PROFILE_BARS; |
| 21 | |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.assertTrue; |
| 24 | |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.testing.AndroidTestingRunner; |
| 30 | import android.testing.TestableLooper.RunWithLooper; |
| 31 | import android.view.Display; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewPropertyAnimator; |
| 34 | import android.view.WindowManager; |
| 35 | |
| 36 | import androidx.test.filters.SmallTest; |
| 37 | |
| 38 | import com.android.systemui.SysuiTestCase; |
| 39 | import com.android.systemui.statusbar.CommandQueue; |
| 40 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 41 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
| 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.ArgumentCaptor; |
| 48 | import org.mockito.Captor; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
Daulet Zhanguzin | d0549ae | 2020-01-03 11:08:54 +0000 | [diff] [blame] | 52 | import java.util.Objects; |
| 53 | |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 54 | @SmallTest |
| 55 | @RunWith(AndroidTestingRunner.class) |
| 56 | @RunWithLooper |
| 57 | public class LightsOutNotifControllerTest extends SysuiTestCase { |
| 58 | private static final int LIGHTS_ON = 0; |
| 59 | private static final int LIGHTS_OUT = APPEARANCE_LOW_PROFILE_BARS; |
| 60 | |
| 61 | @Mock private NotificationEntryManager mEntryManager; |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 62 | @Mock private CommandQueue mCommandQueue; |
| 63 | @Mock private WindowManager mWindowManager; |
| 64 | @Mock private Display mDisplay; |
| 65 | |
| 66 | @Captor private ArgumentCaptor<NotificationEntryListener> mListenerCaptor; |
| 67 | @Captor private ArgumentCaptor<CommandQueue.Callbacks> mCallbacksCaptor; |
| 68 | |
| 69 | private View mLightsOutView; |
| 70 | private LightsOutNotifController mLightsOutNotifController; |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 71 | private int mDisplayId; |
| 72 | private NotificationEntryListener mEntryListener; |
| 73 | private CommandQueue.Callbacks mCallbacks; |
| 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
| 77 | MockitoAnnotations.initMocks(this); |
| 78 | mDisplayId = mContext.getDisplayId(); |
| 79 | mLightsOutView = new View(mContext); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 80 | when(mWindowManager.getDefaultDisplay()).thenReturn(mDisplay); |
| 81 | when(mDisplay.getDisplayId()).thenReturn(mDisplayId); |
| 82 | |
| 83 | mLightsOutNotifController = new LightsOutNotifController(mWindowManager, mEntryManager, |
| 84 | mCommandQueue); |
| 85 | mLightsOutNotifController.setLightsOutNotifView(mLightsOutView); |
| 86 | |
| 87 | // Capture the entry listener object so we can simulate events in tests below |
| 88 | verify(mEntryManager).addNotificationEntryListener(mListenerCaptor.capture()); |
Daulet Zhanguzin | d0549ae | 2020-01-03 11:08:54 +0000 | [diff] [blame] | 89 | mEntryListener = Objects.requireNonNull(mListenerCaptor.getValue()); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 90 | |
| 91 | // Capture the callback object so we can simulate callback events in tests below |
| 92 | verify(mCommandQueue).addCallback(mCallbacksCaptor.capture()); |
Daulet Zhanguzin | d0549ae | 2020-01-03 11:08:54 +0000 | [diff] [blame] | 93 | mCallbacks = Objects.requireNonNull(mCallbacksCaptor.getValue()); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testAreLightsOut_lightsOut() { |
| 98 | mCallbacks.onSystemBarAppearanceChanged( |
| 99 | mDisplayId /* display id */, |
| 100 | LIGHTS_OUT /* appearance */, |
| 101 | null /* appearanceRegions */, |
| 102 | false /* navbarColorManagedByIme */); |
| 103 | assertTrue(mLightsOutNotifController.areLightsOut()); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testAreLightsOut_lightsOn() { |
| 108 | mCallbacks.onSystemBarAppearanceChanged( |
| 109 | mDisplayId /* display id */, |
| 110 | LIGHTS_ON /* appearance */, |
| 111 | null /* appearanceRegions */, |
| 112 | false /* navbarColorManagedByIme */); |
| 113 | assertFalse(mLightsOutNotifController.areLightsOut()); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testIsShowingDot_visible() { |
| 118 | mLightsOutView.setVisibility(View.VISIBLE); |
| 119 | mLightsOutView.setAlpha(1.0f); |
| 120 | assertTrue(mLightsOutNotifController.isShowingDot()); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testIsShowingDot_gone() { |
| 125 | mLightsOutView.setVisibility(View.GONE); |
| 126 | mLightsOutView.setAlpha(0f); |
| 127 | assertFalse(mLightsOutNotifController.isShowingDot()); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testLightsOut_withNotifs_onSystemBarAppearanceChanged() { |
| 132 | // GIVEN active visible notifications |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 133 | when(mEntryManager.hasActiveNotifications()).thenReturn(true); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 134 | |
| 135 | // WHEN lights out |
| 136 | mCallbacks.onSystemBarAppearanceChanged( |
| 137 | mDisplayId /* display id */, |
| 138 | LIGHTS_OUT /* appearance */, |
| 139 | null /* appearanceRegions */, |
| 140 | false /* navbarColorManagedByIme */); |
| 141 | |
| 142 | // THEN we should show dot |
| 143 | assertTrue(mLightsOutNotifController.shouldShowDot()); |
| 144 | assertIsShowingDot(true); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testLightsOut_withoutNotifs_onSystemBarAppearanceChanged() { |
| 149 | // GIVEN no active visible notifications |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 150 | when(mEntryManager.hasActiveNotifications()).thenReturn(false); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 151 | |
| 152 | // WHEN lights out |
| 153 | mCallbacks.onSystemBarAppearanceChanged( |
| 154 | mDisplayId /* display id */, |
| 155 | LIGHTS_OUT /* appearance */, |
| 156 | null /* appearanceRegions */, |
| 157 | false /* navbarColorManagedByIme */); |
| 158 | |
| 159 | // THEN we shouldn't show the dot |
| 160 | assertFalse(mLightsOutNotifController.shouldShowDot()); |
| 161 | assertIsShowingDot(false); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testLightsOn_afterLightsOut_onSystemBarAppearanceChanged() { |
| 166 | // GIVEN active visible notifications |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 167 | when(mEntryManager.hasActiveNotifications()).thenReturn(true); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 168 | |
| 169 | // WHEN lights on |
| 170 | mCallbacks.onSystemBarAppearanceChanged( |
| 171 | mDisplayId /* display id */, |
| 172 | LIGHTS_ON /* appearance */, |
| 173 | null /* appearanceRegions */, |
| 174 | false /* navbarColorManagedByIme */); |
| 175 | |
| 176 | // THEN we shouldn't show the dot |
| 177 | assertFalse(mLightsOutNotifController.shouldShowDot()); |
| 178 | assertIsShowingDot(false); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testEntryAdded() { |
| 183 | // GIVEN no visible notifications and lights out |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 184 | when(mEntryManager.hasActiveNotifications()).thenReturn(false); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 185 | mLightsOutNotifController.mAppearance = LIGHTS_OUT; |
| 186 | mLightsOutNotifController.updateLightsOutView(); |
| 187 | assertIsShowingDot(false); |
| 188 | |
| 189 | // WHEN an active notification is added |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 190 | when(mEntryManager.hasActiveNotifications()).thenReturn(true); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 191 | assertTrue(mLightsOutNotifController.shouldShowDot()); |
| 192 | mEntryListener.onNotificationAdded(mock(NotificationEntry.class)); |
| 193 | |
| 194 | // THEN we should see the dot view |
| 195 | assertIsShowingDot(true); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testEntryRemoved() { |
| 200 | // GIVEN a visible notification and lights out |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 201 | when(mEntryManager.hasActiveNotifications()).thenReturn(true); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 202 | mLightsOutNotifController.mAppearance = LIGHTS_OUT; |
| 203 | mLightsOutNotifController.updateLightsOutView(); |
| 204 | assertIsShowingDot(true); |
| 205 | |
| 206 | // WHEN all active notifications are removed |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 207 | when(mEntryManager.hasActiveNotifications()).thenReturn(false); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 208 | assertFalse(mLightsOutNotifController.shouldShowDot()); |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 209 | mEntryListener.onEntryRemoved( |
| 210 | mock(NotificationEntry.class), null, false, REASON_CANCEL_ALL); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 211 | |
| 212 | // THEN we shouldn't see the dot view |
| 213 | assertIsShowingDot(false); |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void testEntryUpdated() { |
| 218 | // GIVEN no visible notifications and lights out |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 219 | when(mEntryManager.hasActiveNotifications()).thenReturn(false); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 220 | mLightsOutNotifController.mAppearance = LIGHTS_OUT; |
| 221 | mLightsOutNotifController.updateLightsOutView(); |
| 222 | assertIsShowingDot(false); |
| 223 | |
| 224 | // WHEN an active notification is added |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 225 | when(mEntryManager.hasActiveNotifications()).thenReturn(true); |
Beverly | ea2010c | 2019-11-04 09:55:29 -0500 | [diff] [blame] | 226 | assertTrue(mLightsOutNotifController.shouldShowDot()); |
| 227 | mEntryListener.onPostEntryUpdated(mock(NotificationEntry.class)); |
| 228 | |
| 229 | // THEN we should see the dot view |
| 230 | assertIsShowingDot(true); |
| 231 | } |
| 232 | |
| 233 | private void assertIsShowingDot(boolean isShowing) { |
| 234 | // cancel the animation so we can check the end state |
| 235 | final ViewPropertyAnimator animation = mLightsOutView.animate(); |
| 236 | if (animation != null) { |
| 237 | animation.cancel(); |
| 238 | } |
| 239 | |
| 240 | if (isShowing) { |
| 241 | assertTrue(mLightsOutNotifController.isShowingDot()); |
| 242 | } else { |
| 243 | assertFalse(mLightsOutNotifController.isShowingDot()); |
| 244 | } |
| 245 | } |
| 246 | } |