blob: 39bdf204cd65ffc30933f4b4983a8ec9d81fab3a [file] [log] [blame]
Jason Monkcbf591b2017-03-31 15:42:27 -04001/*
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
15package com.android.systemui.statusbar.policy;
16
17import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_NAV_BUTTON_EVENT;
18import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_FLAGS;
19import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_NAV_ACTION;
20
21import static org.mockito.ArgumentMatchers.argThat;
22
23import android.metrics.LogMaker;
24import android.testing.AndroidTestingRunner;
25import android.testing.TestableLooper;
26import android.testing.TestableLooper.RunWithLooper;
27import android.view.KeyEvent;
28
Brett Chabot84151d92019-02-27 15:37:59 -080029import androidx.test.filters.SmallTest;
30
Jason Monkcbf591b2017-03-31 15:42:27 -040031import com.android.internal.logging.MetricsLogger;
32import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
33import com.android.systemui.SysuiTestCase;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.mockito.ArgumentMatcher;
39import org.mockito.Mockito;
40
41import java.util.Objects;
42
43@RunWith(AndroidTestingRunner.class)
44@RunWithLooper
Jason Monkfba8faf2017-05-23 10:42:59 -040045@SmallTest
Jason Monkcbf591b2017-03-31 15:42:27 -040046public 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}