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