blob: db4e76116295b8d893a36f3fb2251b8ade046b01 [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
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000106 EXPECT_TRUE(packet.has_timestamp());
107 EXPECT_TRUE(packet.has_timestamp_clock_id());
108 EXPECT_EQ(packet.timestamp_clock_id(),
109 static_cast<uint32_t>(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC));
110
Prabir Pradhan6ef947a2024-06-10 20:00:51 +0000111 AndroidInputEvent::Decoder event{field.as_bytes()};
112 if (event.has_dispatcher_motion_event()) {
113 tracedMotions.emplace_back(event.dispatcher_motion_event(),
114 /*redacted=*/false);
115 }
116 if (event.has_dispatcher_motion_event_redacted()) {
117 tracedMotions.emplace_back(event.dispatcher_motion_event_redacted(),
118 /*redacted=*/true);
119 }
120 if (event.has_dispatcher_key_event()) {
121 tracedKeys.emplace_back(event.dispatcher_key_event(),
122 /*redacted=*/false);
123 }
124 if (event.has_dispatcher_key_event_redacted()) {
125 tracedKeys.emplace_back(event.dispatcher_key_event_redacted(),
126 /*redacted=*/true);
127 }
128 if (event.has_dispatcher_window_dispatch_event()) {
129 tracedWindowDispatches.emplace_back(event.dispatcher_window_dispatch_event(),
130 /*redacted=*/false);
131 }
132 if (event.has_dispatcher_window_dispatch_event_redacted()) {
133 tracedWindowDispatches
134 .emplace_back(event.dispatcher_window_dispatch_event_redacted(),
135 /*redacted=*/true);
136 }
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000137 }
138 }
139 return std::tuple{std::move(tracedMotions), std::move(tracedKeys),
140 std::move(tracedWindowDispatches)};
141}
142
143bool eventMatches(const MotionEvent& expected, const AndroidMotionEvent::Decoder& traced) {
144 return static_cast<uint32_t>(expected.getId()) == traced.event_id();
145}
146
147bool eventMatches(const KeyEvent& expected, const AndroidKeyEvent::Decoder& traced) {
148 return static_cast<uint32_t>(expected.getId()) == traced.event_id();
149}
150
151bool eventMatches(const InputTraceSession::WindowDispatchEvent& expected,
152 const AndroidWindowInputDispatchEvent::Decoder& traced) {
153 return static_cast<uint32_t>(getId(expected.event)) == traced.event_id() &&
154 expected.window->getId() == traced.window_id();
155}
156
157template <typename ExpectedEvents, typename TracedEvents>
158void verifyExpectedEventsTraced(const ExpectedEvents& expectedEvents,
159 const TracedEvents& tracedEvents, std::string_view name) {
160 uint32_t totalExpectedCount = 0;
161
162 for (const auto& [expectedEvent, expectedLevel] : expectedEvents) {
163 int32_t totalMatchCount = 0;
164 int32_t redactedMatchCount = 0;
165 for (const auto& [tracedEvent, isRedacted] : tracedEvents) {
166 if (eventMatches(expectedEvent, tracedEvent)) {
167 totalMatchCount++;
168 if (isRedacted) {
169 redactedMatchCount++;
170 }
171 }
172 }
173 switch (expectedLevel) {
174 case Level::NONE:
175 ASSERT_EQ(totalMatchCount, 0) << "Event should not be traced, but it was traced"
176 << "\n\tExpected event: " << expectedEvent;
177 break;
178 case Level::REDACTED:
179 case Level::COMPLETE:
180 ASSERT_EQ(totalMatchCount, 1)
181 << "Event should match exactly one traced event, but it matched: "
182 << totalMatchCount << "\n\tExpected event: " << expectedEvent;
183 ASSERT_EQ(redactedMatchCount, expectedLevel == Level::REDACTED ? 1 : 0);
184 totalExpectedCount++;
185 break;
186 }
187 }
188
189 ASSERT_EQ(tracedEvents.size(), totalExpectedCount)
190 << "The number of traced " << name
191 << " events does not exactly match the number of expected events";
192}
193
194} // namespace
195
196InputTraceSession::InputTraceSession(
197 std::function<void(protozero::HeapBuffered<AndroidInputEventConfig>&)> configure)
198 : mPerfettoSession(startTrace(std::move(configure))) {}
199
200InputTraceSession::~InputTraceSession() {
201 const auto rawTrace = stopTrace(std::move(mPerfettoSession));
202 verifyExpectations(rawTrace);
203}
204
205void InputTraceSession::expectMotionTraced(Level level, const MotionEvent& event) {
206 mExpectedMotions.emplace_back(event, level);
207}
208
209void InputTraceSession::expectKeyTraced(Level level, const KeyEvent& event) {
210 mExpectedKeys.emplace_back(event, level);
211}
212
213void InputTraceSession::expectDispatchTraced(Level level, const WindowDispatchEvent& event) {
214 mExpectedWindowDispatches.emplace_back(event, level);
215}
216
217void InputTraceSession::verifyExpectations(const std::string& rawTrace) {
218 auto [tracedMotions, tracedKeys, tracedWindowDispatches] = decodeTrace(rawTrace);
219
220 verifyExpectedEventsTraced(mExpectedMotions, tracedMotions, "motion");
221 verifyExpectedEventsTraced(mExpectedKeys, tracedKeys, "key");
222 verifyExpectedEventsTraced(mExpectedWindowDispatches, tracedWindowDispatches,
223 "window dispatch");
224}
225
226} // namespace android