1 /*
2 * Copyright (C) 2023 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.cts.input
18
19 import android.os.SystemClock
20 import android.util.Log
21 import com.android.compatibility.common.util.SystemUtil
22 import org.junit.AssumptionViolatedException
23 import org.junit.rules.TestWatcher
24 import org.junit.runner.Description
25
26 /**
27 * A test rule that allows for additional debugging of the input pipeline to be enabled for tests.
28 *
29 * To enable additional debugging, add this test rule to the test class and annotate the desired
30 * test methods with [DebugInput].
31 *
32 * Note: This will only work on debuggable builds (e.g. eng, userdebug).
33 */
34 class DebugInputRule : TestWatcher() {
35
36 companion object {
37 private val TAG = "DebugInput"
38
39 // The list of log tags to enable when additional debugging of the input pipeline is
40 // required. These are a special set of log tags that can be dynamically toggled on
41 // debuggable builds.
42 private val debugInputTags = listOf(
43 "InputReaderRawEvents",
44 "InputDispatcherInboundEvent",
45 "InputTransportPublisher",
46 "InputTransportResampling",
47 "InputManager",
48 "InputManagerGlobal",
49 )
50
51 @JvmStatic
dumpInputStateToLogcatnull52 fun dumpInputStateToLogcat() {
53 logShellCommand("dumpsys input")
54 }
55
logShellCommandnull56 private fun logShellCommand(command: String) {
57 try {
58 for (line in SystemUtil.runShellCommandOrThrow(command).split("\\n")) {
59 // Sleed to avoid logging too often - otherwise, some lines may be dropped
60 SystemClock.sleep(10)
61 Log.i(TAG, line)
62 }
63 } catch (e: Exception) {
64 Log.e(TAG, "Failed to execute '$command': $e")
65 }
66 }
67 }
68
69 /**
70 * Annotation for a [org.junit.Test] that enables additional debugging in the input pipeline for
71 * the duration of the test. The test class must use the [DebugInputRule] for annotation to be
72 * functional.
73 */
74 annotation class DebugInput(val bug: Long)
75
76 val initialValues = mutableMapOf<String, String>()
77
startingnull78 override fun starting(description: Description?) {
79 if (!shouldEnableInputDebugging(description!!)) return
80
81 for (tag in debugInputTags) {
82 initialValues[tag] =
83 SystemUtil.runShellCommandOrThrow("getprop log.tag.$tag")!!.trim()
84 SystemUtil.runShellCommandOrThrow("setprop log.tag.$tag DEBUG")
85 }
86 }
87
failednull88 override fun failed(e: Throwable?, description: Description?) {
89 dumpInputStateToLogcat()
90 }
91
finishednull92 override fun finished(description: Description?) {
93 if (!shouldEnableInputDebugging(description!!)) return
94
95 for (entry in initialValues) {
96 val value = entry.value.ifBlank { "UNKNOWN" }
97 SystemUtil.runShellCommandOrThrow("setprop log.tag.${entry.key} $value")
98 }
99 initialValues.clear()
100 }
101
skippednull102 override fun skipped(e: AssumptionViolatedException?, description: Description?) {
103 finished(description)
104 }
105 }
106
shouldEnableInputDebuggingnull107 private fun shouldEnableInputDebugging(description: Description): Boolean {
108 return description.isTest &&
109 description.getAnnotation(DebugInputRule.DebugInput::class.java) != null
110 }
111