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_map>
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, int32_t windowId);
43 
44     /** Add an expectation for a motion event to be traced. */
45     void expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId);
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_map<uint32_t /*eventId*/, trace::TracedEvent> mTracedEvents GUARDED_BY(mLock);
62     std::vector<trace::WindowDispatchArgs> mTracedWindowDispatches GUARDED_BY(mLock);
63     std::vector<std::pair<std::variant<KeyEvent, MotionEvent>, int32_t /*windowId*/>>
64             mExpectedEvents GUARDED_BY(mLock);
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>
71     base::Result<void> verifyEventTraced(const Event&, int32_t windowId) const REQUIRES(mLock);
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:
FakeInputTracingBackend(std::shared_ptr<VerifyingTrace> trace)80     FakeInputTracingBackend(std::shared_ptr<VerifyingTrace> trace) : mTrace(trace) {}
81 
82 private:
83     std::shared_ptr<VerifyingTrace> mTrace;
84 
85     void traceKeyEvent(const trace::TracedKeyEvent& entry,
86                        const trace::TracedEventMetadata&) override;
87     void traceMotionEvent(const trace::TracedMotionEvent& entry,
88                           const trace::TracedEventMetadata&) override;
89     void traceWindowDispatch(const trace::WindowDispatchArgs& entry,
90                              const trace::TracedEventMetadata&) override;
91 };
92 
93 } // namespace android::inputdispatcher
94