Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace android::inputdispatcher { |
| 23 | |
| 24 | namespace { |
| 25 | |
Prabir Pradhan | a2d3cf1 | 2024-02-05 23:02:01 +0000 | [diff] [blame] | 26 | // Use a larger timeout while waiting for events to be traced, compared to the timeout used while |
| 27 | // waiting to receive events through the input channel. Events are traced from a separate thread, |
| 28 | // which does not have the same high thread priority as the InputDispatcher's thread, so the tracer |
| 29 | // is expected to lag behind the Dispatcher at times. |
| 30 | constexpr auto TRACE_TIMEOUT = std::chrono::seconds(5); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 31 | |
| 32 | base::ResultError<> error(const std::ostringstream& ss) { |
| 33 | return base::ResultError(ss.str(), BAD_VALUE); |
| 34 | } |
| 35 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 36 | inline auto getId(const trace::TracedEvent& v) { |
| 37 | return std::visit([](const auto& event) { return event.id; }, v); |
| 38 | } |
| 39 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | // --- VerifyingTrace --- |
| 43 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 44 | void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 45 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 46 | mExpectedEvents.emplace_back(event, windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 49 | void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 50 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 51 | mExpectedEvents.emplace_back(event, windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void VerifyingTrace::verifyExpectedEventsTraced() { |
| 55 | std::unique_lock lock(mLock); |
| 56 | base::ScopedLockAssertion assumeLocked(mLock); |
| 57 | |
| 58 | base::Result<void> result; |
| 59 | mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 60 | for (const auto& [expectedEvent, windowId] : mExpectedEvents) { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 61 | std::visit([&](const auto& event) |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 62 | REQUIRES(mLock) { result = verifyEventTraced(event, windowId); }, |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 63 | expectedEvent); |
| 64 | if (!result.ok()) { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | }); |
| 70 | |
| 71 | EXPECT_TRUE(result.ok()) |
| 72 | << "Timed out waiting for all expected events to be traced successfully: " |
| 73 | << result.error().message(); |
| 74 | } |
| 75 | |
| 76 | void VerifyingTrace::reset() { |
| 77 | std::scoped_lock lock(mLock); |
| 78 | mTracedEvents.clear(); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 79 | mTracedWindowDispatches.clear(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 80 | mExpectedEvents.clear(); |
| 81 | } |
| 82 | |
| 83 | template <typename Event> |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 84 | base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent, |
| 85 | int32_t expectedWindowId) const { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 86 | std::ostringstream msg; |
| 87 | |
| 88 | auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId()); |
| 89 | if (tracedEventsIt == mTracedEvents.end()) { |
| 90 | msg << "Expected event with ID 0x" << std::hex << expectedEvent.getId() |
| 91 | << " to be traced, but it was not.\n" |
| 92 | << "Expected event: " << expectedEvent; |
| 93 | return error(msg); |
| 94 | } |
| 95 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 96 | auto tracedDispatchesIt = |
| 97 | std::find_if(mTracedWindowDispatches.begin(), mTracedWindowDispatches.end(), |
| 98 | [&](const WindowDispatchArgs& args) { |
| 99 | return args.windowId == expectedWindowId && |
| 100 | getId(args.eventEntry) == expectedEvent.getId(); |
| 101 | }); |
| 102 | if (tracedDispatchesIt == mTracedWindowDispatches.end()) { |
| 103 | msg << "Expected dispatch of event with ID 0x" << std::hex << expectedEvent.getId() |
| 104 | << " to window with ID 0x" << expectedWindowId << " to be traced, but it was not." |
| 105 | << "\nExpected event: " << expectedEvent; |
| 106 | return error(msg); |
| 107 | } |
| 108 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 109 | return {}; |
| 110 | } |
| 111 | |
| 112 | // --- FakeInputTracingBackend --- |
| 113 | |
| 114 | void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event) const { |
| 115 | { |
| 116 | std::scoped_lock lock(mTrace->mLock); |
| 117 | mTrace->mTracedEvents.emplace(event.id); |
| 118 | } |
| 119 | mTrace->mEventTracedCondition.notify_all(); |
| 120 | } |
| 121 | |
| 122 | void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event) const { |
| 123 | { |
| 124 | std::scoped_lock lock(mTrace->mLock); |
| 125 | mTrace->mTracedEvents.emplace(event.id); |
| 126 | } |
| 127 | mTrace->mEventTracedCondition.notify_all(); |
| 128 | } |
| 129 | |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 130 | void FakeInputTracingBackend::traceWindowDispatch(const WindowDispatchArgs& args) const { |
| 131 | { |
| 132 | std::scoped_lock lock(mTrace->mLock); |
| 133 | mTrace->mTracedWindowDispatches.push_back(args); |
| 134 | } |
| 135 | mTrace->mEventTracedCondition.notify_all(); |
| 136 | } |
| 137 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 138 | } // namespace android::inputdispatcher |