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