blob: f1d900332607f5be6e761b1061f2720d752f70f0 [file] [log] [blame]
Tony Mak202f25d2019-01-07 14:40:39 +00001/**
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 */
16package com.android.systemui.statusbar.notification.logging;
17
Tony Mak202f25d2019-01-07 14:40:39 +000018import static org.mockito.ArgumentMatchers.anyBoolean;
Gustav Senntona8e38aa2019-01-22 14:55:39 +000019import static org.mockito.ArgumentMatchers.anyInt;
Tony Mak202f25d2019-01-07 14:40:39 +000020import static org.mockito.ArgumentMatchers.eq;
Tony Mak96b3f1b2019-01-23 20:57:08 +000021import static org.mockito.Mockito.times;
Tony Mak202f25d2019-01-07 14:40:39 +000022import static org.mockito.Mockito.verify;
23
24import android.os.RemoteException;
25import android.support.test.filters.SmallTest;
26import android.testing.AndroidTestingRunner;
27import android.testing.TestableLooper;
28
29import com.android.internal.statusbar.IStatusBarService;
30import com.android.internal.statusbar.NotificationVisibility;
31import com.android.systemui.Dependency;
32import com.android.systemui.SysuiTestCase;
33import com.android.systemui.UiOffloadThread;
Tony Mak96b3f1b2019-01-23 20:57:08 +000034import com.android.systemui.statusbar.notification.stack.ExpandableViewState;
Tony Mak202f25d2019-01-07 14:40:39 +000035
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.Mockito;
41import org.mockito.MockitoAnnotations;
42
43import java.util.Collections;
44
45@SmallTest
46@RunWith(AndroidTestingRunner.class)
47@TestableLooper.RunWithLooper
48public class ExpansionStateLoggerTest extends SysuiTestCase {
49 private static final String NOTIFICATION_KEY = "notin_key";
50
51 private NotificationLogger.ExpansionStateLogger mLogger;
52 @Mock
53 private IStatusBarService mBarService;
54
55 @Before
56 public void setUp() {
57 MockitoAnnotations.initMocks(this);
58 mLogger = new NotificationLogger.ExpansionStateLogger(
59 Dependency.get(UiOffloadThread.class));
60 mLogger.mBarService = mBarService;
61 }
62
63 @Test
64 public void testVisible() throws RemoteException {
65 mLogger.onVisibilityChanged(
66 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
67 Collections.emptyList());
68 waitForUiOffloadThread();
69
70 verify(mBarService, Mockito.never()).onNotificationExpansionChanged(
Gustav Senntona8e38aa2019-01-22 14:55:39 +000071 eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt());
Tony Mak202f25d2019-01-07 14:40:39 +000072 }
73
74 @Test
75 public void testExpanded() throws RemoteException {
Gustav Senntonc7d0d322019-01-07 15:36:41 +000076 mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true,
77 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
Tony Mak202f25d2019-01-07 14:40:39 +000078 waitForUiOffloadThread();
79
80 verify(mBarService, Mockito.never()).onNotificationExpansionChanged(
Gustav Senntona8e38aa2019-01-22 14:55:39 +000081 eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt());
Tony Mak202f25d2019-01-07 14:40:39 +000082 }
83
84 @Test
85 public void testVisibleAndNotExpanded() throws RemoteException {
Gustav Senntonc7d0d322019-01-07 15:36:41 +000086 mLogger.onExpansionChanged(NOTIFICATION_KEY, true, false,
87 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
Tony Mak202f25d2019-01-07 14:40:39 +000088 mLogger.onVisibilityChanged(
89 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
90 Collections.emptyList());
91 waitForUiOffloadThread();
92
93 verify(mBarService, Mockito.never()).onNotificationExpansionChanged(
Gustav Senntona8e38aa2019-01-22 14:55:39 +000094 eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt());
Tony Mak202f25d2019-01-07 14:40:39 +000095 }
96
97 @Test
98 public void testVisibleAndExpanded() throws RemoteException {
Gustav Senntonc7d0d322019-01-07 15:36:41 +000099 mLogger.onExpansionChanged(NOTIFICATION_KEY, true, true,
100 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
Tony Mak202f25d2019-01-07 14:40:39 +0000101 mLogger.onVisibilityChanged(
102 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
103 Collections.emptyList());
104 waitForUiOffloadThread();
105
106 verify(mBarService).onNotificationExpansionChanged(
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000107 NOTIFICATION_KEY, true, true,
108 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN.toMetricsEventEnum());
Tony Mak202f25d2019-01-07 14:40:39 +0000109 }
110
111 @Test
112 public void testExpandedAndVisible_expandedBeforeVisible() throws RemoteException {
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000113 mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true,
114 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
Tony Mak202f25d2019-01-07 14:40:39 +0000115 mLogger.onVisibilityChanged(
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000116 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true,
117 NotificationVisibility.NotificationLocation.LOCATION_MAIN_AREA)),
Tony Mak202f25d2019-01-07 14:40:39 +0000118 Collections.emptyList());
119 waitForUiOffloadThread();
120
121 verify(mBarService).onNotificationExpansionChanged(
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000122 NOTIFICATION_KEY, false, true,
123 // The last location seen should be logged (the one passed to onVisibilityChanged).
124 NotificationVisibility.NotificationLocation.LOCATION_MAIN_AREA.toMetricsEventEnum()
125 );
Tony Mak202f25d2019-01-07 14:40:39 +0000126 }
127
128 @Test
129 public void testExpandedAndVisible_visibleBeforeExpanded() throws RemoteException {
130 mLogger.onVisibilityChanged(
131 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
132 Collections.emptyList());
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000133 mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true,
134 NotificationVisibility.NotificationLocation.LOCATION_FIRST_HEADS_UP);
Tony Mak202f25d2019-01-07 14:40:39 +0000135 waitForUiOffloadThread();
136
137 verify(mBarService).onNotificationExpansionChanged(
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000138 NOTIFICATION_KEY, false, true,
139 // The last location seen should be logged (the one passed to onExpansionChanged).
140 NotificationVisibility.NotificationLocation.LOCATION_FIRST_HEADS_UP.toMetricsEventEnum());
Tony Mak202f25d2019-01-07 14:40:39 +0000141 }
142
143 @Test
144 public void testExpandedAndVisible_logOnceOnly() throws RemoteException {
145 mLogger.onVisibilityChanged(
146 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
147 Collections.emptyList());
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000148 mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true,
149 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
150 mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true,
151 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
Tony Mak202f25d2019-01-07 14:40:39 +0000152 waitForUiOffloadThread();
153
154 verify(mBarService).onNotificationExpansionChanged(
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000155 NOTIFICATION_KEY, false, true,
156 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN.toMetricsEventEnum());
Tony Mak202f25d2019-01-07 14:40:39 +0000157 }
158
Tony Mak96b3f1b2019-01-23 20:57:08 +0000159 @Test
160 public void testOnEntryReinflated() throws RemoteException {
161 mLogger.onExpansionChanged(NOTIFICATION_KEY, true, true,
162 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
163 mLogger.onVisibilityChanged(
164 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
165 Collections.emptyList());
166 waitForUiOffloadThread();
167 verify(mBarService).onNotificationExpansionChanged(
168 NOTIFICATION_KEY, true, true, ExpandableViewState.LOCATION_UNKNOWN);
169
170 mLogger.onEntryReinflated(NOTIFICATION_KEY);
171 mLogger.onVisibilityChanged(
172 Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)),
173 Collections.emptyList());
174 waitForUiOffloadThread();
175 // onNotificationExpansionChanged is called the second time.
176 verify(mBarService, times(2)).onNotificationExpansionChanged(
177 NOTIFICATION_KEY, true, true, ExpandableViewState.LOCATION_UNKNOWN);
178 }
179
Tony Mak202f25d2019-01-07 14:40:39 +0000180 private NotificationVisibility createNotificationVisibility(String key, boolean visibility) {
Gustav Senntonc7d0d322019-01-07 15:36:41 +0000181 return createNotificationVisibility(key, visibility,
182 NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN);
183 }
184
185 private NotificationVisibility createNotificationVisibility(String key, boolean visibility,
186 NotificationVisibility.NotificationLocation location) {
187 return NotificationVisibility.obtain(key, 0, 0, visibility, location);
Tony Mak202f25d2019-01-07 14:40:39 +0000188 }
189}