blob: 2bb57b3e81112e66c7b76151fe1a2ea18ea443f4 [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
Prabir Pradhan2719e822023-02-28 17:39:36 +000046FloatPoint 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
Linnan Li13bf76a2024-05-05 19:18:02 +080079void FakePointerController::setSkipScreenshot(ui::LogicalDisplayId displayId, bool skip) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000080 if (skip) {
81 mDisplaysToSkipScreenshot.insert(displayId);
82 } else {
83 mDisplaysToSkipScreenshot.erase(displayId);
84 }
85};
86
Linnan Li13bf76a2024-05-05 19:18:02 +080087void FakePointerController::assertViewportSet(ui::LogicalDisplayId displayId) {
Byoungho Jungee6268f2023-10-30 17:27:26 +090088 ASSERT_TRUE(mDisplayId);
89 ASSERT_EQ(displayId, mDisplayId);
90}
91
92void FakePointerController::assertViewportNotSet() {
93 ASSERT_EQ(std::nullopt, mDisplayId);
94}
95
Harry Cuttsce86cc32022-12-14 20:36:33 +000096void FakePointerController::assertPosition(float x, float y) {
Prabir Pradhan2719e822023-02-28 17:39:36 +000097 const auto [actualX, actualY] = getPosition();
Harry Cuttsce86cc32022-12-14 20:36:33 +000098 ASSERT_NEAR(x, actualX, 1);
99 ASSERT_NEAR(y, actualY, 1);
100}
101
Linnan Li13bf76a2024-05-05 19:18:02 +0800102void FakePointerController::assertSpotCount(ui::LogicalDisplayId displayId, int32_t count) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000103 auto it = mSpotsByDisplay.find(displayId);
104 ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId;
105 ASSERT_EQ(static_cast<size_t>(count), it->second.size());
106}
107
Byoungho Jung99326452023-11-03 20:19:17 +0900108void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) {
109 ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set";
110 ASSERT_EQ(iconId, mIconStyle);
111 mIconStyle.reset();
112}
113
114void FakePointerController::assertPointerIconNotSet() {
115 ASSERT_EQ(std::nullopt, mIconStyle);
116}
117
118void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) {
119 ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set";
120 ASSERT_EQ(iconId, mCustomIconStyle);
121 mCustomIconStyle.reset();
122}
123
124void FakePointerController::assertCustomPointerIconNotSet() {
125 ASSERT_EQ(std::nullopt, mCustomIconStyle);
126}
127
Linnan Li13bf76a2024-05-05 19:18:02 +0800128void FakePointerController::assertIsHiddenOnMirroredDisplays(ui::LogicalDisplayId displayId,
129 bool isHidden) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000130 if (isHidden) {
131 ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) != mDisplaysToSkipScreenshot.end());
132 } else {
133 ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) == mDisplaysToSkipScreenshot.end());
134 }
135}
136
Seunghwan Choi356026c2023-02-01 14:37:25 +0900137bool FakePointerController::isPointerShown() {
138 return mIsPointerShown;
139}
140
Prabir Pradhan2719e822023-02-28 17:39:36 +0000141std::optional<FloatRect> FakePointerController::getBounds() const {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000142 if (!mEnabled) return std::nullopt;
143
Prabir Pradhan2719e822023-02-28 17:39:36 +0000144 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
Harry Cuttsb57f1702022-11-28 15:34:22 +0000145}
146
147void FakePointerController::move(float deltaX, float deltaY) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000148 if (!mEnabled) return;
149
Harry Cuttsb57f1702022-11-28 15:34:22 +0000150 mX += deltaX;
151 if (mX < mMinX) mX = mMinX;
152 if (mX > mMaxX) mX = mMaxX;
153 mY += deltaY;
154 if (mY < mMinY) mY = mMinY;
155 if (mY > mMaxY) mY = mMaxY;
156}
157
Seunghwan Choi356026c2023-02-01 14:37:25 +0900158void FakePointerController::fade(Transition) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000159 if (!mEnabled) return;
160
Seunghwan Choi356026c2023-02-01 14:37:25 +0900161 mIsPointerShown = false;
162}
163void FakePointerController::unfade(Transition) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000164 if (!mEnabled) return;
165
Seunghwan Choi356026c2023-02-01 14:37:25 +0900166 mIsPointerShown = true;
167}
168
Harry Cuttsb57f1702022-11-28 15:34:22 +0000169void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
Linnan Li13bf76a2024-05-05 19:18:02 +0800170 ui::LogicalDisplayId displayId) {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000171 if (!mEnabled) return;
172
Harry Cuttsb57f1702022-11-28 15:34:22 +0000173 std::vector<int32_t> newSpots;
174 // Add spots for fingers that are down.
175 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
176 uint32_t id = idBits.clearFirstMarkedBit();
177 newSpots.push_back(id);
178 }
179
180 mSpotsByDisplay[displayId] = newSpots;
181}
182
183void FakePointerController::clearSpots() {
Harry Cutts3beea7d2024-02-21 15:52:35 +0000184 if (!mEnabled) return;
185
Harry Cuttsb57f1702022-11-28 15:34:22 +0000186 mSpotsByDisplay.clear();
187}
188
189} // namespace android