Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace android { |
| 29 | |
| 30 | enum TestCursorType { |
| 31 | CURSOR_TYPE_DEFAULT = 0, |
| 32 | CURSOR_TYPE_HOVER, |
| 33 | CURSOR_TYPE_TOUCH, |
| 34 | CURSOR_TYPE_ANCHOR, |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 35 | CURSOR_TYPE_ADDITIONAL, |
| 36 | CURSOR_TYPE_ADDITIONAL_ANIM, |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 37 | CURSOR_TYPE_CUSTOM = -1, |
| 38 | }; |
| 39 | |
| 40 | using ::testing::AllOf; |
| 41 | using ::testing::Field; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 42 | using ::testing::Mock; |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 43 | using ::testing::NiceMock; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 44 | using ::testing::Return; |
| 45 | using ::testing::Test; |
| 46 | |
| 47 | std::pair<float, float> getHotSpotCoordinatesForType(int32_t type) { |
| 48 | return std::make_pair(type * 10, type * 10 + 5); |
| 49 | } |
| 50 | |
| 51 | class MockPointerControllerPolicyInterface : public PointerControllerPolicyInterface { |
| 52 | public: |
| 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; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 59 | virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) override; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 60 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 61 | bool allResourcesAreLoaded(); |
| 62 | bool noResourcesAreLoaded(); |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 63 | std::optional<int32_t> getLastReportedPointerDisplayId() { return latestPointerDisplayId; } |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 64 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 65 | private: |
| 66 | void loadPointerIconForType(SpriteIcon* icon, int32_t cursorType); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 67 | |
| 68 | bool pointerIconLoaded{false}; |
| 69 | bool pointerResourcesLoaded{false}; |
| 70 | bool additionalMouseResourcesLoaded{false}; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 71 | std::optional<int32_t /*displayId*/> latestPointerDisplayId; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | void MockPointerControllerPolicyInterface::loadPointerIcon(SpriteIcon* icon, int32_t) { |
| 75 | loadPointerIconForType(icon, CURSOR_TYPE_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 76 | pointerIconLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void MockPointerControllerPolicyInterface::loadPointerResources(PointerResources* outResources, |
| 80 | int32_t) { |
| 81 | loadPointerIconForType(&outResources->spotHover, CURSOR_TYPE_HOVER); |
| 82 | loadPointerIconForType(&outResources->spotTouch, CURSOR_TYPE_TOUCH); |
| 83 | loadPointerIconForType(&outResources->spotAnchor, CURSOR_TYPE_ANCHOR); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 84 | pointerResourcesLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void MockPointerControllerPolicyInterface::loadAdditionalMouseResources( |
| 88 | std::map<int32_t, SpriteIcon>* outResources, |
| 89 | std::map<int32_t, PointerAnimation>* outAnimationResources, |
| 90 | int32_t) { |
| 91 | SpriteIcon icon; |
| 92 | PointerAnimation anim; |
| 93 | |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 94 | // CURSOR_TYPE_ADDITIONAL doesn't have animation resource. |
| 95 | int32_t cursorType = CURSOR_TYPE_ADDITIONAL; |
| 96 | loadPointerIconForType(&icon, cursorType); |
| 97 | (*outResources)[cursorType] = icon; |
| 98 | |
| 99 | // CURSOR_TYPE_ADDITIONAL_ANIM has animation resource. |
| 100 | cursorType = CURSOR_TYPE_ADDITIONAL_ANIM; |
| 101 | loadPointerIconForType(&icon, cursorType); |
| 102 | anim.animationFrames.push_back(icon); |
| 103 | anim.durationPerFrame = 10; |
| 104 | (*outResources)[cursorType] = icon; |
| 105 | (*outAnimationResources)[cursorType] = anim; |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 106 | |
| 107 | additionalMouseResourcesLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int32_t MockPointerControllerPolicyInterface::getDefaultPointerIconId() { |
| 111 | return CURSOR_TYPE_DEFAULT; |
| 112 | } |
| 113 | |
| 114 | int32_t MockPointerControllerPolicyInterface::getCustomPointerIconId() { |
| 115 | return CURSOR_TYPE_CUSTOM; |
| 116 | } |
| 117 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 118 | bool MockPointerControllerPolicyInterface::allResourcesAreLoaded() { |
| 119 | return pointerIconLoaded && pointerResourcesLoaded && additionalMouseResourcesLoaded; |
| 120 | } |
| 121 | |
| 122 | bool MockPointerControllerPolicyInterface::noResourcesAreLoaded() { |
| 123 | return !(pointerIconLoaded || pointerResourcesLoaded || additionalMouseResourcesLoaded); |
| 124 | } |
| 125 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 126 | void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* icon, int32_t type) { |
| 127 | icon->style = type; |
| 128 | std::pair<float, float> hotSpot = getHotSpotCoordinatesForType(type); |
| 129 | icon->hotSpotX = hotSpot.first; |
| 130 | icon->hotSpotY = hotSpot.second; |
| 131 | } |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 132 | |
| 133 | void MockPointerControllerPolicyInterface::onPointerDisplayIdChanged(int32_t displayId, |
| 134 | float /*xPos*/, |
| 135 | float /*yPos*/) { |
| 136 | latestPointerDisplayId = displayId; |
| 137 | } |
| 138 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 139 | class PointerControllerTest : public Test { |
| 140 | protected: |
| 141 | PointerControllerTest(); |
| 142 | ~PointerControllerTest(); |
| 143 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 144 | void ensureDisplayViewportIsSet(int32_t displayId = ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 145 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 146 | sp<MockSprite> mPointerSprite; |
| 147 | sp<MockPointerControllerPolicyInterface> mPolicy; |
| 148 | sp<MockSpriteController> mSpriteController; |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 149 | std::shared_ptr<PointerController> mPointerController; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 150 | |
| 151 | private: |
| 152 | void loopThread(); |
| 153 | |
| 154 | std::atomic<bool> mRunning = true; |
| 155 | class MyLooper : public Looper { |
| 156 | public: |
| 157 | MyLooper() : Looper(false) {} |
| 158 | ~MyLooper() = default; |
| 159 | }; |
| 160 | sp<MyLooper> mLooper; |
| 161 | std::thread mThread; |
| 162 | }; |
| 163 | |
| 164 | PointerControllerTest::PointerControllerTest() : mPointerSprite(new NiceMock<MockSprite>), |
| 165 | mLooper(new MyLooper), mThread(&PointerControllerTest::loopThread, this) { |
| 166 | |
| 167 | mSpriteController = new NiceMock<MockSpriteController>(mLooper); |
| 168 | mPolicy = new MockPointerControllerPolicyInterface(); |
| 169 | |
| 170 | EXPECT_CALL(*mSpriteController, createSprite()) |
| 171 | .WillOnce(Return(mPointerSprite)); |
| 172 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 173 | mPointerController = PointerController::create(mPolicy, mLooper, mSpriteController); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 174 | } |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 175 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 176 | PointerControllerTest::~PointerControllerTest() { |
| 177 | mRunning.store(false, std::memory_order_relaxed); |
| 178 | mThread.join(); |
| 179 | } |
| 180 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 181 | void PointerControllerTest::ensureDisplayViewportIsSet(int32_t displayId) { |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 182 | DisplayViewport viewport; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 183 | viewport.displayId = displayId; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 184 | viewport.logicalRight = 1600; |
| 185 | viewport.logicalBottom = 1200; |
| 186 | viewport.physicalRight = 800; |
| 187 | viewport.physicalBottom = 600; |
| 188 | viewport.deviceWidth = 400; |
| 189 | viewport.deviceHeight = 300; |
| 190 | mPointerController->setDisplayViewport(viewport); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void PointerControllerTest::loopThread() { |
| 194 | Looper::setForThread(mLooper); |
| 195 | |
| 196 | while (mRunning.load(std::memory_order_relaxed)) { |
| 197 | mLooper->pollOnce(100); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | TEST_F(PointerControllerTest, useDefaultCursorTypeByDefault) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 202 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 203 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 204 | |
| 205 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_DEFAULT); |
| 206 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 207 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 208 | EXPECT_CALL(*mPointerSprite, setIcon( |
| 209 | AllOf( |
| 210 | Field(&SpriteIcon::style, CURSOR_TYPE_DEFAULT), |
| 211 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 212 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 213 | mPointerController->reloadPointerResources(); |
| 214 | } |
| 215 | |
| 216 | TEST_F(PointerControllerTest, updatePointerIcon) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 217 | ensureDisplayViewportIsSet(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 218 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 219 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 220 | |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 221 | int32_t type = CURSOR_TYPE_ADDITIONAL; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 222 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type); |
| 223 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 224 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 225 | EXPECT_CALL(*mPointerSprite, setIcon( |
| 226 | AllOf( |
| 227 | Field(&SpriteIcon::style, type), |
| 228 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 229 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 230 | mPointerController->updatePointerIcon(type); |
| 231 | } |
| 232 | |
| 233 | TEST_F(PointerControllerTest, setCustomPointerIcon) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 234 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 235 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 236 | |
| 237 | int32_t style = CURSOR_TYPE_CUSTOM; |
| 238 | float hotSpotX = 15; |
| 239 | float hotSpotY = 20; |
| 240 | |
| 241 | SpriteIcon icon; |
| 242 | icon.style = style; |
| 243 | icon.hotSpotX = hotSpotX; |
| 244 | icon.hotSpotY = hotSpotY; |
| 245 | |
| 246 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 247 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 248 | EXPECT_CALL(*mPointerSprite, setIcon( |
| 249 | AllOf( |
| 250 | Field(&SpriteIcon::style, style), |
| 251 | Field(&SpriteIcon::hotSpotX, hotSpotX), |
| 252 | Field(&SpriteIcon::hotSpotY, hotSpotY)))); |
| 253 | mPointerController->setCustomPointerIcon(icon); |
| 254 | } |
| 255 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 256 | TEST_F(PointerControllerTest, doesNotGetResourcesBeforeSettingViewport) { |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 257 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 258 | mPointerController->setPosition(1.0f, 1.0f); |
| 259 | mPointerController->move(1.0f, 1.0f); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 260 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 261 | mPointerController->fade(PointerController::Transition::IMMEDIATE); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 262 | |
| 263 | EXPECT_TRUE(mPolicy->noResourcesAreLoaded()); |
| 264 | |
| 265 | ensureDisplayViewportIsSet(); |
| 266 | } |
| 267 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 268 | TEST_F(PointerControllerTest, notifiesPolicyWhenPointerDisplayChanges) { |
| 269 | EXPECT_FALSE(mPolicy->getLastReportedPointerDisplayId()) |
| 270 | << "A pointer display change does not occur when PointerController is created."; |
| 271 | |
| 272 | ensureDisplayViewportIsSet(ADISPLAY_ID_DEFAULT); |
| 273 | |
| 274 | const auto lastReportedPointerDisplayId = mPolicy->getLastReportedPointerDisplayId(); |
| 275 | ASSERT_TRUE(lastReportedPointerDisplayId) |
| 276 | << "The policy is notified of a pointer display change when the viewport is first set."; |
| 277 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, *lastReportedPointerDisplayId) |
| 278 | << "Incorrect pointer display notified."; |
| 279 | |
| 280 | ensureDisplayViewportIsSet(42); |
| 281 | |
| 282 | EXPECT_EQ(42, *mPolicy->getLastReportedPointerDisplayId()) |
| 283 | << "The policy is notified when the pointer display changes."; |
| 284 | |
| 285 | // Release the PointerController. |
| 286 | mPointerController = nullptr; |
| 287 | |
| 288 | EXPECT_EQ(ADISPLAY_ID_NONE, *mPolicy->getLastReportedPointerDisplayId()) |
| 289 | << "The pointer display changes to invalid when PointerController is destroyed."; |
| 290 | } |
| 291 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 292 | class PointerControllerWindowInfoListenerTest : public Test {}; |
| 293 | |
| 294 | class TestPointerController : public PointerController { |
| 295 | public: |
| 296 | TestPointerController(sp<android::gui::WindowInfosListener>& registeredListener, |
| 297 | const sp<Looper>& looper) |
| 298 | : PointerController( |
| 299 | new MockPointerControllerPolicyInterface(), looper, |
| 300 | new NiceMock<MockSpriteController>(looper), |
| 301 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) { |
| 302 | // Register listener |
| 303 | registeredListener = listener; |
| 304 | }, |
| 305 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) { |
| 306 | // Unregister listener |
| 307 | if (registeredListener == listener) registeredListener = nullptr; |
| 308 | }) {} |
| 309 | }; |
| 310 | |
| 311 | TEST_F(PointerControllerWindowInfoListenerTest, |
| 312 | doesNotCrashIfListenerCalledAfterPointerControllerDestroyed) { |
| 313 | sp<android::gui::WindowInfosListener> registeredListener; |
| 314 | sp<android::gui::WindowInfosListener> localListenerCopy; |
| 315 | { |
| 316 | TestPointerController pointerController(registeredListener, new Looper(false)); |
| 317 | ASSERT_NE(nullptr, registeredListener) << "WindowInfosListener was not registered"; |
| 318 | localListenerCopy = registeredListener; |
| 319 | } |
| 320 | EXPECT_EQ(nullptr, registeredListener) << "WindowInfosListener was not unregistered"; |
| 321 | localListenerCopy->onWindowInfosChanged({}, {}); |
| 322 | } |
| 323 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 324 | } // namespace android |