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 | |
Prabir Pradhan | 6c7aa6f | 2023-12-12 16:54:59 +0000 | [diff] [blame] | 17 | #include <flag_macros.h> |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 20 | #include <input/PointerController.h> |
| 21 | #include <input/SpriteController.h> |
| 22 | |
| 23 | #include <atomic> |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 24 | #include <thread> |
| 25 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 26 | #include "input/Input.h" |
| 27 | #include "mocks/MockSprite.h" |
| 28 | #include "mocks/MockSpriteController.h" |
| 29 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | enum TestCursorType { |
| 33 | CURSOR_TYPE_DEFAULT = 0, |
| 34 | CURSOR_TYPE_HOVER, |
| 35 | CURSOR_TYPE_TOUCH, |
| 36 | CURSOR_TYPE_ANCHOR, |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 37 | CURSOR_TYPE_ADDITIONAL, |
| 38 | CURSOR_TYPE_ADDITIONAL_ANIM, |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 39 | CURSOR_TYPE_STYLUS, |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 40 | CURSOR_TYPE_CUSTOM = -1, |
| 41 | }; |
| 42 | |
| 43 | using ::testing::AllOf; |
| 44 | using ::testing::Field; |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 45 | using ::testing::NiceMock; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 46 | using ::testing::Return; |
| 47 | using ::testing::Test; |
| 48 | |
| 49 | std::pair<float, float> getHotSpotCoordinatesForType(int32_t type) { |
| 50 | return std::make_pair(type * 10, type * 10 + 5); |
| 51 | } |
| 52 | |
| 53 | class MockPointerControllerPolicyInterface : public PointerControllerPolicyInterface { |
| 54 | public: |
| 55 | virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) override; |
| 56 | virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) override; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 57 | virtual void loadAdditionalMouseResources( |
| 58 | std::map<PointerIconStyle, SpriteIcon>* outResources, |
| 59 | std::map<PointerIconStyle, PointerAnimation>* outAnimationResources, |
| 60 | int32_t displayId) override; |
| 61 | virtual PointerIconStyle getDefaultPointerIconId() override; |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 62 | virtual PointerIconStyle getDefaultStylusIconId() override; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 63 | virtual PointerIconStyle getCustomPointerIconId() override; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 64 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 65 | bool allResourcesAreLoaded(); |
| 66 | bool noResourcesAreLoaded(); |
| 67 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 68 | private: |
| 69 | void loadPointerIconForType(SpriteIcon* icon, int32_t cursorType); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 70 | |
| 71 | bool pointerIconLoaded{false}; |
| 72 | bool pointerResourcesLoaded{false}; |
| 73 | bool additionalMouseResourcesLoaded{false}; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | void MockPointerControllerPolicyInterface::loadPointerIcon(SpriteIcon* icon, int32_t) { |
| 77 | loadPointerIconForType(icon, CURSOR_TYPE_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 78 | pointerIconLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void MockPointerControllerPolicyInterface::loadPointerResources(PointerResources* outResources, |
| 82 | int32_t) { |
| 83 | loadPointerIconForType(&outResources->spotHover, CURSOR_TYPE_HOVER); |
| 84 | loadPointerIconForType(&outResources->spotTouch, CURSOR_TYPE_TOUCH); |
| 85 | loadPointerIconForType(&outResources->spotAnchor, CURSOR_TYPE_ANCHOR); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 86 | pointerResourcesLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void MockPointerControllerPolicyInterface::loadAdditionalMouseResources( |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 90 | std::map<PointerIconStyle, SpriteIcon>* outResources, |
| 91 | std::map<PointerIconStyle, PointerAnimation>* outAnimationResources, int32_t) { |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 92 | SpriteIcon icon; |
| 93 | PointerAnimation anim; |
| 94 | |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 95 | // CURSOR_TYPE_ADDITIONAL doesn't have animation resource. |
| 96 | int32_t cursorType = CURSOR_TYPE_ADDITIONAL; |
| 97 | loadPointerIconForType(&icon, cursorType); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 98 | (*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon; |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 99 | |
| 100 | // CURSOR_TYPE_ADDITIONAL_ANIM has animation resource. |
| 101 | cursorType = CURSOR_TYPE_ADDITIONAL_ANIM; |
| 102 | loadPointerIconForType(&icon, cursorType); |
| 103 | anim.animationFrames.push_back(icon); |
| 104 | anim.durationPerFrame = 10; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 105 | (*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon; |
| 106 | (*outAnimationResources)[static_cast<PointerIconStyle>(cursorType)] = anim; |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 107 | |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 108 | // CURSOR_TYPE_STYLUS doesn't have animation resource. |
| 109 | cursorType = CURSOR_TYPE_STYLUS; |
| 110 | loadPointerIconForType(&icon, cursorType); |
| 111 | (*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon; |
| 112 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 113 | additionalMouseResourcesLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 116 | PointerIconStyle MockPointerControllerPolicyInterface::getDefaultPointerIconId() { |
| 117 | return static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 120 | PointerIconStyle MockPointerControllerPolicyInterface::getDefaultStylusIconId() { |
| 121 | return static_cast<PointerIconStyle>(CURSOR_TYPE_STYLUS); |
| 122 | } |
| 123 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 124 | PointerIconStyle MockPointerControllerPolicyInterface::getCustomPointerIconId() { |
| 125 | return static_cast<PointerIconStyle>(CURSOR_TYPE_CUSTOM); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 128 | bool MockPointerControllerPolicyInterface::allResourcesAreLoaded() { |
| 129 | return pointerIconLoaded && pointerResourcesLoaded && additionalMouseResourcesLoaded; |
| 130 | } |
| 131 | |
| 132 | bool MockPointerControllerPolicyInterface::noResourcesAreLoaded() { |
| 133 | return !(pointerIconLoaded || pointerResourcesLoaded || additionalMouseResourcesLoaded); |
| 134 | } |
| 135 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 136 | void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* icon, int32_t type) { |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 137 | icon->style = static_cast<PointerIconStyle>(type); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 138 | std::pair<float, float> hotSpot = getHotSpotCoordinatesForType(type); |
| 139 | icon->hotSpotX = hotSpot.first; |
| 140 | icon->hotSpotY = hotSpot.second; |
| 141 | } |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 142 | |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 143 | class TestPointerController : public PointerController { |
| 144 | public: |
| 145 | TestPointerController(sp<android::gui::WindowInfosListener>& registeredListener, |
| 146 | sp<PointerControllerPolicyInterface> policy, const sp<Looper>& looper, |
| 147 | SpriteController& spriteController) |
| 148 | : PointerController( |
| 149 | policy, looper, spriteController, |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame] | 150 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) |
Prabir Pradhan | bdf9369 | 2024-01-23 18:08:28 +0000 | [diff] [blame] | 151 | -> std::vector<gui::DisplayInfo> { |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 152 | // Register listener |
| 153 | registeredListener = listener; |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame] | 154 | return {}; |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 155 | }, |
| 156 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) { |
| 157 | // Unregister listener |
| 158 | if (registeredListener == listener) registeredListener = nullptr; |
| 159 | }) {} |
| 160 | ~TestPointerController() override {} |
| 161 | }; |
| 162 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 163 | class PointerControllerTest : public Test { |
| 164 | protected: |
| 165 | PointerControllerTest(); |
| 166 | ~PointerControllerTest(); |
| 167 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 168 | void ensureDisplayViewportIsSet(int32_t displayId = ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 169 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 170 | sp<MockSprite> mPointerSprite; |
| 171 | sp<MockPointerControllerPolicyInterface> mPolicy; |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 172 | std::unique_ptr<MockSpriteController> mSpriteController; |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 173 | std::shared_ptr<PointerController> mPointerController; |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 174 | sp<android::gui::WindowInfosListener> mRegisteredListener; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | void loopThread(); |
| 178 | |
| 179 | std::atomic<bool> mRunning = true; |
| 180 | class MyLooper : public Looper { |
| 181 | public: |
| 182 | MyLooper() : Looper(false) {} |
| 183 | ~MyLooper() = default; |
| 184 | }; |
| 185 | sp<MyLooper> mLooper; |
| 186 | std::thread mThread; |
| 187 | }; |
| 188 | |
| 189 | PointerControllerTest::PointerControllerTest() : mPointerSprite(new NiceMock<MockSprite>), |
| 190 | mLooper(new MyLooper), mThread(&PointerControllerTest::loopThread, this) { |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 191 | mSpriteController.reset(new NiceMock<MockSpriteController>(mLooper)); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 192 | mPolicy = new MockPointerControllerPolicyInterface(); |
| 193 | |
| 194 | EXPECT_CALL(*mSpriteController, createSprite()) |
| 195 | .WillOnce(Return(mPointerSprite)); |
| 196 | |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 197 | mPointerController = std::make_unique<TestPointerController>(mRegisteredListener, mPolicy, |
| 198 | mLooper, *mSpriteController); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 199 | } |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 200 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 201 | PointerControllerTest::~PointerControllerTest() { |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 202 | mPointerController.reset(); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 203 | mRunning.store(false, std::memory_order_relaxed); |
| 204 | mThread.join(); |
| 205 | } |
| 206 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 207 | void PointerControllerTest::ensureDisplayViewportIsSet(int32_t displayId) { |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 208 | DisplayViewport viewport; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 209 | viewport.displayId = displayId; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 210 | viewport.logicalRight = 1600; |
| 211 | viewport.logicalBottom = 1200; |
| 212 | viewport.physicalRight = 800; |
| 213 | viewport.physicalBottom = 600; |
| 214 | viewport.deviceWidth = 400; |
| 215 | viewport.deviceHeight = 300; |
| 216 | mPointerController->setDisplayViewport(viewport); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void PointerControllerTest::loopThread() { |
| 220 | Looper::setForThread(mLooper); |
| 221 | |
| 222 | while (mRunning.load(std::memory_order_relaxed)) { |
| 223 | mLooper->pollOnce(100); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | TEST_F(PointerControllerTest, useDefaultCursorTypeByDefault) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 228 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 229 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 230 | |
| 231 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_DEFAULT); |
| 232 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 233 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 234 | EXPECT_CALL(*mPointerSprite, |
| 235 | setIcon(AllOf(Field(&SpriteIcon::style, |
| 236 | static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT)), |
| 237 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 238 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 239 | mPointerController->reloadPointerResources(); |
| 240 | } |
| 241 | |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 242 | TEST_F(PointerControllerTest, useStylusTypeForStylusHover) { |
| 243 | ensureDisplayViewportIsSet(); |
| 244 | mPointerController->setPresentation(PointerController::Presentation::STYLUS_HOVER); |
| 245 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 246 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_STYLUS); |
| 247 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 248 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 249 | EXPECT_CALL(*mPointerSprite, |
| 250 | setIcon(AllOf(Field(&SpriteIcon::style, |
| 251 | static_cast<PointerIconStyle>(CURSOR_TYPE_STYLUS)), |
| 252 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 253 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 254 | mPointerController->reloadPointerResources(); |
| 255 | } |
| 256 | |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 257 | TEST_F(PointerControllerTest, setPresentationBeforeDisplayViewportDoesNotLoadResources) { |
Prabir Pradhan | 6c7aa6f | 2023-12-12 16:54:59 +0000 | [diff] [blame] | 258 | // Setting the presentation mode before a display viewport is set will not load any resources. |
| 259 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
| 260 | ASSERT_TRUE(mPolicy->noResourcesAreLoaded()); |
| 261 | |
| 262 | // When the display is set, then the resources are loaded. |
| 263 | ensureDisplayViewportIsSet(); |
| 264 | ASSERT_TRUE(mPolicy->allResourcesAreLoaded()); |
| 265 | } |
| 266 | |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 267 | TEST_F(PointerControllerTest, updatePointerIconWithChoreographer) { |
Prabir Pradhan | 6c7aa6f | 2023-12-12 16:54:59 +0000 | [diff] [blame] | 268 | // When PointerChoreographer is enabled, the presentation mode is set before the viewport. |
| 269 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
| 270 | ensureDisplayViewportIsSet(); |
| 271 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 272 | |
| 273 | int32_t type = CURSOR_TYPE_ADDITIONAL; |
| 274 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type); |
| 275 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 276 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 277 | EXPECT_CALL(*mPointerSprite, |
| 278 | setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(type)), |
| 279 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 280 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 281 | mPointerController->updatePointerIcon(static_cast<PointerIconStyle>(type)); |
| 282 | } |
| 283 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 284 | TEST_F(PointerControllerTest, setCustomPointerIcon) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 285 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 286 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 287 | |
| 288 | int32_t style = CURSOR_TYPE_CUSTOM; |
| 289 | float hotSpotX = 15; |
| 290 | float hotSpotY = 20; |
| 291 | |
| 292 | SpriteIcon icon; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 293 | icon.style = static_cast<PointerIconStyle>(style); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 294 | icon.hotSpotX = hotSpotX; |
| 295 | icon.hotSpotY = hotSpotY; |
| 296 | |
| 297 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 298 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 299 | EXPECT_CALL(*mPointerSprite, |
| 300 | setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(style)), |
| 301 | Field(&SpriteIcon::hotSpotX, hotSpotX), |
| 302 | Field(&SpriteIcon::hotSpotY, hotSpotY)))); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 303 | mPointerController->setCustomPointerIcon(icon); |
| 304 | } |
| 305 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 306 | TEST_F(PointerControllerTest, doesNotGetResourcesBeforeSettingViewport) { |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 307 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 308 | mPointerController->setPosition(1.0f, 1.0f); |
| 309 | mPointerController->move(1.0f, 1.0f); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 310 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 311 | mPointerController->fade(PointerController::Transition::IMMEDIATE); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 312 | |
| 313 | EXPECT_TRUE(mPolicy->noResourcesAreLoaded()); |
| 314 | |
| 315 | ensureDisplayViewportIsSet(); |
| 316 | } |
| 317 | |
Arpit Singh | 80fd68a | 2024-03-26 18:41:06 +0000 | [diff] [blame] | 318 | TEST_F(PointerControllerTest, updatesSkipScreenshotFlagForTouchSpots) { |
| 319 | ensureDisplayViewportIsSet(); |
| 320 | |
| 321 | PointerCoords testSpotCoords; |
| 322 | testSpotCoords.clear(); |
| 323 | testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_X, 1); |
| 324 | testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1); |
| 325 | BitSet32 testIdBits; |
| 326 | testIdBits.markBit(0); |
| 327 | std::array<uint32_t, MAX_POINTER_ID + 1> testIdToIndex; |
| 328 | |
| 329 | sp<MockSprite> testSpotSprite(new NiceMock<MockSprite>); |
| 330 | |
| 331 | // By default sprite is not marked secure |
| 332 | EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testSpotSprite)); |
| 333 | EXPECT_CALL(*testSpotSprite, setSkipScreenshot).With(testing::Args<0>(false)); |
| 334 | |
| 335 | // Update spots to sync state with sprite |
| 336 | mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits, |
| 337 | ADISPLAY_ID_DEFAULT); |
| 338 | testing::Mock::VerifyAndClearExpectations(testSpotSprite.get()); |
| 339 | |
| 340 | // Marking the display to skip screenshot should update sprite as well |
| 341 | mPointerController->setSkipScreenshot(ADISPLAY_ID_DEFAULT, true); |
| 342 | EXPECT_CALL(*testSpotSprite, setSkipScreenshot).With(testing::Args<0>(true)); |
| 343 | |
| 344 | // Update spots to sync state with sprite |
| 345 | mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits, |
| 346 | ADISPLAY_ID_DEFAULT); |
| 347 | testing::Mock::VerifyAndClearExpectations(testSpotSprite.get()); |
| 348 | |
| 349 | // Reset flag and verify again |
| 350 | mPointerController->setSkipScreenshot(ADISPLAY_ID_DEFAULT, false); |
| 351 | EXPECT_CALL(*testSpotSprite, setSkipScreenshot).With(testing::Args<0>(false)); |
| 352 | mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits, |
| 353 | ADISPLAY_ID_DEFAULT); |
| 354 | testing::Mock::VerifyAndClearExpectations(testSpotSprite.get()); |
| 355 | } |
| 356 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 357 | class PointerControllerWindowInfoListenerTest : public Test {}; |
| 358 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 359 | TEST_F(PointerControllerWindowInfoListenerTest, |
| 360 | doesNotCrashIfListenerCalledAfterPointerControllerDestroyed) { |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 361 | sp<Looper> looper = new Looper(false); |
| 362 | auto spriteController = NiceMock<MockSpriteController>(looper); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 363 | sp<android::gui::WindowInfosListener> registeredListener; |
| 364 | sp<android::gui::WindowInfosListener> localListenerCopy; |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 365 | sp<MockPointerControllerPolicyInterface> policy = new MockPointerControllerPolicyInterface(); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 366 | { |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 367 | TestPointerController pointerController(registeredListener, policy, looper, |
| 368 | spriteController); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 369 | ASSERT_NE(nullptr, registeredListener) << "WindowInfosListener was not registered"; |
| 370 | localListenerCopy = registeredListener; |
| 371 | } |
| 372 | EXPECT_EQ(nullptr, registeredListener) << "WindowInfosListener was not unregistered"; |
Patrick Williams | 8e47a67 | 2023-05-01 11:30:37 -0500 | [diff] [blame] | 373 | localListenerCopy->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 376 | } // namespace android |