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> |
| 28 | #include <unordered_set> |
| 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. */ |
| 42 | void expectKeyDispatchTraced(const KeyEvent& event); |
| 43 | |
| 44 | /** Add an expectation for a motion event to be traced. */ |
| 45 | void expectMotionDispatchTraced(const MotionEvent& event); |
| 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; |
| 61 | std::unordered_set<uint32_t /*eventId*/> mTracedEvents GUARDED_BY(mLock); |
| 62 | std::vector<std::variant<KeyEvent, MotionEvent>> mExpectedEvents GUARDED_BY(mLock); |
| 63 | |
| 64 | friend class FakeInputTracingBackend; |
| 65 | |
| 66 | // Helper to verify that the given event appears as expected in the trace. If the verification |
| 67 | // fails, the error message describes why. |
| 68 | template <typename Event> |
| 69 | base::Result<void> verifyEventTraced(const Event&) const REQUIRES(mLock); |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * A backend implementation for input tracing that records events to the provided |
| 74 | * VerifyingTrace used for testing. |
| 75 | */ |
| 76 | class FakeInputTracingBackend : public trace::InputTracingBackendInterface { |
| 77 | public: |
| 78 | FakeInputTracingBackend(std::shared_ptr<VerifyingTrace> trace) : mTrace(trace) {} |
| 79 | |
| 80 | private: |
| 81 | std::shared_ptr<VerifyingTrace> mTrace; |
| 82 | |
| 83 | void traceKeyEvent(const trace::TracedKeyEvent& entry) const override; |
| 84 | void traceMotionEvent(const trace::TracedMotionEvent& entry) const override; |
| 85 | void traceWindowDispatch(const WindowDispatchArgs& entry) const override {} |
| 86 | }; |
| 87 | |
| 88 | } // namespace android::inputdispatcher |