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