blob: dbb4512775358ff5dcbb6230ab677aeb1f236fbb [file] [log] [blame]
Beverlyea2010c2019-11-04 09:55:29 -05001/*
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
17package com.android.systemui.statusbar.phone;
18
Julia Reynolds138111f2020-02-26 11:17:39 -050019import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL;
Beverlyea2010c2019-11-04 09:55:29 -050020import static android.view.WindowInsetsController.APPEARANCE_LOW_PROFILE_BARS;
21
Beverlyea2010c2019-11-04 09:55:29 -050022import static junit.framework.Assert.assertFalse;
23import static junit.framework.Assert.assertTrue;
24
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper.RunWithLooper;
31import android.view.Display;
32import android.view.View;
33import android.view.ViewPropertyAnimator;
34import android.view.WindowManager;
35
36import androidx.test.filters.SmallTest;
37
38import com.android.systemui.SysuiTestCase;
39import com.android.systemui.statusbar.CommandQueue;
40import com.android.systemui.statusbar.notification.NotificationEntryListener;
41import com.android.systemui.statusbar.notification.NotificationEntryManager;
Beverlyea2010c2019-11-04 09:55:29 -050042import com.android.systemui.statusbar.notification.collection.NotificationEntry;
43
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.ArgumentCaptor;
48import org.mockito.Captor;
49import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
51
Daulet Zhanguzind0549ae2020-01-03 11:08:54 +000052import java.util.Objects;
53
Beverlyea2010c2019-11-04 09:55:29 -050054@SmallTest
55@RunWith(AndroidTestingRunner.class)
56@RunWithLooper
57public 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;
Beverlyea2010c2019-11-04 09:55:29 -050062 @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;
Beverlyea2010c2019-11-04 09:55:29 -050071 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);
Beverlyea2010c2019-11-04 09:55:29 -050080 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 Zhanguzind0549ae2020-01-03 11:08:54 +000089 mEntryListener = Objects.requireNonNull(mListenerCaptor.getValue());
Beverlyea2010c2019-11-04 09:55:29 -050090
91 // Capture the callback object so we can simulate callback events in tests below
92 verify(mCommandQueue).addCallback(mCallbacksCaptor.capture());
Daulet Zhanguzind0549ae2020-01-03 11:08:54 +000093 mCallbacks = Objects.requireNonNull(mCallbacksCaptor.getValue());
Beverlyea2010c2019-11-04 09:55:29 -050094 }
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 Laird181de622019-10-24 09:53:02 -0400133 when(mEntryManager.hasActiveNotifications()).thenReturn(true);
Beverlyea2010c2019-11-04 09:55:29 -0500134
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 Laird181de622019-10-24 09:53:02 -0400150 when(mEntryManager.hasActiveNotifications()).thenReturn(false);
Beverlyea2010c2019-11-04 09:55:29 -0500151
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 Laird181de622019-10-24 09:53:02 -0400167 when(mEntryManager.hasActiveNotifications()).thenReturn(true);
Beverlyea2010c2019-11-04 09:55:29 -0500168
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 Laird181de622019-10-24 09:53:02 -0400184 when(mEntryManager.hasActiveNotifications()).thenReturn(false);
Beverlyea2010c2019-11-04 09:55:29 -0500185 mLightsOutNotifController.mAppearance = LIGHTS_OUT;
186 mLightsOutNotifController.updateLightsOutView();
187 assertIsShowingDot(false);
188
189 // WHEN an active notification is added
Evan Laird181de622019-10-24 09:53:02 -0400190 when(mEntryManager.hasActiveNotifications()).thenReturn(true);
Beverlyea2010c2019-11-04 09:55:29 -0500191 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 Laird181de622019-10-24 09:53:02 -0400201 when(mEntryManager.hasActiveNotifications()).thenReturn(true);
Beverlyea2010c2019-11-04 09:55:29 -0500202 mLightsOutNotifController.mAppearance = LIGHTS_OUT;
203 mLightsOutNotifController.updateLightsOutView();
204 assertIsShowingDot(true);
205
206 // WHEN all active notifications are removed
Evan Laird181de622019-10-24 09:53:02 -0400207 when(mEntryManager.hasActiveNotifications()).thenReturn(false);
Beverlyea2010c2019-11-04 09:55:29 -0500208 assertFalse(mLightsOutNotifController.shouldShowDot());
Julia Reynolds138111f2020-02-26 11:17:39 -0500209 mEntryListener.onEntryRemoved(
210 mock(NotificationEntry.class), null, false, REASON_CANCEL_ALL);
Beverlyea2010c2019-11-04 09:55:29 -0500211
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 Laird181de622019-10-24 09:53:02 -0400219 when(mEntryManager.hasActiveNotifications()).thenReturn(false);
Beverlyea2010c2019-11-04 09:55:29 -0500220 mLightsOutNotifController.mAppearance = LIGHTS_OUT;
221 mLightsOutNotifController.updateLightsOutView();
222 assertIsShowingDot(false);
223
224 // WHEN an active notification is added
Evan Laird181de622019-10-24 09:53:02 -0400225 when(mEntryManager.hasActiveNotifications()).thenReturn(true);
Beverlyea2010c2019-11-04 09:55:29 -0500226 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}