1 /*
2 * Copyright 2022 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 "FakePointerController.h"
18
19 #include <gtest/gtest.h>
20
21 namespace android {
22
setBounds(float minX,float minY,float maxX,float maxY)23 void FakePointerController::setBounds(float minX, float minY, float maxX, float maxY) {
24 mHaveBounds = true;
25 mMinX = minX;
26 mMinY = minY;
27 mMaxX = maxX;
28 mMaxY = maxY;
29 }
30
clearBounds()31 void FakePointerController::clearBounds() {
32 mHaveBounds = false;
33 }
34
getSpots()35 const std::map<ui::LogicalDisplayId, std::vector<int32_t>>& FakePointerController::getSpots() {
36 return mSpotsByDisplay;
37 }
38
setPosition(float x,float y)39 void FakePointerController::setPosition(float x, float y) {
40 if (!mEnabled) return;
41
42 mX = x;
43 mY = y;
44 }
45
getPosition() const46 FloatPoint FakePointerController::getPosition() const {
47 if (!mEnabled) {
48 return {0, 0};
49 }
50
51 return {mX, mY};
52 }
53
getDisplayId() const54 ui::LogicalDisplayId FakePointerController::getDisplayId() const {
55 if (!mEnabled || !mDisplayId) {
56 return ui::LogicalDisplayId::INVALID;
57 }
58 return *mDisplayId;
59 }
60
setDisplayViewport(const DisplayViewport & viewport)61 void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
62 mDisplayId = viewport.displayId;
63 setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1,
64 viewport.logicalBottom - 1);
65 }
66
updatePointerIcon(PointerIconStyle iconId)67 void FakePointerController::updatePointerIcon(PointerIconStyle iconId) {
68 ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once";
69 mIconStyle = iconId;
70 }
71
setCustomPointerIcon(const SpriteIcon & icon)72 void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) {
73 if (!mEnabled) return;
74
75 ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once";
76 mCustomIconStyle = icon.style;
77 }
78
setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId)79 void FakePointerController::setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) {
80 mDisplaysToSkipScreenshotFlagChanged = true;
81 mDisplaysToSkipScreenshot.insert(displayId);
82 }
83
clearSkipScreenshotFlags()84 void FakePointerController::clearSkipScreenshotFlags() {
85 mDisplaysToSkipScreenshotFlagChanged = true;
86 mDisplaysToSkipScreenshot.clear();
87 }
88
assertViewportSet(ui::LogicalDisplayId displayId)89 void FakePointerController::assertViewportSet(ui::LogicalDisplayId displayId) {
90 ASSERT_TRUE(mDisplayId);
91 ASSERT_EQ(displayId, mDisplayId);
92 }
93
assertViewportNotSet()94 void FakePointerController::assertViewportNotSet() {
95 ASSERT_EQ(std::nullopt, mDisplayId);
96 }
97
assertPosition(float x,float y)98 void FakePointerController::assertPosition(float x, float y) {
99 const auto [actualX, actualY] = getPosition();
100 ASSERT_NEAR(x, actualX, 1);
101 ASSERT_NEAR(y, actualY, 1);
102 }
103
assertSpotCount(ui::LogicalDisplayId displayId,int32_t count)104 void FakePointerController::assertSpotCount(ui::LogicalDisplayId displayId, int32_t count) {
105 auto it = mSpotsByDisplay.find(displayId);
106 ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId;
107 ASSERT_EQ(static_cast<size_t>(count), it->second.size());
108 }
109
assertPointerIconSet(PointerIconStyle iconId)110 void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) {
111 ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set";
112 ASSERT_EQ(iconId, mIconStyle);
113 mIconStyle.reset();
114 }
115
assertPointerIconNotSet()116 void FakePointerController::assertPointerIconNotSet() {
117 ASSERT_EQ(std::nullopt, mIconStyle);
118 }
119
assertCustomPointerIconSet(PointerIconStyle iconId)120 void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) {
121 ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set";
122 ASSERT_EQ(iconId, mCustomIconStyle);
123 mCustomIconStyle.reset();
124 }
125
assertCustomPointerIconNotSet()126 void FakePointerController::assertCustomPointerIconNotSet() {
127 ASSERT_EQ(std::nullopt, mCustomIconStyle);
128 }
129
assertIsSkipScreenshotFlagSet(ui::LogicalDisplayId displayId)130 void FakePointerController::assertIsSkipScreenshotFlagSet(ui::LogicalDisplayId displayId) {
131 ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) != mDisplaysToSkipScreenshot.end());
132 }
133
assertIsSkipScreenshotFlagNotSet(ui::LogicalDisplayId displayId)134 void FakePointerController::assertIsSkipScreenshotFlagNotSet(ui::LogicalDisplayId displayId) {
135 ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) == mDisplaysToSkipScreenshot.end());
136 }
137
assertSkipScreenshotFlagChanged()138 void FakePointerController::assertSkipScreenshotFlagChanged() {
139 ASSERT_TRUE(mDisplaysToSkipScreenshotFlagChanged);
140 mDisplaysToSkipScreenshotFlagChanged = false;
141 }
142
assertSkipScreenshotFlagNotChanged()143 void FakePointerController::assertSkipScreenshotFlagNotChanged() {
144 ASSERT_FALSE(mDisplaysToSkipScreenshotFlagChanged);
145 }
146
isPointerShown()147 bool FakePointerController::isPointerShown() {
148 return mIsPointerShown;
149 }
150
getBounds() const151 std::optional<FloatRect> FakePointerController::getBounds() const {
152 if (!mEnabled) return std::nullopt;
153
154 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
155 }
156
move(float deltaX,float deltaY)157 void FakePointerController::move(float deltaX, float deltaY) {
158 if (!mEnabled) return;
159
160 mX += deltaX;
161 if (mX < mMinX) mX = mMinX;
162 if (mX > mMaxX) mX = mMaxX;
163 mY += deltaY;
164 if (mY < mMinY) mY = mMinY;
165 if (mY > mMaxY) mY = mMaxY;
166 }
167
fade(Transition)168 void FakePointerController::fade(Transition) {
169 if (!mEnabled) return;
170
171 mIsPointerShown = false;
172 }
unfade(Transition)173 void FakePointerController::unfade(Transition) {
174 if (!mEnabled) return;
175
176 mIsPointerShown = true;
177 }
178
setSpots(const PointerCoords *,const uint32_t *,BitSet32 spotIdBits,ui::LogicalDisplayId displayId)179 void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
180 ui::LogicalDisplayId displayId) {
181 if (!mEnabled) return;
182
183 std::vector<int32_t> newSpots;
184 // Add spots for fingers that are down.
185 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
186 uint32_t id = idBits.clearFirstMarkedBit();
187 newSpots.push_back(id);
188 }
189
190 mSpotsByDisplay[displayId] = newSpots;
191 }
192
clearSpots()193 void FakePointerController::clearSpots() {
194 if (!mEnabled) return;
195
196 mSpotsByDisplay.clear();
197 }
198
199 } // namespace android
200