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; |
| 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_ACTION; |
| 23 | |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | import static org.mockito.Matchers.argThat; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.when; |
| 30 | |
| 31 | import android.content.Intent; |
| 32 | import android.metrics.LogMaker; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame^] | 33 | import android.support.test.filters.SmallTest; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 34 | import android.testing.AndroidTestingRunner; |
| 35 | import android.testing.TestableLooper; |
| 36 | import android.testing.TestableLooper.RunWithLooper; |
| 37 | |
| 38 | import com.android.internal.logging.MetricsLogger; |
| 39 | import com.android.systemui.Dependency; |
| 40 | import com.android.systemui.SysuiTestCase; |
| 41 | import com.android.systemui.plugins.qs.QSTile; |
| 42 | import com.android.systemui.qs.QSHost; |
| 43 | import com.android.systemui.qs.QSTileHost; |
| 44 | |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.mockito.ArgumentMatcher; |
| 49 | |
| 50 | @RunWith(AndroidTestingRunner.class) |
| 51 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame^] | 52 | @SmallTest |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 53 | public class QSTileImplTest extends SysuiTestCase { |
| 54 | |
| 55 | public static final int POSITION = 14; |
| 56 | private TestableLooper mTestableLooper; |
| 57 | private TileImpl mTile; |
| 58 | private QSTileHost mHost; |
| 59 | private MetricsLogger mMetricsLogger; |
| 60 | |
| 61 | @Before |
| 62 | public void setup() throws Exception { |
| 63 | String spec = "spec"; |
| 64 | mTestableLooper = TestableLooper.get(this); |
| 65 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper()); |
| 66 | mMetricsLogger = mDependency.injectMockDependency(MetricsLogger.class); |
| 67 | mHost = mock(QSTileHost.class); |
| 68 | when(mHost.indexOf(spec)).thenReturn(POSITION); |
| 69 | mTestableLooper.runWithLooper(() -> { |
| 70 | mTile = new TileImpl(mHost); |
| 71 | mTile.setTileSpec(spec); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testClick_Metrics() { |
| 77 | mTile.click(); |
| 78 | verify(mMetricsLogger).write(argThat(new TileLogMatcher(ACTION_QS_CLICK))); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testSecondaryClick_Metrics() { |
| 83 | mTile.secondaryClick(); |
| 84 | verify(mMetricsLogger).write(argThat(new TileLogMatcher(ACTION_QS_SECONDARY_CLICK))); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testLongClick_Metrics() { |
| 89 | mTile.longClick(); |
| 90 | verify(mMetricsLogger).write(argThat(new TileLogMatcher(ACTION_QS_LONG_PRESS))); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testPopulate() { |
| 95 | LogMaker maker = mock(LogMaker.class); |
| 96 | when(maker.setSubtype(anyInt())).thenReturn(maker); |
| 97 | mTile.getState().value = true; |
| 98 | mTile.populate(maker); |
| 99 | verify(maker).addTaggedData(eq(FIELD_QS_VALUE), eq(1)); |
| 100 | verify(maker).addTaggedData(eq(FIELD_QS_POSITION), eq(POSITION)); |
| 101 | } |
| 102 | |
| 103 | private class TileLogMatcher implements ArgumentMatcher<LogMaker> { |
| 104 | |
| 105 | private final int mCategory; |
| 106 | public String mInvalid; |
| 107 | |
| 108 | public TileLogMatcher(int category) { |
| 109 | mCategory = category; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean matches(LogMaker arg) { |
| 114 | if (arg.getCategory() != mCategory) { |
| 115 | mInvalid = "Expected category " + mCategory + " but was " + arg.getCategory(); |
| 116 | return false; |
| 117 | } |
| 118 | if (arg.getType() != TYPE_ACTION) { |
| 119 | mInvalid = "Expected type " + TYPE_ACTION + " but was " + arg.getType(); |
| 120 | return false; |
| 121 | } |
| 122 | if (arg.getSubtype() != mTile.getMetricsCategory()) { |
| 123 | mInvalid = "Expected subtype " + mTile.getMetricsCategory() + " but was " |
| 124 | + arg.getSubtype(); |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return mInvalid; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private static class TileImpl extends QSTileImpl<QSTile.BooleanState> { |
| 137 | protected TileImpl(QSHost host) { |
| 138 | super(host); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public BooleanState newTileState() { |
| 143 | return new BooleanState(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected void handleClick() { |
| 148 | |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 153 | |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int getMetricsCategory() { |
| 158 | return 42; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Intent getLongClickIntent() { |
| 163 | return null; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected void setListening(boolean listening) { |
| 168 | |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CharSequence getTileLabel() { |
| 173 | return null; |
| 174 | } |
| 175 | } |
| 176 | } |