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
19 #include <gtest/gtest.h>
20
21 namespace android {
22
setBounds(float minX,float minY,float maxX,float maxY)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
getSpots()31 const std::map<int32_t, std::vector<int32_t>>& FakePointerController::getSpots() {
32 return mSpotsByDisplay;
33 }
34
setPosition(float x,float y)35 void FakePointerController::setPosition(float x, float y) {
36 mX = x;
37 mY = y;
38 }
39
getPosition() const40 FloatPoint FakePointerController::getPosition() const {
41 return {mX, mY};
42 }
43
getDisplayId() const44 int32_t FakePointerController::getDisplayId() const {
45 return mDisplayId;
46 }
47
setDisplayViewport(const DisplayViewport & viewport)48 void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
49 mDisplayId = viewport.displayId;
50 }
51
assertPosition(float x,float y)52 void FakePointerController::assertPosition(float x, float y) {
53 const auto [actualX, actualY] = getPosition();
54 ASSERT_NEAR(x, actualX, 1);
55 ASSERT_NEAR(y, actualY, 1);
56 }
57
isPointerShown()58 bool FakePointerController::isPointerShown() {
59 return mIsPointerShown;
60 }
61
getBounds() const62 std::optional<FloatRect> FakePointerController::getBounds() const {
63 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
64 }
65
move(float deltaX,float deltaY)66 void FakePointerController::move(float deltaX, float deltaY) {
67 mX += deltaX;
68 if (mX < mMinX) mX = mMinX;
69 if (mX > mMaxX) mX = mMaxX;
70 mY += deltaY;
71 if (mY < mMinY) mY = mMinY;
72 if (mY > mMaxY) mY = mMaxY;
73 }
74
fade(Transition)75 void FakePointerController::fade(Transition) {
76 mIsPointerShown = false;
77 }
unfade(Transition)78 void FakePointerController::unfade(Transition) {
79 mIsPointerShown = true;
80 }
81
setSpots(const PointerCoords *,const uint32_t *,BitSet32 spotIdBits,int32_t displayId)82 void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
83 int32_t displayId) {
84 std::vector<int32_t> newSpots;
85 // Add spots for fingers that are down.
86 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
87 uint32_t id = idBits.clearFirstMarkedBit();
88 newSpots.push_back(id);
89 }
90
91 mSpotsByDisplay[displayId] = newSpots;
92 }
93
clearSpots()94 void FakePointerController::clearSpots() {
95 mSpotsByDisplay.clear();
96 }
97
98 } // namespace android
99