blob: 80319f2487b412edb94479617efb86bef2918de8 [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 Jung99326452023-11-03 20:19:17 +090057void FakePointerController::updatePointerIcon(PointerIconStyle iconId) {
58 ASSERT_FALSE(mIconStyle.has_value()) << "Pointer icon was set more than once";
59 mIconStyle = iconId;
60}
61
62void FakePointerController::setCustomPointerIcon(const SpriteIcon& icon) {
63 ASSERT_FALSE(mCustomIconStyle.has_value()) << "Custom pointer icon was set more than once";
64 mCustomIconStyle = icon.style;
65}
66
Byoungho Jungee6268f2023-10-30 17:27:26 +090067void FakePointerController::assertViewportSet(int32_t displayId) {
68 ASSERT_TRUE(mDisplayId);
69 ASSERT_EQ(displayId, mDisplayId);
70}
71
72void FakePointerController::assertViewportNotSet() {
73 ASSERT_EQ(std::nullopt, mDisplayId);
74}
75
Harry Cuttsce86cc32022-12-14 20:36:33 +000076void FakePointerController::assertPosition(float x, float y) {
Prabir Pradhan2719e822023-02-28 17:39:36 +000077 const auto [actualX, actualY] = getPosition();
Harry Cuttsce86cc32022-12-14 20:36:33 +000078 ASSERT_NEAR(x, actualX, 1);
79 ASSERT_NEAR(y, actualY, 1);
80}
81
Prabir Pradhan16788792023-11-08 21:07:21 +000082void FakePointerController::assertSpotCount(int32_t displayId, int32_t count) {
83 auto it = mSpotsByDisplay.find(displayId);
84 ASSERT_TRUE(it != mSpotsByDisplay.end()) << "Spots not found for display " << displayId;
85 ASSERT_EQ(static_cast<size_t>(count), it->second.size());
86}
87
Byoungho Jung99326452023-11-03 20:19:17 +090088void FakePointerController::assertPointerIconSet(PointerIconStyle iconId) {
89 ASSERT_TRUE(mIconStyle) << "Pointer icon style was not set";
90 ASSERT_EQ(iconId, mIconStyle);
91 mIconStyle.reset();
92}
93
94void FakePointerController::assertPointerIconNotSet() {
95 ASSERT_EQ(std::nullopt, mIconStyle);
96}
97
98void FakePointerController::assertCustomPointerIconSet(PointerIconStyle iconId) {
99 ASSERT_TRUE(mCustomIconStyle) << "Custom pointer icon was not set";
100 ASSERT_EQ(iconId, mCustomIconStyle);
101 mCustomIconStyle.reset();
102}
103
104void FakePointerController::assertCustomPointerIconNotSet() {
105 ASSERT_EQ(std::nullopt, mCustomIconStyle);
106}
107
Seunghwan Choi356026c2023-02-01 14:37:25 +0900108bool FakePointerController::isPointerShown() {
109 return mIsPointerShown;
110}
111
Prabir Pradhan2719e822023-02-28 17:39:36 +0000112std::optional<FloatRect> FakePointerController::getBounds() const {
113 return mHaveBounds ? std::make_optional<FloatRect>(mMinX, mMinY, mMaxX, mMaxY) : std::nullopt;
Harry Cuttsb57f1702022-11-28 15:34:22 +0000114}
115
116void FakePointerController::move(float deltaX, float deltaY) {
117 mX += deltaX;
118 if (mX < mMinX) mX = mMinX;
119 if (mX > mMaxX) mX = mMaxX;
120 mY += deltaY;
121 if (mY < mMinY) mY = mMinY;
122 if (mY > mMaxY) mY = mMaxY;
123}
124
Seunghwan Choi356026c2023-02-01 14:37:25 +0900125void FakePointerController::fade(Transition) {
126 mIsPointerShown = false;
127}
128void FakePointerController::unfade(Transition) {
129 mIsPointerShown = true;
130}
131
Harry Cuttsb57f1702022-11-28 15:34:22 +0000132void FakePointerController::setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
133 int32_t displayId) {
134 std::vector<int32_t> newSpots;
135 // Add spots for fingers that are down.
136 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty();) {
137 uint32_t id = idBits.clearFirstMarkedBit();
138 newSpots.push_back(id);
139 }
140
141 mSpotsByDisplay[displayId] = newSpots;
142}
143
144void FakePointerController::clearSpots() {
145 mSpotsByDisplay.clear();
146}
147
148} // namespace android