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 | #pragma once |
| 18 | |
| 19 | #include "../dispatcher/trace/InputTracingBackendInterface.h" |
| 20 | |
| 21 | #include <android-base/result.h> |
| 22 | #include <android-base/thread_annotations.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <input/Input.h> |
| 25 | #include <condition_variable> |
| 26 | #include <memory> |
| 27 | #include <mutex> |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 28 | #include <unordered_map> |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
| 31 | namespace android::inputdispatcher { |
| 32 | |
| 33 | /** |
| 34 | * A class representing an input trace, used to make assertions on what was traced by |
| 35 | * InputDispatcher in tests. This class is thread-safe. |
| 36 | */ |
| 37 | class VerifyingTrace { |
| 38 | public: |
| 39 | VerifyingTrace() = default; |
| 40 | |
| 41 | /** Add an expectation for a key event to be traced. */ |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 42 | void expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 43 | |
| 44 | /** Add an expectation for a motion event to be traced. */ |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 45 | void expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Wait and verify that all expected events are traced. |
| 49 | * This is a lenient verifier that does not expect the events to be traced in the order |
| 50 | * that the events were expected, and does not fail if there are events that are traced that |
| 51 | * were not expected. Verifying does not clear the expectations. |
| 52 | */ |
| 53 | void verifyExpectedEventsTraced(); |
| 54 | |
| 55 | /** Reset the trace and clear all expectations. */ |
| 56 | void reset(); |
| 57 | |
| 58 | private: |
| 59 | std::mutex mLock; |
| 60 | std::condition_variable mEventTracedCondition; |
Prabir Pradhan | 65a071a | 2024-01-05 20:52:09 +0000 | [diff] [blame] | 61 | std::unordered_map<uint32_t /*eventId*/, trace::TracedEvent> mTracedEvents GUARDED_BY(mLock); |
Prabir Pradhan | 4b408be | 2024-02-21 19:19:47 +0000 | [diff] [blame] | 62 | std::vector<trace::WindowDispatchArgs> mTracedWindowDispatches GUARDED_BY(mLock); |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 63 | std::vector<std::pair<std::variant<KeyEvent, MotionEvent>, int32_t /*windowId*/>> |
| 64 | mExpectedEvents GUARDED_BY(mLock); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 65 | |
| 66 | friend class FakeInputTracingBackend; |
| 67 | |
| 68 | // Helper to verify that the given event appears as expected in the trace. If the verification |
| 69 | // fails, the error message describes why. |
| 70 | template <typename Event> |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 71 | base::Result<void> verifyEventTraced(const Event&, int32_t windowId) const REQUIRES(mLock); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * A backend implementation for input tracing that records events to the provided |
| 76 | * VerifyingTrace used for testing. |
| 77 | */ |
| 78 | class FakeInputTracingBackend : public trace::InputTracingBackendInterface { |
| 79 | public: |
| 80 | FakeInputTracingBackend(std::shared_ptr<VerifyingTrace> trace) : mTrace(trace) {} |
| 81 | |
| 82 | private: |
| 83 | std::shared_ptr<VerifyingTrace> mTrace; |
| 84 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 85 | void traceKeyEvent(const trace::TracedKeyEvent& entry, const trace::TracedEventArgs&) override; |
| 86 | void traceMotionEvent(const trace::TracedMotionEvent& entry, |
| 87 | const trace::TracedEventArgs&) override; |
Prabir Pradhan | 4b408be | 2024-02-21 19:19:47 +0000 | [diff] [blame] | 88 | void traceWindowDispatch(const trace::WindowDispatchArgs& entry, |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 89 | const trace::TracedEventArgs&) override; |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace android::inputdispatcher |