EditorEntityContextComponentTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzCore/UserSettings/UserSettingsComponent.h>
  11. #include <AzToolsFramework/Entity/EditorEntityContextComponent.h>
  12. #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
  13. namespace AzToolsFramework
  14. {
  15. class EditorEntityContextComponentTests
  16. : public UnitTest::LeakDetectionFixture
  17. {
  18. protected:
  19. void SetUp() override
  20. {
  21. AZ::ComponentApplication::StartupParameters startupParameters;
  22. startupParameters.m_loadSettingsRegistry = false;
  23. m_app.Start(m_descriptor, startupParameters);
  24. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  25. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  26. // in the unit tests.
  27. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  28. }
  29. void TearDown() override
  30. {
  31. m_app.Stop();
  32. }
  33. UnitTest::ToolsTestApplication m_app{ "EditorEntityContextComponent" }; // Shorted because Settings Registry specializations
  34. // are 32 characters max.
  35. AZ::ComponentApplication::Descriptor m_descriptor;
  36. };
  37. TEST_F(EditorEntityContextComponentTests, EditorEntityContextTests_CreateEditorEntity_CreatesValidEntity)
  38. {
  39. AZStd::string entityName("TestName");
  40. AZ::EntityId createdEntityId;
  41. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  42. createdEntityId,
  43. &AzToolsFramework::EditorEntityContextRequests::CreateNewEditorEntity,
  44. entityName.c_str());
  45. EXPECT_TRUE(createdEntityId.IsValid());
  46. AZ::Entity* createdEntity = nullptr;
  47. AZ::ComponentApplicationBus::BroadcastResult(createdEntity, &AZ::ComponentApplicationRequests::FindEntity, createdEntityId);
  48. EXPECT_NE(createdEntity, nullptr);
  49. EXPECT_EQ(entityName.compare(createdEntity->GetName()), 0);
  50. EXPECT_EQ(createdEntity->GetId(), createdEntityId);
  51. }
  52. TEST_F(EditorEntityContextComponentTests, EditorEntityContextTests_CreateEditorEntityWithValidId_CreatesValidEntity)
  53. {
  54. AZ::EntityId validId(AZ::Entity::MakeId());
  55. EXPECT_TRUE(validId.IsValid());
  56. AZStd::string entityName("TestName");
  57. AZ::EntityId createdEntityId;
  58. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  59. createdEntityId,
  60. &AzToolsFramework::EditorEntityContextRequestBus::Events::CreateNewEditorEntityWithId,
  61. entityName.c_str(),
  62. validId);
  63. EXPECT_TRUE(createdEntityId.IsValid());
  64. EXPECT_EQ(createdEntityId, validId);
  65. AZ::Entity* createdEntity = nullptr;
  66. AZ::ComponentApplicationBus::BroadcastResult(createdEntity, &AZ::ComponentApplicationRequests::FindEntity, createdEntityId);
  67. EXPECT_NE(createdEntity, nullptr);
  68. EXPECT_EQ(entityName.compare(createdEntity->GetName()), 0);
  69. EXPECT_EQ(createdEntity->GetId(), validId);
  70. }
  71. TEST_F(EditorEntityContextComponentTests, EditorEntityContextTests_CreateEditorEntityWithInvalidId_NoEntityCreated)
  72. {
  73. AZ::EntityId invalidId;
  74. EXPECT_FALSE(invalidId.IsValid());
  75. AZStd::string entityName("TestName");
  76. AZ::EntityId createdEntityId;
  77. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  78. createdEntityId,
  79. &AzToolsFramework::EditorEntityContextRequestBus::Events::CreateNewEditorEntityWithId,
  80. entityName.c_str(),
  81. invalidId);
  82. EXPECT_FALSE(createdEntityId.IsValid());
  83. AZ::Entity* createdEntity = nullptr;
  84. AZ::ComponentApplicationBus::BroadcastResult(createdEntity, &AZ::ComponentApplicationRequests::FindEntity, createdEntityId);
  85. EXPECT_EQ(createdEntity, nullptr);
  86. }
  87. TEST_F(EditorEntityContextComponentTests, EditorEntityContextTests_CreateEditorEntityWithInUseId_NoEntityCreated)
  88. {
  89. // Create an entity so we can grab an in-use entity ID.
  90. AZStd::string entityName("TestName");
  91. AZ::EntityId createdEntityId;
  92. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  93. createdEntityId,
  94. &AzToolsFramework::EditorEntityContextRequestBus::Events::CreateNewEditorEntity,
  95. entityName.c_str());
  96. EXPECT_TRUE(createdEntityId.IsValid());
  97. // Attempt to create another entity with the same ID, and verify this call fails.
  98. AZ::EntityId secondEntityId;
  99. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  100. secondEntityId,
  101. &AzToolsFramework::EditorEntityContextRequestBus::Events::CreateNewEditorEntityWithId,
  102. entityName.c_str(),
  103. createdEntityId);
  104. EXPECT_FALSE(secondEntityId.IsValid());
  105. }
  106. }