diff options
author | 2023-10-03 14:04:18 -0700 | |
---|---|---|
committer | 2023-10-04 09:58:21 -0700 | |
commit | 88daa90013a028ecb62dba46113d3935b98dcbc6 (patch) | |
tree | 3706daf385cca42368cc8dde0177be7b2da3e8d6 | |
parent | cc89e85f3aa9225d977d3f78197307ec2f6f47e1 (diff) |
Allow values from SourceClass inside rust Source
Inside ViewTest::testOnTouchEventScroll, some MotionEvents are created
without a valid source. During injection, they get appended with
SourceClass::Pointer.
If these events are sent into the InputVerifier, it's basically being
asked to convert 0x2 === AINPUT_SOURCE_CLASS_POINTER into rust's
input::Source.
This fails during unwrap:
$ TEST=libinput_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
[ RUN ] InputVerifierTest.BadSourceProcess
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', frameworks/native/libs/input/rust/lib.rs:84:35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
Aborted
To mitigate this, add the SourceClass definitions into Source, thus
allowing such conversions.
Bug: 303143553
Bug: 211379801
Test: TEST=libinput_rust_test; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
Test: TEST=libinput_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
Change-Id: I46415d88251937959104bb82a3976e72ffcfcaf9
-rw-r--r-- | libs/input/rust/input.rs | 24 | ||||
-rw-r--r-- | libs/input/tests/InputVerifier_test.cpp | 25 |
2 files changed, 49 insertions, 0 deletions
diff --git a/libs/input/rust/input.rs b/libs/input/rust/input.rs index 9725b00212..804f96db98 100644 --- a/libs/input/rust/input.rs +++ b/libs/input/rust/input.rs @@ -35,7 +35,20 @@ pub enum SourceClass { bitflags! { /// Source of the input device or input events. + #[derive(Debug, PartialEq)] pub struct Source: u32 { + // Constants from SourceClass, added here for compatibility reasons + /// SourceClass::Button + const SourceClassButton = SourceClass::Button as u32; + /// SourceClass::Pointer + const SourceClassPointer = SourceClass::Pointer as u32; + /// SourceClass::Navigation + const SourceClassNavigation = SourceClass::Navigation as u32; + /// SourceClass::Position + const SourceClassPosition = SourceClass::Position as u32; + /// SourceClass::Joystick + const SourceClassJoystick = SourceClass::Joystick as u32; + /// SOURCE_UNKNOWN const Unknown = input_bindgen::AINPUT_SOURCE_UNKNOWN; /// SOURCE_KEYBOARD @@ -190,3 +203,14 @@ impl Source { self.bits() & class_bits == class_bits } } + +#[cfg(test)] +mod tests { + use crate::input::SourceClass; + use crate::Source; + #[test] + fn convert_source_class_pointer() { + let source = Source::from_bits(input_bindgen::AINPUT_SOURCE_CLASS_POINTER).unwrap(); + assert!(source.is_from_class(SourceClass::Pointer)); + } +} diff --git a/libs/input/tests/InputVerifier_test.cpp b/libs/input/tests/InputVerifier_test.cpp index e24fa6ed0b..e2eb08096b 100644 --- a/libs/input/tests/InputVerifier_test.cpp +++ b/libs/input/tests/InputVerifier_test.cpp @@ -20,10 +20,35 @@ namespace android { +using android::base::Result; + TEST(InputVerifierTest, CreationWithInvalidUtfStringDoesNotCrash) { constexpr char bytes[] = {static_cast<char>(0xC0), static_cast<char>(0x80)}; const std::string name(bytes, sizeof(bytes)); InputVerifier verifier(name); } +TEST(InputVerifierTest, ProcessSourceClassPointer) { + InputVerifier verifier("Verify testOnTouchEventScroll"); + + std::vector<PointerProperties> properties; + properties.push_back({}); + properties.back().clear(); + properties.back().id = 0; + properties.back().toolType = ToolType::UNKNOWN; + + std::vector<PointerCoords> coords; + coords.push_back({}); + coords.back().clear(); + coords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 75); + coords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 300); + + const Result<void> result = + verifier.processMovement(/*deviceId=*/0, AINPUT_SOURCE_CLASS_POINTER, + AMOTION_EVENT_ACTION_DOWN, + /*pointerCount=*/properties.size(), properties.data(), + coords.data(), /*flags=*/0); + ASSERT_TRUE(result.ok()); +} + } // namespace android |