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; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper; |
| 27 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 28 | import androidx.test.filters.SmallTest; |
| 29 | |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 30 | import com.android.internal.statusbar.IStatusBarService; |
| 31 | import com.android.internal.statusbar.NotificationVisibility; |
| 32 | import com.android.systemui.Dependency; |
| 33 | import com.android.systemui.SysuiTestCase; |
| 34 | import com.android.systemui.UiOffloadThread; |
Tony Mak | 96b3f1b | 2019-01-23 20:57:08 +0000 | [diff] [blame] | 35 | import com.android.systemui.statusbar.notification.stack.ExpandableViewState; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.Mockito; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | |
| 44 | import java.util.Collections; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidTestingRunner.class) |
| 48 | @TestableLooper.RunWithLooper |
| 49 | public class ExpansionStateLoggerTest extends SysuiTestCase { |
| 50 | private static final String NOTIFICATION_KEY = "notin_key"; |
| 51 | |
| 52 | private NotificationLogger.ExpansionStateLogger mLogger; |
| 53 | @Mock |
| 54 | private IStatusBarService mBarService; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | MockitoAnnotations.initMocks(this); |
| 59 | mLogger = new NotificationLogger.ExpansionStateLogger( |
| 60 | Dependency.get(UiOffloadThread.class)); |
| 61 | mLogger.mBarService = mBarService; |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testVisible() throws RemoteException { |
| 66 | mLogger.onVisibilityChanged( |
| 67 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 68 | Collections.emptyList()); |
| 69 | waitForUiOffloadThread(); |
| 70 | |
| 71 | verify(mBarService, Mockito.never()).onNotificationExpansionChanged( |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 72 | eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testExpanded() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 77 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 78 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 79 | waitForUiOffloadThread(); |
| 80 | |
| 81 | verify(mBarService, Mockito.never()).onNotificationExpansionChanged( |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 82 | eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testVisibleAndNotExpanded() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 87 | mLogger.onExpansionChanged(NOTIFICATION_KEY, true, false, |
| 88 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 89 | mLogger.onVisibilityChanged( |
| 90 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 91 | Collections.emptyList()); |
| 92 | waitForUiOffloadThread(); |
| 93 | |
| 94 | verify(mBarService, Mockito.never()).onNotificationExpansionChanged( |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 95 | eq(NOTIFICATION_KEY), anyBoolean(), anyBoolean(), anyInt()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testVisibleAndExpanded() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 100 | mLogger.onExpansionChanged(NOTIFICATION_KEY, true, true, |
| 101 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 102 | mLogger.onVisibilityChanged( |
| 103 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 104 | Collections.emptyList()); |
| 105 | waitForUiOffloadThread(); |
| 106 | |
| 107 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 108 | NOTIFICATION_KEY, true, true, |
| 109 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN.toMetricsEventEnum()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testExpandedAndVisible_expandedBeforeVisible() throws RemoteException { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 114 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 115 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 116 | mLogger.onVisibilityChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 117 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true, |
| 118 | NotificationVisibility.NotificationLocation.LOCATION_MAIN_AREA)), |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 119 | Collections.emptyList()); |
| 120 | waitForUiOffloadThread(); |
| 121 | |
| 122 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 123 | NOTIFICATION_KEY, false, true, |
| 124 | // The last location seen should be logged (the one passed to onVisibilityChanged). |
| 125 | NotificationVisibility.NotificationLocation.LOCATION_MAIN_AREA.toMetricsEventEnum() |
| 126 | ); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testExpandedAndVisible_visibleBeforeExpanded() throws RemoteException { |
| 131 | mLogger.onVisibilityChanged( |
| 132 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 133 | Collections.emptyList()); |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 134 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 135 | NotificationVisibility.NotificationLocation.LOCATION_FIRST_HEADS_UP); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 136 | waitForUiOffloadThread(); |
| 137 | |
| 138 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 139 | NOTIFICATION_KEY, false, true, |
| 140 | // The last location seen should be logged (the one passed to onExpansionChanged). |
| 141 | NotificationVisibility.NotificationLocation.LOCATION_FIRST_HEADS_UP.toMetricsEventEnum()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testExpandedAndVisible_logOnceOnly() throws RemoteException { |
| 146 | mLogger.onVisibilityChanged( |
| 147 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 148 | Collections.emptyList()); |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 149 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 150 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
| 151 | mLogger.onExpansionChanged(NOTIFICATION_KEY, false, true, |
| 152 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 153 | waitForUiOffloadThread(); |
| 154 | |
| 155 | verify(mBarService).onNotificationExpansionChanged( |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 156 | NOTIFICATION_KEY, false, true, |
| 157 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN.toMetricsEventEnum()); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Tony Mak | 96b3f1b | 2019-01-23 20:57:08 +0000 | [diff] [blame] | 160 | @Test |
| 161 | public void testOnEntryReinflated() throws RemoteException { |
| 162 | mLogger.onExpansionChanged(NOTIFICATION_KEY, true, true, |
| 163 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
| 164 | mLogger.onVisibilityChanged( |
| 165 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 166 | Collections.emptyList()); |
| 167 | waitForUiOffloadThread(); |
| 168 | verify(mBarService).onNotificationExpansionChanged( |
| 169 | NOTIFICATION_KEY, true, true, ExpandableViewState.LOCATION_UNKNOWN); |
| 170 | |
| 171 | mLogger.onEntryReinflated(NOTIFICATION_KEY); |
| 172 | mLogger.onVisibilityChanged( |
| 173 | Collections.singletonList(createNotificationVisibility(NOTIFICATION_KEY, true)), |
| 174 | Collections.emptyList()); |
| 175 | waitForUiOffloadThread(); |
| 176 | // onNotificationExpansionChanged is called the second time. |
| 177 | verify(mBarService, times(2)).onNotificationExpansionChanged( |
| 178 | NOTIFICATION_KEY, true, true, ExpandableViewState.LOCATION_UNKNOWN); |
| 179 | } |
| 180 | |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 181 | private NotificationVisibility createNotificationVisibility(String key, boolean visibility) { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 182 | return createNotificationVisibility(key, visibility, |
| 183 | NotificationVisibility.NotificationLocation.LOCATION_UNKNOWN); |
| 184 | } |
| 185 | |
| 186 | private NotificationVisibility createNotificationVisibility(String key, boolean visibility, |
| 187 | NotificationVisibility.NotificationLocation location) { |
| 188 | return NotificationVisibility.obtain(key, 0, 0, visibility, location); |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 189 | } |
| 190 | } |