blob: d16dc168d74c41d4a88cffd452f9cff04ffdc6b2 [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
Mady Mellor390bff42019-04-05 15:09:01 -070021import static junit.framework.Assert.assertEquals;
Jason Monkcbf591b2017-03-31 15:42:27 -040022
Mady Mellor390bff42019-04-05 15:09:01 -070023import static org.mockito.ArgumentMatchers.anyInt;
24import static org.mockito.ArgumentMatchers.argThat;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.times;
27import static org.mockito.Mockito.verify;
28import static org.mockito.Mockito.when;
29
30import android.hardware.input.InputManager;
Jason Monkcbf591b2017-03-31 15:42:27 -040031import android.metrics.LogMaker;
32import android.testing.AndroidTestingRunner;
33import android.testing.TestableLooper;
34import android.testing.TestableLooper.RunWithLooper;
35import android.view.KeyEvent;
36
Brett Chabot84151d92019-02-27 15:37:59 -080037import androidx.test.filters.SmallTest;
38
Jason Monkcbf591b2017-03-31 15:42:27 -040039import com.android.internal.logging.MetricsLogger;
40import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
41import com.android.systemui.SysuiTestCase;
Mady Mellor390bff42019-04-05 15:09:01 -070042import com.android.systemui.bubbles.BubbleController;
Jason Monkcbf591b2017-03-31 15:42:27 -040043
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
Mady Mellor390bff42019-04-05 15:09:01 -070047import org.mockito.ArgumentCaptor;
Jason Monkcbf591b2017-03-31 15:42:27 -040048import org.mockito.ArgumentMatcher;
Mady Mellor390bff42019-04-05 15:09:01 -070049import org.mockito.Captor;
50import org.mockito.MockitoAnnotations;
Jason Monkcbf591b2017-03-31 15:42:27 -040051
52import java.util.Objects;
53
54@RunWith(AndroidTestingRunner.class)
55@RunWithLooper
Jason Monkfba8faf2017-05-23 10:42:59 -040056@SmallTest
Jason Monkcbf591b2017-03-31 15:42:27 -040057public class KeyButtonViewTest extends SysuiTestCase {
58
59 private KeyButtonView mKeyButtonView;
60 private MetricsLogger mMetricsLogger;
Mady Mellor390bff42019-04-05 15:09:01 -070061 private BubbleController mBubbleController;
62 private InputManager mInputManager = mock(InputManager.class);
63 @Captor
64 private ArgumentCaptor<KeyEvent> mInputEventCaptor;
Jason Monkcbf591b2017-03-31 15:42:27 -040065
66 @Before
67 public void setup() throws Exception {
Mady Mellor390bff42019-04-05 15:09:01 -070068 MockitoAnnotations.initMocks(this);
Jason Monkcbf591b2017-03-31 15:42:27 -040069 mMetricsLogger = mDependency.injectMockDependency(MetricsLogger.class);
Mady Mellor390bff42019-04-05 15:09:01 -070070 mBubbleController = mDependency.injectMockDependency(BubbleController.class);
71 TestableLooper.get(this).runWithLooper(() -> {
72 mKeyButtonView = new KeyButtonView(mContext, null, 0, mInputManager);
73 });
Jason Monkcbf591b2017-03-31 15:42:27 -040074 }
75
76 @Test
77 public void testMetrics() {
78 int action = 42;
79 int flags = 0x141;
80 int code = KeyEvent.KEYCODE_ENTER;
81 mKeyButtonView.setCode(code);
82 mKeyButtonView.sendEvent(action, flags);
83
Mady Mellor390bff42019-04-05 15:09:01 -070084 verify(mMetricsLogger).write(argThat(new ArgumentMatcher<LogMaker>() {
Jason Monkcbf591b2017-03-31 15:42:27 -040085 public String mReason;
86
87 @Override
88 public boolean matches(LogMaker argument) {
89 return checkField("category", argument.getCategory(), ACTION_NAV_BUTTON_EVENT)
90 && checkField("type", argument.getType(), MetricsEvent.TYPE_ACTION)
91 && checkField("subtype", argument.getSubtype(), code)
92 && checkField("FIELD_FLAGS", argument.getTaggedData(FIELD_FLAGS), flags)
93 && checkField("FIELD_NAV_ACTION", argument.getTaggedData(FIELD_NAV_ACTION),
94 action);
95 }
96
97 private boolean checkField(String field, Object val, Object val2) {
98 if (!Objects.equals(val, val2)) {
99 mReason = "Expected " + field + " " + val2 + " but was " + val;
100 return false;
101 }
102 return true;
103 }
104
105 @Override
106 public String toString() {
107 return mReason;
108 }
109 }));
110 }
111
Mady Mellor390bff42019-04-05 15:09:01 -0700112 @Test
113 public void testBubbleEvents_bubbleExpanded() {
114 when(mBubbleController.getExpandedDisplayId(mContext)).thenReturn(3);
115
116 int action = KeyEvent.ACTION_DOWN;
117 int flags = 0;
118 int code = KeyEvent.KEYCODE_BACK;
119 mKeyButtonView.setCode(code);
120 mKeyButtonView.sendEvent(action, flags);
121
122 verify(mInputManager, times(1)).injectInputEvent(mInputEventCaptor.capture(),
123 anyInt());
124 assertEquals(3, mInputEventCaptor.getValue().getDisplayId());
125 }
Jason Monkcbf591b2017-03-31 15:42:27 -0400126}