EntityIdQLineEditTests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/Serialization/SerializeContext.h>
  9. #include <AzTest/AzTest.h>
  10. #include <AzToolsFramework/Application/ToolsApplication.h>
  11. #include <AzToolsFramework/UI/PropertyEditor/EntityPropertyEditor.hxx>
  12. #include <AzToolsFramework/API/EntityPropertyEditorRequestsBus.h>
  13. #include <AzToolsFramework/UI/PropertyEditor/EntityIdQLineEdit.h>
  14. #include <QApplication>
  15. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  16. namespace UnitTest
  17. {
  18. using namespace AZ;
  19. using namespace AzToolsFramework;
  20. // Test widget to store an EntityIdQLineEdit
  21. class TestEntityIdParentWidget
  22. : public QWidget
  23. {
  24. public:
  25. explicit TestEntityIdParentWidget(QWidget* parent = nullptr)
  26. : QWidget(nullptr)
  27. {
  28. AZ_UNUSED(parent);
  29. // ensure TestWidget can intercept and filter any incoming events itself
  30. installEventFilter(this);
  31. m_testLineEdit = new EntityIdQLineEdit(this);
  32. }
  33. EntityIdQLineEdit* m_testLineEdit = nullptr;
  34. };
  35. class EntityIdQLineEditTests
  36. : public ToolsApplicationFixture<>
  37. {
  38. };
  39. TEST_F(EntityIdQLineEditTests, DoubleClickWontSelectInvalidEntity)
  40. {
  41. AZ::Entity* entity = aznew AZ::Entity();
  42. ASSERT_TRUE(entity != nullptr);
  43. entity->Init();
  44. entity->Activate();
  45. AZ::EntityId entityId = entity->GetId();
  46. ASSERT_TRUE(entityId.IsValid());
  47. TestEntityIdParentWidget* widget = new TestEntityIdParentWidget(nullptr);
  48. ASSERT_TRUE(widget != nullptr);
  49. ASSERT_TRUE(widget->m_testLineEdit != nullptr);
  50. // Set a valid EntityId
  51. widget->m_testLineEdit->setFocus();
  52. widget->m_testLineEdit->SetEntityId(entityId, {});
  53. // Simulate double click which will cause the EntityId to be set as selected
  54. QTest::mouseDClick(widget->m_testLineEdit, Qt::LeftButton);
  55. // If successful we expect the label's entity to be selected.
  56. EntityIdList selectedEntities;
  57. ToolsApplicationRequestBus::BroadcastResult(selectedEntities, &ToolsApplicationRequests::GetSelectedEntities);
  58. EXPECT_EQ(selectedEntities.size(), 1) << "Double clicking on an EntityIdQLabel should only select a single entity";
  59. EXPECT_TRUE(selectedEntities[0] == entityId) << "The selected entity is not the one that was double clicked";
  60. selectedEntities.clear();
  61. ToolsApplicationRequestBus::Broadcast(&ToolsApplicationRequests::SetSelectedEntities, selectedEntities);
  62. // Now set an invalid EntityId
  63. widget->m_testLineEdit->SetEntityId(AZ::EntityId(), {});
  64. // Simulate double clicking again, which should not trigger a selection change since the EntityId is invalid
  65. QTest::mouseDClick(widget->m_testLineEdit, Qt::LeftButton);
  66. ToolsApplicationRequestBus::BroadcastResult(selectedEntities, &ToolsApplicationRequests::GetSelectedEntities);
  67. EXPECT_TRUE(selectedEntities.empty()) << "Double clicking on an EntityIdQLabel with an invalid entity ID shouldn't change anything";
  68. delete entity;
  69. delete widget;
  70. }
  71. }