blob: 35c31205a4214b8a25534d397df27fcd0d0eb6a6 [file]
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <com_android_server_display_feature_flags.h>
#include <flag_macros.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <input/PointerController.h>
#include <input/ScopedFlagOverride.h>
#include <input/SpriteController.h>
#define SCOPED_DISPLAY_FLAG_OVERRIDE(NAME, VALUE) \
android::ReadFlagValueFunction read##NAME = \
com::android::server::display::feature::flags::NAME; \
android::WriteFlagValueFunction write##NAME = \
com::android::server::display::feature::flags::NAME; \
android::ScopedFlagOverride override##NAME(read##NAME, write##NAME, (VALUE))
#include <atomic>
#include <thread>
#include "input/Input.h"
#include "mocks/MockSprite.h"
#include "mocks/MockSpriteController.h"
namespace android {
enum TestCursorType {
CURSOR_TYPE_DEFAULT = 0,
CURSOR_TYPE_HOVER,
CURSOR_TYPE_TOUCH,
CURSOR_TYPE_ANCHOR,
CURSOR_TYPE_ADDITIONAL,
CURSOR_TYPE_ADDITIONAL_ANIM,
CURSOR_TYPE_STYLUS,
CURSOR_TYPE_CUSTOM = -1,
};
static constexpr float EPSILON = MotionEvent::ROUNDING_PRECISION;
using ::testing::AllOf;
using ::testing::Field;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::Test;
std::pair<float, float> getHotSpotCoordinatesForType(int32_t type) {
return std::make_pair(type * 10, type * 10 + 5);
}
class MockPointerControllerPolicyInterface : public PointerControllerPolicyInterface {
public:
virtual void loadPointerIcon(SpriteIcon* icon, ui::LogicalDisplayId displayId) override;
virtual void loadPointerResources(PointerResources* outResources,
ui::LogicalDisplayId displayId) override;
virtual void loadAdditionalMouseResources(
std::map<PointerIconStyle, SpriteIcon>* outResources,
std::map<PointerIconStyle, PointerAnimation>* outAnimationResources,
ui::LogicalDisplayId displayId) override;
virtual PointerIconStyle getDefaultPointerIconId() override;
virtual PointerIconStyle getDefaultStylusIconId() override;
virtual PointerIconStyle getCustomPointerIconId() override;
bool allResourcesAreLoaded();
bool noResourcesAreLoaded();
private:
void loadPointerIconForType(SpriteIcon* icon, int32_t cursorType);
bool pointerIconLoaded{false};
bool pointerResourcesLoaded{false};
bool additionalMouseResourcesLoaded{false};
};
void MockPointerControllerPolicyInterface::loadPointerIcon(SpriteIcon* icon, ui::LogicalDisplayId) {
loadPointerIconForType(icon, CURSOR_TYPE_DEFAULT);
pointerIconLoaded = true;
}
void MockPointerControllerPolicyInterface::loadPointerResources(PointerResources* outResources,
ui::LogicalDisplayId) {
loadPointerIconForType(&outResources->spotHover, CURSOR_TYPE_HOVER);
loadPointerIconForType(&outResources->spotTouch, CURSOR_TYPE_TOUCH);
loadPointerIconForType(&outResources->spotAnchor, CURSOR_TYPE_ANCHOR);
pointerResourcesLoaded = true;
}
void MockPointerControllerPolicyInterface::loadAdditionalMouseResources(
std::map<PointerIconStyle, SpriteIcon>* outResources,
std::map<PointerIconStyle, PointerAnimation>* outAnimationResources, ui::LogicalDisplayId) {
SpriteIcon icon;
PointerAnimation anim;
// CURSOR_TYPE_ADDITIONAL doesn't have animation resource.
int32_t cursorType = CURSOR_TYPE_ADDITIONAL;
loadPointerIconForType(&icon, cursorType);
(*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon;
// CURSOR_TYPE_ADDITIONAL_ANIM has animation resource.
cursorType = CURSOR_TYPE_ADDITIONAL_ANIM;
loadPointerIconForType(&icon, cursorType);
anim.animationFrames.push_back(icon);
anim.durationPerFrame = 10;
(*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon;
(*outAnimationResources)[static_cast<PointerIconStyle>(cursorType)] = anim;
// CURSOR_TYPE_STYLUS doesn't have animation resource.
cursorType = CURSOR_TYPE_STYLUS;
loadPointerIconForType(&icon, cursorType);
(*outResources)[static_cast<PointerIconStyle>(cursorType)] = icon;
additionalMouseResourcesLoaded = true;
}
PointerIconStyle MockPointerControllerPolicyInterface::getDefaultPointerIconId() {
return static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT);
}
PointerIconStyle MockPointerControllerPolicyInterface::getDefaultStylusIconId() {
return static_cast<PointerIconStyle>(CURSOR_TYPE_STYLUS);
}
PointerIconStyle MockPointerControllerPolicyInterface::getCustomPointerIconId() {
return static_cast<PointerIconStyle>(CURSOR_TYPE_CUSTOM);
}
bool MockPointerControllerPolicyInterface::allResourcesAreLoaded() {
return pointerIconLoaded && pointerResourcesLoaded && additionalMouseResourcesLoaded;
}
bool MockPointerControllerPolicyInterface::noResourcesAreLoaded() {
return !(pointerIconLoaded || pointerResourcesLoaded || additionalMouseResourcesLoaded);
}
void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* icon, int32_t type) {
icon->style = static_cast<PointerIconStyle>(type);
std::pair<float, float> hotSpot = getHotSpotCoordinatesForType(type);
icon->hotSpotX = hotSpot.first;
icon->hotSpotY = hotSpot.second;
}
class TestPointerController : public PointerController {
public:
TestPointerController(sp<PointerControllerPolicyInterface> policy, const sp<Looper>& looper,
SpriteController& spriteController)
: PointerController(policy, looper, spriteController, /*initialDisplayInfos=*/{}) {}
~TestPointerController() override {}
void fade(PointerController::Transition transition) override {
if (transition == PointerController::Transition::GRADUAL) {
fadeGradualCalled = true;
}
PointerController::fade(transition);
}
bool fadeGradualCalled = false;
};
class PointerControllerTest : public Test {
private:
void loopThread();
std::atomic<bool> mRunning = true;
class MyLooper : public Looper {
public:
MyLooper() : Looper(false) {}
~MyLooper() = default;
};
protected:
PointerControllerTest();
~PointerControllerTest();
void ensureDisplayViewportIsSet(ui::LogicalDisplayId displayId = ui::LogicalDisplayId::DEFAULT);
void addDisplay(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
ui::Rotation rotation = ui::ROTATION_0);
sp<MockSprite> mPointerSprite;
sp<MockPointerControllerPolicyInterface> mPolicy;
std::unique_ptr<MockSpriteController> mSpriteController;
std::shared_ptr<PointerController> mPointerController;
sp<MyLooper> mLooper;
private:
std::thread mThread;
};
PointerControllerTest::PointerControllerTest()
: mPointerSprite(new NiceMock<MockSprite>),
mLooper(new MyLooper),
mThread(&PointerControllerTest::loopThread, this) {
mSpriteController.reset(new NiceMock<MockSpriteController>(mLooper));
mPolicy = new MockPointerControllerPolicyInterface();
EXPECT_CALL(*mSpriteController, createSprite())
.WillOnce(Return(mPointerSprite));
mPointerController =
std::make_unique<TestPointerController>(mPolicy, mLooper, *mSpriteController);
}
PointerControllerTest::~PointerControllerTest() {
mPointerController.reset();
mRunning.store(false, std::memory_order_relaxed);
mThread.join();
}
void PointerControllerTest::ensureDisplayViewportIsSet(ui::LogicalDisplayId displayId) {
DisplayViewport viewport;
viewport.displayId = displayId;
viewport.logicalRight = 1600;
viewport.logicalBottom = 1200;
viewport.physicalRight = 800;
viewport.physicalBottom = 600;
viewport.deviceWidth = 400;
viewport.deviceHeight = 300;
mPointerController->setDisplayViewport(viewport);
}
void PointerControllerTest::addDisplay(ui::LogicalDisplayId displayId, int32_t logicalWidth,
int32_t logicalHeight, ui::Rotation rotation) {
gui::DisplayInfo displayInfo;
displayInfo.displayId = displayId;
displayInfo.logicalWidth = logicalWidth;
displayInfo.logicalHeight = logicalHeight;
displayInfo.transform.set(ui::Transform::toRotationFlags(rotation), logicalHeight,
logicalWidth);
mPointerController->onDisplayInfosChanged({displayInfo});
}
void PointerControllerTest::loopThread() {
Looper::setForThread(mLooper);
while (mRunning.load(std::memory_order_relaxed)) {
mLooper->pollOnce(100);
}
}
TEST_F(PointerControllerTest, getPosition) {
addDisplay(ui::LogicalDisplayId::DEFAULT, 800, 600);
ensureDisplayViewportIsSet();
mPointerController->setPosition(100, 200);
vec2 absolutePosition = mPointerController->getPosition();
EXPECT_EQ(100, absolutePosition.x);
EXPECT_EQ(200, absolutePosition.y);
vec2 logicalPosition = mPointerController->getPositionInLogicalDisplay();
EXPECT_EQ(100, logicalPosition.x);
EXPECT_EQ(200, logicalPosition.y);
}
TEST_F(PointerControllerTest, inactivityTimeout_OFF_byDefaultDoesNotFade) {
ensureDisplayViewportIsSet();
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
// Initial state: pointer is visible and alpha is 1.0.
EXPECT_CALL(*mPointerSprite, setVisible(true)).Times(testing::AnyNumber());
EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)).Times(testing::AnyNumber());
// Movement should not trigger an automatic fade when timeout is OFF.
EXPECT_CALL(*mPointerSprite, setVisible(false)).Times(0);
EXPECT_CALL(*mPointerSprite, setAlpha(testing::Lt(1.0f))).Times(0);
mPointerController->move(10, 10);
// Verify that the fading mechanism itself is still functional.
// Manual triggers (e.g., from system events) should still hide the pointer,
// proving that OFF only disables the timer, not the capability to fade.
EXPECT_CALL(*mPointerSprite, setVisible(false)).Times(1);
mPointerController->fade(PointerController::Transition::IMMEDIATE);
}
// Verify that the inactivity timeout mechanism still works if explicitly
// enabled.
TEST_F(PointerControllerTest, inactivityTimeout_canBeEnabledExplicitly) {
ensureDisplayViewportIsSet();
mPointerController->setInactivityTimeout(InactivityTimeout::SHORT);
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
// Initial state: pointer is visible and alpha is 1.0.
EXPECT_CALL(*mPointerSprite, setVisible(true)).Times(testing::AnyNumber());
EXPECT_CALL(*mPointerSprite, setAlpha(1.0f)).Times(testing::AnyNumber());
// We still expect setVisible(false) when we later complete the fade.
EXPECT_CALL(*mPointerSprite, setVisible(false)).Times(1);
mPointerController->move(10, 10);
// Simulate the inactivity timeout.
mPointerController->doInactivityTimeout();
// Verify that a gradual fade was initiated by the timeout.
auto testController = static_cast<TestPointerController*>(mPointerController.get());
EXPECT_TRUE(testController->fadeGradualCalled);
// In this test environment, we use an immediate transition to verify
// the end state because gradual animations require vsync events.
mPointerController->fade(PointerController::Transition::IMMEDIATE);
}
TEST_F(PointerControllerTest, getPosition_withDisplayRotation) {
addDisplay(ui::LogicalDisplayId::DEFAULT, 800, 600, ui::ROTATION_90);
DisplayViewport viewport;
viewport.displayId = ui::LogicalDisplayId::DEFAULT;
viewport.logicalRight = 800;
viewport.logicalBottom = 600;
viewport.physicalRight = 600;
viewport.physicalBottom = 800;
viewport.deviceWidth = 600;
viewport.deviceHeight = 800;
mPointerController->setDisplayViewport(viewport);
mPointerController->setPosition(100, 200);
vec2 absolutePosition = mPointerController->getPosition();
EXPECT_EQ(100, absolutePosition.x);
EXPECT_EQ(200, absolutePosition.y);
vec2 logicalPosition = mPointerController->getPositionInLogicalDisplay();
EXPECT_EQ(400, logicalPosition.x);
EXPECT_EQ(100, logicalPosition.y);
}
TEST_F(PointerControllerTest, useDefaultCursorTypeByDefault) {
ensureDisplayViewportIsSet();
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_DEFAULT);
EXPECT_CALL(*mPointerSprite, setVisible(true));
EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
EXPECT_CALL(*mPointerSprite,
setIcon(AllOf(Field(&SpriteIcon::style,
static_cast<PointerIconStyle>(CURSOR_TYPE_DEFAULT)),
Field(&SpriteIcon::hotSpotX, hotspot.first),
Field(&SpriteIcon::hotSpotY, hotspot.second))));
mPointerController->reloadPointerResources();
}
TEST_F(PointerControllerTest, useStylusTypeForStylusHover) {
ensureDisplayViewportIsSet();
mPointerController->setPresentation(PointerController::Presentation::STYLUS_HOVER);
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
std::pair<float, float> hotspot = getHotSpotCoordinatesForType(CURSOR_TYPE_STYLUS);
EXPECT_CALL(*mPointerSprite, setVisible(true));
EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
EXPECT_CALL(*mPointerSprite,
setIcon(AllOf(Field(&SpriteIcon::style,
static_cast<PointerIconStyle>(CURSOR_TYPE_STYLUS)),
Field(&SpriteIcon::hotSpotX, hotspot.first),
Field(&SpriteIcon::hotSpotY, hotspot.second))));
mPointerController->reloadPointerResources();
}
TEST_F(PointerControllerTest, setPresentationBeforeDisplayViewportDoesNotLoadResources) {
// Setting the presentation mode before a display viewport is set will not load any resources.
mPointerController->setPresentation(PointerController::Presentation::POINTER);
ASSERT_TRUE(mPolicy->noResourcesAreLoaded());
// When the display is set, then the resources are loaded.
ensureDisplayViewportIsSet();
ASSERT_TRUE(mPolicy->allResourcesAreLoaded());
}
TEST_F(PointerControllerTest, updatePointerIconWithChoreographer) {
// When PointerChoreographer is enabled, the presentation mode is set before the viewport.
mPointerController->setPresentation(PointerController::Presentation::POINTER);
ensureDisplayViewportIsSet();
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
int32_t type = CURSOR_TYPE_ADDITIONAL;
std::pair<float, float> hotspot = getHotSpotCoordinatesForType(type);
EXPECT_CALL(*mPointerSprite, setVisible(true));
EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
EXPECT_CALL(*mPointerSprite,
setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(type)),
Field(&SpriteIcon::hotSpotX, hotspot.first),
Field(&SpriteIcon::hotSpotY, hotspot.second))));
mPointerController->updatePointerIcon(static_cast<PointerIconStyle>(type));
}
TEST_F(PointerControllerTest, setCustomPointerIcon) {
ensureDisplayViewportIsSet();
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
int32_t style = CURSOR_TYPE_CUSTOM;
float hotSpotX = 15;
float hotSpotY = 20;
SpriteIcon icon;
icon.style = static_cast<PointerIconStyle>(style);
icon.hotSpotX = hotSpotX;
icon.hotSpotY = hotSpotY;
EXPECT_CALL(*mPointerSprite, setVisible(true));
EXPECT_CALL(*mPointerSprite, setAlpha(1.0f));
EXPECT_CALL(*mPointerSprite,
setIcon(AllOf(Field(&SpriteIcon::style, static_cast<PointerIconStyle>(style)),
Field(&SpriteIcon::hotSpotX, hotSpotX),
Field(&SpriteIcon::hotSpotY, hotSpotY))));
mPointerController->setCustomPointerIcon(icon);
}
TEST_F(PointerControllerTest, doesNotGetResourcesBeforeSettingViewport) {
mPointerController->setPresentation(PointerController::Presentation::POINTER);
mPointerController->setPosition(1.0f, 1.0f);
mPointerController->move(1.0f, 1.0f);
mPointerController->unfade(PointerController::Transition::IMMEDIATE);
mPointerController->fade(PointerController::Transition::IMMEDIATE);
EXPECT_TRUE(mPolicy->noResourcesAreLoaded());
ensureDisplayViewportIsSet();
}
TEST_F(PointerControllerTest, updatesSkipScreenshotFlagForTouchSpots_FlagOff) {
SCOPED_DISPLAY_FLAG_OVERRIDE(show_cursor_on_secure_mirrors, false);
ensureDisplayViewportIsSet();
PointerCoords testSpotCoords;
testSpotCoords.clear();
testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
BitSet32 testIdBits;
testIdBits.markBit(0);
std::array<uint32_t, MAX_POINTER_ID + 1> testIdToIndex;
sp<MockSprite> testSpotSprite(new NiceMock<MockSprite>);
// By default sprite is not marked secure
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testSpotSprite));
EXPECT_CALL(*testSpotSprite, setSkipScreenshot(false));
// Update spots to sync state with sprite
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
// Marking the display to skip screenshot should update sprite as well
mPointerController->setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId::DEFAULT);
EXPECT_CALL(*testSpotSprite, setSkipScreenshot(true));
// Update spots to sync state with sprite
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
// Reset flag and verify again
mPointerController->clearSkipScreenshotFlags();
EXPECT_CALL(*testSpotSprite, setSkipScreenshot(false));
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
}
TEST_F(PointerControllerTest, updatesSkipScreenshotFlagForTouchSpots_FlagOn) {
SCOPED_DISPLAY_FLAG_OVERRIDE(show_cursor_on_secure_mirrors, true);
ensureDisplayViewportIsSet();
PointerCoords testSpotCoords;
testSpotCoords.clear();
testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
BitSet32 testIdBits;
testIdBits.markBit(0);
std::array<uint32_t, MAX_POINTER_ID + 1> testIdToIndex;
sp<MockSprite> testSpotSprite(new NiceMock<MockSprite>);
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testSpotSprite));
EXPECT_CALL(*testSpotSprite, setSkipScreenshot).Times(0);
// Update spots to sync state with sprite
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
EXPECT_CALL(*testSpotSprite, setSkipScreenshot).Times(0);
// Marking the display to skip screenshot should not update spot sprite when flag is ON
mPointerController->setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId::DEFAULT);
// Update spots to sync state with sprite
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
}
TEST_F(PointerControllerTest, updatesSkipOnUnsecureMirroredDisplayFlagForTouchSpots) {
ensureDisplayViewportIsSet();
PointerCoords testSpotCoords;
testSpotCoords.clear();
testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
testSpotCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, 1);
BitSet32 testIdBits;
testIdBits.markBit(0);
std::array<uint32_t, MAX_POINTER_ID + 1> testIdToIndex;
sp<MockSprite> testSpotSprite(new NiceMock<MockSprite>);
// By default sprite is not marked to skip on unsecure mirrored
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testSpotSprite));
EXPECT_CALL(*testSpotSprite, setSkipOnUnsecureMirroredDisplay(false));
// Update spots to sync state with sprite
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
// Marking the display to skip on unsecure mirrored should update sprite as well
mPointerController->setSkipOnUnsecureMirroredDisplayFlagForDisplay(
ui::LogicalDisplayId::DEFAULT);
EXPECT_CALL(*testSpotSprite, setSkipOnUnsecureMirroredDisplay(true));
// Update spots to sync state with sprite
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
// Reset flag and verify again
mPointerController->clearSkipOnUnsecureMirroredDisplayFlags();
EXPECT_CALL(*testSpotSprite, setSkipOnUnsecureMirroredDisplay(false));
mPointerController->setSpots(&testSpotCoords, testIdToIndex.cbegin(), testIdBits,
ui::LogicalDisplayId::DEFAULT);
testing::Mock::VerifyAndClearExpectations(testSpotSprite.get());
}
class PointerControllerSkipScreenshotFlagTest
: public PointerControllerTest,
public testing::WithParamInterface<PointerControllerInterface::ControllerType> {};
TEST_P(PointerControllerSkipScreenshotFlagTest, updatesSkipScreenshotFlag_FlagOff) {
SCOPED_DISPLAY_FLAG_OVERRIDE(show_cursor_on_secure_mirrors, false);
sp<MockSprite> testPointerSprite(new NiceMock<MockSprite>);
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testPointerSprite));
// Create a pointer controller
mPointerController = PointerController::create(mPolicy, mLooper, *mSpriteController, GetParam(),
/*initialDisplayInfos=*/{});
ensureDisplayViewportIsSet(ui::LogicalDisplayId::DEFAULT);
// By default skip screenshot flag is not set for the sprite
EXPECT_CALL(*testPointerSprite, setSkipScreenshot(false));
// Update pointer to sync state with sprite
mPointerController->setPosition(100, 100);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
// Marking the controller to skip screenshot should update pointer sprite
mPointerController->setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId::DEFAULT);
EXPECT_CALL(*testPointerSprite, setSkipScreenshot(true));
// Update pointer to sync state with sprite
mPointerController->move(10, 10);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
// Reset flag and verify again
mPointerController->clearSkipScreenshotFlags();
EXPECT_CALL(*testPointerSprite, setSkipScreenshot(false));
mPointerController->move(10, 10);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
}
TEST_P(PointerControllerSkipScreenshotFlagTest, updatesSkipScreenshotFlag_FlagOn) {
SCOPED_DISPLAY_FLAG_OVERRIDE(show_cursor_on_secure_mirrors, true);
sp<MockSprite> testPointerSprite(new NiceMock<MockSprite>);
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testPointerSprite));
// Create a pointer controller
mPointerController = PointerController::create(mPolicy, mLooper, *mSpriteController, GetParam(),
/*initialDisplayInfos=*/{});
ensureDisplayViewportIsSet(ui::LogicalDisplayId::DEFAULT);
EXPECT_CALL(*testPointerSprite, setSkipScreenshot).Times(0);
// Update pointer to sync state with sprite
mPointerController->setPosition(100, 100);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
EXPECT_CALL(*testPointerSprite, setSkipScreenshot).Times(0);
mPointerController->setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId::DEFAULT);
// Update pointer to sync state with sprite
mPointerController->move(10, 10);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
}
INSTANTIATE_TEST_SUITE_P(PointerControllerSkipScreenshotFlagTest,
PointerControllerSkipScreenshotFlagTest,
testing::Values(PointerControllerInterface::ControllerType::MOUSE,
PointerControllerInterface::ControllerType::STYLUS));
class PointerControllerSkipOnUnsecureMirroredDisplayFlagTest
: public PointerControllerTest,
public testing::WithParamInterface<PointerControllerInterface::ControllerType> {};
TEST_P(PointerControllerSkipOnUnsecureMirroredDisplayFlagTest,
updatesSkipOnUnsecureMirroredDisplayFlag) {
sp<MockSprite> testPointerSprite(new NiceMock<MockSprite>);
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testPointerSprite));
// Create a pointer controller
mPointerController = PointerController::create(mPolicy, mLooper, *mSpriteController, GetParam(),
/*initialDisplayInfos=*/{});
ensureDisplayViewportIsSet(ui::LogicalDisplayId::DEFAULT);
// By default skip flag is not set for the sprite
EXPECT_CALL(*testPointerSprite, setSkipOnUnsecureMirroredDisplay(false));
// Update pointer to sync state with sprite
mPointerController->setPosition(100, 100);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
// Marking the controller to skip should update pointer sprite
mPointerController->setSkipOnUnsecureMirroredDisplayFlagForDisplay(
ui::LogicalDisplayId::DEFAULT);
EXPECT_CALL(*testPointerSprite, setSkipOnUnsecureMirroredDisplay(true));
// Update pointer to sync state with sprite
mPointerController->move(10, 10);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
// Reset flag and verify again
mPointerController->clearSkipOnUnsecureMirroredDisplayFlags();
EXPECT_CALL(*testPointerSprite, setSkipOnUnsecureMirroredDisplay(false));
mPointerController->move(10, 10);
testing::Mock::VerifyAndClearExpectations(testPointerSprite.get());
}
INSTANTIATE_TEST_SUITE_P(PointerControllerSkipOnUnsecureMirroredDisplayFlagTest,
PointerControllerSkipOnUnsecureMirroredDisplayFlagTest,
testing::Values(PointerControllerInterface::ControllerType::MOUSE,
PointerControllerInterface::ControllerType::STYLUS));
class MousePointerControllerTest : public PointerControllerTest {
protected:
MousePointerControllerTest() {
sp<MockSprite> testPointerSprite(new NiceMock<MockSprite>);
EXPECT_CALL(*mSpriteController, createSprite).WillOnce(Return(testPointerSprite));
// create a mouse pointer controller
mPointerController =
PointerController::create(mPolicy, mLooper, *mSpriteController,
PointerControllerInterface::ControllerType::MOUSE,
/*initialDisplayInfos=*/{});
// set display viewport
DisplayViewport viewport;
viewport.displayId = ui::LogicalDisplayId::DEFAULT;
viewport.logicalRight = 5;
viewport.logicalBottom = 5;
viewport.physicalRight = 5;
viewport.physicalBottom = 5;
viewport.deviceWidth = 5;
viewport.deviceHeight = 5;
mPointerController->setDisplayViewport(viewport);
}
};
struct MousePointerControllerTestParam {
vec2 startPosition;
vec2 delta;
vec2 endPosition;
vec2 unconsumedDelta;
};
class PointerControllerViewportTransitionTest
: public MousePointerControllerTest,
public testing::WithParamInterface<MousePointerControllerTestParam> {};
TEST_P(PointerControllerViewportTransitionTest, testPointerViewportTransition) {
const auto& params = GetParam();
mPointerController->setPosition(params.startPosition.x, params.startPosition.y);
auto unconsumedDelta = mPointerController->move(params.delta.x, params.delta.y);
auto position = mPointerController->getPosition();
EXPECT_NEAR(position.x, params.endPosition.x, EPSILON);
EXPECT_NEAR(position.y, params.endPosition.y, EPSILON);
EXPECT_NEAR(unconsumedDelta.x, params.unconsumedDelta.x, EPSILON);
EXPECT_NEAR(unconsumedDelta.y, params.unconsumedDelta.y, EPSILON);
}
INSTANTIATE_TEST_SUITE_P(PointerControllerViewportTransitionTest,
PointerControllerViewportTransitionTest,
testing::Values(
// no transition
MousePointerControllerTestParam{{2.0f, 2.0f},
{2.0f, 2.0f},
{4.0f, 4.0f},
{0.0f, 0.0f}},
// right boundary
MousePointerControllerTestParam{{2.0f, 2.0f},
{3.0f, 0.0f},
{4.0f, 2.0f},
{1.0f, 0.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{3.0f, -1.0f},
{4.0f, 1.0f},
{1.0f, 0.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{3.0f, 1.0f},
{4.0f, 3.0f},
{1.0f, 0.0f}},
// left boundary
MousePointerControllerTestParam{{2.0f, 2.0f},
{-3.0f, 0.0f},
{0.0f, 2.0f},
{-1.0f, 0.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{-3.0f, -1.0f},
{0.0f, 1.0f},
{-1.0f, 0.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{-3.0f, 1.0f},
{0.0f, 3.0f},
{-1.0f, 0.0f}},
// bottom boundary
MousePointerControllerTestParam{{2.0f, 2.0f},
{0.0f, 3.0f},
{2.0f, 4.0f},
{0.0f, 1.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{-1.0f, 3.0f},
{1.0f, 4.0f},
{0.0f, 1.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{1.0f, 3.0f},
{3.0f, 4.0f},
{0.0f, 1.0f}},
// top boundary
MousePointerControllerTestParam{{2.0f, 2.0f},
{0.0f, -3.0f},
{2.0f, 0.0f},
{0.0f, -1.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{-1.0f, -3.0f},
{1.0f, 0.0f},
{0.0f, -1.0f}},
MousePointerControllerTestParam{{2.0f, 2.0f},
{1.0f, -3.0f},
{3.0f, 0.0f},
{0.0f, -1.0f}},
// top-left corner
MousePointerControllerTestParam{{2.0f, 2.0f},
{-3.0f, -3.0f},
{0.0f, 0.0f},
{-1.0f, -1.0f}},
// top-right corner
MousePointerControllerTestParam{{2.0f, 2.0f},
{3.0f, -3.0f},
{4.0f, 0.0f},
{1.0f, -1.0f}},
// bottom-right corner
MousePointerControllerTestParam{{2.0f, 2.0f},
{3.0f, 3.0f},
{4.0f, 4.0f},
{1.0f, 1.0f}},
// bottom-left corner
MousePointerControllerTestParam{{2.0f, 2.0f},
{-3.0f, 3.0f},
{0.0f, 4.0f},
{-1.0f, 1.0f}}));
} // namespace android