blob: 31e117352b8b60594198dc64b5437dc0ea6c9e56 [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
Harry Cutts7ecbb992023-12-18 14:45:09 +000031void FakePointerController::clearBounds() {
32 mHaveBounds = false;
33}
34
Harry Cuttsb57f1702022-11-28 15:34:22 +000035const std::map<int32_t, std::vector<int32_t>>& FakePointerController::getSpots() {
36 return mSpotsByDisplay;
37}
38
39void FakePointerController::setPosition(float x, float y) {
40 mX = x;
41 mY = y;
42}
43
Prabir Pradhan2719e822023-02-28 17:39:36 +000044FloatPoint FakePointerController::getPosition() const {
45 return {mX, mY};
Harry Cuttsb57f1702022-11-28 15:34:22 +000046}
47
48int32_t FakePointerController::getDisplayId() const {
Byoungho Jungee6268f2023-10-30 17:27:26 +090049 if (!mDisplayId) {
50 return ADISPLAY_ID_NONE;
51 }
52 return *mDisplayId;
Harry Cuttsb57f1702022-11-28 15:34:22 +000053}
54
55void FakePointerController::setDisplayViewport(const DisplayViewport& viewport) {
56 mDisplayId = viewport.displayId;
Byoungho Jung1fe1f042023-10-24 15:27:18 +090057 setBounds(viewport.logicalLeft, viewport.logicalTop, viewport.logicalRight - 1,
58 viewport.logicalBottom - 1);
Harry Cuttsb57f1702022-11-28 15:34:22 +000059}
60
Byoungho Jung99326452023-11-03 20:19:17 +090061void FakePointerController::updatePointerIcon(PointerIconStyle iconId) {
62 ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once";
63 mIconStyle = iconId;
64}
65
66void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) {
67 ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once";
68 mCustomIconStyle = icon.style;
69}
70
Byoungho Jungee6268f2023-10-30 17:27:26 +090071void FakePointerController::assertViewportSet(int32_t displayId) {
72 ASSERT_TRUE(mDisplayId);
73 ASSERT_EQ(displayId, mDisplayId);
74}
75
76void FakePointerController::assertViewportNotSet() {
77 ASSERT_EQ(std::nullopt, mDisplayId);
78}
79
Harry Cuttsce86cc32022-12-14 20:36:33 +000080void FakePointerController::assertPosition(float x, float y) {
Prabir Pradhan2719e822023-02-28 17:39:36 +000081 const auto [actualX, actualY] = getPosition();
Harry Cuttsce86cc32022-12-14 20:36:33 +000082 ASSERT_NEAR(x, actualX, 1);
83 ASSERT_NEAR(y, actualY, 1);
84}
85
Prabir Pradhan16788792023-11-08 21:07:21 +000086void FakePointerController::assertSpotCount(int32_t displayId, int32_t count) {
87 auto it = mSpotsByDisplay.find(displayId);
88 ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId;
89 ASSERT_EQ(static_cast<size_t>(count), it->second.size());
90}
91
Byoungho Jung99326452023-11-03 20:19:17 +090092void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) {
93 ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set";
94 ASSERT_EQ(iconId, mIconStyle);
95 mIconStyle.reset();
96}
97
98void FakePointerController::assertPointerIconNotSet() {
99 ASSERT_EQ(std::nullopt, mIconStyle);
100}
101
102void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) {
103 ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set";
104 ASSERT_EQ(iconId, mCustomIconStyle);
105 mCustomIconStyle.reset();
106}
107
108void FakePointerController::assertCustomPointerIconNotSet() {
109 ASSERT_EQ(std::nullopt, mCustomIconStyle);
110}
111
Seunghwan Choi356026c2023-02-01 14:37:25 +0900112bool FakePointerController::isPointerShown() {
113 return mIsPointerShown;
114}
115
Prabir Pradhan2719e822023-02-28 17:39:36 +0000116std::optional<FloatRect> FakePointerController::getBounds() const {
117 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
Harry Cuttsb57f1702022-11-28 15:34:22 +0000118}
119
120void FakePointerController::move(float deltaX, float deltaY) {
121 mX += deltaX;
122 if (mX < mMinX) mX = mMinX;
123 if (mX > mMaxX) mX = mMaxX;
124 mY += deltaY;
125 if (mY < mMinY) mY = mMinY;
126 if (mY > mMaxY) mY = mMaxY;
127}
128
Seunghwan Choi356026c2023-02-01 14:37:25 +0900129void FakePointerController::fade(Transition) {
130 mIsPointerShown = false;
131}
132void FakePointerController::unfade(Transition) {
133 mIsPointerShown = true;
134}
135
Harry Cuttsb57f1702022-11-28 15:34:22 +0000136void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
137 int32_t displayId) {
138 std::vector<int32_t> newSpots;
139 // Add spots for fingers that are down.
140 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
141 uint32_t id = idBits.clearFirstMarkedBit();
142 newSpots.push_back(id);
143 }
144
145 mSpotsByDisplay[displayId] = newSpots;
146}
147
148void FakePointerController::clearSpots() {
149 mSpotsByDisplay.clear();
150}
151
152} // namespace android