blob: 804381299925c272de6bcf62700ab62d6978c82c [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 {
Byoungho Jungee6268f2023-10-30 17:27:26 +090045 if (!mDisplayId) {
46 return ADISPLAY_ID_NONE;
47 }
48 return *mDisplayId;
Harry Cuttsb57f1702022-11-28 15:34:22 +000049}
50
51void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
52 mDisplayId = viewport.displayId;
Byoungho Jung1fe1f042023-10-24 15:27:18 +090053 setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1,
54 viewport.logicalBottom - 1);
Harry Cuttsb57f1702022-11-28 15:34:22 +000055}
56
Byoungho Jungee6268f2023-10-30 17:27:26 +090057void FakePointerController::assertViewportSet(int32_t displayId) {
58 ASSERT_TRUE(mDisplayId);
59 ASSERT_EQ(displayId, mDisplayId);
60}
61
62void FakePointerController::assertViewportNotSet() {
63 ASSERT_EQ(std::nullopt, mDisplayId);
64}
65
Harry Cuttsce86cc32022-12-14 20:36:33 +000066void FakePointerController::assertPosition(float x, float y) {
Prabir Pradhan2719e822023-02-28 17:39:36 +000067 const auto [actualX, actualY] = getPosition();
Harry Cuttsce86cc32022-12-14 20:36:33 +000068 ASSERT_NEAR(x, actualX, 1);
69 ASSERT_NEAR(y, actualY, 1);
70}
71
Prabir Pradhan16788792023-11-08 21:07:21 +000072void FakePointerController::assertSpotCount(int32_t displayId, int32_t count) {
73 auto it = mSpotsByDisplay.find(displayId);
74 ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId;
75 ASSERT_EQ(static_cast<size_t>(count), it->second.size());
76}
77
Seunghwan Choi356026c2023-02-01 14:37:25 +090078bool FakePointerController::isPointerShown() {
79 return mIsPointerShown;
80}
81
Prabir Pradhan2719e822023-02-28 17:39:36 +000082std::optional<FloatRect> FakePointerController::getBounds() const {
83 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
Harry Cuttsb57f1702022-11-28 15:34:22 +000084}
85
86void FakePointerController::move(float deltaX, float deltaY) {
87 mX += deltaX;
88 if (mX < mMinX) mX = mMinX;
89 if (mX > mMaxX) mX = mMaxX;
90 mY += deltaY;
91 if (mY < mMinY) mY = mMinY;
92 if (mY > mMaxY) mY = mMaxY;
93}
94
Seunghwan Choi356026c2023-02-01 14:37:25 +090095void FakePointerController::fade(Transition) {
96 mIsPointerShown = false;
97}
98void FakePointerController::unfade(Transition) {
99 mIsPointerShown = true;
100}
101
Harry Cuttsb57f1702022-11-28 15:34:22 +0000102void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
103 int32_t displayId) {
104 std::vector<int32_t> newSpots;
105 // Add spots for fingers that are down.
106 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
107 uint32_t id = idBits.clearFirstMarkedBit();
108 newSpots.push_back(id);
109 }
110
111 mSpotsByDisplay[displayId] = newSpots;
112}
113
114void FakePointerController::clearSpots() {
115 mSpotsByDisplay.clear();
116}
117
118} // namespace android