blob: a9d370aeddb98212866d11b61475ca8139bc3691 [file] [log] [blame]
Prabir Pradhan9a9897d2024-03-21 21:52:57 +00001/*
2 * Copyright 2024 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#include "InputTraceSession.h"
18
19#include <NotifyArgsBuilders.h>
20#include <android-base/logging.h>
21#include <gtest/gtest.h>
22#include <input/PrintTools.h>
Prabir Pradhan6ef947a2024-06-10 20:00:51 +000023#include <perfetto/trace/android/android_input_event.pbzero.h>
24#include <perfetto/trace/android/winscope_extensions.pbzero.h>
25#include <perfetto/trace/android/winscope_extensions_impl.pbzero.h>
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000026
27#include <utility>
28
29namespace android {
30
31using perfetto::protos::pbzero::AndroidInputEvent;
32using perfetto::protos::pbzero::AndroidInputEventConfig;
33using perfetto::protos::pbzero::AndroidKeyEvent;
34using perfetto::protos::pbzero::AndroidMotionEvent;
35using perfetto::protos::pbzero::AndroidWindowInputDispatchEvent;
Prabir Pradhan6ef947a2024-06-10 20:00:51 +000036using perfetto::protos::pbzero::WinscopeExtensions;
37using perfetto::protos::pbzero::WinscopeExtensionsImpl;
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000038
39// These operator<< definitions must be in the global namespace for them to be accessible to the
40// GTEST library. They cannot be in the anonymous namespace.
41static std::ostream& operator<<(std::ostream& out,
42 const std::variant<KeyEvent, MotionEvent>& event) {
43 std::visit([&](const auto& e) { out << e; }, event);
44 return out;
45}
46
47static std::ostream& operator<<(std::ostream& out,
48 const InputTraceSession::WindowDispatchEvent& event) {
49 out << "Window dispatch to windowId: " << event.window->getId() << ", event: " << event.event;
50 return out;
51}
52
53namespace {
54
55inline uint32_t getId(const std::variant<KeyEvent, MotionEvent>& event) {
56 return std::visit([&](const auto& e) { return e.getId(); }, event);
57}
58
59std::unique_ptr<perfetto::TracingSession> startTrace(
60 const std::function<void(protozero::HeapBuffered<AndroidInputEventConfig>&)>& configure) {
61 protozero::HeapBuffered<AndroidInputEventConfig> inputEventConfig{};
62 configure(inputEventConfig);
63
64 perfetto::TraceConfig config;
65 config.add_buffers()->set_size_kb(1024); // Record up to 1 MiB.
66 auto* dataSourceConfig = config.add_data_sources()->mutable_config();
67 dataSourceConfig->set_name("android.input.inputevent");
68 dataSourceConfig->set_android_input_event_config_raw(inputEventConfig.SerializeAsString());
69
70 std::unique_ptr<perfetto::TracingSession> tracingSession(perfetto::Tracing::NewTrace());
71 tracingSession->Setup(config);
72 tracingSession->StartBlocking();
73 return tracingSession;
74}
75
76std::string stopTrace(std::unique_ptr<perfetto::TracingSession> tracingSession) {
77 tracingSession->StopBlocking();
78 std::vector<char> traceChars(tracingSession->ReadTraceBlocking());
79 return {traceChars.data(), traceChars.size()};
80}
81
82// Decodes the trace, and returns all of the traced input events, and whether they were each
83// traced as a redacted event.
84auto decodeTrace(const std::string& rawTrace) {
85 using namespace perfetto::protos::pbzero;
86
87 ArrayMap<AndroidMotionEvent::Decoder, bool /*redacted*/> tracedMotions;
88 ArrayMap<AndroidKeyEvent::Decoder, bool /*redacted*/> tracedKeys;
89 ArrayMap<AndroidWindowInputDispatchEvent::Decoder, bool /*redacted*/> tracedWindowDispatches;
90
91 Trace::Decoder trace{rawTrace};
92 if (trace.has_packet()) {
Prabir Pradhan6ef947a2024-06-10 20:00:51 +000093 for (auto it = trace.packet(); it; it++) {
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000094 TracePacket::Decoder packet{it->as_bytes()};
Prabir Pradhan6ef947a2024-06-10 20:00:51 +000095 if (!packet.has_winscope_extensions()) {
96 continue;
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000097 }
Prabir Pradhan6ef947a2024-06-10 20:00:51 +000098
99 WinscopeExtensions::Decoder extensions{packet.winscope_extensions()};
100 const auto& field =
101 extensions.Get(WinscopeExtensionsImpl::kAndroidInputEventFieldNumber);
102 if (!field.valid()) {
103 continue;
104 }
105
106 AndroidInputEvent::Decoder event{field.as_bytes()};
107 if (event.has_dispatcher_motion_event()) {
108 tracedMotions.emplace_back(event.dispatcher_motion_event(),
109 /*redacted=*/false);
110 }
111 if (event.has_dispatcher_motion_event_redacted()) {
112 tracedMotions.emplace_back(event.dispatcher_motion_event_redacted(),
113 /*redacted=*/true);
114 }
115 if (event.has_dispatcher_key_event()) {
116 tracedKeys.emplace_back(event.dispatcher_key_event(),
117 /*redacted=*/false);
118 }
119 if (event.has_dispatcher_key_event_redacted()) {
120 tracedKeys.emplace_back(event.dispatcher_key_event_redacted(),
121 /*redacted=*/true);
122 }
123 if (event.has_dispatcher_window_dispatch_event()) {
124 tracedWindowDispatches.emplace_back(event.dispatcher_window_dispatch_event(),
125 /*redacted=*/false);
126 }
127 if (event.has_dispatcher_window_dispatch_event_redacted()) {
128 tracedWindowDispatches
129 .emplace_back(event.dispatcher_window_dispatch_event_redacted(),
130 /*redacted=*/true);
131 }
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000132 }
133 }
134 return std::tuple{std::move(tracedMotions), std::move(tracedKeys),
135 std::move(tracedWindowDispatches)};
136}
137
138bool eventMatches(const MotionEvent& expected, const AndroidMotionEvent::Decoder& traced) {
139 return static_cast<uint32_t>(expected.getId()) == traced.event_id();
140}
141
142bool eventMatches(const KeyEvent& expected, const AndroidKeyEvent::Decoder& traced) {
143 return static_cast<uint32_t>(expected.getId()) == traced.event_id();
144}
145
146bool eventMatches(const InputTraceSession::WindowDispatchEvent& expected,
147 const AndroidWindowInputDispatchEvent::Decoder& traced) {
148 return static_cast<uint32_t>(getId(expected.event)) == traced.event_id() &&
149 expected.window->getId() == traced.window_id();
150}
151
152template <typename ExpectedEvents, typename TracedEvents>
153void verifyExpectedEventsTraced(const ExpectedEvents& expectedEvents,
154 const TracedEvents& tracedEvents, std::string_view name) {
155 uint32_t totalExpectedCount = 0;
156
157 for (const auto& [expectedEvent, expectedLevel] : expectedEvents) {
158 int32_t totalMatchCount = 0;
159 int32_t redactedMatchCount = 0;
160 for (const auto& [tracedEvent, isRedacted] : tracedEvents) {
161 if (eventMatches(expectedEvent, tracedEvent)) {
162 totalMatchCount++;
163 if (isRedacted) {
164 redactedMatchCount++;
165 }
166 }
167 }
168 switch (expectedLevel) {
169 case Level::NONE:
170 ASSERT_EQ(totalMatchCount, 0) << "Event should not be traced, but it was traced"
171 << "\n\tExpected event: " << expectedEvent;
172 break;
173 case Level::REDACTED:
174 case Level::COMPLETE:
175 ASSERT_EQ(totalMatchCount, 1)
176 << "Event should match exactly one traced event, but it matched: "
177 << totalMatchCount << "\n\tExpected event: " << expectedEvent;
178 ASSERT_EQ(redactedMatchCount, expectedLevel == Level::REDACTED ? 1 : 0);
179 totalExpectedCount++;
180 break;
181 }
182 }
183
184 ASSERT_EQ(tracedEvents.size(), totalExpectedCount)
185 << "The number of traced " << name
186 << " events does not exactly match the number of expected events";
187}
188
189} // namespace
190
191InputTraceSession::InputTraceSession(
192 std::function<void(protozero::HeapBuffered<AndroidInputEventConfig>&)> configure)
193 : mPerfettoSession(startTrace(std::move(configure))) {}
194
195InputTraceSession::~InputTraceSession() {
196 const auto rawTrace = stopTrace(std::move(mPerfettoSession));
197 verifyExpectations(rawTrace);
198}
199
200void InputTraceSession::expectMotionTraced(Level level, const MotionEvent& event) {
201 mExpectedMotions.emplace_back(event, level);
202}
203
204void InputTraceSession::expectKeyTraced(Level level, const KeyEvent& event) {
205 mExpectedKeys.emplace_back(event, level);
206}
207
208void InputTraceSession::expectDispatchTraced(Level level, const WindowDispatchEvent& event) {
209 mExpectedWindowDispatches.emplace_back(event, level);
210}
211
212void InputTraceSession::verifyExpectations(const std::string& rawTrace) {
213 auto [tracedMotions, tracedKeys, tracedWindowDispatches] = decodeTrace(rawTrace);
214
215 verifyExpectedEventsTraced(mExpectedMotions, tracedMotions, "motion");
216 verifyExpectedEventsTraced(mExpectedKeys, tracedKeys, "key");
217 verifyExpectedEventsTraced(mExpectedWindowDispatches, tracedWindowDispatches,
218 "window dispatch");
219}
220
221} // namespace android