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 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 35 | const std::map<int32_t, std::vector<int32_t>>& FakePointerController::getSpots() { |
| 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 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 46 | FloatPoint 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 | |
| 54 | int32_t FakePointerController::getDisplayId() const { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 55 | if (!mEnabled || !mDisplayId) { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 56 | return ADISPLAY_ID_NONE; |
| 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 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 79 | void FakePointerController::assertViewportSet(int32_t displayId) { |
| 80 | ASSERT_TRUE(mDisplayId); |
| 81 | ASSERT_EQ(displayId, mDisplayId); |
| 82 | } |
| 83 | |
| 84 | void FakePointerController::assertViewportNotSet() { |
| 85 | ASSERT_EQ(std::nullopt, mDisplayId); |
| 86 | } |
| 87 | |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 88 | void FakePointerController::assertPosition(float x, float y) { |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 89 | const auto [actualX, actualY] = getPosition(); |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 90 | ASSERT_NEAR(x, actualX, 1); |
| 91 | ASSERT_NEAR(y, actualY, 1); |
| 92 | } |
| 93 | |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 94 | void FakePointerController::assertSpotCount(int32_t displayId, int32_t count) { |
| 95 | auto it = mSpotsByDisplay.find(displayId); |
| 96 | ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId; |
| 97 | ASSERT_EQ(static_cast<size_t>(count), it->second.size()); |
| 98 | } |
| 99 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 100 | void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) { |
| 101 | ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set"; |
| 102 | ASSERT_EQ(iconId, mIconStyle); |
| 103 | mIconStyle.reset(); |
| 104 | } |
| 105 | |
| 106 | void FakePointerController::assertPointerIconNotSet() { |
| 107 | ASSERT_EQ(std::nullopt, mIconStyle); |
| 108 | } |
| 109 | |
| 110 | void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) { |
| 111 | ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set"; |
| 112 | ASSERT_EQ(iconId, mCustomIconStyle); |
| 113 | mCustomIconStyle.reset(); |
| 114 | } |
| 115 | |
| 116 | void FakePointerController::assertCustomPointerIconNotSet() { |
| 117 | ASSERT_EQ(std::nullopt, mCustomIconStyle); |
| 118 | } |
| 119 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 120 | bool FakePointerController::isPointerShown() { |
| 121 | return mIsPointerShown; |
| 122 | } |
| 123 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 124 | std::optional<FloatRect> FakePointerController::getBounds() const { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 125 | if (!mEnabled) return std::nullopt; |
| 126 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 127 | return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void FakePointerController::move(float deltaX, float deltaY) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 131 | if (!mEnabled) return; |
| 132 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 133 | mX += deltaX; |
| 134 | if (mX < mMinX) mX = mMinX; |
| 135 | if (mX > mMaxX) mX = mMaxX; |
| 136 | mY += deltaY; |
| 137 | if (mY < mMinY) mY = mMinY; |
| 138 | if (mY > mMaxY) mY = mMaxY; |
| 139 | } |
| 140 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 141 | void FakePointerController::fade(Transition) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 142 | if (!mEnabled) return; |
| 143 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 144 | mIsPointerShown = false; |
| 145 | } |
| 146 | void FakePointerController::unfade(Transition) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 147 | if (!mEnabled) return; |
| 148 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 149 | mIsPointerShown = true; |
| 150 | } |
| 151 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 152 | void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits, |
| 153 | int32_t displayId) { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 154 | if (!mEnabled) return; |
| 155 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 156 | std::vector<int32_t> newSpots; |
| 157 | // Add spots for fingers that are down. |
| 158 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) { |
| 159 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 160 | newSpots.push_back(id); |
| 161 | } |
| 162 | |
| 163 | mSpotsByDisplay[displayId] = newSpots; |
| 164 | } |
| 165 | |
| 166 | void FakePointerController::clearSpots() { |
Harry Cutts | 3beea7d | 2024-02-21 15:52:35 +0000 | [diff] [blame^] | 167 | if (!mEnabled) return; |
| 168 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 169 | mSpotsByDisplay.clear(); |
| 170 | } |
| 171 | |
| 172 | } // namespace android |