blob: 57d00a0727908a33f161a7fd8ae47e653c66dcc4 [file] [log] [blame]
Harry Cuttsb57f1702022-11-28 15:34:22 +00001/*
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 Cuttsce86cc32022-12-14 20:36:33 +000019#include <gtest/gtest.h>
20
Harry Cuttsb57f1702022-11-28 15:34:22 +000021namespace android {
22
23void 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
31const std::map<int32_t, std::vector<int32_t>>& FakePointerController::getSpots() {
32 return mSpotsByDisplay;
33}
34
35void FakePointerController::setPosition(float x, float y) {
36 mX = x;
37 mY = y;
38}
39
Prabir Pradhan2719e822023-02-28 17:39:36 +000040FloatPoint FakePointerController::getPosition() const {
41 return {mX, mY};
Harry Cuttsb57f1702022-11-28 15:34:22 +000042}
43
44int32_t FakePointerController::getDisplayId() const {
45 return mDisplayId;
46}
47
48void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
49 mDisplayId = viewport.displayId;
Byoungho Jung1fe1f042023-10-24 15:27:18 +090050 setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1,
51 viewport.logicalBottom - 1);
Harry Cuttsb57f1702022-11-28 15:34:22 +000052}
53
Harry Cuttsce86cc32022-12-14 20:36:33 +000054void FakePointerController::assertPosition(float x, float y) {
Prabir Pradhan2719e822023-02-28 17:39:36 +000055 const auto [actualX, actualY] = getPosition();
Harry Cuttsce86cc32022-12-14 20:36:33 +000056 ASSERT_NEAR(x, actualX, 1);
57 ASSERT_NEAR(y, actualY, 1);
58}
59
Seunghwan Choi356026c2023-02-01 14:37:25 +090060bool FakePointerController::isPointerShown() {
61 return mIsPointerShown;
62}
63
Prabir Pradhan2719e822023-02-28 17:39:36 +000064std::optional<FloatRect> FakePointerController::getBounds() const {
65 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
Harry Cuttsb57f1702022-11-28 15:34:22 +000066}
67
68void FakePointerController::move(float deltaX, float deltaY) {
69 mX += deltaX;
70 if (mX < mMinX) mX = mMinX;
71 if (mX > mMaxX) mX = mMaxX;
72 mY += deltaY;
73 if (mY < mMinY) mY = mMinY;
74 if (mY > mMaxY) mY = mMaxY;
75}
76
Seunghwan Choi356026c2023-02-01 14:37:25 +090077void FakePointerController::fade(Transition) {
78 mIsPointerShown = false;
79}
80void FakePointerController::unfade(Transition) {
81 mIsPointerShown = true;
82}
83
Harry Cuttsb57f1702022-11-28 15:34:22 +000084void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
85 int32_t displayId) {
86 std::vector<int32_t> newSpots;
87 // Add spots for fingers that are down.
88 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
89 uint32_t id = idBits.clearFirstMarkedBit();
90 newSpots.push_back(id);
91 }
92
93 mSpotsByDisplay[displayId] = newSpots;
94}
95
96void FakePointerController::clearSpots() {
97 mSpotsByDisplay.clear();
98}
99
100} // namespace android