xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/JoystickInputMapper_test.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 
19 #include <list>
20 #include <optional>
21 
22 #include <EventHub.h>
23 #include <NotifyArgs.h>
24 #include <ftl/flags.h>
25 #include <gmock/gmock.h>
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"
33 #include "TestEventMatchers.h"
34 
35 namespace android {
36 
37 using namespace ftl::flag_operators;
38 using testing::ElementsAre;
39 using testing::IsEmpty;
40 using testing::Return;
41 using testing::VariantWith;
42 
43 class JoystickInputMapperTest : public InputMapperUnitTest {
44 protected:
SetUp()45     void SetUp() override {
46         InputMapperUnitTest::SetUp();
47         EXPECT_CALL(mMockEventHub, getDeviceClasses(EVENTHUB_ID))
48                 .WillRepeatedly(Return(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL));
49 
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));
55 
56         setupAxis(ABS_X, /*valid=*/true, /*min=*/-32767, /*max=*/32767, /*resolution=*/0);
57         setupAxis(ABS_Y, /*valid=*/true, /*min=*/-32767, /*max=*/32767, /*resolution=*/0);
58     }
59 };
60 
TEST_F(JoystickInputMapperTest,Configure_AssignsDisplayUniqueId)61 TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
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());
67 
68     std::list<NotifyArgs> out;
69 
70     // Send an axis event
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))));
75 
76     // Send another axis event
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))));
81 }
82 
83 } // namespace android
84