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 | |
| 31 | const std::map<int32_t, std::vector<int32_t>>& FakePointerController::getSpots() { |
| 32 | return mSpotsByDisplay; |
| 33 | } |
| 34 | |
| 35 | void FakePointerController::setPosition(float x, float y) { |
| 36 | mX = x; |
| 37 | mY = y; |
| 38 | } |
| 39 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 40 | FloatPoint FakePointerController::getPosition() const { |
| 41 | return {mX, mY}; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int32_t FakePointerController::getDisplayId() const { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 45 | if (!mDisplayId) { |
| 46 | return ADISPLAY_ID_NONE; |
| 47 | } |
| 48 | return *mDisplayId; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) { |
| 52 | mDisplayId = viewport.displayId; |
Byoungho Jung | 1fe1f04 | 2023-10-24 15:27:18 +0900 | [diff] [blame] | 53 | setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1, |
| 54 | viewport.logicalBottom - 1); |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame^] | 57 | void FakePointerController::updatePointerIcon(PointerIconStyle iconId) { |
| 58 | ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once"; |
| 59 | mIconStyle = iconId; |
| 60 | } |
| 61 | |
| 62 | void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) { |
| 63 | ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once"; |
| 64 | mCustomIconStyle = icon.style; |
| 65 | } |
| 66 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 67 | void FakePointerController::assertViewportSet(int32_t displayId) { |
| 68 | ASSERT_TRUE(mDisplayId); |
| 69 | ASSERT_EQ(displayId, mDisplayId); |
| 70 | } |
| 71 | |
| 72 | void FakePointerController::assertViewportNotSet() { |
| 73 | ASSERT_EQ(std::nullopt, mDisplayId); |
| 74 | } |
| 75 | |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 76 | void FakePointerController::assertPosition(float x, float y) { |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 77 | const auto [actualX, actualY] = getPosition(); |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 78 | ASSERT_NEAR(x, actualX, 1); |
| 79 | ASSERT_NEAR(y, actualY, 1); |
| 80 | } |
| 81 | |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 82 | void FakePointerController::assertSpotCount(int32_t displayId, int32_t count) { |
| 83 | auto it = mSpotsByDisplay.find(displayId); |
| 84 | ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId; |
| 85 | ASSERT_EQ(static_cast<size_t>(count), it->second.size()); |
| 86 | } |
| 87 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame^] | 88 | void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) { |
| 89 | ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set"; |
| 90 | ASSERT_EQ(iconId, mIconStyle); |
| 91 | mIconStyle.reset(); |
| 92 | } |
| 93 | |
| 94 | void FakePointerController::assertPointerIconNotSet() { |
| 95 | ASSERT_EQ(std::nullopt, mIconStyle); |
| 96 | } |
| 97 | |
| 98 | void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) { |
| 99 | ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set"; |
| 100 | ASSERT_EQ(iconId, mCustomIconStyle); |
| 101 | mCustomIconStyle.reset(); |
| 102 | } |
| 103 | |
| 104 | void FakePointerController::assertCustomPointerIconNotSet() { |
| 105 | ASSERT_EQ(std::nullopt, mCustomIconStyle); |
| 106 | } |
| 107 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 108 | bool FakePointerController::isPointerShown() { |
| 109 | return mIsPointerShown; |
| 110 | } |
| 111 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame] | 112 | std::optional<FloatRect> FakePointerController::getBounds() const { |
| 113 | return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void FakePointerController::move(float deltaX, float deltaY) { |
| 117 | mX += deltaX; |
| 118 | if (mX < mMinX) mX = mMinX; |
| 119 | if (mX > mMaxX) mX = mMaxX; |
| 120 | mY += deltaY; |
| 121 | if (mY < mMinY) mY = mMinY; |
| 122 | if (mY > mMaxY) mY = mMaxY; |
| 123 | } |
| 124 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 125 | void FakePointerController::fade(Transition) { |
| 126 | mIsPointerShown = false; |
| 127 | } |
| 128 | void FakePointerController::unfade(Transition) { |
| 129 | mIsPointerShown = true; |
| 130 | } |
| 131 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 132 | void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits, |
| 133 | int32_t displayId) { |
| 134 | std::vector<int32_t> newSpots; |
| 135 | // Add spots for fingers that are down. |
| 136 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) { |
| 137 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 138 | newSpots.push_back(id); |
| 139 | } |
| 140 | |
| 141 | mSpotsByDisplay[displayId] = newSpots; |
| 142 | } |
| 143 | |
| 144 | void FakePointerController::clearSpots() { |
| 145 | mSpotsByDisplay.clear(); |
| 146 | } |
| 147 | |
| 148 | } // namespace android |