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 | |
| 36 | } // namespace |
| 37 | |
| 38 | // --- VerifyingTrace --- |
| 39 | |
| 40 | void VerifyingTrace::expectKeyDispatchTraced(const KeyEvent& event) { |
| 41 | std::scoped_lock lock(mLock); |
| 42 | mExpectedEvents.emplace_back(event); |
| 43 | } |
| 44 | |
| 45 | void VerifyingTrace::expectMotionDispatchTraced(const MotionEvent& event) { |
| 46 | std::scoped_lock lock(mLock); |
| 47 | mExpectedEvents.emplace_back(event); |
| 48 | } |
| 49 | |
| 50 | void VerifyingTrace::verifyExpectedEventsTraced() { |
| 51 | std::unique_lock lock(mLock); |
| 52 | base::ScopedLockAssertion assumeLocked(mLock); |
| 53 | |
| 54 | base::Result<void> result; |
| 55 | mEventTracedCondition.wait_for(lock, TRACE_TIMEOUT, [&]() REQUIRES(mLock) { |
| 56 | for (const auto& expectedEvent : mExpectedEvents) { |
| 57 | std::visit([&](const auto& event) |
| 58 | REQUIRES(mLock) { result = verifyEventTraced(event); }, |
| 59 | expectedEvent); |
| 60 | if (!result.ok()) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | return true; |
| 65 | }); |
| 66 | |
| 67 | EXPECT_TRUE(result.ok()) |
| 68 | << "Timed out waiting for all expected events to be traced successfully: " |
| 69 | << result.error().message(); |
| 70 | } |
| 71 | |
| 72 | void VerifyingTrace::reset() { |
| 73 | std::scoped_lock lock(mLock); |
| 74 | mTracedEvents.clear(); |
| 75 | mExpectedEvents.clear(); |
| 76 | } |
| 77 | |
| 78 | template <typename Event> |
| 79 | base::Result<void> VerifyingTrace::verifyEventTraced(const Event& expectedEvent) const { |
| 80 | std::ostringstream msg; |
| 81 | |
| 82 | auto tracedEventsIt = mTracedEvents.find(expectedEvent.getId()); |
| 83 | if (tracedEventsIt == mTracedEvents.end()) { |
| 84 | msg << "Expected event with ID 0x" << std::hex << expectedEvent.getId() |
| 85 | << " to be traced, but it was not.\n" |
| 86 | << "Expected event: " << expectedEvent; |
| 87 | return error(msg); |
| 88 | } |
| 89 | |
| 90 | return {}; |
| 91 | } |
| 92 | |
| 93 | // --- FakeInputTracingBackend --- |
| 94 | |
| 95 | void FakeInputTracingBackend::traceKeyEvent(const trace::TracedKeyEvent& event) const { |
| 96 | { |
| 97 | std::scoped_lock lock(mTrace->mLock); |
| 98 | mTrace->mTracedEvents.emplace(event.id); |
| 99 | } |
| 100 | mTrace->mEventTracedCondition.notify_all(); |
| 101 | } |
| 102 | |
| 103 | void FakeInputTracingBackend::traceMotionEvent(const trace::TracedMotionEvent& event) const { |
| 104 | { |
| 105 | std::scoped_lock lock(mTrace->mLock); |
| 106 | mTrace->mTracedEvents.emplace(event.id); |
| 107 | } |
| 108 | mTrace->mEventTracedCondition.notify_all(); |
| 109 | } |
| 110 | |
| 111 | } // namespace android::inputdispatcher |