bugfix: correct mouseMove under AzToolsFrameworkHelper (#6493)
* bugfix: correct mouseMove under AzToolsFrameworkHelper REF: https://github.com/o3de/o3de/issues/6481 Signed-off-by: Michael Pollind <mpollind@gmail.com> * chore: added unit test Signed-off-by: Michael Pollind <mpollind@gmail.com> * chore: address comments Signed-off-by: Michael Pollind <mpollind@gmail.com> * chore: correct fixture Signed-off-by: Michael Pollind <mpollind@gmail.com> * chore: tweak mouse move logic Signed-off-by: Michael Pollind <mpollind@gmail.com> * updates to track mouse/cursor position via events instead of using QCursor::pos() Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com> * rename AzToolFrameworkTestHelperTest.cpp to AzToolsFrameworkTestHelpersTest.cpp Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com> Co-authored-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>
This commit is contained in:
+24
-5
@@ -30,8 +30,7 @@ namespace UnitTest
|
||||
void MousePressAndMove(
|
||||
QWidget* widget, const QPoint& initialPositionWidget, const QPoint& mouseDelta, const Qt::MouseButton mouseButton)
|
||||
{
|
||||
QPoint position = widget->mapToGlobal(initialPositionWidget);
|
||||
QTest::mousePress(widget, mouseButton, Qt::NoModifier, position);
|
||||
QTest::mousePress(widget, mouseButton, Qt::NoModifier, initialPositionWidget);
|
||||
|
||||
MouseMove(widget, initialPositionWidget, mouseDelta, mouseButton);
|
||||
}
|
||||
@@ -45,14 +44,15 @@ namespace UnitTest
|
||||
// - https://lists.qt-project.org/pipermail/development/2019-July/036873.html
|
||||
void MouseMove(QWidget* widget, const QPoint& initialPositionWidget, const QPoint& mouseDelta, const Qt::MouseButton mouseButton)
|
||||
{
|
||||
QPoint nextPosition = widget->mapToGlobal(initialPositionWidget + mouseDelta);
|
||||
const QPoint nextLocalPosition = initialPositionWidget + mouseDelta;
|
||||
const QPoint nextGlobalPosition = widget->mapToGlobal(nextLocalPosition);
|
||||
|
||||
// ^1 To ensure a mouse move event is fired we must call the test mouse move function
|
||||
// and also send a mouse move event that matches. Each on their own do not appear to
|
||||
// work - please see the links above for more context.
|
||||
QTest::mouseMove(widget, nextPosition);
|
||||
QTest::mouseMove(widget, nextLocalPosition);
|
||||
QMouseEvent mouseMoveEvent(
|
||||
QEvent::MouseMove, QPointF(nextPosition), QPointF(nextPosition), Qt::NoButton, mouseButton, Qt::NoModifier);
|
||||
QEvent::MouseMove, QPointF(nextLocalPosition), QPointF(nextGlobalPosition), Qt::NoButton, mouseButton, Qt::NoModifier);
|
||||
QApplication::sendEvent(widget, &mouseMoveEvent);
|
||||
}
|
||||
|
||||
@@ -157,6 +157,23 @@ namespace UnitTest
|
||||
return QWidget::event(event);
|
||||
}
|
||||
|
||||
MouseMoveDetector::MouseMoveDetector(QWidget* parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
bool MouseMoveDetector::eventFilter(QObject* watched, QEvent* event)
|
||||
{
|
||||
if (const auto eventType = event->type(); eventType == QEvent::Type::MouseMove)
|
||||
{
|
||||
auto mouseEvent = static_cast<QMouseEvent*>(event);
|
||||
m_mouseGlobalPosition = mouseEvent->globalPos();
|
||||
m_mouseLocalPosition = mouseEvent->pos();
|
||||
}
|
||||
|
||||
return QObject::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void TestEditorActions::Connect()
|
||||
{
|
||||
using AzToolsFramework::GetEntityContextId;
|
||||
@@ -571,3 +588,5 @@ namespace UnitTest
|
||||
sliceAssets.clear();
|
||||
}
|
||||
} // namespace UnitTest
|
||||
|
||||
#include <moc_AzToolsFrameworkTestHelpers.cpp>
|
||||
|
||||
+20
-1
@@ -111,10 +111,29 @@ namespace UnitTest
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
FocusInteractionWidget(QWidget* parent = nullptr) : QWidget(parent) {}
|
||||
FocusInteractionWidget(QWidget* parent = nullptr)
|
||||
: QWidget(parent)
|
||||
{
|
||||
}
|
||||
|
||||
bool event(QEvent* event) override;
|
||||
};
|
||||
|
||||
/// Records mouse move events and stores the local and global position of the cursor.
|
||||
/// @note To use, install as an event filter for the widget being interacted with
|
||||
/// e.g. m_testWidget->installEventFilter(&m_mouseMoveDetector);
|
||||
class MouseMoveDetector : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MouseMoveDetector(QWidget* parent = nullptr);
|
||||
|
||||
bool eventFilter([[maybe_unused]] QObject* watched, QEvent* event) override;
|
||||
|
||||
QPoint m_mouseGlobalPosition;
|
||||
QPoint m_mouseLocalPosition;
|
||||
};
|
||||
|
||||
/// Stores actions registered for either normal mode (regular viewport) editing and
|
||||
/// component mode editing.
|
||||
class TestEditorActions
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
|
||||
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
|
||||
namespace UnitTest
|
||||
{
|
||||
class AzToolsFrameworkTestHelpersFixture : public AllocatorsTestFixture
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
AllocatorsTestFixture::SetUp();
|
||||
|
||||
m_rootWidget = AZStd::make_unique<QWidget>();
|
||||
m_rootWidget->setFixedSize(0, 0);
|
||||
m_rootWidget->setMouseTracking(true);
|
||||
m_rootWidget->move(0, 0); // explicitly set the widget to be in the upper left corner
|
||||
|
||||
m_mouseMoveDetector = AZStd::make_unique<MouseMoveDetector>();
|
||||
m_rootWidget->installEventFilter(m_mouseMoveDetector.get());
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
m_rootWidget->removeEventFilter(m_mouseMoveDetector.get());
|
||||
m_rootWidget.reset();
|
||||
m_mouseMoveDetector.reset();
|
||||
|
||||
AllocatorsTestFixture::TearDown();
|
||||
}
|
||||
|
||||
AZStd::unique_ptr<QWidget> m_rootWidget;
|
||||
AZStd::unique_ptr<MouseMoveDetector> m_mouseMoveDetector;
|
||||
};
|
||||
|
||||
struct MouseMoveParams
|
||||
{
|
||||
QSize m_widgetSize;
|
||||
QPoint m_widgetPosition;
|
||||
QPoint m_localCursorPosition;
|
||||
QPoint m_cursorDelta;
|
||||
};
|
||||
|
||||
class MouseMoveAzToolsFrameworkTestHelperFixture
|
||||
: public AzToolsFrameworkTestHelpersFixture
|
||||
, public ::testing::WithParamInterface<MouseMoveParams>
|
||||
{
|
||||
};
|
||||
|
||||
TEST_P(MouseMoveAzToolsFrameworkTestHelperFixture, MouseMoveCorrectlyTransformsCursorPositionInGlobalAndLocalSpace)
|
||||
{
|
||||
// given
|
||||
const MouseMoveParams mouseMoveParams = GetParam();
|
||||
m_rootWidget->move(mouseMoveParams.m_widgetPosition);
|
||||
m_rootWidget->setFixedSize(mouseMoveParams.m_widgetSize);
|
||||
|
||||
// when
|
||||
MouseMove(m_rootWidget.get(), mouseMoveParams.m_localCursorPosition, mouseMoveParams.m_cursorDelta);
|
||||
|
||||
// then
|
||||
const QPoint mouseLocalPosition = m_mouseMoveDetector->m_mouseLocalPosition;
|
||||
const QPoint mouseLocalPositionFromGlobal = m_rootWidget->mapFromGlobal(m_mouseMoveDetector->m_mouseGlobalPosition);
|
||||
const QPoint expectedPosition = mouseMoveParams.m_localCursorPosition + mouseMoveParams.m_cursorDelta;
|
||||
|
||||
using ::testing::Eq;
|
||||
EXPECT_THAT(mouseLocalPosition.x(), Eq(expectedPosition.x()));
|
||||
EXPECT_THAT(mouseLocalPosition.y(), Eq(expectedPosition.y()));
|
||||
EXPECT_THAT(mouseLocalPositionFromGlobal.x(), Eq(expectedPosition.x()));
|
||||
EXPECT_THAT(mouseLocalPositionFromGlobal.y(), Eq(expectedPosition.y()));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
All,
|
||||
MouseMoveAzToolsFrameworkTestHelperFixture,
|
||||
testing::Values(
|
||||
MouseMoveParams{ QSize(100, 100), QPoint(0, 0), QPoint(0, 0), QPoint(10, 10) },
|
||||
MouseMoveParams{ QSize(100, 100), QPoint(100, 100), QPoint(0, 0), QPoint(10, 10) },
|
||||
MouseMoveParams{ QSize(100, 100), QPoint(20, 20), QPoint(50, 50), QPoint(20, 20) }));
|
||||
} // namespace UnitTest
|
||||
@@ -12,6 +12,7 @@ set(FILES
|
||||
AssetFileInfoListComparison.cpp
|
||||
AssetSeedManager.cpp
|
||||
AssetSystemMocks.h
|
||||
AzToolsFrameworkTestHelpersTest.cpp
|
||||
BoundsTestComponent.cpp
|
||||
BoundsTestComponent.h
|
||||
ComponentAdapterTests.cpp
|
||||
|
||||
Reference in New Issue
Block a user