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 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 21 | import static junit.framework.Assert.assertEquals; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 22 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | import static org.mockito.ArgumentMatchers.argThat; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
| 30 | import android.hardware.input.InputManager; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 31 | import android.metrics.LogMaker; |
| 32 | import android.testing.AndroidTestingRunner; |
| 33 | import android.testing.TestableLooper; |
| 34 | import android.testing.TestableLooper.RunWithLooper; |
| 35 | import android.view.KeyEvent; |
| 36 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 37 | import androidx.test.filters.SmallTest; |
| 38 | |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 39 | import com.android.internal.logging.MetricsLogger; |
| 40 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 41 | import com.android.systemui.SysuiTestCase; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 42 | import com.android.systemui.bubbles.BubbleController; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 47 | import org.mockito.ArgumentCaptor; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 48 | import org.mockito.ArgumentMatcher; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 49 | import org.mockito.Captor; |
| 50 | import org.mockito.MockitoAnnotations; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 51 | |
| 52 | import java.util.Objects; |
| 53 | |
| 54 | @RunWith(AndroidTestingRunner.class) |
| 55 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 56 | @SmallTest |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 57 | public class KeyButtonViewTest extends SysuiTestCase { |
| 58 | |
| 59 | private KeyButtonView mKeyButtonView; |
| 60 | private MetricsLogger mMetricsLogger; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 61 | private BubbleController mBubbleController; |
| 62 | private InputManager mInputManager = mock(InputManager.class); |
| 63 | @Captor |
| 64 | private ArgumentCaptor<KeyEvent> mInputEventCaptor; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 65 | |
| 66 | @Before |
| 67 | public void setup() throws Exception { |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 68 | MockitoAnnotations.initMocks(this); |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 69 | mMetricsLogger = mDependency.injectMockDependency(MetricsLogger.class); |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 70 | mBubbleController = mDependency.injectMockDependency(BubbleController.class); |
| 71 | TestableLooper.get(this).runWithLooper(() -> { |
| 72 | mKeyButtonView = new KeyButtonView(mContext, null, 0, mInputManager); |
| 73 | }); |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 74 | } |
| 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 Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 84 | verify(mMetricsLogger).write(argThat(new ArgumentMatcher<LogMaker>() { |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 85 | 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 Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 112 | @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 Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 126 | } |