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 
26 import java.util.function.BiConsumer;
27 
28 public final class TestLogging {
29     private static BiConsumer<String, String> sEventConsumer;
30     public static boolean sHadEventsNotFromTest;
31 
recordEventSlow(String sequence, String event)32     private static void recordEventSlow(String sequence, String event) {
33         Log.d(TestProtocol.TAPL_EVENTS_TAG, sequence + " / " + event);
34         final BiConsumer<String, String> eventConsumer = sEventConsumer;
35         if (eventConsumer != null) {
36             eventConsumer.accept(sequence, event);
37         }
38     }
39 
recordEvent(String sequence, String event)40     public static void recordEvent(String sequence, String event) {
41         if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
42             recordEventSlow(sequence, event);
43         }
44     }
45 
recordEvent(String sequence, String message, Object parameter)46     public static void recordEvent(String sequence, String message, Object parameter) {
47         if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
48             recordEventSlow(sequence, message + ": " + parameter);
49         }
50     }
51 
registerEventNotFromTest(InputEvent event)52     private static void registerEventNotFromTest(InputEvent event) {
53         if (!sHadEventsNotFromTest && event.getDeviceId() != -1) {
54             sHadEventsNotFromTest = true;
55             Log.d(TestProtocol.PERMANENT_DIAG_TAG, "First event not from test: " + event);
56         }
57     }
58 
recordKeyEvent(String sequence, String message, KeyEvent event)59     public static void recordKeyEvent(String sequence, String message, KeyEvent event) {
60         if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
61             recordEventSlow(sequence, message + ": " + event);
62             registerEventNotFromTest(event);
63         }
64     }
65 
recordMotionEvent(String sequence, String message, MotionEvent event)66     public static void recordMotionEvent(String sequence, String message, MotionEvent event) {
67         if (Utilities.IS_RUNNING_IN_TEST_HARNESS && event.getAction() != MotionEvent.ACTION_MOVE) {
68             recordEventSlow(sequence, message + ": " + event);
69             registerEventNotFromTest(event);
70         }
71     }
72 
setEventConsumer(BiConsumer<String, String> consumer)73     static void setEventConsumer(BiConsumer<String, String> consumer) {
74         sEventConsumer = consumer;
75     }
76 }
77