Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs.tileimpl; |
| 16 | |
| 17 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_CLICK; |
| 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_LONG_PRESS; |
| 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_QS_SECONDARY_CLICK; |
| 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_QS_POSITION; |
| 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_QS_VALUE; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_STATUS_BAR_STATE; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 23 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_ACTION; |
| 24 | |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
Jason Monk | 2ef8ed6 | 2017-09-07 14:51:41 -0400 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.any; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 27 | import static org.mockito.ArgumentMatchers.anyInt; |
| 28 | import static org.mockito.ArgumentMatchers.eq; |
| 29 | import static org.mockito.Matchers.argThat; |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 30 | import static org.mockito.Mockito.clearInvocations; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 31 | import static org.mockito.Mockito.mock; |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 32 | import static org.mockito.Mockito.never; |
| 33 | import static org.mockito.Mockito.spy; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 34 | import static org.mockito.Mockito.verify; |
| 35 | import static org.mockito.Mockito.when; |
| 36 | |
| 37 | import android.content.Intent; |
| 38 | import android.metrics.LogMaker; |
| 39 | import android.testing.AndroidTestingRunner; |
| 40 | import android.testing.TestableLooper; |
| 41 | import android.testing.TestableLooper.RunWithLooper; |
| 42 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 43 | import androidx.test.filters.SmallTest; |
| 44 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 45 | import com.android.internal.logging.MetricsLogger; |
| 46 | import com.android.systemui.Dependency; |
| 47 | import com.android.systemui.SysuiTestCase; |
| 48 | import com.android.systemui.plugins.qs.QSTile; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 49 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 50 | import com.android.systemui.qs.QSHost; |
| 51 | import com.android.systemui.qs.QSTileHost; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 52 | import com.android.systemui.statusbar.StatusBarState; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 53 | |
| 54 | import org.junit.Before; |
Geoffrey Pitsch | 659a91b | 2017-11-28 13:33:13 -0500 | [diff] [blame] | 55 | import org.junit.Ignore; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 56 | import org.junit.Test; |
| 57 | import org.junit.runner.RunWith; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 58 | import org.mockito.ArgumentCaptor; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 59 | import org.mockito.ArgumentMatcher; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 60 | import org.mockito.Captor; |
| 61 | import org.mockito.MockitoAnnotations; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 62 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 63 | import static java.lang.Thread.sleep; |
| 64 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 65 | @RunWith(AndroidTestingRunner.class) |
| 66 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 67 | @SmallTest |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 68 | public class QSTileImplTest extends SysuiTestCase { |
| 69 | |
| 70 | public static final int POSITION = 14; |
| 71 | private TestableLooper mTestableLooper; |
| 72 | private TileImpl mTile; |
| 73 | private QSTileHost mHost; |
| 74 | private MetricsLogger mMetricsLogger; |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 75 | private StatusBarStateController mStatusBarStateController; |
| 76 | |
| 77 | @Captor |
| 78 | private ArgumentCaptor<LogMaker> mLogCaptor; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 79 | |
| 80 | @Before |
| 81 | public void setup() throws Exception { |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 82 | MockitoAnnotations.initMocks(this); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 83 | String spec = "spec"; |
| 84 | mTestableLooper = TestableLooper.get(this); |
| 85 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper()); |
| 86 | mMetricsLogger = mDependency.injectMockDependency(MetricsLogger.class); |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 87 | mStatusBarStateController = |
| 88 | mDependency.injectMockDependency(StatusBarStateController.class); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 89 | mHost = mock(QSTileHost.class); |
| 90 | when(mHost.indexOf(spec)).thenReturn(POSITION); |
Rohan Shah | db2cfa3 | 2018-02-20 11:27:22 -0800 | [diff] [blame] | 91 | when(mHost.getContext()).thenReturn(mContext.getBaseContext()); |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 92 | |
| 93 | mTile = spy(new TileImpl(mHost)); |
| 94 | mTile.mHandler = mTile.new H(mTestableLooper.getLooper()); |
| 95 | mTile.setTileSpec(spec); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testClick_Metrics() { |
| 100 | mTile.click(); |
| 101 | verify(mMetricsLogger).write(argThat(new TileLogMatcher(ACTION_QS_CLICK))); |
| 102 | } |
| 103 | |
| 104 | @Test |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 105 | public void testClick_Metrics_Status_Bar_Status() { |
| 106 | when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE); |
| 107 | mTile.click(); |
| 108 | verify(mMetricsLogger).write(mLogCaptor.capture()); |
| 109 | assertEquals(StatusBarState.SHADE, mLogCaptor.getValue() |
| 110 | .getTaggedData(FIELD_STATUS_BAR_STATE)); |
| 111 | } |
| 112 | |
| 113 | @Test |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 114 | public void testSecondaryClick_Metrics() { |
| 115 | mTile.secondaryClick(); |
| 116 | verify(mMetricsLogger).write(argThat(new TileLogMatcher(ACTION_QS_SECONDARY_CLICK))); |
| 117 | } |
| 118 | |
| 119 | @Test |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 120 | public void testSecondaryClick_Metrics_Status_Bar_Status() { |
| 121 | when(mStatusBarStateController.getState()).thenReturn(StatusBarState.KEYGUARD); |
| 122 | mTile.secondaryClick(); |
| 123 | verify(mMetricsLogger).write(mLogCaptor.capture()); |
| 124 | assertEquals(StatusBarState.KEYGUARD, mLogCaptor.getValue() |
| 125 | .getTaggedData(FIELD_STATUS_BAR_STATE)); |
| 126 | } |
| 127 | |
| 128 | @Test |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 129 | public void testLongClick_Metrics() { |
| 130 | mTile.longClick(); |
| 131 | verify(mMetricsLogger).write(argThat(new TileLogMatcher(ACTION_QS_LONG_PRESS))); |
| 132 | } |
| 133 | |
| 134 | @Test |
Susi Kharraz-Post | 9b03367 | 2018-11-28 08:14:07 -0500 | [diff] [blame] | 135 | public void testLongClick_Metrics_Status_Bar_Status() { |
| 136 | when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE_LOCKED); |
| 137 | mTile.click(); |
| 138 | verify(mMetricsLogger).write(mLogCaptor.capture()); |
| 139 | assertEquals(StatusBarState.SHADE_LOCKED, mLogCaptor.getValue() |
| 140 | .getTaggedData(FIELD_STATUS_BAR_STATE)); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testPopulateWithLockedScreen() { |
| 145 | LogMaker maker = mock(LogMaker.class); |
| 146 | when(maker.setSubtype(anyInt())).thenReturn(maker); |
| 147 | when(maker.addTaggedData(anyInt(), any())).thenReturn(maker); |
| 148 | mTile.getState().value = true; |
| 149 | mTile.populate(maker); |
| 150 | verify(maker).addTaggedData(eq(FIELD_QS_VALUE), eq(1)); |
| 151 | verify(maker).addTaggedData(eq(FIELD_QS_POSITION), eq(POSITION)); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testPopulateWithUnlockedScreen() { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 156 | LogMaker maker = mock(LogMaker.class); |
| 157 | when(maker.setSubtype(anyInt())).thenReturn(maker); |
Jason Monk | 2ef8ed6 | 2017-09-07 14:51:41 -0400 | [diff] [blame] | 158 | when(maker.addTaggedData(anyInt(), any())).thenReturn(maker); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 159 | mTile.getState().value = true; |
| 160 | mTile.populate(maker); |
| 161 | verify(maker).addTaggedData(eq(FIELD_QS_VALUE), eq(1)); |
| 162 | verify(maker).addTaggedData(eq(FIELD_QS_POSITION), eq(POSITION)); |
| 163 | } |
| 164 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 165 | @Test |
Geoffrey Pitsch | 659a91b | 2017-11-28 13:33:13 -0500 | [diff] [blame] | 166 | @Ignore("flaky") |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 167 | public void testStaleTimeout() throws InterruptedException { |
| 168 | when(mTile.getStaleTimeout()).thenReturn(5l); |
| 169 | clearInvocations(mTile); |
| 170 | |
| 171 | mTile.handleRefreshState(null); |
| 172 | mTestableLooper.processAllMessages(); |
| 173 | verify(mTile, never()).handleStale(); |
| 174 | |
| 175 | sleep(10); |
| 176 | mTestableLooper.processAllMessages(); |
| 177 | verify(mTile).handleStale(); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testStaleListening() { |
| 182 | mTile.handleStale(); |
| 183 | mTestableLooper.processAllMessages(); |
| 184 | verify(mTile).handleSetListening(eq(true)); |
| 185 | |
| 186 | mTile.handleRefreshState(null); |
| 187 | mTestableLooper.processAllMessages(); |
| 188 | verify(mTile).handleSetListening(eq(false)); |
| 189 | } |
| 190 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 191 | private class TileLogMatcher implements ArgumentMatcher<LogMaker> { |
| 192 | |
| 193 | private final int mCategory; |
| 194 | public String mInvalid; |
| 195 | |
| 196 | public TileLogMatcher(int category) { |
| 197 | mCategory = category; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean matches(LogMaker arg) { |
| 202 | if (arg.getCategory() != mCategory) { |
| 203 | mInvalid = "Expected category " + mCategory + " but was " + arg.getCategory(); |
| 204 | return false; |
| 205 | } |
| 206 | if (arg.getType() != TYPE_ACTION) { |
| 207 | mInvalid = "Expected type " + TYPE_ACTION + " but was " + arg.getType(); |
| 208 | return false; |
| 209 | } |
| 210 | if (arg.getSubtype() != mTile.getMetricsCategory()) { |
| 211 | mInvalid = "Expected subtype " + mTile.getMetricsCategory() + " but was " |
| 212 | + arg.getSubtype(); |
| 213 | return false; |
| 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String toString() { |
| 220 | return mInvalid; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private static class TileImpl extends QSTileImpl<QSTile.BooleanState> { |
| 225 | protected TileImpl(QSHost host) { |
| 226 | super(host); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public BooleanState newTileState() { |
| 231 | return new BooleanState(); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | protected void handleClick() { |
| 236 | |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 241 | |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public int getMetricsCategory() { |
| 246 | return 42; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public Intent getLongClickIntent() { |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 255 | protected void handleSetListening(boolean listening) { |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 256 | |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public CharSequence getTileLabel() { |
| 261 | return null; |
| 262 | } |
| 263 | } |
| 264 | } |