1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.testing; 18 19 import android.util.Log; 20 import android.view.InputEvent; 21 import android.view.KeyEvent; 22 import android.view.MotionEvent; 23 24 import com.android.launcher3.Utilities; 25 import com.android.launcher3.testing.shared.TestProtocol; 26 27 import java.util.function.BiConsumer; 28 29 public final class TestLogging { 30 private static final String TAPL_EVENTS_TAG = "TaplEvents"; 31 private static final String LAUNCHER_EVENTS_TAG = "LauncherEvents"; 32 private static BiConsumer<String, String> sEventConsumer; 33 public static boolean sHadEventsNotFromTest; 34 recordEventSlow(String sequence, String event, boolean reportToTapl)35 private static void recordEventSlow(String sequence, String event, boolean reportToTapl) { 36 Log.d(reportToTapl ? TAPL_EVENTS_TAG : LAUNCHER_EVENTS_TAG, 37 sequence + " / " + event); 38 final BiConsumer<String, String> eventConsumer = sEventConsumer; 39 if (reportToTapl && eventConsumer != null) { 40 eventConsumer.accept(sequence, event); 41 } 42 } 43 recordEvent(String sequence, String event)44 public static void recordEvent(String sequence, String event) { 45 if (Utilities.isRunningInTestHarness()) { 46 recordEventSlow(sequence, event, true); 47 } 48 } 49 recordEvent(String sequence, String message, Object parameter)50 public static void recordEvent(String sequence, String message, Object parameter) { 51 if (Utilities.isRunningInTestHarness()) { 52 recordEventSlow(sequence, message + ": " + parameter, true); 53 } 54 } 55 registerEventNotFromTest(InputEvent event)56 private static void registerEventNotFromTest(InputEvent event) { 57 if (!sHadEventsNotFromTest && event.getDeviceId() != -1) { 58 sHadEventsNotFromTest = true; 59 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "First event not from test: " + event); 60 } 61 } 62 recordKeyEvent(String sequence, String message, KeyEvent event)63 public static void recordKeyEvent(String sequence, String message, KeyEvent event) { 64 if (Utilities.isRunningInTestHarness()) { 65 // This removes expecting ACTION_DOWN key event in the test. ACTION_UP is 66 // always preceded by ACTION_DOWN. 67 // Sometimes test doesn't receive ACTION_DOWN key event and we will assume that 68 // Launcher relies only on ACTION_UP. 69 // However in the test we will send both ACTION_DOWN and ACTION_UP key events. 70 // But stop reporting to tapl if action is down. 71 boolean reportToTapl = event.getAction() != KeyEvent.ACTION_DOWN; 72 recordEventSlow(sequence, message + ": " + event, reportToTapl); 73 registerEventNotFromTest(event); 74 } 75 } 76 recordMotionEvent(String sequence, String message, MotionEvent event)77 public static void recordMotionEvent(String sequence, String message, MotionEvent event) { 78 final int action = event.getAction(); 79 if (Utilities.isRunningInTestHarness() && action != MotionEvent.ACTION_MOVE) { 80 // "Expecting" in TAPL motion events was thought to be producing considerable noise in 81 // tests due to failed checks for expected events. So we are not sending them to TAPL. 82 // Other events, such as EVENT_PILFER_POINTERS produce less noise and are thought to 83 // be more useful. 84 // That's why we pass false as the value for the 'reportToTapl' parameter. 85 recordEventSlow(sequence, message + ": " + event, false); 86 if (action != MotionEvent.ACTION_CANCEL) registerEventNotFromTest(event); 87 } 88 } 89 setEventConsumer(BiConsumer<String, String> consumer)90 static void setEventConsumer(BiConsumer<String, String> consumer) { 91 sEventConsumer = consumer; 92 } 93 } 94