Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [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 | package com.android.systemui.statusbar.notification.logging; |
| 17 | |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 18 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.anyInt; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.eq; |
Tony Mak | 96b3f1b | 2019-01-23 20:57:08 +0000 | [diff] [blame] | 21 | import static org.mockito.Mockito.times; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 22 | import static org.mockito.Mockito.verify; |
| 23 | |
| 24 | import android.os.RemoteException; |
| 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.testing.AndroidTestingRunner; |
| 27 | import android.testing.TestableLooper; |
| 28 | |
| 29 | import com.android.internal.statusbar.IStatusBarService; |
| 30 | import com.android.internal.statusbar.NotificationVisibility; |
| 31 | import com.android.systemui.Dependency; |
| 32 | import com.android.systemui.SysuiTestCase; |
| 33 | import com.android.systemui.UiOffloadThread; |
Tony Mak | 96b3f1b | 2019-01-23 20:57:08 +0000 | [diff] [blame] | 34 | import com.android.systemui.statusbar.notification.stack.ExpandableViewState; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.Mockito; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | import java.util.Collections; |
| 44 | |
| 45 | @SmallTest |
| 46 | @RunWith(AndroidTestingRunner.class) |
| 47 | @TestableLooper.RunWithLooper |
| 48 | public 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 Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 71 | eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testExpanded() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 76 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 77 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 78 | waitForUiOffloadThread(); |
| 79 | |
| 80 | verify(mBarService, Mockito.never()).onNotificationExpansionChanged( |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 81 | eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testVisibleAndNotExpanded() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 86 | mLogger.onExpansionChanged(NOTIFICATION_KEY, true, false, |
| 87 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 88 | mLogger.onVisibilityChanged( |
| 89 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 90 | Collections.emptyList()); |
| 91 | waitForUiOffloadThread(); |
| 92 | |
| 93 | verify(mBarService, Mockito.never()).onNotificationExpansionChanged( |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 94 | eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testVisibleAndExpanded() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 99 | mLogger.onExpansionChanged(NOTIFICATION_KEY, true, true, |
| 100 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 101 | mLogger.onVisibilityChanged( |
| 102 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 103 | Collections.emptyList()); |
| 104 | waitForUiOffloadThread(); |
| 105 | |
| 106 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 107 | NOTIFICATION_KEY, true, true, |
| 108 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN.toMetricsEventEnum()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testExpandedAndVisible_expandedBeforeVisible() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 113 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 114 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 115 | mLogger.onVisibilityChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 116 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true, |
| 117 | NotificationVisibility.NotificationLocation.LOCATION_MAIN_AREA)), |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 118 | Collections.emptyList()); |
| 119 | waitForUiOffloadThread(); |
| 120 | |
| 121 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 122 | 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 Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testExpandedAndVisible_visibleBeforeExpanded() throws RemoteException { |
| 130 | mLogger.onVisibilityChanged( |
| 131 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 132 | Collections.emptyList()); |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 133 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 134 | NotificationVisibility.NotificationLocation.LOCATION_FIRST_HEADS_UP); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 135 | waitForUiOffloadThread(); |
| 136 | |
| 137 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 138 | 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 Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testExpandedAndVisible_logOnceOnly() throws RemoteException { |
| 145 | mLogger.onVisibilityChanged( |
| 146 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 147 | Collections.emptyList()); |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 148 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 149 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
| 150 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 151 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 152 | waitForUiOffloadThread(); |
| 153 | |
| 154 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 155 | NOTIFICATION_KEY, false, true, |
| 156 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN.toMetricsEventEnum()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Tony Mak | 96b3f1b | 2019-01-23 20:57:08 +0000 | [diff] [blame] | 159 | @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 Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 180 | private NotificationVisibility createNotificationVisibility(String key, boolean visibility) { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 181 | 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 Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 188 | } |
| 189 | } |