Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 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 "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 Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 25 | #include <perfetto/trace/trace.pbzero.h> |
| 26 | #include <perfetto/tracing.h> |
| 27 | #include <variant> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | /** |
| 33 | * Tracing level constants used for adding expectations to the InputTraceSession. |
| 34 | */ |
| 35 | enum class Level { |
| 36 | NONE, |
| 37 | REDACTED, |
| 38 | COMPLETE, |
| 39 | }; |
| 40 | |
| 41 | template <typename K, typename V> |
| 42 | using 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 | */ |
| 56 | class InputTraceSession { |
| 57 | public: |
| 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 | |
| 75 | private: |
| 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 |