blob: 77d35fbd1debdb433042223cf1810b46c38f681b [file] [log] [blame]
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +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 "FakeInputTracingBackend.h"
18
19#include <android-base/logging.h>
20#include <utils/Errors.h>
21
22namespace android::inputdispatcher {
23
24namespace {
25
26constexpr auto TRACE_TIMEOUT = std::chrono::milliseconds(100);
27
28base::ResultError<> error(const std::ostringstream& ss) {
29 return base::ResultError(ss.str(), BAD_VALUE);
30}
31
32} // namespace
33
34// --- VerifyingTrace ---
35
36void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event) {
37 std::scoped_lock lock(mLock);
38 mExpectedEvents.emplace_back(event);
39}
40
41void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event) {
42 std::scoped_lock lock(mLock);
43 mExpectedEvents.emplace_back(event);
44}
45
46void VerifyingTrace::verifyExpectedEventsTraced() {
47 std::unique_lock lock(mLock);
48 base::ScopedLockAssertion assumeLocked(mLock);
49
50 base::Result<void> result;
51 mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) {
52 for (const auto& expectedEvent : mExpectedEvents) {
53 std::visit([&](const auto& event)
54 REQUIRES(mLock) { result = verifyEventTraced(event); },
55 expectedEvent);
56 if (!result.ok()) {
57 return false;
58 }
59 }
60 return true;
61 });
62
63 EXPECT_TRUE(result.ok())
64 << "Timed out waiting for all expected events to be traced successfully: "
65 << result.error().message();
66}
67
68void VerifyingTrace::reset() {
69 std::scoped_lock lock(mLock);
70 mTracedEvents.clear();
71 mExpectedEvents.clear();
72}
73
74template <typename Event>
75base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent) const {
76 std::ostringstream msg;
77
78 auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId());
79 if (tracedEventsIt == mTracedEvents.end()) {
80 msg << "Expected event with ID 0x" << std::hex << expectedEvent.getId()
81 << " to be traced, but it was not.\n"
82 << "Expected event: " << expectedEvent;
83 return error(msg);
84 }
85
86 return {};
87}
88
89// --- FakeInputTracingBackend ---
90
91void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event) const {
92 {
93 std::scoped_lock lock(mTrace->mLock);
94 mTrace->mTracedEvents.emplace(event.id);
95 }
96 mTrace->mEventTracedCondition.notify_all();
97}
98
99void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event) const {
100 {
101 std::scoped_lock lock(mTrace->mLock);
102 mTrace->mTracedEvents.emplace(event.id);
103 }
104 mTrace->mEventTracedCondition.notify_all();
105}
106
107} // namespace android::inputdispatcher