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, |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame^] | 163 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) |
| 164 | -> std::pair<std::vector<gui::WindowInfo>, |
| 165 | std::vector<gui::DisplayInfo>> { |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 166 | // Register listener |
| 167 | registeredListener = listener; |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame^] | 168 | return {}; |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 169 | }, |
| 170 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) { |
| 171 | // Unregister listener |
| 172 | if (registeredListener == listener) registeredListener = nullptr; |
| 173 | }) {} |
| 174 | ~TestPointerController() override {} |
| 175 | }; |
| 176 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 177 | class PointerControllerTest : public Test { |
| 178 | protected: |
| 179 | PointerControllerTest(); |
| 180 | ~PointerControllerTest(); |
| 181 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 182 | void ensureDisplayViewportIsSet(int32_t displayId = ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 183 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 184 | sp<MockSprite> mPointerSprite; |
| 185 | sp<MockPointerControllerPolicyInterface> mPolicy; |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 186 | std::unique_ptr<MockSpriteController> mSpriteController; |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 187 | std::shared_ptr<PointerController> mPointerController; |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 188 | sp<android::gui::WindowInfosListener> mRegisteredListener; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 189 | |
| 190 | private: |
| 191 | void loopThread(); |
| 192 | |
| 193 | std::atomic<bool> mRunning = true; |
| 194 | class MyLooper : public Looper { |
| 195 | public: |
| 196 | MyLooper() : Looper(false) {} |
| 197 | ~MyLooper() = default; |
| 198 | }; |
| 199 | sp<MyLooper> mLooper; |
| 200 | std::thread mThread; |
| 201 | }; |
| 202 | |
| 203 | PointerControllerTest::PointerControllerTest() : mPointerSprite(new NiceMock<MockSprite>), |
| 204 | mLooper(new MyLooper), mThread(&PointerControllerTest::loopThread, this) { |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 205 | mSpriteController.reset(new NiceMock<MockSpriteController>(mLooper)); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 206 | mPolicy = new MockPointerControllerPolicyInterface(); |
| 207 | |
| 208 | EXPECT_CALL(*mSpriteController, createSprite()) |
| 209 | .WillOnce(Return(mPointerSprite)); |
| 210 | |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 211 | mPointerController = std::make_unique<TestPointerController>(mRegisteredListener, mPolicy, |
| 212 | mLooper, *mSpriteController); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 213 | } |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 214 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 215 | PointerControllerTest::~PointerControllerTest() { |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 216 | mPointerController.reset(); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 217 | mRunning.store(false, std::memory_order_relaxed); |
| 218 | mThread.join(); |
| 219 | } |
| 220 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 221 | void PointerControllerTest::ensureDisplayViewportIsSet(int32_t displayId) { |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 222 | DisplayViewport viewport; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 223 | viewport.displayId = displayId; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 224 | viewport.logicalRight = 1600; |
| 225 | viewport.logicalBottom = 1200; |
| 226 | viewport.physicalRight = 800; |
| 227 | viewport.physicalBottom = 600; |
| 228 | viewport.deviceWidth = 400; |
| 229 | viewport.deviceHeight = 300; |
| 230 | mPointerController->setDisplayViewport(viewport); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void PointerControllerTest::loopThread() { |
| 234 | Looper::setForThread(mLooper); |
| 235 | |
| 236 | while (mRunning.load(std::memory_order_relaxed)) { |
| 237 | mLooper->pollOnce(100); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | TEST_F(PointerControllerTest, useDefaultCursorTypeByDefault) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 242 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 243 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 244 | |
| 245 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_DEFAULT); |
| 246 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 247 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 248 | EXPECT_CALL(*mPointerSprite, |
| 249 | setIcon(AllOf(Field(&SpriteIcon::style, |
| 250 | static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT)), |
| 251 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 252 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 253 | mPointerController->reloadPointerResources(); |
| 254 | } |
| 255 | |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 256 | TEST_F(PointerControllerTest, useStylusTypeForStylusHover) { |
| 257 | ensureDisplayViewportIsSet(); |
| 258 | mPointerController->setPresentation(PointerController::Presentation::STYLUS_HOVER); |
| 259 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 260 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_STYLUS); |
| 261 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 262 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 263 | EXPECT_CALL(*mPointerSprite, |
| 264 | setIcon(AllOf(Field(&SpriteIcon::style, |
| 265 | static_cast<PointerIconStyle>(CURSOR_TYPE_STYLUS)), |
| 266 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 267 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 268 | mPointerController->reloadPointerResources(); |
| 269 | } |
| 270 | |
Prabir Pradhan | 6c7aa6f | 2023-12-12 16:54:59 +0000 | [diff] [blame] | 271 | TEST_F_WITH_FLAGS(PointerControllerTest, setPresentationBeforeDisplayViewportDoesNotLoadResources, |
| 272 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(input_flags, enable_pointer_choreographer))) { |
| 273 | // Setting the presentation mode before a display viewport is set will not load any resources. |
| 274 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
| 275 | ASSERT_TRUE(mPolicy->noResourcesAreLoaded()); |
| 276 | |
| 277 | // When the display is set, then the resources are loaded. |
| 278 | ensureDisplayViewportIsSet(); |
| 279 | ASSERT_TRUE(mPolicy->allResourcesAreLoaded()); |
| 280 | } |
| 281 | |
| 282 | TEST_F_WITH_FLAGS(PointerControllerTest, updatePointerIcon, |
| 283 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(input_flags, |
| 284 | enable_pointer_choreographer))) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 285 | ensureDisplayViewportIsSet(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 286 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 287 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 288 | |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 289 | int32_t type = CURSOR_TYPE_ADDITIONAL; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 290 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type); |
| 291 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 292 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 293 | EXPECT_CALL(*mPointerSprite, |
| 294 | setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(type)), |
| 295 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 296 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 297 | mPointerController->updatePointerIcon(static_cast<PointerIconStyle>(type)); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Prabir Pradhan | 6c7aa6f | 2023-12-12 16:54:59 +0000 | [diff] [blame] | 300 | TEST_F_WITH_FLAGS(PointerControllerTest, updatePointerIconWithChoreographer, |
| 301 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(input_flags, enable_pointer_choreographer))) { |
| 302 | // When PointerChoreographer is enabled, the presentation mode is set before the viewport. |
| 303 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
| 304 | ensureDisplayViewportIsSet(); |
| 305 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 306 | |
| 307 | int32_t type = CURSOR_TYPE_ADDITIONAL; |
| 308 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type); |
| 309 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 310 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
| 311 | EXPECT_CALL(*mPointerSprite, |
| 312 | setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(type)), |
| 313 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 314 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 315 | mPointerController->updatePointerIcon(static_cast<PointerIconStyle>(type)); |
| 316 | } |
| 317 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 318 | TEST_F(PointerControllerTest, setCustomPointerIcon) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 319 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 320 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 321 | |
| 322 | int32_t style = CURSOR_TYPE_CUSTOM; |
| 323 | float hotSpotX = 15; |
| 324 | float hotSpotY = 20; |
| 325 | |
| 326 | SpriteIcon icon; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 327 | icon.style = static_cast<PointerIconStyle>(style); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 328 | icon.hotSpotX = hotSpotX; |
| 329 | icon.hotSpotY = hotSpotY; |
| 330 | |
| 331 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 332 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 333 | EXPECT_CALL(*mPointerSprite, |
| 334 | setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(style)), |
| 335 | Field(&SpriteIcon::hotSpotX, hotSpotX), |
| 336 | Field(&SpriteIcon::hotSpotY, hotSpotY)))); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 337 | mPointerController->setCustomPointerIcon(icon); |
| 338 | } |
| 339 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 340 | TEST_F(PointerControllerTest, doesNotGetResourcesBeforeSettingViewport) { |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 341 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 342 | mPointerController->setPosition(1.0f, 1.0f); |
| 343 | mPointerController->move(1.0f, 1.0f); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 344 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 345 | mPointerController->fade(PointerController::Transition::IMMEDIATE); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 346 | |
| 347 | EXPECT_TRUE(mPolicy->noResourcesAreLoaded()); |
| 348 | |
| 349 | ensureDisplayViewportIsSet(); |
| 350 | } |
| 351 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 352 | TEST_F(PointerControllerTest, notifiesPolicyWhenPointerDisplayChanges) { |
| 353 | EXPECT_FALSE(mPolicy->getLastReportedPointerDisplayId()) |
| 354 | << "A pointer display change does not occur when PointerController is created."; |
| 355 | |
| 356 | ensureDisplayViewportIsSet(ADISPLAY_ID_DEFAULT); |
| 357 | |
| 358 | const auto lastReportedPointerDisplayId = mPolicy->getLastReportedPointerDisplayId(); |
| 359 | ASSERT_TRUE(lastReportedPointerDisplayId) |
| 360 | << "The policy is notified of a pointer display change when the viewport is first set."; |
| 361 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, *lastReportedPointerDisplayId) |
| 362 | << "Incorrect pointer display notified."; |
| 363 | |
| 364 | ensureDisplayViewportIsSet(42); |
| 365 | |
| 366 | EXPECT_EQ(42, *mPolicy->getLastReportedPointerDisplayId()) |
| 367 | << "The policy is notified when the pointer display changes."; |
| 368 | |
| 369 | // Release the PointerController. |
| 370 | mPointerController = nullptr; |
| 371 | |
| 372 | EXPECT_EQ(ADISPLAY_ID_NONE, *mPolicy->getLastReportedPointerDisplayId()) |
| 373 | << "The pointer display changes to invalid when PointerController is destroyed."; |
| 374 | } |
| 375 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 376 | class PointerControllerWindowInfoListenerTest : public Test {}; |
| 377 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 378 | TEST_F(PointerControllerWindowInfoListenerTest, |
| 379 | doesNotCrashIfListenerCalledAfterPointerControllerDestroyed) { |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 380 | sp<Looper> looper = new Looper(false); |
| 381 | auto spriteController = NiceMock<MockSpriteController>(looper); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 382 | sp<android::gui::WindowInfosListener> registeredListener; |
| 383 | sp<android::gui::WindowInfosListener> localListenerCopy; |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 384 | sp<MockPointerControllerPolicyInterface> policy = new MockPointerControllerPolicyInterface(); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 385 | { |
Siarhei Vishniakou | a4ea300 | 2023-09-26 18:40:00 -0700 | [diff] [blame] | 386 | TestPointerController pointerController(registeredListener, policy, looper, |
| 387 | spriteController); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 388 | ASSERT_NE(nullptr, registeredListener) << "WindowInfosListener was not registered"; |
| 389 | localListenerCopy = registeredListener; |
| 390 | } |
| 391 | EXPECT_EQ(nullptr, registeredListener) << "WindowInfosListener was not unregistered"; |
Patrick Williams | 8e47a67 | 2023-05-01 11:30:37 -0500 | [diff] [blame] | 392 | localListenerCopy->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 395 | } // namespace android |