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 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 19 | #include <input/PointerController.h> |
| 20 | #include <input/SpriteController.h> |
| 21 | |
| 22 | #include <atomic> |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 23 | #include <thread> |
| 24 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 25 | #include "input/Input.h" |
| 26 | #include "mocks/MockSprite.h" |
| 27 | #include "mocks/MockSpriteController.h" |
| 28 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | enum TestCursorType { |
| 32 | CURSOR_TYPE_DEFAULT = 0, |
| 33 | CURSOR_TYPE_HOVER, |
| 34 | CURSOR_TYPE_TOUCH, |
| 35 | CURSOR_TYPE_ANCHOR, |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 36 | CURSOR_TYPE_ADDITIONAL, |
| 37 | CURSOR_TYPE_ADDITIONAL_ANIM, |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 38 | CURSOR_TYPE_CUSTOM = -1, |
| 39 | }; |
| 40 | |
| 41 | using ::testing::AllOf; |
| 42 | using ::testing::Field; |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 43 | using ::testing::NiceMock; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 44 | using ::testing::Return; |
| 45 | using ::testing::Test; |
| 46 | |
| 47 | std::pair<float, float> getHotSpotCoordinatesForType(int32_t type) { |
| 48 | return std::make_pair(type * 10, type * 10 + 5); |
| 49 | } |
| 50 | |
| 51 | class MockPointerControllerPolicyInterface : public PointerControllerPolicyInterface { |
| 52 | public: |
| 53 | virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) override; |
| 54 | virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) override; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 55 | virtual void loadAdditionalMouseResources( |
| 56 | std::map<PointerIconStyle, SpriteIcon>* outResources, |
| 57 | std::map<PointerIconStyle, PointerAnimation>* outAnimationResources, |
| 58 | int32_t displayId) override; |
| 59 | virtual PointerIconStyle getDefaultPointerIconId() override; |
| 60 | virtual PointerIconStyle getCustomPointerIconId() override; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 61 | virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) override; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 62 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 63 | bool allResourcesAreLoaded(); |
| 64 | bool noResourcesAreLoaded(); |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 65 | std::optional<int32_t> getLastReportedPointerDisplayId() { return latestPointerDisplayId; } |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 66 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 67 | private: |
| 68 | void loadPointerIconForType(SpriteIcon* icon, int32_t cursorType); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 69 | |
| 70 | bool pointerIconLoaded{false}; |
| 71 | bool pointerResourcesLoaded{false}; |
| 72 | bool additionalMouseResourcesLoaded{false}; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 73 | std::optional<int32_t /*displayId*/> latestPointerDisplayId; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | void MockPointerControllerPolicyInterface::loadPointerIcon(SpriteIcon* icon, int32_t) { |
| 77 | loadPointerIconForType(icon, CURSOR_TYPE_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 78 | pointerIconLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void MockPointerControllerPolicyInterface::loadPointerResources(PointerResources* outResources, |
| 82 | int32_t) { |
| 83 | loadPointerIconForType(&outResources->spotHover, CURSOR_TYPE_HOVER); |
| 84 | loadPointerIconForType(&outResources->spotTouch, CURSOR_TYPE_TOUCH); |
| 85 | loadPointerIconForType(&outResources->spotAnchor, CURSOR_TYPE_ANCHOR); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 86 | pointerResourcesLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void MockPointerControllerPolicyInterface::loadAdditionalMouseResources( |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 90 | std::map<PointerIconStyle, SpriteIcon>* outResources, |
| 91 | std::map<PointerIconStyle, PointerAnimation>* outAnimationResources, int32_t) { |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 92 | SpriteIcon icon; |
| 93 | PointerAnimation anim; |
| 94 | |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 95 | // CURSOR_TYPE_ADDITIONAL doesn't have animation resource. |
| 96 | int32_t cursorType = CURSOR_TYPE_ADDITIONAL; |
| 97 | loadPointerIconForType(&icon, cursorType); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 98 | (*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon; |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 99 | |
| 100 | // CURSOR_TYPE_ADDITIONAL_ANIM has animation resource. |
| 101 | cursorType = CURSOR_TYPE_ADDITIONAL_ANIM; |
| 102 | loadPointerIconForType(&icon, cursorType); |
| 103 | anim.animationFrames.push_back(icon); |
| 104 | anim.durationPerFrame = 10; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 105 | (*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon; |
| 106 | (*outAnimationResources)[static_cast<PointerIconStyle>(cursorType)] = anim; |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 107 | |
| 108 | additionalMouseResourcesLoaded = true; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 111 | PointerIconStyle MockPointerControllerPolicyInterface::getDefaultPointerIconId() { |
| 112 | return static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 115 | PointerIconStyle MockPointerControllerPolicyInterface::getCustomPointerIconId() { |
| 116 | return static_cast<PointerIconStyle>(CURSOR_TYPE_CUSTOM); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 119 | bool MockPointerControllerPolicyInterface::allResourcesAreLoaded() { |
| 120 | return pointerIconLoaded && pointerResourcesLoaded && additionalMouseResourcesLoaded; |
| 121 | } |
| 122 | |
| 123 | bool MockPointerControllerPolicyInterface::noResourcesAreLoaded() { |
| 124 | return !(pointerIconLoaded || pointerResourcesLoaded || additionalMouseResourcesLoaded); |
| 125 | } |
| 126 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 127 | void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* icon, int32_t type) { |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 128 | icon->style = static_cast<PointerIconStyle>(type); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 129 | std::pair<float, float> hotSpot = getHotSpotCoordinatesForType(type); |
| 130 | icon->hotSpotX = hotSpot.first; |
| 131 | icon->hotSpotY = hotSpot.second; |
| 132 | } |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 133 | |
| 134 | void MockPointerControllerPolicyInterface::onPointerDisplayIdChanged(int32_t displayId, |
| 135 | float /*xPos*/, |
| 136 | float /*yPos*/) { |
| 137 | latestPointerDisplayId = displayId; |
| 138 | } |
| 139 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 140 | class PointerControllerTest : public Test { |
| 141 | protected: |
| 142 | PointerControllerTest(); |
| 143 | ~PointerControllerTest(); |
| 144 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 145 | void ensureDisplayViewportIsSet(int32_t displayId = ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 146 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 147 | sp<MockSprite> mPointerSprite; |
| 148 | sp<MockPointerControllerPolicyInterface> mPolicy; |
| 149 | sp<MockSpriteController> mSpriteController; |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 150 | std::shared_ptr<PointerController> mPointerController; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | void loopThread(); |
| 154 | |
| 155 | std::atomic<bool> mRunning = true; |
| 156 | class MyLooper : public Looper { |
| 157 | public: |
| 158 | MyLooper() : Looper(false) {} |
| 159 | ~MyLooper() = default; |
| 160 | }; |
| 161 | sp<MyLooper> mLooper; |
| 162 | std::thread mThread; |
| 163 | }; |
| 164 | |
| 165 | PointerControllerTest::PointerControllerTest() : mPointerSprite(new NiceMock<MockSprite>), |
| 166 | mLooper(new MyLooper), mThread(&PointerControllerTest::loopThread, this) { |
| 167 | |
| 168 | mSpriteController = new NiceMock<MockSpriteController>(mLooper); |
| 169 | mPolicy = new MockPointerControllerPolicyInterface(); |
| 170 | |
| 171 | EXPECT_CALL(*mSpriteController, createSprite()) |
| 172 | .WillOnce(Return(mPointerSprite)); |
| 173 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 174 | mPointerController = PointerController::create(mPolicy, mLooper, mSpriteController); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 175 | } |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 176 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 177 | PointerControllerTest::~PointerControllerTest() { |
| 178 | mRunning.store(false, std::memory_order_relaxed); |
| 179 | mThread.join(); |
| 180 | } |
| 181 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 182 | void PointerControllerTest::ensureDisplayViewportIsSet(int32_t displayId) { |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 183 | DisplayViewport viewport; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 184 | viewport.displayId = displayId; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 185 | viewport.logicalRight = 1600; |
| 186 | viewport.logicalBottom = 1200; |
| 187 | viewport.physicalRight = 800; |
| 188 | viewport.physicalBottom = 600; |
| 189 | viewport.deviceWidth = 400; |
| 190 | viewport.deviceHeight = 300; |
| 191 | mPointerController->setDisplayViewport(viewport); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void PointerControllerTest::loopThread() { |
| 195 | Looper::setForThread(mLooper); |
| 196 | |
| 197 | while (mRunning.load(std::memory_order_relaxed)) { |
| 198 | mLooper->pollOnce(100); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | TEST_F(PointerControllerTest, useDefaultCursorTypeByDefault) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 203 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 204 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 205 | |
| 206 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_DEFAULT); |
| 207 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 208 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 209 | EXPECT_CALL(*mPointerSprite, |
| 210 | setIcon(AllOf(Field(&SpriteIcon::style, |
| 211 | static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT)), |
| 212 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 213 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 214 | mPointerController->reloadPointerResources(); |
| 215 | } |
| 216 | |
| 217 | TEST_F(PointerControllerTest, updatePointerIcon) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 218 | ensureDisplayViewportIsSet(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 219 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 220 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 221 | |
Garfield Tan | e9c6151 | 2019-11-21 16:42:13 -0800 | [diff] [blame] | 222 | int32_t type = CURSOR_TYPE_ADDITIONAL; |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 223 | std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type); |
| 224 | EXPECT_CALL(*mPointerSprite, setVisible(true)); |
| 225 | EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)); |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 226 | EXPECT_CALL(*mPointerSprite, |
| 227 | setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(type)), |
| 228 | Field(&SpriteIcon::hotSpotX, hotspot.first), |
| 229 | Field(&SpriteIcon::hotSpotY, hotspot.second)))); |
| 230 | mPointerController->updatePointerIcon(static_cast<PointerIconStyle>(type)); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST_F(PointerControllerTest, setCustomPointerIcon) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 234 | ensureDisplayViewportIsSet(); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 235 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 236 | |
| 237 | int32_t style = CURSOR_TYPE_CUSTOM; |
| 238 | float hotSpotX = 15; |
| 239 | float hotSpotY = 20; |
| 240 | |
| 241 | SpriteIcon icon; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame^] | 242 | icon.style = static_cast<PointerIconStyle>(style); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 243 | icon.hotSpotX = hotSpotX; |
| 244 | icon.hotSpotY = hotSpotY; |
| 245 | |
| 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, static_cast<PointerIconStyle>(style)), |
| 250 | Field(&SpriteIcon::hotSpotX, hotSpotX), |
| 251 | Field(&SpriteIcon::hotSpotY, hotSpotY)))); |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 252 | mPointerController->setCustomPointerIcon(icon); |
| 253 | } |
| 254 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 255 | TEST_F(PointerControllerTest, doesNotGetResourcesBeforeSettingViewport) { |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 256 | mPointerController->setPresentation(PointerController::Presentation::POINTER); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 257 | mPointerController->setPosition(1.0f, 1.0f); |
| 258 | mPointerController->move(1.0f, 1.0f); |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 259 | mPointerController->unfade(PointerController::Transition::IMMEDIATE); |
| 260 | mPointerController->fade(PointerController::Transition::IMMEDIATE); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 261 | |
| 262 | EXPECT_TRUE(mPolicy->noResourcesAreLoaded()); |
| 263 | |
| 264 | ensureDisplayViewportIsSet(); |
| 265 | } |
| 266 | |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 267 | TEST_F(PointerControllerTest, notifiesPolicyWhenPointerDisplayChanges) { |
| 268 | EXPECT_FALSE(mPolicy->getLastReportedPointerDisplayId()) |
| 269 | << "A pointer display change does not occur when PointerController is created."; |
| 270 | |
| 271 | ensureDisplayViewportIsSet(ADISPLAY_ID_DEFAULT); |
| 272 | |
| 273 | const auto lastReportedPointerDisplayId = mPolicy->getLastReportedPointerDisplayId(); |
| 274 | ASSERT_TRUE(lastReportedPointerDisplayId) |
| 275 | << "The policy is notified of a pointer display change when the viewport is first set."; |
| 276 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, *lastReportedPointerDisplayId) |
| 277 | << "Incorrect pointer display notified."; |
| 278 | |
| 279 | ensureDisplayViewportIsSet(42); |
| 280 | |
| 281 | EXPECT_EQ(42, *mPolicy->getLastReportedPointerDisplayId()) |
| 282 | << "The policy is notified when the pointer display changes."; |
| 283 | |
| 284 | // Release the PointerController. |
| 285 | mPointerController = nullptr; |
| 286 | |
| 287 | EXPECT_EQ(ADISPLAY_ID_NONE, *mPolicy->getLastReportedPointerDisplayId()) |
| 288 | << "The pointer display changes to invalid when PointerController is destroyed."; |
| 289 | } |
| 290 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 291 | class PointerControllerWindowInfoListenerTest : public Test {}; |
| 292 | |
| 293 | class TestPointerController : public PointerController { |
| 294 | public: |
| 295 | TestPointerController(sp<android::gui::WindowInfosListener>& registeredListener, |
| 296 | const sp<Looper>& looper) |
| 297 | : PointerController( |
| 298 | new MockPointerControllerPolicyInterface(), looper, |
| 299 | new NiceMock<MockSpriteController>(looper), |
| 300 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) { |
| 301 | // Register listener |
| 302 | registeredListener = listener; |
| 303 | }, |
| 304 | [®isteredListener](const sp<android::gui::WindowInfosListener>& listener) { |
| 305 | // Unregister listener |
| 306 | if (registeredListener == listener) registeredListener = nullptr; |
| 307 | }) {} |
| 308 | }; |
| 309 | |
| 310 | TEST_F(PointerControllerWindowInfoListenerTest, |
| 311 | doesNotCrashIfListenerCalledAfterPointerControllerDestroyed) { |
| 312 | sp<android::gui::WindowInfosListener> registeredListener; |
| 313 | sp<android::gui::WindowInfosListener> localListenerCopy; |
| 314 | { |
| 315 | TestPointerController pointerController(registeredListener, new Looper(false)); |
| 316 | ASSERT_NE(nullptr, registeredListener) << "WindowInfosListener was not registered"; |
| 317 | localListenerCopy = registeredListener; |
| 318 | } |
| 319 | EXPECT_EQ(nullptr, registeredListener) << "WindowInfosListener was not unregistered"; |
| 320 | localListenerCopy->onWindowInfosChanged({}, {}); |
| 321 | } |
| 322 | |
Garfield Tan | c15eb91 | 2019-08-05 16:47:40 -0700 | [diff] [blame] | 323 | } // namespace android |