blob: ec7019256fb8060a8258d276a795de99999fa4e9 [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
19#include <optional>
20
21#include <EventHub.h>
22#include <NotifyArgs.h>
23#include <ftl/flags.h>
24#include <gtest/gtest.h>
25#include <input/DisplayViewport.h>
26#include <linux/input-event-codes.h>
27#include <ui/LogicalDisplayId.h>
28
29#include "InputMapperTest.h"
30#include "TestConstants.h"
31
32namespace android {
33
34namespace {
35
36using namespace ftl::flag_operators;
37
38} // namespace
39
40class JoystickInputMapperTest : public InputMapperTest {
41protected:
42 static const int32_t RAW_X_MIN;
43 static const int32_t RAW_X_MAX;
44 static const int32_t RAW_Y_MIN;
45 static const int32_t RAW_Y_MAX;
46
47 static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
48 static const char* const VIRTUAL_DISPLAY_UNIQUE_ID;
49
50 void SetUp() override {
51 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
52 }
53 void prepareAxes() {
54 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
55 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
56 }
57
58 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
59 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
60 }
61
62 void processSync(JoystickInputMapper& mapper) {
63 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
64 }
65
66 void prepareVirtualDisplay(ui::Rotation orientation) {
67 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, /*width=*/400, /*height=*/500, orientation,
68 VIRTUAL_DISPLAY_UNIQUE_ID, /*physicalPort=*/std::nullopt,
69 ViewportType::VIRTUAL);
70 }
71};
72
73const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
74const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
75const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
76const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
77const char* const JoystickInputMapperTest::VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
78
79TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
80 prepareAxes();
81 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
82
83 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
84
85 prepareVirtualDisplay(ui::ROTATION_0);
86
87 // Send an axis event
88 processAxis(mapper, ABS_X, 100);
89 processSync(mapper);
90
91 NotifyMotionArgs args;
92 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
93 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
94
95 // Send another axis event
96 processAxis(mapper, ABS_Y, 100);
97 processSync(mapper);
98
99 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
100 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
101}
102
103} // namespace android