blob: ab05d6b5323d12148f25268eb21d54951a41c720 [file] [log] [blame]
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001/*
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 Pradhan65a071a2024-01-05 20:52:09 +000028#include <unordered_map>
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000029#include <vector>
30
31namespace 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 */
37class VerifyingTrace {
38public:
39 VerifyingTrace() = default;
40
41 /** Add an expectation for a key event to be traced. */
Prabir Pradhan4497c862023-12-15 07:13:30 +000042 void expectKeyDispatchTraced(const KeyEvent& event, int32_t windowId);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000043
44 /** Add an expectation for a motion event to be traced. */
Prabir Pradhan4497c862023-12-15 07:13:30 +000045 void expectMotionDispatchTraced(const MotionEvent& event, int32_t windowId);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000046
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
58private:
59 std::mutex mLock;
60 std::condition_variable mEventTracedCondition;
Prabir Pradhan65a071a2024-01-05 20:52:09 +000061 std::unordered_map<uint32_t /*eventId*/, trace::TracedEvent> mTracedEvents GUARDED_BY(mLock);
Prabir Pradhan4b408be2024-02-21 19:19:47 +000062 std::vector<trace::WindowDispatchArgs> mTracedWindowDispatches GUARDED_BY(mLock);
Prabir Pradhan4497c862023-12-15 07:13:30 +000063 std::vector<std::pair<std::variant<KeyEvent, MotionEvent>, int32_t /*windowId*/>>
64 mExpectedEvents GUARDED_BY(mLock);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000065
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 Pradhan4497c862023-12-15 07:13:30 +000071 base::Result<void> verifyEventTraced(const Event&, int32_t windowId) const REQUIRES(mLock);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000072};
73
74/**
75 * A backend implementation for input tracing that records events to the provided
76 * VerifyingTrace used for testing.
77 */
78class FakeInputTracingBackend : public trace::InputTracingBackendInterface {
79public:
80 FakeInputTracingBackend(std::shared_ptr<VerifyingTrace> trace) : mTrace(trace) {}
81
82private:
83 std::shared_ptr<VerifyingTrace> mTrace;
84
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000085 void traceKeyEvent(const trace::TracedKeyEvent& entry, const trace::TracedEventArgs&) override;
86 void traceMotionEvent(const trace::TracedMotionEvent& entry,
87 const trace::TracedEventArgs&) override;
Prabir Pradhan4b408be2024-02-21 19:19:47 +000088 void traceWindowDispatch(const trace::WindowDispatchArgs& entry,
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000089 const trace::TracedEventArgs&) override;
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000090};
91
92} // namespace android::inputdispatcher