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