2
0

EntityIdQLabelTests.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Component/Entity.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzCore/UserSettings/UserSettingsComponent.h>
  11. #include <AzFramework/Application/Application.h>
  12. #include <AzFramework/Entity/EntityContext.h>
  13. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  14. #include <AzToolsFramework/Application/ToolsApplication.h>
  15. #include <AzToolsFramework/UI/PropertyEditor/EntityIdQLabel.hxx>
  16. #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
  17. #include <AzToolsFramework/Viewport/ActionBus.h>
  18. #include <QtTest/QtTest>
  19. using namespace AzToolsFramework;
  20. namespace UnitTest
  21. {
  22. // Test widget to store a EntityIdQLabel
  23. class EntityIdQLabel_TestWidget
  24. : public QWidget
  25. {
  26. public:
  27. explicit EntityIdQLabel_TestWidget(QWidget* parent = nullptr)
  28. : QWidget(nullptr)
  29. {
  30. AZ_UNUSED(parent);
  31. // ensure EntityIdQLabel_TestWidget can intercept and filter any incoming events itself
  32. installEventFilter(this);
  33. m_testLabel = new EntityIdQLabel(this);
  34. }
  35. EntityIdQLabel* m_testLabel = nullptr;
  36. };
  37. // Used to simulate a system implementing the EditorRequests bus to validate that the double click will
  38. // result in a GoToSelectedEntitiesInViewports event
  39. class EditorRequestHandlerTest : AzToolsFramework::EditorRequests::Bus::Handler
  40. {
  41. public:
  42. EditorRequestHandlerTest()
  43. {
  44. AzToolsFramework::EditorRequests::Bus::Handler::BusConnect();
  45. }
  46. ~EditorRequestHandlerTest() override
  47. {
  48. AzToolsFramework::EditorRequests::Bus::Handler::BusDisconnect();
  49. }
  50. void BrowseForAssets(AssetBrowser::AssetSelectionModel& /*selection*/) override {}
  51. void GoToSelectedEntitiesInViewports() override
  52. {
  53. m_wentToSelectedEntitiesInViewport = true;
  54. }
  55. bool m_wentToSelectedEntitiesInViewport = false;
  56. };
  57. // Fixture to support testing EntityIdQLabel functionality
  58. class EntityIdQLabelTest
  59. : public LeakDetectionFixture
  60. {
  61. public:
  62. void SetUp() override
  63. {
  64. AZ::ComponentApplication::StartupParameters startupParameters;
  65. startupParameters.m_loadSettingsRegistry = false;
  66. m_app.Start(AzFramework::Application::Descriptor(), startupParameters);
  67. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  68. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  69. // in the unit tests.
  70. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  71. }
  72. void TearDown() override
  73. {
  74. m_app.Stop();
  75. }
  76. EntityIdQLabel_TestWidget* m_widget = nullptr;
  77. private:
  78. ToolsTestApplication m_app{ "EntityIdQLabelTest" };
  79. };
  80. TEST_F(EntityIdQLabelTest, DoubleClickEntitySelectionTest)
  81. {
  82. AZ::Entity* entity = aznew AZ::Entity();
  83. ASSERT_TRUE(entity != nullptr);
  84. entity->Init();
  85. entity->Activate();
  86. AZ::EntityId entityId = entity->GetId();
  87. ASSERT_TRUE(entityId.IsValid());
  88. EntityIdQLabel_TestWidget* widget = new EntityIdQLabel_TestWidget(nullptr);
  89. ASSERT_TRUE(widget != nullptr);
  90. ASSERT_TRUE(widget->m_testLabel != nullptr);
  91. widget->m_testLabel->setFocus();
  92. widget->m_testLabel->SetEntityId(entityId, {});
  93. EditorRequestHandlerTest editorRequestHandler;
  94. // Simulate double clicking the label
  95. QTest::mouseDClick(widget->m_testLabel, Qt::LeftButton);
  96. // If successful we expect the label's entity to be selected.
  97. EntityIdList selectedEntities;
  98. ToolsApplicationRequestBus::BroadcastResult(selectedEntities, &ToolsApplicationRequests::GetSelectedEntities);
  99. EXPECT_FALSE(selectedEntities.empty()) << "Double clicking on an EntityIdQLabel should select the entity";
  100. EXPECT_TRUE(selectedEntities[0] == entityId) << "The selected entity is not the one that was double clicked";
  101. selectedEntities.clear();
  102. ToolsApplicationRequestBus::Broadcast(&ToolsApplicationRequests::SetSelectedEntities, selectedEntities);
  103. widget->m_testLabel->SetEntityId(AZ::EntityId(), {});
  104. ToolsApplicationRequestBus::BroadcastResult(selectedEntities, &ToolsApplicationRequests::GetSelectedEntities);
  105. EXPECT_TRUE(selectedEntities.empty()) << "Double clicking on an EntityIdQLabel with an invalid entity ID shouldn't change anything";
  106. EXPECT_TRUE(editorRequestHandler.m_wentToSelectedEntitiesInViewport) << "Double clicking an EntityIdQLabel should result in a GoToSelectedEntitiesInViewports call";
  107. delete entity;
  108. delete widget;
  109. }
  110. }