blob: bda552165e040227fc106499552d4203582f67a8 [file] [log] [blame]
Prabir Pradhan9a9897d2024-03-21 21:52:57 +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 "FakeWindows.h"
20
21#include <android-base/logging.h>
22#include <gtest/gtest.h>
23#include <input/Input.h>
24#include <perfetto/config/android/android_input_event_config.pbzero.h>
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000025#include <perfetto/trace/trace.pbzero.h>
26#include <perfetto/tracing.h>
27#include <variant>
28#include <vector>
29
30namespace android {
31
32/**
33 * Tracing level constants used for adding expectations to the InputTraceSession.
34 */
35enum class Level {
36 NONE,
37 REDACTED,
38 COMPLETE,
39};
40
41template <typename K, typename V>
42using ArrayMap = std::vector<std::pair<K, V>>;
43
44/**
45 * A scoped representation of a tracing session that is used to make assertions on the trace.
46 *
47 * When the trace session is created, an "android.input.inputevent" trace will be started
48 * synchronously with the given configuration. While the trace is ongoing, the caller must
49 * specify the events that are expected to be in the trace using the expect* methods.
50 *
51 * When the session is destroyed, the trace is stopped synchronously, and all expectations will
52 * be verified using the gtest framework. This acts as a strict verifier, where the verification
53 * will fail both if an expected event does not show up in the trace and if there is an extra
54 * event in the trace that was not expected. Ordering is NOT verified for any events.
55 */
56class InputTraceSession {
57public:
58 explicit InputTraceSession(
59 std::function<void(
60 protozero::HeapBuffered<perfetto::protos::pbzero::AndroidInputEventConfig>&)>
61 configure);
62
63 ~InputTraceSession();
64
65 void expectMotionTraced(Level level, const MotionEvent& event);
66
67 void expectKeyTraced(Level level, const KeyEvent& event);
68
69 struct WindowDispatchEvent {
70 std::variant<KeyEvent, MotionEvent> event;
71 sp<FakeWindowHandle> window;
72 };
73 void expectDispatchTraced(Level level, const WindowDispatchEvent& event);
74
75private:
76 std::unique_ptr<perfetto::TracingSession> mPerfettoSession;
77 ArrayMap<WindowDispatchEvent, Level> mExpectedWindowDispatches;
78 ArrayMap<MotionEvent, Level> mExpectedMotions;
79 ArrayMap<KeyEvent, Level> mExpectedKeys;
80
81 void verifyExpectations(const std::string& rawTrace);
82};
83
84} // namespace android