blob: f033e57ddd7e53e1fca1f6995d3ce3eaebb1f71a [file] [log] [blame]
Harry Cuttsb57f1702022-11-28 15:34:22 +00001/*
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
Harry Cuttsce86cc32022-12-14 20:36:33 +000019#include <gtest/gtest.h>
20
Harry Cuttsb57f1702022-11-28 15:34:22 +000021namespace android {
22
23void 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
Harry Cutts7ecbb992023-12-18 14:45:09 +000031void FakePointerController::clearBounds() {
32 mHaveBounds = false;
33}
34
Linnan Li13bf76a2024-05-05 19:18:02 +080035const std::map<ui::LogicalDisplayId, std::vector<int32_t>>& FakePointerController::getSpots() {
Harry Cuttsb57f1702022-11-28 15:34:22 +000036 return mSpotsByDisplay;
37}
38
39void FakePointerController::setPosition(float x, float y) {
Harry Cutts3beea7d2024-02-21 15:52:35 +000040 if (!mEnabled) return;
41
Harry Cuttsb57f1702022-11-28 15:34:22 +000042 mX = x;
43 mY = y;
44}
45
Arpit Singh7f21fa32024-11-26 15:44:26 +000046vec2 FakePointerController::getPosition() const {
Harry Cutts3beea7d2024-02-21 15:52:35 +000047 if (!mEnabled) {
48 return {0, 0};
49 }
50
Prabir Pradhan2719e822023-02-28 17:39:36 +000051 return {mX, mY};
Harry Cuttsb57f1702022-11-28 15:34:22 +000052}
53
Linnan Li13bf76a2024-05-05 19:18:02 +080054ui::LogicalDisplayId FakePointerController::getDisplayId() const {
Harry Cutts3beea7d2024-02-21 15:52:35 +000055 if (!mEnabled || !mDisplayId) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070056 return ui::LogicalDisplayId::INVALID;
Byoungho Jungee6268f2023-10-30 17:27:26 +090057 }
58 return *mDisplayId;
Harry Cuttsb57f1702022-11-28 15:34:22 +000059}
60
61void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
62 mDisplayId = viewport.displayId;
Byoungho Jung1fe1f042023-10-24 15:27:18 +090063 setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1,
64 viewport.logicalBottom - 1);
Harry Cuttsb57f1702022-11-28 15:34:22 +000065}
66
Byoungho Jung99326452023-11-03 20:19:17 +090067void FakePointerController::updatePointerIcon(PointerIconStyle iconId) {
68 ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once";
69 mIconStyle = iconId;
70}
71
72void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) {
Harry Cutts3beea7d2024-02-21 15:52:35 +000073 if (!mEnabled) return;
74
Byoungho Jung99326452023-11-03 20:19:17 +090075 ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once";
76 mCustomIconStyle = icon.style;
77}
78
Arpit Singh420d0742024-04-04 11:54:20 +000079void FakePointerController::setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) {
Arpit Singhbd49b282024-05-23 18:02:54 +000080 mDisplaysToSkipScreenshotFlagChanged = true;
Arpit Singh420d0742024-04-04 11:54:20 +000081 mDisplaysToSkipScreenshot.insert(displayId);
82}
83
84void FakePointerController::clearSkipScreenshotFlags() {
Arpit Singhbd49b282024-05-23 18:02:54 +000085 mDisplaysToSkipScreenshotFlagChanged = true;
Arpit Singh420d0742024-04-04 11:54:20 +000086 mDisplaysToSkipScreenshot.clear();
87}
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000088
Linnan Li13bf76a2024-05-05 19:18:02 +080089void FakePointerController::assertViewportSet(ui::LogicalDisplayId displayId) {
Byoungho Jungee6268f2023-10-30 17:27:26 +090090 ASSERT_TRUE(mDisplayId);
91 ASSERT_EQ(displayId, mDisplayId);
92}
93
94void FakePointerController::assertViewportNotSet() {
95 ASSERT_EQ(std::nullopt, mDisplayId);
96}
97
Harry Cuttsce86cc32022-12-14 20:36:33 +000098void FakePointerController::assertPosition(float x, float y) {
Arpit Singh7f21fa32024-11-26 15:44:26 +000099 const auto actual = getPosition();
100 ASSERT_NEAR(x, actual.x, 1);
101 ASSERT_NEAR(y, actual.y, 1);
Harry Cuttsce86cc32022-12-14 20:36:33 +0000102}
103
Linnan Li13bf76a2024-05-05 19:18:02 +0800104void FakePointerController::assertSpotCount(ui::LogicalDisplayId displayId, int32_t count) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000105 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
Byoungho Jung99326452023-11-03 20:19:17 +0900110void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) {
111 ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set";
112 ASSERT_EQ(iconId, mIconStyle);
113 mIconStyle.reset();
114}
115
116void FakePointerController::assertPointerIconNotSet() {
117 ASSERT_EQ(std::nullopt, mIconStyle);
118}
119
120void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) {
121 ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set";
122 ASSERT_EQ(iconId, mCustomIconStyle);
123 mCustomIconStyle.reset();
124}
125
126void FakePointerController::assertCustomPointerIconNotSet() {
127 ASSERT_EQ(std::nullopt, mCustomIconStyle);
128}
129
Arpit Singhbd49b282024-05-23 18:02:54 +0000130void FakePointerController::assertIsSkipScreenshotFlagSet(ui::LogicalDisplayId displayId) {
131 ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) != mDisplaysToSkipScreenshot.end());
132}
133
134void FakePointerController::assertIsSkipScreenshotFlagNotSet(ui::LogicalDisplayId displayId) {
135 ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) == mDisplaysToSkipScreenshot.end());
136}
137
138void FakePointerController::assertSkipScreenshotFlagChanged() {
139 ASSERT_TRUE(mDisplaysToSkipScreenshotFlagChanged);
140 mDisplaysToSkipScreenshotFlagChanged = false;
141}
142
143void FakePointerController::assertSkipScreenshotFlagNotChanged() {
144 ASSERT_FALSE(mDisplaysToSkipScreenshotFlagChanged);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000145}
146
Seunghwan Choi356026c2023-02-01 14:37:25 +0900147bool FakePointerController::isPointerShown() {
148 return mIsPointerShown;
149}
150
Arpit Singh7f21fa32024-11-26 15:44:26 +0000151vec2 FakePointerController::move(float deltaX, float deltaY) {
Arpit Singhe33845c2024-10-25 20:59:24 +0000152 if (!mEnabled) return {0, 0};
Harry Cutts3beea7d2024-02-21 15:52:35 +0000153
Harry Cuttsb57f1702022-11-28 15:34:22 +0000154 mX += deltaX;
Arpit Singhe33845c2024-10-25 20:59:24 +0000155 mY += deltaY;
156
Arpit Singh7f21fa32024-11-26 15:44:26 +0000157 const vec2 position(mX, mY);
Arpit Singhe33845c2024-10-25 20:59:24 +0000158
Harry Cuttsb57f1702022-11-28 15:34:22 +0000159 if (mX < mMinX) mX = mMinX;
160 if (mX > mMaxX) mX = mMaxX;
Harry Cuttsb57f1702022-11-28 15:34:22 +0000161 if (mY < mMinY) mY = mMinY;
162 if (mY > mMaxY) mY = mMaxY;
Arpit Singhe33845c2024-10-25 20:59:24 +0000163
164 return {position.x - mX, position.y - mY};
Harry Cuttsb57f1702022-11-28 15:34:22 +0000165}
166
Seunghwan Choi356026c2023-02-01 14:37:25 +0900167void FakePointerController::fade(Transition) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000168 if (!mEnabled) return;
169
Seunghwan Choi356026c2023-02-01 14:37:25 +0900170 mIsPointerShown = false;
171}
172void FakePointerController::unfade(Transition) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000173 if (!mEnabled) return;
174
Seunghwan Choi356026c2023-02-01 14:37:25 +0900175 mIsPointerShown = true;
176}
177
Harry Cuttsb57f1702022-11-28 15:34:22 +0000178void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
Linnan Li13bf76a2024-05-05 19:18:02 +0800179 ui::LogicalDisplayId displayId) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000180 if (!mEnabled) return;
181
Harry Cuttsb57f1702022-11-28 15:34:22 +0000182 std::vector<int32_t> newSpots;
183 // Add spots for fingers that are down.
184 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
185 uint32_t id = idBits.clearFirstMarkedBit();
186 newSpots.push_back(id);
187 }
188
189 mSpotsByDisplay[displayId] = newSpots;
190}
191
192void FakePointerController::clearSpots() {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000193 if (!mEnabled) return;
194
Harry Cuttsb57f1702022-11-28 15:34:22 +0000195 mSpotsByDisplay.clear();
196}
197
Arpit Singhf0169ab2024-11-18 20:44:35 +0000198ui::Transform FakePointerController::getDisplayTransform() const {
199 return ui::Transform();
200}
201
Harry Cuttsb57f1702022-11-28 15:34:22 +0000202} // namespace android