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
|
||||
|
||||
Reference in New Issue
Block a user