blob: 92efb4ea86ff040f2a1d568b515e3a9eaf300a59 [file] [log] [blame]
Garfield Tan67e479a2019-08-05 16:47:40 -07001/*
2 * Copyright (C) 2019 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 "mocks/MockSprite.h"
18#include "mocks/MockSpriteController.h"
19
20#include <input/PointerController.h>
21#include <input/SpriteController.h>
22
23#include <atomic>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26#include <thread>
27
28namespace android {
29
30enum TestCursorType {
31 CURSOR_TYPE_DEFAULT = 0,
32 CURSOR_TYPE_HOVER,
33 CURSOR_TYPE_TOUCH,
34 CURSOR_TYPE_ANCHOR,
35 CURSOR_TYPE_ADDITIONAL_1,
36 CURSOR_TYPE_ADDITIONAL_2,
37 CURSOR_TYPE_CUSTOM = -1,
38};
39
40using ::testing::AllOf;
41using ::testing::Field;
42using ::testing::NiceMock;
43using ::testing::Mock;
44using ::testing::Return;
45using ::testing::Test;
46
47std::pair<float, float> getHotSpotCoordinatesForType(int32_t type) {
48 return std::make_pair(type * 10, type * 10 + 5);
49}
50
51class MockPointerControllerPolicyInterface : public PointerControllerPolicyInterface {
52public:
53 virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) override;
54 virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) override;
55 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
56 std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) override;
57 virtual int32_t getDefaultPointerIconId() override;
58 virtual int32_t getCustomPointerIconId() override;
59
60private:
61 void loadPointerIconForType(SpriteIcon* icon, int32_t cursorType);
62};
63
64void MockPointerControllerPolicyInterface::loadPointerIcon(SpriteIcon* icon, int32_t) {
65 loadPointerIconForType(icon, CURSOR_TYPE_DEFAULT);
66}
67
68void MockPointerControllerPolicyInterface::loadPointerResources(PointerResources* outResources,
69 int32_t) {
70 loadPointerIconForType(&outResources->spotHover, CURSOR_TYPE_HOVER);
71 loadPointerIconForType(&outResources->spotTouch, CURSOR_TYPE_TOUCH);
72 loadPointerIconForType(&outResources->spotAnchor, CURSOR_TYPE_ANCHOR);
73}
74
75void MockPointerControllerPolicyInterface::loadAdditionalMouseResources(
76 std::map<int32_t, SpriteIcon>* outResources,
77 std::map<int32_t, PointerAnimation>* outAnimationResources,
78 int32_t) {
79 SpriteIcon icon;
80 PointerAnimation anim;
81
82 for (int32_t cursorType : {CURSOR_TYPE_ADDITIONAL_1, CURSOR_TYPE_ADDITIONAL_2}) {
83 loadPointerIconForType(&icon, cursorType);
84 anim.animationFrames.push_back(icon);
85 anim.durationPerFrame = 10;
86 (*outResources)[cursorType] = icon;
87 (*outAnimationResources)[cursorType] = anim;
88 }
89}
90
91int32_t MockPointerControllerPolicyInterface::getDefaultPointerIconId() {
92 return CURSOR_TYPE_DEFAULT;
93}
94
95int32_t MockPointerControllerPolicyInterface::getCustomPointerIconId() {
96 return CURSOR_TYPE_CUSTOM;
97}
98
99void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* icon, int32_t type) {
100 icon->style = type;
101 std::pair<float, float> hotSpot = getHotSpotCoordinatesForType(type);
102 icon->hotSpotX = hotSpot.first;
103 icon->hotSpotY = hotSpot.second;
104}
105
106class PointerControllerTest : public Test {
107protected:
108 PointerControllerTest();
109 ~PointerControllerTest();
110
111 sp<MockSprite> mPointerSprite;
112 sp<MockPointerControllerPolicyInterface> mPolicy;
113 sp<MockSpriteController> mSpriteController;
114 sp<PointerController> mPointerController;
115
116private:
117 void loopThread();
118
119 std::atomic<bool> mRunning = true;
120 class MyLooper : public Looper {
121 public:
122 MyLooper() : Looper(false) {}
123 ~MyLooper() = default;
124 };
125 sp<MyLooper> mLooper;
126 std::thread mThread;
127};
128
129PointerControllerTest::PointerControllerTest() : mPointerSprite(new NiceMock<MockSprite>),
130 mLooper(new MyLooper), mThread(&PointerControllerTest::loopThread, this) {
131
132 mSpriteController = new NiceMock<MockSpriteController>(mLooper);
133 mPolicy = new MockPointerControllerPolicyInterface();
134
135 EXPECT_CALL(*mSpriteController, createSprite())
136 .WillOnce(Return(mPointerSprite));
137
138 mPointerController = new PointerController(mPolicy, mLooper, mSpriteController);
139
140 DisplayViewport viewport;
141 viewport.displayId = ADISPLAY_ID_DEFAULT;
142 viewport.logicalRight = 1600;
143 viewport.logicalBottom = 1200;
144 viewport.physicalRight = 800;
145 viewport.physicalBottom = 600;
146 viewport.deviceWidth = 400;
147 viewport.deviceHeight = 300;
148 mPointerController->setDisplayViewport(viewport);
149}
150
151PointerControllerTest::~PointerControllerTest() {
152 mRunning.store(false, std::memory_order_relaxed);
153 mThread.join();
154}
155
156void PointerControllerTest::loopThread() {
157 Looper::setForThread(mLooper);
158
159 while (mRunning.load(std::memory_order_relaxed)) {
160 mLooper->pollOnce(100);
161 }
162}
163
164TEST_F(PointerControllerTest, useDefaultCursorTypeByDefault) {
165 mPointerController->unfade(PointerController::TRANSITION_IMMEDIATE);
166
167 std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_DEFAULT);
168 EXPECT_CALL(*mPointerSprite, setVisible(true));
169 EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
170 EXPECT_CALL(*mPointerSprite, setIcon(
171 AllOf(
172 Field(&SpriteIcon::style, CURSOR_TYPE_DEFAULT),
173 Field(&SpriteIcon::hotSpotX, hotspot.first),
174 Field(&SpriteIcon::hotSpotY, hotspot.second))));
175 mPointerController->reloadPointerResources();
176}
177
178TEST_F(PointerControllerTest, updatePointerIcon) {
179 mPointerController->unfade(PointerController::TRANSITION_IMMEDIATE);
180
181 int32_t type = CURSOR_TYPE_ADDITIONAL_1;
182 std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type);
183 EXPECT_CALL(*mPointerSprite, setVisible(true));
184 EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
185 EXPECT_CALL(*mPointerSprite, setIcon(
186 AllOf(
187 Field(&SpriteIcon::style, type),
188 Field(&SpriteIcon::hotSpotX, hotspot.first),
189 Field(&SpriteIcon::hotSpotY, hotspot.second))));
190 mPointerController->updatePointerIcon(type);
191}
192
193TEST_F(PointerControllerTest, setCustomPointerIcon) {
194 mPointerController->unfade(PointerController::TRANSITION_IMMEDIATE);
195
196 int32_t style = CURSOR_TYPE_CUSTOM;
197 float hotSpotX = 15;
198 float hotSpotY = 20;
199
200 SpriteIcon icon;
201 icon.style = style;
202 icon.hotSpotX = hotSpotX;
203 icon.hotSpotY = hotSpotY;
204
205 EXPECT_CALL(*mPointerSprite, setVisible(true));
206 EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
207 EXPECT_CALL(*mPointerSprite, setIcon(
208 AllOf(
209 Field(&SpriteIcon::style, style),
210 Field(&SpriteIcon::hotSpotX, hotSpotX),
211 Field(&SpriteIcon::hotSpotY, hotSpotY))));
212 mPointerController->setCustomPointerIcon(icon);
213}
214
215} // namespace android