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