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 | |
| 40 | void FakePointerController::setButtonState(int32_t buttonState) { |
| 41 | mButtonState = buttonState; |
| 42 | } |
| 43 | |
| 44 | int32_t FakePointerController::getButtonState() const { |
| 45 | return mButtonState; |
| 46 | } |
| 47 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame^] | 48 | FloatPoint FakePointerController::getPosition() const { |
| 49 | return {mX, mY}; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int32_t FakePointerController::getDisplayId() const { |
| 53 | return mDisplayId; |
| 54 | } |
| 55 | |
| 56 | void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) { |
| 57 | mDisplayId = viewport.displayId; |
| 58 | } |
| 59 | |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 60 | void FakePointerController::assertPosition(float x, float y) { |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame^] | 61 | const auto [actualX, actualY] = getPosition(); |
Harry Cutts | ce86cc3 | 2022-12-14 20:36:33 +0000 | [diff] [blame] | 62 | ASSERT_NEAR(x, actualX, 1); |
| 63 | ASSERT_NEAR(y, actualY, 1); |
| 64 | } |
| 65 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 66 | bool FakePointerController::isPointerShown() { |
| 67 | return mIsPointerShown; |
| 68 | } |
| 69 | |
Prabir Pradhan | 2719e82 | 2023-02-28 17:39:36 +0000 | [diff] [blame^] | 70 | std::optional<FloatRect> FakePointerController::getBounds() const { |
| 71 | return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt; |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void FakePointerController::move(float deltaX, float deltaY) { |
| 75 | mX += deltaX; |
| 76 | if (mX < mMinX) mX = mMinX; |
| 77 | if (mX > mMaxX) mX = mMaxX; |
| 78 | mY += deltaY; |
| 79 | if (mY < mMinY) mY = mMinY; |
| 80 | if (mY > mMaxY) mY = mMaxY; |
| 81 | } |
| 82 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 83 | void FakePointerController::fade(Transition) { |
| 84 | mIsPointerShown = false; |
| 85 | } |
| 86 | void FakePointerController::unfade(Transition) { |
| 87 | mIsPointerShown = true; |
| 88 | } |
| 89 | |
Harry Cutts | b57f170 | 2022-11-28 15:34:22 +0000 | [diff] [blame] | 90 | void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits, |
| 91 | int32_t displayId) { |
| 92 | std::vector<int32_t> newSpots; |
| 93 | // Add spots for fingers that are down. |
| 94 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) { |
| 95 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 96 | newSpots.push_back(id); |
| 97 | } |
| 98 | |
| 99 | mSpotsByDisplay[displayId] = newSpots; |
| 100 | } |
| 101 | |
| 102 | void FakePointerController::clearSpots() { |
| 103 | mSpotsByDisplay.clear(); |
| 104 | } |
| 105 | |
| 106 | } // namespace android |