1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2024 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include "FakeWindows.h" 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h> 22*38e8c45fSAndroid Build Coastguard Worker #include <gtest/gtest.h> 23*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h> 24*38e8c45fSAndroid Build Coastguard Worker #include <perfetto/config/android/android_input_event_config.pbzero.h> 25*38e8c45fSAndroid Build Coastguard Worker #include <perfetto/trace/trace.pbzero.h> 26*38e8c45fSAndroid Build Coastguard Worker #include <perfetto/tracing.h> 27*38e8c45fSAndroid Build Coastguard Worker #include <variant> 28*38e8c45fSAndroid Build Coastguard Worker #include <vector> 29*38e8c45fSAndroid Build Coastguard Worker 30*38e8c45fSAndroid Build Coastguard Worker namespace android { 31*38e8c45fSAndroid Build Coastguard Worker 32*38e8c45fSAndroid Build Coastguard Worker /** 33*38e8c45fSAndroid Build Coastguard Worker * Tracing level constants used for adding expectations to the InputTraceSession. 34*38e8c45fSAndroid Build Coastguard Worker */ 35*38e8c45fSAndroid Build Coastguard Worker enum class Level { 36*38e8c45fSAndroid Build Coastguard Worker NONE, 37*38e8c45fSAndroid Build Coastguard Worker REDACTED, 38*38e8c45fSAndroid Build Coastguard Worker COMPLETE, 39*38e8c45fSAndroid Build Coastguard Worker }; 40*38e8c45fSAndroid Build Coastguard Worker 41*38e8c45fSAndroid Build Coastguard Worker template <typename K, typename V> 42*38e8c45fSAndroid Build Coastguard Worker using ArrayMap = std::vector<std::pair<K, V>>; 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker /** 45*38e8c45fSAndroid Build Coastguard Worker * A scoped representation of a tracing session that is used to make assertions on the trace. 46*38e8c45fSAndroid Build Coastguard Worker * 47*38e8c45fSAndroid Build Coastguard Worker * When the trace session is created, an "android.input.inputevent" trace will be started 48*38e8c45fSAndroid Build Coastguard Worker * synchronously with the given configuration. While the trace is ongoing, the caller must 49*38e8c45fSAndroid Build Coastguard Worker * specify the events that are expected to be in the trace using the expect* methods. 50*38e8c45fSAndroid Build Coastguard Worker * 51*38e8c45fSAndroid Build Coastguard Worker * When the session is destroyed, the trace is stopped synchronously, and all expectations will 52*38e8c45fSAndroid Build Coastguard Worker * be verified using the gtest framework. This acts as a strict verifier, where the verification 53*38e8c45fSAndroid Build Coastguard Worker * will fail both if an expected event does not show up in the trace and if there is an extra 54*38e8c45fSAndroid Build Coastguard Worker * event in the trace that was not expected. Ordering is NOT verified for any events. 55*38e8c45fSAndroid Build Coastguard Worker */ 56*38e8c45fSAndroid Build Coastguard Worker class InputTraceSession { 57*38e8c45fSAndroid Build Coastguard Worker public: 58*38e8c45fSAndroid Build Coastguard Worker explicit InputTraceSession( 59*38e8c45fSAndroid Build Coastguard Worker std::function<void( 60*38e8c45fSAndroid Build Coastguard Worker protozero::HeapBuffered<perfetto::protos::pbzero::AndroidInputEventConfig>&)> 61*38e8c45fSAndroid Build Coastguard Worker configure); 62*38e8c45fSAndroid Build Coastguard Worker 63*38e8c45fSAndroid Build Coastguard Worker ~InputTraceSession(); 64*38e8c45fSAndroid Build Coastguard Worker 65*38e8c45fSAndroid Build Coastguard Worker void expectMotionTraced(Level level, const MotionEvent& event); 66*38e8c45fSAndroid Build Coastguard Worker 67*38e8c45fSAndroid Build Coastguard Worker void expectKeyTraced(Level level, const KeyEvent& event); 68*38e8c45fSAndroid Build Coastguard Worker 69*38e8c45fSAndroid Build Coastguard Worker struct WindowDispatchEvent { 70*38e8c45fSAndroid Build Coastguard Worker std::variant<KeyEvent, MotionEvent> event; 71*38e8c45fSAndroid Build Coastguard Worker sp<FakeWindowHandle> window; 72*38e8c45fSAndroid Build Coastguard Worker }; 73*38e8c45fSAndroid Build Coastguard Worker void expectDispatchTraced(Level level, const WindowDispatchEvent& event); 74*38e8c45fSAndroid Build Coastguard Worker 75*38e8c45fSAndroid Build Coastguard Worker private: 76*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<perfetto::TracingSession> mPerfettoSession; 77*38e8c45fSAndroid Build Coastguard Worker ArrayMap<WindowDispatchEvent, Level> mExpectedWindowDispatches; 78*38e8c45fSAndroid Build Coastguard Worker ArrayMap<MotionEvent, Level> mExpectedMotions; 79*38e8c45fSAndroid Build Coastguard Worker ArrayMap<KeyEvent, Level> mExpectedKeys; 80*38e8c45fSAndroid Build Coastguard Worker 81*38e8c45fSAndroid Build Coastguard Worker void verifyExpectations(const std::string& rawTrace); 82*38e8c45fSAndroid Build Coastguard Worker }; 83*38e8c45fSAndroid Build Coastguard Worker 84*38e8c45fSAndroid Build Coastguard Worker } // namespace android 85