Add an integration test to validate pick mode crash (#2935)

* add an integration test to validate pick mode crash

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* update to test after review feedback

Signed-off-by: hultonha <hultonha@amazon.co.uk>
This commit is contained in:
hultonha
2021-08-09 11:56:19 +01:00
committed by GitHub
parent 5baa74ea40
commit 659e486cd7
4 changed files with 89 additions and 11 deletions
@@ -65,6 +65,35 @@ namespace UnitTest
} }
} }
bool FocusInteractionWidget::event(QEvent* event)
{
using AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus;
auto eventType = event->type();
switch (eventType)
{
case QEvent::MouseButtonPress:
EditorInteractionSystemViewportSelectionRequestBus::Event(
AzToolsFramework::GetEntityContextId(), &EditorInteractionSystemViewportSelectionRequestBus::Events::SetDefaultHandler);
return true;
case QEvent::FocusIn:
case QEvent::FocusOut:
{
bool handled = false;
AzToolsFramework::ViewportInteraction::MouseInteraction mouseInteraction;
EditorInteractionSystemViewportSelectionRequestBus::EventResult(
handled, AzToolsFramework::GetEntityContextId(),
&EditorInteractionSystemViewportSelectionRequestBus::Events::InternalHandleMouseViewportInteraction,
AzToolsFramework::ViewportInteraction::MouseInteractionEvent(
mouseInteraction, AzToolsFramework::ViewportInteraction::MouseEvent::Down));
return handled;
}
}
return QWidget::event(event);
}
void TestEditorActions::Connect() void TestEditorActions::Connect()
{ {
using AzToolsFramework::GetEntityContextId; using AzToolsFramework::GetEntityContextId;
@@ -8,6 +8,7 @@
#pragma once #pragma once
#if !defined(Q_MOC_RUN)
#include <AzCore/Asset/AssetCommon.h> #include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Component/ComponentApplicationBus.h> #include <AzCore/Component/ComponentApplicationBus.h>
#include <AzCore/Slice/SliceAsset.h> #include <AzCore/Slice/SliceAsset.h>
@@ -31,6 +32,7 @@
#include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h> #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
#include <AzToolsFramework/SourceControl/PerforceConnection.h> #include <AzToolsFramework/SourceControl/PerforceConnection.h>
#include <AzToolsFramework/UnitTest/ToolsTestApplication.h> #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
#endif // !defined(Q_MOC_RUN)
#include <ostream> #include <ostream>
@@ -40,7 +42,7 @@ AZ_POP_DISABLE_WARNING
#define AUTO_RESULT_IF_SETTING_TRUE(_settingName, _result) \ #define AUTO_RESULT_IF_SETTING_TRUE(_settingName, _result) \
{ \ { \
bool settingValue = true; \ bool settingValue = true; \
if (auto* registry = AZ::SettingsRegistry::Get()) \ if (auto* registry = AZ::SettingsRegistry::Get()) \
{ \ { \
registry->Get(settingValue, _settingName); \ registry->Get(settingValue, _settingName); \
@@ -51,23 +53,16 @@ AZ_POP_DISABLE_WARNING
EXPECT_TRUE(_result); \ EXPECT_TRUE(_result); \
return; \ return; \
} \ } \
} }
namespace AZ
{
class Entity;
class EntityId;
} // namespace AZ
namespace UnitTest namespace UnitTest
{ {
constexpr AZStd::string_view prefabSystemSetting = "/Amazon/Preferences/EnablePrefabSystem"; constexpr AZStd::string_view prefabSystemSetting = "/Amazon/Preferences/EnablePrefabSystem";
/// Test widget to store QActions generated by EditorTransformComponentSelection. /// Test widget to store QActions generated by EditorTransformComponentSelection.
class TestWidget class TestWidget : public QWidget
: public QWidget
{ {
Q_OBJECT
public: public:
TestWidget() TestWidget()
: QWidget() : QWidget()
@@ -79,6 +74,15 @@ namespace UnitTest
bool eventFilter(QObject* watched, QEvent* event) override; bool eventFilter(QObject* watched, QEvent* event) override;
}; };
/// Widget used to trigger a viewport interaction event while a focus change is happening.
class FocusInteractionWidget : public QWidget
{
Q_OBJECT
public:
FocusInteractionWidget(QWidget* parent = nullptr) : QWidget(parent) {}
bool event(QEvent* event) override;
};
/// Stores actions registered for either normal mode (regular viewport) editing and /// Stores actions registered for either normal mode (regular viewport) editing and
/// component mode editing. /// component mode editing.
class TestEditorActions class TestEditorActions
@@ -50,6 +50,7 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
ly_add_target( ly_add_target(
NAME AzToolsFrameworkTestCommon STATIC NAME AzToolsFrameworkTestCommon STATIC
NAMESPACE AZ NAMESPACE AZ
AUTOMOC
FILES_CMAKE FILES_CMAKE
AzToolsFramework/aztoolsframeworktestcommon_files.cmake AzToolsFramework/aztoolsframeworktestcommon_files.cmake
INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES
@@ -68,6 +69,7 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
ly_add_target( ly_add_target(
NAME AzToolsFramework.Tests ${PAL_TRAIT_TEST_TARGET_TYPE} NAME AzToolsFramework.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
NAMESPACE AZ NAMESPACE AZ
AUTOMOC
FILES_CMAKE FILES_CMAKE
Tests/aztoolsframeworktests_files.cmake Tests/aztoolsframeworktests_files.cmake
INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES
@@ -31,8 +31,10 @@
#include <AzToolsFramework/Viewport/ActionBus.h> #include <AzToolsFramework/Viewport/ActionBus.h>
#include <AzToolsFramework/ViewportSelection/EditorDefaultSelection.h> #include <AzToolsFramework/ViewportSelection/EditorDefaultSelection.h>
#include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h> #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
#include <AzToolsFramework/ViewportSelection/EditorPickEntitySelection.h>
#include <AzToolsFramework/ViewportSelection/EditorTransformComponentSelection.h> #include <AzToolsFramework/ViewportSelection/EditorTransformComponentSelection.h>
#include <AzToolsFramework/ViewportSelection/EditorVisibleEntityDataCache.h> #include <AzToolsFramework/ViewportSelection/EditorVisibleEntityDataCache.h>
#include <AzToolsFramework/ViewportUi/ViewportUiManager.h>
namespace AZ namespace AZ
{ {
@@ -188,6 +190,47 @@ namespace UnitTest
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EditorTransformComponentSelection Tests // EditorTransformComponentSelection Tests
TEST_F(EditorTransformComponentSelectionFixture, Focus_is_not_changed_while_switching_viewport_interaction_request_instance)
{
// setup a dummy widget and make it the active window to ensure focus in/out events are fired
auto dummyWidget = AZStd::make_unique<QWidget>();
QApplication::setActiveWindow(dummyWidget.get());
// note: it is important to make sure the focus widget is parented to the dummy widget to have focus in/out events fire
auto focusWidget = AZStd::make_unique<UnitTest::FocusInteractionWidget>(dummyWidget.get());
const auto previousFocusWidget = QApplication::focusWidget();
// Given
// setup viewport ui system
AzToolsFramework::ViewportUi::ViewportUiManager viewportUiManager;
viewportUiManager.ConnectViewportUiBus(AzToolsFramework::ViewportUi::DefaultViewportId);
viewportUiManager.InitializeViewportUi(&m_editorActions.m_defaultWidget, focusWidget.get());
// begin EditorPickEntitySelection
using AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus;
EditorInteractionSystemViewportSelectionRequestBus::Event(
AzToolsFramework::GetEntityContextId(), &EditorInteractionSystemViewportSelectionRequestBus::Events::SetHandler,
[](const AzToolsFramework::EditorVisibleEntityDataCache* entityDataCache)
{
return AZStd::make_unique<AzToolsFramework::EditorPickEntitySelection>(entityDataCache);
});
// When
// a mouse event is sent to the focus widget (set to be the render overlay in the viewport ui system)
QTest::mouseClick(focusWidget.get(), Qt::MouseButton::LeftButton);
// Then
// focus should not change
EXPECT_FALSE(focusWidget->hasFocus());
EXPECT_EQ(previousFocusWidget, QApplication::focusWidget());
// clean up
viewportUiManager.DisconnectViewportUiBus();
focusWidget.reset();
dummyWidget.reset();
}
TEST_F(EditorTransformComponentSelectionFixture, ManipulatorOrientationIsResetWhenEntityOrientationIsReset) TEST_F(EditorTransformComponentSelectionFixture, ManipulatorOrientationIsResetWhenEntityOrientationIsReset)
{ {
using AzToolsFramework::EditorTransformComponentSelectionRequestBus; using AzToolsFramework::EditorTransformComponentSelectionRequestBus;