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