xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/FakePointerController.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include "FakePointerController.h"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker namespace android {
22*38e8c45fSAndroid Build Coastguard Worker 
setBounds(float minX,float minY,float maxX,float maxY)23*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::setBounds(float minX, float minY, float maxX, float maxY) {
24*38e8c45fSAndroid Build Coastguard Worker     mHaveBounds = true;
25*38e8c45fSAndroid Build Coastguard Worker     mMinX = minX;
26*38e8c45fSAndroid Build Coastguard Worker     mMinY = minY;
27*38e8c45fSAndroid Build Coastguard Worker     mMaxX = maxX;
28*38e8c45fSAndroid Build Coastguard Worker     mMaxY = maxY;
29*38e8c45fSAndroid Build Coastguard Worker }
30*38e8c45fSAndroid Build Coastguard Worker 
clearBounds()31*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::clearBounds() {
32*38e8c45fSAndroid Build Coastguard Worker     mHaveBounds = false;
33*38e8c45fSAndroid Build Coastguard Worker }
34*38e8c45fSAndroid Build Coastguard Worker 
getSpots()35*38e8c45fSAndroid Build Coastguard Worker const std::map<ui::LogicalDisplayId, std::vector<int32_t>>& FakePointerController::getSpots() {
36*38e8c45fSAndroid Build Coastguard Worker     return mSpotsByDisplay;
37*38e8c45fSAndroid Build Coastguard Worker }
38*38e8c45fSAndroid Build Coastguard Worker 
setPosition(float x,float y)39*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::setPosition(float x, float y) {
40*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return;
41*38e8c45fSAndroid Build Coastguard Worker 
42*38e8c45fSAndroid Build Coastguard Worker     mX = x;
43*38e8c45fSAndroid Build Coastguard Worker     mY = y;
44*38e8c45fSAndroid Build Coastguard Worker }
45*38e8c45fSAndroid Build Coastguard Worker 
getPosition() const46*38e8c45fSAndroid Build Coastguard Worker vec2 FakePointerController::getPosition() const {
47*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) {
48*38e8c45fSAndroid Build Coastguard Worker         return {0, 0};
49*38e8c45fSAndroid Build Coastguard Worker     }
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker     return {mX, mY};
52*38e8c45fSAndroid Build Coastguard Worker }
53*38e8c45fSAndroid Build Coastguard Worker 
getDisplayId() const54*38e8c45fSAndroid Build Coastguard Worker ui::LogicalDisplayId FakePointerController::getDisplayId() const {
55*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled || !mDisplayId) {
56*38e8c45fSAndroid Build Coastguard Worker         return ui::LogicalDisplayId::INVALID;
57*38e8c45fSAndroid Build Coastguard Worker     }
58*38e8c45fSAndroid Build Coastguard Worker     return *mDisplayId;
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker 
setDisplayViewport(const DisplayViewport & viewport)61*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
62*38e8c45fSAndroid Build Coastguard Worker     mDisplayId = viewport.displayId;
63*38e8c45fSAndroid Build Coastguard Worker     setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1,
64*38e8c45fSAndroid Build Coastguard Worker               viewport.logicalBottom - 1);
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker 
updatePointerIcon(PointerIconStyle iconId)67*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::updatePointerIcon(PointerIconStyle iconId) {
68*38e8c45fSAndroid Build Coastguard Worker     ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once";
69*38e8c45fSAndroid Build Coastguard Worker     mIconStyle = iconId;
70*38e8c45fSAndroid Build Coastguard Worker }
71*38e8c45fSAndroid Build Coastguard Worker 
setCustomPointerIcon(const SpriteIcon & icon)72*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) {
73*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return;
74*38e8c45fSAndroid Build Coastguard Worker 
75*38e8c45fSAndroid Build Coastguard Worker     ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once";
76*38e8c45fSAndroid Build Coastguard Worker     mCustomIconStyle = icon.style;
77*38e8c45fSAndroid Build Coastguard Worker }
78*38e8c45fSAndroid Build Coastguard Worker 
setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId)79*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) {
80*38e8c45fSAndroid Build Coastguard Worker     mDisplaysToSkipScreenshotFlagChanged = true;
81*38e8c45fSAndroid Build Coastguard Worker     mDisplaysToSkipScreenshot.insert(displayId);
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker 
clearSkipScreenshotFlags()84*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::clearSkipScreenshotFlags() {
85*38e8c45fSAndroid Build Coastguard Worker     mDisplaysToSkipScreenshotFlagChanged = true;
86*38e8c45fSAndroid Build Coastguard Worker     mDisplaysToSkipScreenshot.clear();
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker 
assertViewportSet(ui::LogicalDisplayId displayId)89*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertViewportSet(ui::LogicalDisplayId displayId) {
90*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(mDisplayId);
91*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(displayId, mDisplayId);
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker 
assertViewportNotSet()94*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertViewportNotSet() {
95*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(std::nullopt, mDisplayId);
96*38e8c45fSAndroid Build Coastguard Worker }
97*38e8c45fSAndroid Build Coastguard Worker 
assertPosition(float x,float y)98*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertPosition(float x, float y) {
99*38e8c45fSAndroid Build Coastguard Worker     const auto actual = getPosition();
100*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NEAR(x, actual.x, 1);
101*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NEAR(y, actual.y, 1);
102*38e8c45fSAndroid Build Coastguard Worker }
103*38e8c45fSAndroid Build Coastguard Worker 
assertSpotCount(ui::LogicalDisplayId displayId,int32_t count)104*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertSpotCount(ui::LogicalDisplayId displayId, int32_t count) {
105*38e8c45fSAndroid Build Coastguard Worker     auto it = mSpotsByDisplay.find(displayId);
106*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId;
107*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(static_cast<size_t>(count), it->second.size());
108*38e8c45fSAndroid Build Coastguard Worker }
109*38e8c45fSAndroid Build Coastguard Worker 
assertPointerIconSet(PointerIconStyle iconId)110*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) {
111*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set";
112*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(iconId, mIconStyle);
113*38e8c45fSAndroid Build Coastguard Worker     mIconStyle.reset();
114*38e8c45fSAndroid Build Coastguard Worker }
115*38e8c45fSAndroid Build Coastguard Worker 
assertPointerIconNotSet()116*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertPointerIconNotSet() {
117*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(std::nullopt, mIconStyle);
118*38e8c45fSAndroid Build Coastguard Worker }
119*38e8c45fSAndroid Build Coastguard Worker 
assertCustomPointerIconSet(PointerIconStyle iconId)120*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) {
121*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set";
122*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(iconId, mCustomIconStyle);
123*38e8c45fSAndroid Build Coastguard Worker     mCustomIconStyle.reset();
124*38e8c45fSAndroid Build Coastguard Worker }
125*38e8c45fSAndroid Build Coastguard Worker 
assertCustomPointerIconNotSet()126*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertCustomPointerIconNotSet() {
127*38e8c45fSAndroid Build Coastguard Worker     ASSERT_EQ(std::nullopt, mCustomIconStyle);
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker 
assertIsSkipScreenshotFlagSet(ui::LogicalDisplayId displayId)130*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertIsSkipScreenshotFlagSet(ui::LogicalDisplayId displayId) {
131*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) != mDisplaysToSkipScreenshot.end());
132*38e8c45fSAndroid Build Coastguard Worker }
133*38e8c45fSAndroid Build Coastguard Worker 
assertIsSkipScreenshotFlagNotSet(ui::LogicalDisplayId displayId)134*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertIsSkipScreenshotFlagNotSet(ui::LogicalDisplayId displayId) {
135*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) == mDisplaysToSkipScreenshot.end());
136*38e8c45fSAndroid Build Coastguard Worker }
137*38e8c45fSAndroid Build Coastguard Worker 
assertSkipScreenshotFlagChanged()138*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertSkipScreenshotFlagChanged() {
139*38e8c45fSAndroid Build Coastguard Worker     ASSERT_TRUE(mDisplaysToSkipScreenshotFlagChanged);
140*38e8c45fSAndroid Build Coastguard Worker     mDisplaysToSkipScreenshotFlagChanged = false;
141*38e8c45fSAndroid Build Coastguard Worker }
142*38e8c45fSAndroid Build Coastguard Worker 
assertSkipScreenshotFlagNotChanged()143*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::assertSkipScreenshotFlagNotChanged() {
144*38e8c45fSAndroid Build Coastguard Worker     ASSERT_FALSE(mDisplaysToSkipScreenshotFlagChanged);
145*38e8c45fSAndroid Build Coastguard Worker }
146*38e8c45fSAndroid Build Coastguard Worker 
isPointerShown()147*38e8c45fSAndroid Build Coastguard Worker bool FakePointerController::isPointerShown() {
148*38e8c45fSAndroid Build Coastguard Worker     return mIsPointerShown;
149*38e8c45fSAndroid Build Coastguard Worker }
150*38e8c45fSAndroid Build Coastguard Worker 
move(float deltaX,float deltaY)151*38e8c45fSAndroid Build Coastguard Worker vec2 FakePointerController::move(float deltaX, float deltaY) {
152*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return {0, 0};
153*38e8c45fSAndroid Build Coastguard Worker 
154*38e8c45fSAndroid Build Coastguard Worker     mX += deltaX;
155*38e8c45fSAndroid Build Coastguard Worker     mY += deltaY;
156*38e8c45fSAndroid Build Coastguard Worker 
157*38e8c45fSAndroid Build Coastguard Worker     const vec2 position(mX, mY);
158*38e8c45fSAndroid Build Coastguard Worker 
159*38e8c45fSAndroid Build Coastguard Worker     if (mX < mMinX) mX = mMinX;
160*38e8c45fSAndroid Build Coastguard Worker     if (mX > mMaxX) mX = mMaxX;
161*38e8c45fSAndroid Build Coastguard Worker     if (mY < mMinY) mY = mMinY;
162*38e8c45fSAndroid Build Coastguard Worker     if (mY > mMaxY) mY = mMaxY;
163*38e8c45fSAndroid Build Coastguard Worker 
164*38e8c45fSAndroid Build Coastguard Worker     return {position.x - mX, position.y - mY};
165*38e8c45fSAndroid Build Coastguard Worker }
166*38e8c45fSAndroid Build Coastguard Worker 
fade(Transition)167*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::fade(Transition) {
168*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return;
169*38e8c45fSAndroid Build Coastguard Worker 
170*38e8c45fSAndroid Build Coastguard Worker     mIsPointerShown = false;
171*38e8c45fSAndroid Build Coastguard Worker }
unfade(Transition)172*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::unfade(Transition) {
173*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return;
174*38e8c45fSAndroid Build Coastguard Worker 
175*38e8c45fSAndroid Build Coastguard Worker     mIsPointerShown = true;
176*38e8c45fSAndroid Build Coastguard Worker }
177*38e8c45fSAndroid Build Coastguard Worker 
setSpots(const PointerCoords *,const uint32_t *,BitSet32 spotIdBits,ui::LogicalDisplayId displayId)178*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
179*38e8c45fSAndroid Build Coastguard Worker                                      ui::LogicalDisplayId displayId) {
180*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return;
181*38e8c45fSAndroid Build Coastguard Worker 
182*38e8c45fSAndroid Build Coastguard Worker     std::vector<int32_t> newSpots;
183*38e8c45fSAndroid Build Coastguard Worker     // Add spots for fingers that are down.
184*38e8c45fSAndroid Build Coastguard Worker     for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
185*38e8c45fSAndroid Build Coastguard Worker         uint32_t id = idBits.clearFirstMarkedBit();
186*38e8c45fSAndroid Build Coastguard Worker         newSpots.push_back(id);
187*38e8c45fSAndroid Build Coastguard Worker     }
188*38e8c45fSAndroid Build Coastguard Worker 
189*38e8c45fSAndroid Build Coastguard Worker     mSpotsByDisplay[displayId] = newSpots;
190*38e8c45fSAndroid Build Coastguard Worker }
191*38e8c45fSAndroid Build Coastguard Worker 
clearSpots()192*38e8c45fSAndroid Build Coastguard Worker void FakePointerController::clearSpots() {
193*38e8c45fSAndroid Build Coastguard Worker     if (!mEnabled) return;
194*38e8c45fSAndroid Build Coastguard Worker 
195*38e8c45fSAndroid Build Coastguard Worker     mSpotsByDisplay.clear();
196*38e8c45fSAndroid Build Coastguard Worker }
197*38e8c45fSAndroid Build Coastguard Worker 
getDisplayTransform() const198*38e8c45fSAndroid Build Coastguard Worker ui::Transform FakePointerController::getDisplayTransform() const {
199*38e8c45fSAndroid Build Coastguard Worker     return ui::Transform();
200*38e8c45fSAndroid Build Coastguard Worker }
201*38e8c45fSAndroid Build Coastguard Worker 
202*38e8c45fSAndroid Build Coastguard Worker } // namespace android
203