blob: 94cfc3274b440fab8cfcfe344b463aefa27fd87a [file] [log] [blame]
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +01001/*
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 "RotaryEncoderInputMapper.h"
18
19#include <list>
20#include <string>
21#include <tuple>
22#include <variant>
23
24#include <android-base/logging.h>
25#include <gtest/gtest.h>
26#include <input/DisplayViewport.h>
27#include <linux/input-event-codes.h>
28#include <linux/input.h>
29#include <utils/Timers.h>
30
31#include "InputMapperTest.h"
32#include "InputReaderBase.h"
33#include "InterfaceMocks.h"
34#include "NotifyArgs.h"
35#include "TestEventMatchers.h"
36#include "ui/Rotation.h"
37
38#define TAG "RotaryEncoderInputMapper_test"
39
40namespace android {
41
42using testing::AllOf;
43using testing::Return;
44using testing::VariantWith;
45constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
46constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID = ui::LogicalDisplayId{DISPLAY_ID.val() + 1};
47constexpr int32_t DISPLAY_WIDTH = 480;
48constexpr int32_t DISPLAY_HEIGHT = 800;
49
50namespace {
51
52DisplayViewport createViewport() {
53 DisplayViewport v;
54 v.orientation = ui::Rotation::Rotation0;
55 v.logicalRight = DISPLAY_HEIGHT;
56 v.logicalBottom = DISPLAY_WIDTH;
57 v.physicalRight = DISPLAY_HEIGHT;
58 v.physicalBottom = DISPLAY_WIDTH;
59 v.deviceWidth = DISPLAY_HEIGHT;
60 v.deviceHeight = DISPLAY_WIDTH;
61 v.isActive = true;
62 return v;
63}
64
65DisplayViewport createPrimaryViewport() {
66 DisplayViewport v = createViewport();
67 v.displayId = DISPLAY_ID;
68 v.uniqueId = "local:1";
69 return v;
70}
71
72DisplayViewport createSecondaryViewport() {
73 DisplayViewport v = createViewport();
74 v.displayId = SECONDARY_DISPLAY_ID;
75 v.uniqueId = "local:2";
76 v.type = ViewportType::EXTERNAL;
77 return v;
78}
79
80/**
81 * A fake InputDeviceContext that allows the associated viewport to be specified for the mapper.
82 *
83 * This is currently necessary because InputMapperUnitTest doesn't register the mappers it creates
84 * with the InputDevice object, meaning that InputDevice::isIgnored becomes true, and the input
85 * device doesn't set its associated viewport when it's configured.
86 *
87 * TODO(b/319217713): work out a way to avoid this fake.
88 */
89class ViewportFakingInputDeviceContext : public InputDeviceContext {
90public:
91 ViewportFakingInputDeviceContext(InputDevice& device, int32_t eventHubId,
92 std::optional<DisplayViewport> viewport)
93 : InputDeviceContext(device, eventHubId), mAssociatedViewport(viewport) {}
94
95 ViewportFakingInputDeviceContext(InputDevice& device, int32_t eventHubId)
96 : ViewportFakingInputDeviceContext(device, eventHubId, createPrimaryViewport()) {}
97
98 std::optional<DisplayViewport> getAssociatedViewport() const override {
99 return mAssociatedViewport;
100 }
101
102 void setViewport(const std::optional<DisplayViewport>& viewport) {
103 mAssociatedViewport = viewport;
104 }
105
106private:
107 std::optional<DisplayViewport> mAssociatedViewport;
108};
109
110} // namespace
111
112/**
113 * Unit tests for RotaryEncoderInputMapper.
114 */
115class RotaryEncoderInputMapperTest : public InputMapperUnitTest {
116protected:
117 void SetUp() override { SetUpWithBus(BUS_USB); }
118 void SetUpWithBus(int bus) override {
119 InputMapperUnitTest::SetUpWithBus(bus);
120
121 EXPECT_CALL(mMockEventHub, hasRelativeAxis(EVENTHUB_ID, REL_WHEEL))
122 .WillRepeatedly(Return(true));
123 EXPECT_CALL(mMockEventHub, hasRelativeAxis(EVENTHUB_ID, REL_HWHEEL))
124 .WillRepeatedly(Return(false));
125 }
126};
127
128TEST_F(RotaryEncoderInputMapperTest, ConfigureDisplayIdWithAssociatedViewport) {
129 DisplayViewport primaryViewport = createPrimaryViewport();
130 DisplayViewport secondaryViewport = createSecondaryViewport();
131 mReaderConfiguration.setDisplayViewports({primaryViewport, secondaryViewport});
132
133 // Set up the secondary display as the associated viewport of the mapper.
134 createDevice();
135 ViewportFakingInputDeviceContext deviceContext(*mDevice, EVENTHUB_ID, secondaryViewport);
136 mMapper = createInputMapper<RotaryEncoderInputMapper>(deviceContext, mReaderConfiguration);
137
138 std::list<NotifyArgs> args;
139 // Ensure input events are generated for the secondary display.
140 args += process(ARBITRARY_TIME, EV_REL, REL_WHEEL, 1);
141 args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
142 EXPECT_THAT(args,
143 ElementsAre(VariantWith<NotifyMotionArgs>(
144 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_SCROLL),
145 WithSource(AINPUT_SOURCE_ROTARY_ENCODER),
146 WithDisplayId(SECONDARY_DISPLAY_ID)))));
147}
148
149TEST_F(RotaryEncoderInputMapperTest, ConfigureDisplayIdNoAssociatedViewport) {
150 // Set up the default display.
151 mFakePolicy->clearViewports();
152 mFakePolicy->addDisplayViewport(createPrimaryViewport());
153
154 // Set up the mapper with no associated viewport.
155 createDevice();
156 mMapper = createInputMapper<RotaryEncoderInputMapper>(*mDeviceContext, mReaderConfiguration);
157
158 // Ensure input events are generated without display ID
159 std::list<NotifyArgs> args;
160 args += process(ARBITRARY_TIME, EV_REL, REL_WHEEL, 1);
161 args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
162 EXPECT_THAT(args,
163 ElementsAre(VariantWith<NotifyMotionArgs>(
164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_SCROLL),
165 WithSource(AINPUT_SOURCE_ROTARY_ENCODER),
166 WithDisplayId(ui::LogicalDisplayId::INVALID)))));
167}
168
169} // namespace android