Jason Monk | cbf591b | 2017-03-31 15:42:27 -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.statusbar.policy; |
| 16 | |
| 17 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_NAV_BUTTON_EVENT; |
| 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_FLAGS; |
| 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_NAV_ACTION; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.argThat; |
| 22 | |
| 23 | import android.metrics.LogMaker; |
| 24 | import android.testing.AndroidTestingRunner; |
| 25 | import android.testing.TestableLooper; |
| 26 | import android.testing.TestableLooper.RunWithLooper; |
| 27 | import android.view.KeyEvent; |
| 28 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 29 | import androidx.test.filters.SmallTest; |
| 30 | |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 31 | import com.android.internal.logging.MetricsLogger; |
| 32 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 33 | import com.android.systemui.SysuiTestCase; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.ArgumentMatcher; |
| 39 | import org.mockito.Mockito; |
| 40 | |
| 41 | import java.util.Objects; |
| 42 | |
| 43 | @RunWith(AndroidTestingRunner.class) |
| 44 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 45 | @SmallTest |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 46 | public class KeyButtonViewTest extends SysuiTestCase { |
| 47 | |
| 48 | private KeyButtonView mKeyButtonView; |
| 49 | private MetricsLogger mMetricsLogger; |
| 50 | |
| 51 | @Before |
| 52 | public void setup() throws Exception { |
| 53 | mMetricsLogger = mDependency.injectMockDependency(MetricsLogger.class); |
| 54 | TestableLooper.get(this).runWithLooper(() -> |
| 55 | mKeyButtonView = new KeyButtonView(mContext, null)); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testMetrics() { |
| 60 | int action = 42; |
| 61 | int flags = 0x141; |
| 62 | int code = KeyEvent.KEYCODE_ENTER; |
| 63 | mKeyButtonView.setCode(code); |
| 64 | mKeyButtonView.sendEvent(action, flags); |
| 65 | |
| 66 | Mockito.verify(mMetricsLogger).write(argThat(new ArgumentMatcher<LogMaker>() { |
| 67 | public String mReason; |
| 68 | |
| 69 | @Override |
| 70 | public boolean matches(LogMaker argument) { |
| 71 | return checkField("category", argument.getCategory(), ACTION_NAV_BUTTON_EVENT) |
| 72 | && checkField("type", argument.getType(), MetricsEvent.TYPE_ACTION) |
| 73 | && checkField("subtype", argument.getSubtype(), code) |
| 74 | && checkField("FIELD_FLAGS", argument.getTaggedData(FIELD_FLAGS), flags) |
| 75 | && checkField("FIELD_NAV_ACTION", argument.getTaggedData(FIELD_NAV_ACTION), |
| 76 | action); |
| 77 | } |
| 78 | |
| 79 | private boolean checkField(String field, Object val, Object val2) { |
| 80 | if (!Objects.equals(val, val2)) { |
| 81 | mReason = "Expected " + field + " " + val2 + " but was " + val; |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return mReason; |
| 90 | } |
| 91 | })); |
| 92 | } |
| 93 | |
| 94 | } |