Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +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 | #include "JoystickInputMapper.h" |
| 18 | |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 19 | #include <list> |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 20 | #include <optional> |
| 21 | |
| 22 | #include <EventHub.h> |
| 23 | #include <NotifyArgs.h> |
| 24 | #include <ftl/flags.h> |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 25 | #include <gmock/gmock.h> |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <input/DisplayViewport.h> |
| 28 | #include <linux/input-event-codes.h> |
| 29 | #include <ui/LogicalDisplayId.h> |
| 30 | |
| 31 | #include "InputMapperTest.h" |
| 32 | #include "TestConstants.h" |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 33 | #include "TestEventMatchers.h" |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 37 | using namespace ftl::flag_operators; |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 38 | using testing::ElementsAre; |
| 39 | using testing::IsEmpty; |
| 40 | using testing::Return; |
| 41 | using testing::VariantWith; |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 42 | |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 43 | class JoystickInputMapperTest : public InputMapperUnitTest { |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 44 | protected: |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 45 | void SetUp() override { |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 46 | InputMapperUnitTest::SetUp(); |
| 47 | EXPECT_CALL(mMockEventHub, getDeviceClasses(EVENTHUB_ID)) |
| 48 | .WillRepeatedly(Return(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL)); |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 49 | |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 50 | // The mapper requests info on all ABS axis IDs, including ones which aren't actually used |
| 51 | // (e.g. in the range from 0x0b (ABS_BRAKE) to 0x0f (ABS_HAT0X)), so just return nullopt for |
| 52 | // all axes we don't explicitly set up below. |
| 53 | EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, testing::_)) |
| 54 | .WillRepeatedly(Return(std::nullopt)); |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 55 | |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 56 | setupAxis(ABS_X, /*valid=*/true, /*min=*/-32767, /*max=*/32767, /*resolution=*/0); |
| 57 | setupAxis(ABS_Y, /*valid=*/true, /*min=*/-32767, /*max=*/32767, /*resolution=*/0); |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 61 | TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) { |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 62 | DisplayViewport viewport; |
| 63 | viewport.displayId = ui::LogicalDisplayId{1}; |
| 64 | EXPECT_CALL((*mDevice), getAssociatedViewport).WillRepeatedly(Return(viewport)); |
| 65 | mMapper = createInputMapper<JoystickInputMapper>(*mDeviceContext, |
| 66 | mFakePolicy->getReaderConfiguration()); |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 67 | |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 68 | std::list<NotifyArgs> out; |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 69 | |
| 70 | // Send an axis event |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 71 | out = process(EV_ABS, ABS_X, 100); |
| 72 | ASSERT_THAT(out, IsEmpty()); |
| 73 | out = process(EV_SYN, SYN_REPORT, 0); |
| 74 | ASSERT_THAT(out, ElementsAre(VariantWith<NotifyMotionArgs>(WithDisplayId(viewport.displayId)))); |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 75 | |
| 76 | // Send another axis event |
Harry Cutts | 511ce6a | 2024-07-24 11:38:25 +0000 | [diff] [blame] | 77 | out = process(EV_ABS, ABS_Y, 100); |
| 78 | ASSERT_THAT(out, IsEmpty()); |
| 79 | out = process(EV_SYN, SYN_REPORT, 0); |
| 80 | ASSERT_THAT(out, ElementsAre(VariantWith<NotifyMotionArgs>(WithDisplayId(viewport.displayId)))); |
Harry Cutts | 4bf934b | 2024-07-19 14:09:44 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | } // namespace android |