diff options
author | 2025-01-28 14:56:05 -0800 | |
---|---|---|
committer | 2025-01-28 14:56:05 -0800 | |
commit | 7fd4c33d2bc17ca43177787153d063ea1b4a360d (patch) | |
tree | 74badef32fa6e28e0c2cdbe82f782f5979ec0dfc | |
parent | 3001fd617e795e68f67faae4151cf55012d411af (diff) | |
parent | ea395bfbeb4a53e3c9b7466b7031a8c8819d7659 (diff) |
Merge changes I73c090e8,I03e0960f into main
* changes:
Add tests for AndroidInputEventProtoConverter::toProtoMotionEvent
AndroidInputEventProtoConverter: Move impl into templated header
6 files changed, 459 insertions, 227 deletions
diff --git a/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.cpp b/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.cpp deleted file mode 100644 index 9039751959..0000000000 --- a/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.cpp +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "AndroidInputEventProtoConverter.h" - -#include <android-base/logging.h> -#include <perfetto/trace/android/android_input_event.pbzero.h> - -namespace android::inputdispatcher::trace { - -namespace { - -using namespace ftl::flag_operators; - -// The trace config to use for maximal tracing. -const impl::TraceConfig CONFIG_TRACE_ALL{ - .flags = impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS | - impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH, - .rules = {impl::TraceRule{.level = impl::TraceLevel::TRACE_LEVEL_COMPLETE, - .matchAllPackages = {}, - .matchAnyPackages = {}, - .matchSecure{}, - .matchImeConnectionActive = {}}}, -}; - -} // namespace - -void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event, - proto::AndroidMotionEvent& outProto, - bool isRedacted) { - outProto.set_event_id(event.id); - outProto.set_event_time_nanos(event.eventTime); - outProto.set_down_time_nanos(event.downTime); - outProto.set_source(event.source); - outProto.set_action(event.action); - outProto.set_device_id(event.deviceId); - outProto.set_display_id(event.displayId.val()); - outProto.set_classification(static_cast<int32_t>(event.classification)); - outProto.set_flags(event.flags); - outProto.set_policy_flags(event.policyFlags); - outProto.set_button_state(event.buttonState); - outProto.set_action_button(event.actionButton); - - if (!isRedacted) { - outProto.set_cursor_position_x(event.xCursorPosition); - outProto.set_cursor_position_y(event.yCursorPosition); - outProto.set_meta_state(event.metaState); - outProto.set_precision_x(event.xPrecision); - outProto.set_precision_y(event.yPrecision); - } - - for (uint32_t i = 0; i < event.pointerProperties.size(); i++) { - auto* pointer = outProto.add_pointer(); - - const auto& props = event.pointerProperties[i]; - pointer->set_pointer_id(props.id); - pointer->set_tool_type(static_cast<int32_t>(props.toolType)); - - const auto& coords = event.pointerCoords[i]; - auto bits = BitSet64(coords.bits); - for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { - const auto axis = bits.clearFirstMarkedBit(); - auto axisEntry = pointer->add_axis_value(); - axisEntry->set_axis(axis); - - if (!isRedacted) { - axisEntry->set_value(coords.values[axisIndex]); - } - } - } -} - -void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event, - proto::AndroidKeyEvent& outProto, - bool isRedacted) { - outProto.set_event_id(event.id); - outProto.set_event_time_nanos(event.eventTime); - outProto.set_down_time_nanos(event.downTime); - outProto.set_source(event.source); - outProto.set_action(event.action); - outProto.set_device_id(event.deviceId); - outProto.set_display_id(event.displayId.val()); - outProto.set_repeat_count(event.repeatCount); - outProto.set_flags(event.flags); - outProto.set_policy_flags(event.policyFlags); - - if (!isRedacted) { - outProto.set_key_code(event.keyCode); - outProto.set_scan_code(event.scanCode); - outProto.set_meta_state(event.metaState); - } -} - -void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent( - const WindowDispatchArgs& args, proto::AndroidWindowInputDispatchEvent& outProto, - bool isRedacted) { - std::visit([&](auto entry) { outProto.set_event_id(entry.id); }, args.eventEntry); - outProto.set_vsync_id(args.vsyncId); - outProto.set_window_id(args.windowId); - outProto.set_resolved_flags(args.resolvedFlags); - - if (isRedacted) { - return; - } - if (auto* motion = std::get_if<TracedMotionEvent>(&args.eventEntry); motion != nullptr) { - for (size_t i = 0; i < motion->pointerProperties.size(); i++) { - auto* pointerProto = outProto.add_dispatched_pointer(); - pointerProto->set_pointer_id(motion->pointerProperties[i].id); - const auto& coords = motion->pointerCoords[i]; - const auto rawXY = - MotionEvent::calculateTransformedXY(motion->source, args.rawTransform, - coords.getXYValue()); - if (coords.getXYValue() != rawXY) { - // These values are only traced if they were modified by the raw transform - // to save space. Trace consumers should be aware of this optimization. - pointerProto->set_x_in_display(rawXY.x); - pointerProto->set_y_in_display(rawXY.y); - } - - const auto coordsInWindow = - MotionEvent::calculateTransformedCoords(motion->source, motion->flags, - args.transform, coords); - auto bits = BitSet64(coords.bits); - for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { - const uint32_t axis = bits.clearFirstMarkedBit(); - const float axisValueInWindow = coordsInWindow.values[axisIndex]; - // Only values that are modified by the window transform are traced. - if (coords.values[axisIndex] != axisValueInWindow) { - auto* axisEntry = pointerProto->add_axis_value_in_window(); - axisEntry->set_axis(axis); - axisEntry->set_value(axisValueInWindow); - } - } - } - } -} - -impl::TraceConfig AndroidInputEventProtoConverter::parseConfig( - proto::AndroidInputEventConfig::Decoder& protoConfig) { - if (protoConfig.has_mode() && - protoConfig.mode() == proto::AndroidInputEventConfig::TRACE_MODE_TRACE_ALL) { - // User has requested the preset for maximal tracing - return CONFIG_TRACE_ALL; - } - - impl::TraceConfig config; - - // Parse trace flags - if (protoConfig.has_trace_dispatcher_input_events() && - protoConfig.trace_dispatcher_input_events()) { - config.flags |= impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS; - } - if (protoConfig.has_trace_dispatcher_window_dispatch() && - protoConfig.trace_dispatcher_window_dispatch()) { - config.flags |= impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH; - } - - // Parse trace rules - auto rulesIt = protoConfig.rules(); - while (rulesIt) { - proto::AndroidInputEventConfig::TraceRule::Decoder protoRule{rulesIt->as_bytes()}; - config.rules.emplace_back(); - auto& rule = config.rules.back(); - - rule.level = protoRule.has_trace_level() - ? static_cast<impl::TraceLevel>(protoRule.trace_level()) - : impl::TraceLevel::TRACE_LEVEL_NONE; - - if (protoRule.has_match_all_packages()) { - auto pkgIt = protoRule.match_all_packages(); - while (pkgIt) { - rule.matchAllPackages.emplace_back(pkgIt->as_std_string()); - pkgIt++; - } - } - - if (protoRule.has_match_any_packages()) { - auto pkgIt = protoRule.match_any_packages(); - while (pkgIt) { - rule.matchAnyPackages.emplace_back(pkgIt->as_std_string()); - pkgIt++; - } - } - - if (protoRule.has_match_secure()) { - rule.matchSecure = protoRule.match_secure(); - } - - if (protoRule.has_match_ime_connection_active()) { - rule.matchImeConnectionActive = protoRule.match_ime_connection_active(); - } - - rulesIt++; - } - - return config; -} - -} // namespace android::inputdispatcher::trace diff --git a/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.h b/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.h index 887913f463..eb33e2b894 100644 --- a/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.h +++ b/services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.h @@ -26,20 +26,198 @@ namespace proto = perfetto::protos::pbzero; namespace android::inputdispatcher::trace { +namespace internal { + +using namespace ftl::flag_operators; + +// The trace config to use for maximal tracing. +const impl::TraceConfig CONFIG_TRACE_ALL{ + .flags = impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS | + impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH, + .rules = {impl::TraceRule{.level = impl::TraceLevel::TRACE_LEVEL_COMPLETE, + .matchAllPackages = {}, + .matchAnyPackages = {}, + .matchSecure{}, + .matchImeConnectionActive = {}}}, +}; + +} // namespace internal + /** * Write traced events into Perfetto protos. + * + * This class is templated so that the logic can be tested while substituting the proto classes + * auto-generated by Perfetto's pbzero library with mock implementations. */ +template <typename ProtoMotion, typename ProtoKey, typename ProtoDispatch, + typename ProtoConfigDecoder> class AndroidInputEventProtoConverter { public: - static void toProtoMotionEvent(const TracedMotionEvent& event, - proto::AndroidMotionEvent& outProto, bool isRedacted); - static void toProtoKeyEvent(const TracedKeyEvent& event, proto::AndroidKeyEvent& outProto, - bool isRedacted); - static void toProtoWindowDispatchEvent(const WindowDispatchArgs&, - proto::AndroidWindowInputDispatchEvent& outProto, - bool isRedacted); - - static impl::TraceConfig parseConfig(proto::AndroidInputEventConfig::Decoder& protoConfig); + static void toProtoMotionEvent(const TracedMotionEvent& event, ProtoMotion& outProto, + bool isRedacted) { + outProto.set_event_id(event.id); + outProto.set_event_time_nanos(event.eventTime); + outProto.set_down_time_nanos(event.downTime); + outProto.set_source(event.source); + outProto.set_action(event.action); + outProto.set_device_id(event.deviceId); + outProto.set_display_id(event.displayId.val()); + outProto.set_classification(static_cast<int32_t>(event.classification)); + outProto.set_flags(event.flags); + outProto.set_policy_flags(event.policyFlags); + outProto.set_button_state(event.buttonState); + outProto.set_action_button(event.actionButton); + + if (!isRedacted) { + outProto.set_cursor_position_x(event.xCursorPosition); + outProto.set_cursor_position_y(event.yCursorPosition); + outProto.set_meta_state(event.metaState); + outProto.set_precision_x(event.xPrecision); + outProto.set_precision_y(event.yPrecision); + } + + for (uint32_t i = 0; i < event.pointerProperties.size(); i++) { + auto* pointer = outProto.add_pointer(); + + const auto& props = event.pointerProperties[i]; + pointer->set_pointer_id(props.id); + pointer->set_tool_type(static_cast<int32_t>(props.toolType)); + + const auto& coords = event.pointerCoords[i]; + auto bits = BitSet64(coords.bits); + for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { + const auto axis = bits.clearFirstMarkedBit(); + auto axisEntry = pointer->add_axis_value(); + axisEntry->set_axis(axis); + + if (!isRedacted) { + axisEntry->set_value(coords.values[axisIndex]); + } + } + } + } + + static void toProtoKeyEvent(const TracedKeyEvent& event, ProtoKey& outProto, bool isRedacted) { + outProto.set_event_id(event.id); + outProto.set_event_time_nanos(event.eventTime); + outProto.set_down_time_nanos(event.downTime); + outProto.set_source(event.source); + outProto.set_action(event.action); + outProto.set_device_id(event.deviceId); + outProto.set_display_id(event.displayId.val()); + outProto.set_repeat_count(event.repeatCount); + outProto.set_flags(event.flags); + outProto.set_policy_flags(event.policyFlags); + + if (!isRedacted) { + outProto.set_key_code(event.keyCode); + outProto.set_scan_code(event.scanCode); + outProto.set_meta_state(event.metaState); + } + } + + static void toProtoWindowDispatchEvent(const WindowDispatchArgs& args, ProtoDispatch& outProto, + bool isRedacted) { + std::visit([&](auto entry) { outProto.set_event_id(entry.id); }, args.eventEntry); + outProto.set_vsync_id(args.vsyncId); + outProto.set_window_id(args.windowId); + outProto.set_resolved_flags(args.resolvedFlags); + + if (isRedacted) { + return; + } + if (auto* motion = std::get_if<TracedMotionEvent>(&args.eventEntry); motion != nullptr) { + for (size_t i = 0; i < motion->pointerProperties.size(); i++) { + auto* pointerProto = outProto.add_dispatched_pointer(); + pointerProto->set_pointer_id(motion->pointerProperties[i].id); + const auto& coords = motion->pointerCoords[i]; + const auto rawXY = + MotionEvent::calculateTransformedXY(motion->source, args.rawTransform, + coords.getXYValue()); + if (coords.getXYValue() != rawXY) { + // These values are only traced if they were modified by the raw transform + // to save space. Trace consumers should be aware of this optimization. + pointerProto->set_x_in_display(rawXY.x); + pointerProto->set_y_in_display(rawXY.y); + } + + const auto coordsInWindow = + MotionEvent::calculateTransformedCoords(motion->source, motion->flags, + args.transform, coords); + auto bits = BitSet64(coords.bits); + for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { + const uint32_t axis = bits.clearFirstMarkedBit(); + const float axisValueInWindow = coordsInWindow.values[axisIndex]; + // Only values that are modified by the window transform are traced. + if (coords.values[axisIndex] != axisValueInWindow) { + auto* axisEntry = pointerProto->add_axis_value_in_window(); + axisEntry->set_axis(axis); + axisEntry->set_value(axisValueInWindow); + } + } + } + } + } + + static impl::TraceConfig parseConfig(ProtoConfigDecoder& protoConfig) { + if (protoConfig.has_mode() && + protoConfig.mode() == proto::AndroidInputEventConfig::TRACE_MODE_TRACE_ALL) { + // User has requested the preset for maximal tracing + return internal::CONFIG_TRACE_ALL; + } + + impl::TraceConfig config; + + // Parse trace flags + if (protoConfig.has_trace_dispatcher_input_events() && + protoConfig.trace_dispatcher_input_events()) { + config.flags |= impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS; + } + if (protoConfig.has_trace_dispatcher_window_dispatch() && + protoConfig.trace_dispatcher_window_dispatch()) { + config.flags |= impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH; + } + + // Parse trace rules + auto rulesIt = protoConfig.rules(); + while (rulesIt) { + proto::AndroidInputEventConfig::TraceRule::Decoder protoRule{rulesIt->as_bytes()}; + config.rules.emplace_back(); + auto& rule = config.rules.back(); + + rule.level = protoRule.has_trace_level() + ? static_cast<impl::TraceLevel>(protoRule.trace_level()) + : impl::TraceLevel::TRACE_LEVEL_NONE; + + if (protoRule.has_match_all_packages()) { + auto pkgIt = protoRule.match_all_packages(); + while (pkgIt) { + rule.matchAllPackages.emplace_back(pkgIt->as_std_string()); + pkgIt++; + } + } + + if (protoRule.has_match_any_packages()) { + auto pkgIt = protoRule.match_any_packages(); + while (pkgIt) { + rule.matchAnyPackages.emplace_back(pkgIt->as_std_string()); + pkgIt++; + } + } + + if (protoRule.has_match_secure()) { + rule.matchSecure = protoRule.match_secure(); + } + + if (protoRule.has_match_ime_connection_active()) { + rule.matchImeConnectionActive = protoRule.match_ime_connection_active(); + } + + rulesIt++; + } + + return config; + } }; } // namespace android::inputdispatcher::trace diff --git a/services/inputflinger/dispatcher/trace/InputTracingBackendInterface.h b/services/inputflinger/dispatcher/trace/InputTracingBackendInterface.h index 761d619cec..823f8a5882 100644 --- a/services/inputflinger/dispatcher/trace/InputTracingBackendInterface.h +++ b/services/inputflinger/dispatcher/trace/InputTracingBackendInterface.h @@ -70,7 +70,7 @@ struct TracedMotionEvent { uint32_t policyFlags; int32_t deviceId; uint32_t source; - ui::LogicalDisplayId displayId; + ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID; int32_t action; int32_t actionButton; int32_t flags; diff --git a/services/inputflinger/dispatcher/trace/InputTracingPerfettoBackend.cpp b/services/inputflinger/dispatcher/trace/InputTracingPerfettoBackend.cpp index 77b5c2ebcd..ebcd9c986e 100644 --- a/services/inputflinger/dispatcher/trace/InputTracingPerfettoBackend.cpp +++ b/services/inputflinger/dispatcher/trace/InputTracingPerfettoBackend.cpp @@ -34,6 +34,11 @@ namespace { constexpr auto INPUT_EVENT_TRACE_DATA_SOURCE_NAME = "android.input.inputevent"; +using ProtoConverter = + AndroidInputEventProtoConverter<proto::AndroidMotionEvent, proto::AndroidKeyEvent, + proto::AndroidWindowInputDispatchEvent, + proto::AndroidInputEventConfig::Decoder>; + bool isPermanentlyAllowed(gui::Uid uid) { switch (uid.val()) { case AID_SYSTEM: @@ -85,7 +90,7 @@ void PerfettoBackend::InputEventDataSource::OnSetup(const InputEventDataSource:: const auto rawConfig = args.config->android_input_event_config_raw(); auto protoConfig = perfetto::protos::pbzero::AndroidInputEventConfig::Decoder{rawConfig}; - mConfig = AndroidInputEventProtoConverter::parseConfig(protoConfig); + mConfig = ProtoConverter::parseConfig(protoConfig); } void PerfettoBackend::InputEventDataSource::OnStart(const InputEventDataSource::StartArgs&) { @@ -238,7 +243,7 @@ void PerfettoBackend::traceMotionEvent(const TracedMotionEvent& event, auto* inputEvent = winscopeExtensions->set_android_input_event(); auto* dispatchMotion = isRedacted ? inputEvent->set_dispatcher_motion_event_redacted() : inputEvent->set_dispatcher_motion_event(); - AndroidInputEventProtoConverter::toProtoMotionEvent(event, *dispatchMotion, isRedacted); + ProtoConverter::toProtoMotionEvent(event, *dispatchMotion, isRedacted); }); } @@ -266,7 +271,7 @@ void PerfettoBackend::traceKeyEvent(const TracedKeyEvent& event, auto* inputEvent = winscopeExtensions->set_android_input_event(); auto* dispatchKey = isRedacted ? inputEvent->set_dispatcher_key_event_redacted() : inputEvent->set_dispatcher_key_event(); - AndroidInputEventProtoConverter::toProtoKeyEvent(event, *dispatchKey, isRedacted); + ProtoConverter::toProtoKeyEvent(event, *dispatchKey, isRedacted); }); } @@ -295,8 +300,7 @@ void PerfettoBackend::traceWindowDispatch(const WindowDispatchArgs& dispatchArgs auto* dispatchEvent = isRedacted ? inputEvent->set_dispatcher_window_dispatch_event_redacted() : inputEvent->set_dispatcher_window_dispatch_event(); - AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(dispatchArgs, *dispatchEvent, - isRedacted); + ProtoConverter::toProtoWindowDispatchEvent(dispatchArgs, *dispatchEvent, isRedacted); }); } diff --git a/services/inputflinger/tests/Android.bp b/services/inputflinger/tests/Android.bp index 600ae526f1..f678bbe650 100644 --- a/services/inputflinger/tests/Android.bp +++ b/services/inputflinger/tests/Android.bp @@ -48,6 +48,7 @@ cc_test { ], srcs: [ ":inputdispatcher_common_test_sources", + "AndroidInputEventProtoConverter_test.cpp", "AnrTracker_test.cpp", "CapturedTouchpadEventConverter_test.cpp", "CursorInputMapper_test.cpp", diff --git a/services/inputflinger/tests/AndroidInputEventProtoConverter_test.cpp b/services/inputflinger/tests/AndroidInputEventProtoConverter_test.cpp new file mode 100644 index 0000000000..414da66cd2 --- /dev/null +++ b/services/inputflinger/tests/AndroidInputEventProtoConverter_test.cpp @@ -0,0 +1,261 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../dispatcher/trace/AndroidInputEventProtoConverter.h" + +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +namespace android::inputdispatcher::trace { + +namespace { + +using testing::Return, testing::_; + +class MockProtoAxisValue { +public: + MOCK_METHOD(void, set_axis, (int32_t)); + MOCK_METHOD(void, set_value, (float)); +}; + +class MockProtoPointer { +public: + MOCK_METHOD(void, set_pointer_id, (uint32_t)); + MOCK_METHOD(void, set_tool_type, (int32_t)); + MOCK_METHOD(MockProtoAxisValue*, add_axis_value, ()); +}; + +class MockProtoMotion { +public: + MOCK_METHOD(void, set_event_id, (uint32_t)); + MOCK_METHOD(void, set_event_time_nanos, (int64_t)); + MOCK_METHOD(void, set_down_time_nanos, (int64_t)); + MOCK_METHOD(void, set_source, (uint32_t)); + MOCK_METHOD(void, set_action, (int32_t)); + MOCK_METHOD(void, set_device_id, (uint32_t)); + MOCK_METHOD(void, set_display_id, (uint32_t)); + MOCK_METHOD(void, set_classification, (int32_t)); + MOCK_METHOD(void, set_flags, (uint32_t)); + MOCK_METHOD(void, set_policy_flags, (uint32_t)); + MOCK_METHOD(void, set_button_state, (uint32_t)); + MOCK_METHOD(void, set_action_button, (uint32_t)); + MOCK_METHOD(void, set_cursor_position_x, (float)); + MOCK_METHOD(void, set_cursor_position_y, (float)); + MOCK_METHOD(void, set_meta_state, (uint32_t)); + MOCK_METHOD(void, set_precision_x, (float)); + MOCK_METHOD(void, set_precision_y, (float)); + MOCK_METHOD(MockProtoPointer*, add_pointer, ()); +}; + +using TestProtoConverter = AndroidInputEventProtoConverter<MockProtoMotion, proto::AndroidKeyEvent, + proto::AndroidWindowInputDispatchEvent, + proto::AndroidInputEventConfig::Decoder>; + +TEST(AndroidInputEventProtoConverterTest, ToProtoMotionEvent) { + TracedMotionEvent event{}; + event.id = 1; + event.eventTime = 2; + event.downTime = 3; + event.source = AINPUT_SOURCE_MOUSE; + event.action = AMOTION_EVENT_ACTION_BUTTON_PRESS; + event.deviceId = 4; + event.displayId = ui::LogicalDisplayId(5); + event.classification = MotionClassification::PINCH; + event.flags = 6; + event.policyFlags = 7; + event.buttonState = 8; + event.actionButton = 9; + event.xCursorPosition = 10.0f; + event.yCursorPosition = 11.0f; + event.metaState = 12; + event.xPrecision = 13.0f; + event.yPrecision = 14.0f; + event.pointerProperties.emplace_back(PointerProperties{ + .id = 15, + .toolType = ToolType::MOUSE, + }); + event.pointerProperties.emplace_back(PointerProperties{ + .id = 16, + .toolType = ToolType::FINGER, + }); + event.pointerCoords.emplace_back(); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 17.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 18.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 19.0f); + event.pointerCoords.emplace_back(); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 20.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 21.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 22.0f); + + testing::StrictMock<MockProtoMotion> proto; + testing::StrictMock<MockProtoPointer> pointer1; + testing::StrictMock<MockProtoPointer> pointer2; + testing::StrictMock<MockProtoAxisValue> axisValue1; + testing::StrictMock<MockProtoAxisValue> axisValue2; + testing::StrictMock<MockProtoAxisValue> axisValue3; + testing::StrictMock<MockProtoAxisValue> axisValue4; + testing::StrictMock<MockProtoAxisValue> axisValue5; + testing::StrictMock<MockProtoAxisValue> axisValue6; + + EXPECT_CALL(proto, set_event_id(1)); + EXPECT_CALL(proto, set_event_time_nanos(2)); + EXPECT_CALL(proto, set_down_time_nanos(3)); + EXPECT_CALL(proto, set_source(AINPUT_SOURCE_MOUSE)); + EXPECT_CALL(proto, set_action(AMOTION_EVENT_ACTION_BUTTON_PRESS)); + EXPECT_CALL(proto, set_device_id(4)); + EXPECT_CALL(proto, set_display_id(5)); + EXPECT_CALL(proto, set_classification(AMOTION_EVENT_CLASSIFICATION_PINCH)); + EXPECT_CALL(proto, set_flags(6)); + EXPECT_CALL(proto, set_policy_flags(7)); + EXPECT_CALL(proto, set_button_state(8)); + EXPECT_CALL(proto, set_action_button(9)); + EXPECT_CALL(proto, set_cursor_position_x(10.0f)); + EXPECT_CALL(proto, set_cursor_position_y(11.0f)); + EXPECT_CALL(proto, set_meta_state(12)); + EXPECT_CALL(proto, set_precision_x(13.0f)); + EXPECT_CALL(proto, set_precision_y(14.0f)); + + EXPECT_CALL(proto, add_pointer()).WillOnce(Return(&pointer1)).WillOnce(Return(&pointer2)); + + EXPECT_CALL(pointer1, set_pointer_id(15)); + EXPECT_CALL(pointer1, set_tool_type(AMOTION_EVENT_TOOL_TYPE_MOUSE)); + EXPECT_CALL(pointer1, add_axis_value()) + .WillOnce(Return(&axisValue1)) + .WillOnce(Return(&axisValue2)) + .WillOnce(Return(&axisValue3)); + EXPECT_CALL(axisValue1, set_axis(AMOTION_EVENT_AXIS_X)); + EXPECT_CALL(axisValue1, set_value(17.0f)); + EXPECT_CALL(axisValue2, set_axis(AMOTION_EVENT_AXIS_Y)); + EXPECT_CALL(axisValue2, set_value(18.0f)); + EXPECT_CALL(axisValue3, set_axis(AMOTION_EVENT_AXIS_PRESSURE)); + EXPECT_CALL(axisValue3, set_value(19.0f)); + + EXPECT_CALL(pointer2, set_pointer_id(16)); + EXPECT_CALL(pointer2, set_tool_type(AMOTION_EVENT_TOOL_TYPE_FINGER)); + EXPECT_CALL(pointer2, add_axis_value()) + .WillOnce(Return(&axisValue4)) + .WillOnce(Return(&axisValue5)) + .WillOnce(Return(&axisValue6)); + EXPECT_CALL(axisValue4, set_axis(AMOTION_EVENT_AXIS_X)); + EXPECT_CALL(axisValue4, set_value(20.0f)); + EXPECT_CALL(axisValue5, set_axis(AMOTION_EVENT_AXIS_Y)); + EXPECT_CALL(axisValue5, set_value(21.0f)); + EXPECT_CALL(axisValue6, set_axis(AMOTION_EVENT_AXIS_PRESSURE)); + EXPECT_CALL(axisValue6, set_value(22.0f)); + + TestProtoConverter::toProtoMotionEvent(event, proto, /*isRedacted=*/false); +} + +TEST(AndroidInputEventProtoConverterTest, ToProtoMotionEvent_Redacted) { + TracedMotionEvent event{}; + event.id = 1; + event.eventTime = 2; + event.downTime = 3; + event.source = AINPUT_SOURCE_MOUSE; + event.action = AMOTION_EVENT_ACTION_BUTTON_PRESS; + event.deviceId = 4; + event.displayId = ui::LogicalDisplayId(5); + event.classification = MotionClassification::PINCH; + event.flags = 6; + event.policyFlags = 7; + event.buttonState = 8; + event.actionButton = 9; + event.xCursorPosition = 10.0f; + event.yCursorPosition = 11.0f; + event.metaState = 12; + event.xPrecision = 13.0f; + event.yPrecision = 14.0f; + event.pointerProperties.emplace_back(PointerProperties{ + .id = 15, + .toolType = ToolType::MOUSE, + }); + event.pointerProperties.emplace_back(PointerProperties{ + .id = 16, + .toolType = ToolType::FINGER, + }); + event.pointerCoords.emplace_back(); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 17.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 18.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 19.0f); + event.pointerCoords.emplace_back(); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 20.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 21.0f); + event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 22.0f); + + testing::StrictMock<MockProtoMotion> proto; + testing::StrictMock<MockProtoPointer> pointer1; + testing::StrictMock<MockProtoPointer> pointer2; + testing::StrictMock<MockProtoAxisValue> axisValue1; + testing::StrictMock<MockProtoAxisValue> axisValue2; + testing::StrictMock<MockProtoAxisValue> axisValue3; + testing::StrictMock<MockProtoAxisValue> axisValue4; + testing::StrictMock<MockProtoAxisValue> axisValue5; + testing::StrictMock<MockProtoAxisValue> axisValue6; + + EXPECT_CALL(proto, set_event_id(1)); + EXPECT_CALL(proto, set_event_time_nanos(2)); + EXPECT_CALL(proto, set_down_time_nanos(3)); + EXPECT_CALL(proto, set_source(AINPUT_SOURCE_MOUSE)); + EXPECT_CALL(proto, set_action(AMOTION_EVENT_ACTION_BUTTON_PRESS)); + EXPECT_CALL(proto, set_device_id(4)); + EXPECT_CALL(proto, set_display_id(5)); + EXPECT_CALL(proto, set_classification(AMOTION_EVENT_CLASSIFICATION_PINCH)); + EXPECT_CALL(proto, set_flags(6)); + EXPECT_CALL(proto, set_policy_flags(7)); + EXPECT_CALL(proto, set_button_state(8)); + EXPECT_CALL(proto, set_action_button(9)); + + EXPECT_CALL(proto, add_pointer()).WillOnce(Return(&pointer1)).WillOnce(Return(&pointer2)); + + EXPECT_CALL(pointer1, set_pointer_id(15)); + EXPECT_CALL(pointer1, set_tool_type(AMOTION_EVENT_TOOL_TYPE_MOUSE)); + EXPECT_CALL(pointer1, add_axis_value()) + .WillOnce(Return(&axisValue1)) + .WillOnce(Return(&axisValue2)) + .WillOnce(Return(&axisValue3)); + EXPECT_CALL(axisValue1, set_axis(AMOTION_EVENT_AXIS_X)); + EXPECT_CALL(axisValue2, set_axis(AMOTION_EVENT_AXIS_Y)); + EXPECT_CALL(axisValue3, set_axis(AMOTION_EVENT_AXIS_PRESSURE)); + + EXPECT_CALL(pointer2, set_pointer_id(16)); + EXPECT_CALL(pointer2, set_tool_type(AMOTION_EVENT_TOOL_TYPE_FINGER)); + EXPECT_CALL(pointer2, add_axis_value()) + .WillOnce(Return(&axisValue4)) + .WillOnce(Return(&axisValue5)) + .WillOnce(Return(&axisValue6)); + EXPECT_CALL(axisValue4, set_axis(AMOTION_EVENT_AXIS_X)); + EXPECT_CALL(axisValue5, set_axis(AMOTION_EVENT_AXIS_Y)); + EXPECT_CALL(axisValue6, set_axis(AMOTION_EVENT_AXIS_PRESSURE)); + + // Redacted fields + EXPECT_CALL(proto, set_meta_state(_)).Times(0); + EXPECT_CALL(proto, set_cursor_position_x(_)).Times(0); + EXPECT_CALL(proto, set_cursor_position_y(_)).Times(0); + EXPECT_CALL(proto, set_precision_x(_)).Times(0); + EXPECT_CALL(proto, set_precision_y(_)).Times(0); + EXPECT_CALL(axisValue1, set_value(_)).Times(0); + EXPECT_CALL(axisValue2, set_value(_)).Times(0); + EXPECT_CALL(axisValue3, set_value(_)).Times(0); + EXPECT_CALL(axisValue4, set_value(_)).Times(0); + EXPECT_CALL(axisValue5, set_value(_)).Times(0); + EXPECT_CALL(axisValue6, set_value(_)).Times(0); + + TestProtoConverter::toProtoMotionEvent(event, proto, /*isRedacted=*/true); +} + +} // namespace + +} // namespace android::inputdispatcher::trace |