| /* | |
| * Copyright (C) 2026 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 "MotionButtonInterceptor.h" | |
| #include <android/input.h> | |
| #include <android/keycodes.h> | |
| #include <gmock/gmock.h> | |
| #include <gtest/gtest.h> | |
| #include <input/Input.h> | |
| #include <MotionButtonInterceptorPolicyInterface.h> | |
| #include <NotifyArgs.h> | |
| #include <NotifyArgsBuilders.h> | |
| #include <cstdint> | |
| #include <memory> | |
| #include <variant> | |
| #include <vector> | |
| #include "TestInputListener.h" | |
| #include "TestEventMatchers.h" | |
| namespace android { | |
| using testing::AllOf; | |
| class MockMotionButtonInterceptorPolicy : public MotionButtonInterceptorPolicyInterface { | |
| public: | |
| MOCK_METHOD(void, notifyButtonIntercepted, | |
| (DeviceId deviceId, int32_t button, int32_t action, ui::LogicalDisplayId displayId), | |
| (override)); | |
| }; | |
| template <typename... V> | |
| struct Visitor : V... { | |
| using V::operator()...; | |
| }; | |
| template <typename... V> | |
| Visitor(V...) -> Visitor<V...>; | |
| class MotionButtonInterceptorTest : public testing::Test { | |
| protected: | |
| static constexpr DeviceId DEVICE_ID = 3; | |
| TestInputListener mTestListener; | |
| MockMotionButtonInterceptorPolicy mPolicy; | |
| std::unique_ptr<MotionButtonInterceptor> mInterceptor; | |
| void SetUp() override { | |
| mInterceptor = std::make_unique<MotionButtonInterceptor>(mTestListener, mPolicy); | |
| } | |
| NotifyMotionArgs createMouseEvent( | |
| int32_t action, int32_t buttonState, int32_t actionButton = 0, | |
| DeviceId deviceId = DEVICE_ID, | |
| ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT) { | |
| return MotionArgsBuilder(action, AINPUT_SOURCE_MOUSE) | |
| .deviceId(deviceId) | |
| .displayId(displayId) | |
| .buttonState(buttonState) | |
| .actionButton(actionButton) | |
| .pointer(PointerBuilder(0, ToolType::MOUSE).x(100.f).y(200.f)) | |
| .build(); | |
| } | |
| }; | |
| TEST_F(MotionButtonInterceptorTest, ForwardsArgsToInnerListener) { | |
| const std::vector<NotifyArgs> allArgs{ | |
| NotifyInputDevicesChangedArgs{}, | |
| NotifyWindowInfosArgs{}, | |
| KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) | |
| .deviceId(2) | |
| .keyCode(AKEYCODE_A) | |
| .eventTime(1000) | |
| .build(), | |
| createMouseEvent(AMOTION_EVENT_ACTION_DOWN, 0), | |
| NotifySwitchArgs{}, | |
| NotifySensorArgs{}, | |
| NotifyVibratorStateArgs{}, | |
| NotifyDeviceResetArgs{}, | |
| NotifyPointerCaptureChangedArgs{} | |
| }; | |
| for (const auto& notifyArgs : allArgs) { | |
| mInterceptor->notify(notifyArgs); | |
| std::visit(Visitor{ | |
| [&](const NotifyInputDevicesChangedArgs& args) { | |
| mTestListener.assertNotifyInputDevicesChangedWasCalled(); | |
| }, | |
| [&](const NotifyWindowInfosArgs& args) { | |
| mTestListener.assertNotifyWindowInfosWasCalled(); | |
| }, | |
| [&](const NotifyKeyArgs& args) { | |
| mTestListener.assertNotifyKeyWasCalled(testing::Eq(args)); | |
| }, | |
| [&](const NotifyMotionArgs& args) { | |
| mTestListener.assertNotifyMotionWasCalled(testing::Eq(args)); | |
| }, | |
| [&](const NotifySwitchArgs& args) { | |
| mTestListener.assertNotifySwitchWasCalled(testing::Eq(args)); | |
| }, | |
| [&](const NotifySensorArgs& args) { | |
| mTestListener.assertNotifySensorWasCalled(); | |
| }, | |
| [&](const NotifyVibratorStateArgs& args) { | |
| mTestListener.assertNotifyVibratorStateWasCalled(); | |
| }, | |
| [&](const NotifyDeviceResetArgs& args) { | |
| mTestListener.assertNotifyDeviceResetWasCalled(testing::Eq(args)); | |
| }, | |
| [&](const NotifyPointerCaptureChangedArgs& args) { | |
| mTestListener.assertNotifyCaptureWasCalled(); | |
| }, | |
| [&](const auto& args) { | |
| FAIL() << "Unexpected argument args during visitor test execution."; | |
| }, | |
| }, | |
| notifyArgs); | |
| } | |
| } | |
| /* | |
| * Verify that remapped button click transactions are suppressed and notify the policy. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |--------------|---------------|--------------|---------| | |
| * | BUTTON_PRESS | BACK | BACK | Dropped | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, DropsInterceptedButtons) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_BACK); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_BACK, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_BACK, | |
| AMOTION_EVENT_BUTTON_BACK)); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that relative mouse drags are converted to hover moves when custom buttons are held. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |--------------|---------------|--------------|----------------------| | |
| * | BUTTON_PRESS | TERTIARY | TERTIARY | Dropped | | |
| * | MOVE | - | TERTIARY | HOVER_MOVE (scrubbed)| | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, ConvertsMoveToHoverMoveDuringInterception) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| NotifyMotionArgs moveArgs = | |
| createMouseEvent(AMOTION_EVENT_ACTION_MOVE, AMOTION_EVENT_BUTTON_TERTIARY); | |
| mInterceptor->notifyMotion(moveArgs); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that synthetic DOWN events are generated during simultaneous presses. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |--------------|---------------|---------------------|------------------| | |
| * | DOWN | - | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | TERTIARY | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | PRIMARY | PRIMARY or TERTIARY | DOWN synthesized | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, SynthesizesDownAndUpDuringInterception) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_DOWN, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| NotifyMotionArgs primaryPress = | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY | AMOTION_EVENT_BUTTON_PRIMARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY); | |
| mInterceptor->notifyMotion(primaryPress); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that a full physical remapped button sequence is dropped completely. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|--------------|----------------------| | |
| * | DOWN | - | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | TERTIARY | TERTIARY | Dropped | | |
| * | MOVE | - | TERTIARY | HOVER_MOVE (scrubbed)| | |
| * | BUTTON_RELEASE | TERTIARY | - | Dropped | | |
| * | UP | - | - | Dropped | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, DropsCustomButtonPressSequences) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| testing::Sequence s; | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_MOVE, AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| 0, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_UP, 0)); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that click stream consistency is maintained when standard and custom keys are pressed. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|---------------------|----------------------| | |
| * | DOWN | - | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | TERTIARY | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | PRIMARY | PRIMARY or TERTIARY | DOWN synthesized | | |
| * | BUTTON_RELEASE | TERTIARY | PRIMARY | Dropped | | |
| * | BUTTON_RELEASE | PRIMARY | - | Passed | | |
| * | UP | - | - | Passed | | |
| * | HOVER_MOVE | - | - | Passed | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, HandlesSimultaneousButtonPressSequences) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| testing::Sequence s; | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| AMOTION_EVENT_BUTTON_PRIMARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| 0, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_UP, 0)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_HOVER_MOVE, 0)); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that standard drag gestures terminate cleanly during simultaneous button releases. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|---------------------|----------------------| | |
| * | DOWN | - | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | TERTIARY | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | PRIMARY | PRIMARY | TERTIARY | DOWN synthesized | | |
| * | BUTTON_RELEASE | PRIMARY | TERTIARY | Passed & UP synth | | |
| * | BUTTON_RELEASE | TERTIARY | - | Dropped | | |
| * | UP | - | - | Dropped | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, HandlesTransitionToUpDuringSimultaneousReleases) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| testing::Sequence s; | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| 0, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_UP, 0)); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that standard drag gestures are initiated correctly during simultaneous DOWN states. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|---------------------|-------------------| | |
| * | DOWN | - | PRIMARY | TERTIARY | Passed (scrubbed) | | |
| * | BUTTON_PRESS | PRIMARY | PRIMARY | TERTIARY | Passed (scrubbed) | | |
| * | BUTTON_PRESS | TERTIARY | PRIMARY | TERTIARY | Dropped | | |
| * | MOVE | - | PRIMARY | TERTIARY | Passed (scrubbed) | | |
| * | BUTTON_RELEASE | TERTIARY | PRIMARY | Dropped | | |
| * | BUTTON_RELEASE | PRIMARY | - | Passed | | |
| * | UP | - | - | Passed | | |
| * | HOVER_MOVE | - | - | Passed | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, HandlesSimultaneousFirstDownSequences) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| testing::Sequence s; | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_DOWN, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_MOVE, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| AMOTION_EVENT_BUTTON_PRIMARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| 0, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_UP, 0)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_HOVER_MOVE, 0)); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that simultaneous DOWN gestures are terminated correctly when standard buttons | |
| * are released first. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|---------------------|----------------------| | |
| * | DOWN | - | PRIMARY | TERTIARY | Passed (scrubbed) | | |
| * | BUTTON_PRESS | PRIMARY | PRIMARY | TERTIARY | Passed (scrubbed) | | |
| * | BUTTON_PRESS | TERTIARY | PRIMARY | TERTIARY | Dropped | | |
| * | MOVE | - | PRIMARY | TERTIARY | Passed (scrubbed) | | |
| * | BUTTON_RELEASE | PRIMARY | TERTIARY | Passed & UP synth | | |
| * | BUTTON_RELEASE | TERTIARY | - | Dropped | | |
| * | UP | - | - | Dropped | | |
| * | HOVER_MOVE | - | - | Passed | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, HandlesSimultaneousFirstDownSequencesWithInversedRelease) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| testing::Sequence s; | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| ui::LogicalDisplayId{0})) | |
| .Times(1) | |
| .InSequence(s); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_DOWN, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_MOVE, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| 0, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_UP, 0)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_HOVER_MOVE, 0)); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that stream remains consistent when requestPolicyInterceptionForButtons | |
| * is called while one or more buttons are held down. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|---------------------|----------------------| | |
| * | DOWN | - | TERTIARY | Dropped | | |
| * | BUTTON_PRESS | TERTIARY | TERTIARY | Dropped | | |
| * | CFG_CHANGE | - | - | Interception removed | | |
| * | BUTTON_RELEASE | TERTIARY | - | Dropped | | |
| * | UP | - | - | Dropped | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, ConfigChangeDuringActiveInterceptionIsConsistent) { | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_DOWN, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| // Clear the config mask while the button is still physically held | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, 0); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | |
| 0, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_UP, 0)); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| /* | |
| * Verify that requesting policy button interception for PRIMARY and SECONDARY keys is rejected. | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, RejectsForbiddenButtons) { | |
| ASSERT_DEATH(mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, | |
| AMOTION_EVENT_BUTTON_PRIMARY), | |
| "Should not be requesting interception for PRIMARY or SECONDARY mouse " | |
| "buttons"); | |
| ASSERT_DEATH(mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, | |
| AMOTION_EVENT_BUTTON_SECONDARY), | |
| "Should not be requesting interception for PRIMARY or SECONDARY mouse " | |
| "buttons"); | |
| ASSERT_DEATH(mInterceptor->requestPolicyInterceptionForButtons( | |
| DEVICE_ID, AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_BACK), | |
| "Should not be requesting interception for PRIMARY or SECONDARY mouse " | |
| "buttons"); | |
| EXPECT_NO_FATAL_FAILURE(mInterceptor->requestPolicyInterceptionForButtons( | |
| DEVICE_ID, AMOTION_EVENT_BUTTON_BACK)); | |
| } | |
| /* | |
| * Verify that multiple registered buttons are handled and scrubbed from buttonState concurrently. | |
| * | |
| * | Action | Action button | Button state | Result | | |
| * |----------------|---------------|-------------------------|-------------------| | |
| * | DOWN | - | PRIMARY | Passed | | |
| * | BUTTON_PRESS | PRIMARY | PRIMARY | Passed | | |
| * | BUTTON_PRESS | TERTIARY | PRI | TER | Dropped | | |
| * | BUTTON_PRESS | BACK | PRI | TER | BACK | Dropped | | |
| * | MOVE | - | PRI | TER | BACK | Passed (scrubbed) | | |
| */ | |
| TEST_F(MotionButtonInterceptorTest, ScrubsMultipleInterceptedButtons) { | |
| mInterceptor->requestPolicyInterceptionForButtons( | |
| DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY | AMOTION_EVENT_BUTTON_BACK); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_BACK, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, ui::LogicalDisplayId{0})) | |
| .Times(1); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_DOWN, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY, | |
| AMOTION_EVENT_BUTTON_PRIMARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | | |
| AMOTION_EVENT_BUTTON_TERTIARY, | |
| AMOTION_EVENT_BUTTON_TERTIARY)); | |
| mInterceptor->notifyMotion(createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, | |
| AMOTION_EVENT_BUTTON_PRIMARY | | |
| AMOTION_EVENT_BUTTON_TERTIARY | | |
| AMOTION_EVENT_BUTTON_BACK, | |
| AMOTION_EVENT_BUTTON_BACK)); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_MOVE, | |
| AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_TERTIARY | | |
| AMOTION_EVENT_BUTTON_BACK)); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), | |
| WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasCalled( | |
| AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), | |
| WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| TEST_F(MotionButtonInterceptorTest, RoutesDisplayIdCorrectly) { | |
| constexpr ui::LogicalDisplayId CUSTOM_DISPLAY_ID = ui::LogicalDisplayId{1}; | |
| mInterceptor->requestPolicyInterceptionForButtons(DEVICE_ID, AMOTION_EVENT_BUTTON_BACK); | |
| EXPECT_CALL(mPolicy, | |
| notifyButtonIntercepted(DEVICE_ID, AMOTION_EVENT_BUTTON_BACK, | |
| AMOTION_EVENT_ACTION_BUTTON_PRESS, CUSTOM_DISPLAY_ID)) | |
| .Times(1); | |
| mInterceptor->notifyMotion( | |
| createMouseEvent(AMOTION_EVENT_ACTION_BUTTON_PRESS, AMOTION_EVENT_BUTTON_BACK, | |
| AMOTION_EVENT_BUTTON_BACK, DEVICE_ID, CUSTOM_DISPLAY_ID)); | |
| mTestListener.assertNotifyMotionWasNotCalled(); | |
| } | |
| } // namespace android |