Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 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 | |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 21 | namespace android { |
| 22 | |
| 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 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 31 | void FakePointerController::clearBounds() { |
| 32 | mHaveBounds = false; |
| 33 | } |
| 34 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 35 | const std::map<ui::LogicalDisplayId, std::vector<int32_t>>& FakePointerController::getSpots() { |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 36 | return mSpotsByDisplay; |
| 37 | } |
| 38 | |
| 39 | void FakePointerController::setPosition(float x, float y) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 40 | if (!mEnabled) return; |
| 41 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 42 | mX = x; |
| 43 | mY = y; |
| 44 | } |
| 45 | |
Arpit Singh | 7f21fa3 | 2024-11-26 15:44:26 +0000 | [diff] [blame^] | 46 | vec2 FakePointerController::getPosition() const { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 47 | if (!mEnabled) { |
| 48 | return {0, 0}; |
| 49 | } |
| 50 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 51 | return {mX, mY}; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 54 | ui::LogicalDisplayId FakePointerController::getDisplayId() const { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 55 | if (!mEnabled || !mDisplayId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 56 | return ui::LogicalDisplayId::INVALID; |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 57 | } |
| 58 | return *mDisplayId; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) { |
| 62 | mDisplayId = viewport.displayId; |
Byoungho Jung | 1fe1f04 | 2023-10-24 15:27:18 +0900 | [diff] [blame] | 63 | setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1, |
| 64 | viewport.logicalBottom - 1); |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 67 | void FakePointerController::updatePointerIcon(PointerIconStyle iconId) { |
| 68 | ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once"; |
| 69 | mIconStyle = iconId; |
| 70 | } |
| 71 | |
| 72 | void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 73 | if (!mEnabled) return; |
| 74 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 75 | ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once"; |
| 76 | mCustomIconStyle = icon.style; |
| 77 | } |
| 78 | |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 79 | void FakePointerController::setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) { |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 80 | mDisplaysToSkipScreenshotFlagChanged = true; |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 81 | mDisplaysToSkipScreenshot.insert(displayId); |
| 82 | } |
| 83 | |
| 84 | void FakePointerController::clearSkipScreenshotFlags() { |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 85 | mDisplaysToSkipScreenshotFlagChanged = true; |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 86 | mDisplaysToSkipScreenshot.clear(); |
| 87 | } |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 88 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 89 | void FakePointerController::assertViewportSet(ui::LogicalDisplayId displayId) { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 90 | ASSERT_TRUE(mDisplayId); |
| 91 | ASSERT_EQ(displayId, mDisplayId); |
| 92 | } |
| 93 | |
| 94 | void FakePointerController::assertViewportNotSet() { |
| 95 | ASSERT_EQ(std::nullopt, mDisplayId); |
| 96 | } |
| 97 | |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 98 | void FakePointerController::assertPosition(float x, float y) { |
Arpit Singh | 7f21fa3 | 2024-11-26 15:44:26 +0000 | [diff] [blame^] | 99 | const auto actual = getPosition(); |
| 100 | ASSERT_NEAR(x, actual.x, 1); |
| 101 | ASSERT_NEAR(y, actual.y, 1); |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 104 | void FakePointerController::assertSpotCount(ui::LogicalDisplayId displayId, int32_t count) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 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 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 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 | |
| 116 | void FakePointerController::assertPointerIconNotSet() { |
| 117 | ASSERT_EQ(std::nullopt, mIconStyle); |
| 118 | } |
| 119 | |
| 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 | |
| 126 | void FakePointerController::assertCustomPointerIconNotSet() { |
| 127 | ASSERT_EQ(std::nullopt, mCustomIconStyle); |
| 128 | } |
| 129 | |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 130 | void FakePointerController::assertIsSkipScreenshotFlagSet(ui::LogicalDisplayId displayId) { |
| 131 | ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) != mDisplaysToSkipScreenshot.end()); |
| 132 | } |
| 133 | |
| 134 | void FakePointerController::assertIsSkipScreenshotFlagNotSet(ui::LogicalDisplayId displayId) { |
| 135 | ASSERT_TRUE(mDisplaysToSkipScreenshot.find(displayId) == mDisplaysToSkipScreenshot.end()); |
| 136 | } |
| 137 | |
| 138 | void FakePointerController::assertSkipScreenshotFlagChanged() { |
| 139 | ASSERT_TRUE(mDisplaysToSkipScreenshotFlagChanged); |
| 140 | mDisplaysToSkipScreenshotFlagChanged = false; |
| 141 | } |
| 142 | |
| 143 | void FakePointerController::assertSkipScreenshotFlagNotChanged() { |
| 144 | ASSERT_FALSE(mDisplaysToSkipScreenshotFlagChanged); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 147 | bool FakePointerController::isPointerShown() { |
| 148 | return mIsPointerShown; |
| 149 | } |
| 150 | |
Arpit Singh | 7f21fa3 | 2024-11-26 15:44:26 +0000 | [diff] [blame^] | 151 | vec2 FakePointerController::move(float deltaX, float deltaY) { |
Arpit Singh | e33845c | 2024-10-25 20:59:24 +0000 | [diff] [blame] | 152 | if (!mEnabled) return {0, 0}; |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 153 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 154 | mX += deltaX; |
Arpit Singh | e33845c | 2024-10-25 20:59:24 +0000 | [diff] [blame] | 155 | mY += deltaY; |
| 156 | |
Arpit Singh | 7f21fa3 | 2024-11-26 15:44:26 +0000 | [diff] [blame^] | 157 | const vec2 position(mX, mY); |
Arpit Singh | e33845c | 2024-10-25 20:59:24 +0000 | [diff] [blame] | 158 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 159 | if (mX < mMinX) mX = mMinX; |
| 160 | if (mX > mMaxX) mX = mMaxX; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 161 | if (mY < mMinY) mY = mMinY; |
| 162 | if (mY > mMaxY) mY = mMaxY; |
Arpit Singh | e33845c | 2024-10-25 20:59:24 +0000 | [diff] [blame] | 163 | |
| 164 | return {position.x - mX, position.y - mY}; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 167 | void FakePointerController::fade(Transition) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 168 | if (!mEnabled) return; |
| 169 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 170 | mIsPointerShown = false; |
| 171 | } |
| 172 | void FakePointerController::unfade(Transition) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 173 | if (!mEnabled) return; |
| 174 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 175 | mIsPointerShown = true; |
| 176 | } |
| 177 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 178 | void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 179 | ui::LogicalDisplayId displayId) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 180 | if (!mEnabled) return; |
| 181 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 182 | 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 | |
| 192 | void FakePointerController::clearSpots() { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame] | 193 | if (!mEnabled) return; |
| 194 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 195 | mSpotsByDisplay.clear(); |
| 196 | } |
| 197 | |
Arpit Singh | f0169ab | 2024-11-18 20:44:35 +0000 | [diff] [blame] | 198 | ui::Transform FakePointerController::getDisplayTransform() const { |
| 199 | return ui::Transform(); |
| 200 | } |
| 201 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 202 | } // namespace android |