blob: adebd72a034c860117d39e2dec01e98fddf29799 [file] [log] [blame]
Harry Cutts4bf934b2024-07-19 14:09:44 +00001/*
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 Cutts511ce6a2024-07-24 11:38:25 +000019#include <list>
Harry Cutts4bf934b2024-07-19 14:09:44 +000020#include <optional>
21
22#include <EventHub.h>
23#include <NotifyArgs.h>
24#include <ftl/flags.h>
Harry Cutts511ce6a2024-07-24 11:38:25 +000025#include <gmock/gmock.h>
Harry Cutts4bf934b2024-07-19 14:09:44 +000026#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 Cutts511ce6a2024-07-24 11:38:25 +000033#include "TestEventMatchers.h"
Harry Cutts4bf934b2024-07-19 14:09:44 +000034
35namespace android {
36
Harry Cutts4bf934b2024-07-19 14:09:44 +000037using namespace ftl::flag_operators;
Harry Cutts511ce6a2024-07-24 11:38:25 +000038using testing::ElementsAre;
39using testing::IsEmpty;
40using testing::Return;
41using testing::VariantWith;
Harry Cutts4bf934b2024-07-19 14:09:44 +000042
Harry Cutts511ce6a2024-07-24 11:38:25 +000043class JoystickInputMapperTest : public InputMapperUnitTest {
Harry Cutts4bf934b2024-07-19 14:09:44 +000044protected:
Harry Cutts4bf934b2024-07-19 14:09:44 +000045 void SetUp() override {
Harry Cutts511ce6a2024-07-24 11:38:25 +000046 InputMapperUnitTest::SetUp();
47 EXPECT_CALL(mMockEventHub, getDeviceClasses(EVENTHUB_ID))
48 .WillRepeatedly(Return(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL));
Harry Cutts4bf934b2024-07-19 14:09:44 +000049
Harry Cutts511ce6a2024-07-24 11:38:25 +000050 // 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 Cutts4bf934b2024-07-19 14:09:44 +000055
Harry Cutts511ce6a2024-07-24 11:38:25 +000056 setupAxis(ABS_X, /*valid=*/true, /*min=*/-32767, /*max=*/32767, /*resolution=*/0);
57 setupAxis(ABS_Y, /*valid=*/true, /*min=*/-32767, /*max=*/32767, /*resolution=*/0);
Harry Cutts4bf934b2024-07-19 14:09:44 +000058 }
59};
60
Harry Cutts4bf934b2024-07-19 14:09:44 +000061TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
Harry Cutts511ce6a2024-07-24 11:38:25 +000062 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 Cutts4bf934b2024-07-19 14:09:44 +000067
Harry Cutts511ce6a2024-07-24 11:38:25 +000068 std::list<NotifyArgs> out;
Harry Cutts4bf934b2024-07-19 14:09:44 +000069
70 // Send an axis event
Harry Cutts511ce6a2024-07-24 11:38:25 +000071 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 Cutts4bf934b2024-07-19 14:09:44 +000075
76 // Send another axis event
Harry Cutts511ce6a2024-07-24 11:38:25 +000077 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 Cutts4bf934b2024-07-19 14:09:44 +000081}
82
83} // namespace android