ReadOnlyEntityFixture.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <Tests/Entity/ReadOnly/ReadOnlyEntityFixture.h>
  9. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  10. namespace AzToolsFramework
  11. {
  12. void ReadOnlyEntityFixture::SetUpEditorFixtureImpl()
  13. {
  14. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  15. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  16. // in the unit tests.
  17. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  18. m_readOnlyEntityPublicInterface = AZ::Interface<ReadOnlyEntityPublicInterface>::Get();
  19. ASSERT_TRUE(m_readOnlyEntityPublicInterface != nullptr);
  20. GenerateTestHierarchy();
  21. }
  22. void ReadOnlyEntityFixture::TearDownEditorFixtureImpl()
  23. {
  24. }
  25. void ReadOnlyEntityFixture::GenerateTestHierarchy()
  26. {
  27. /*
  28. * Root
  29. * |_ Child
  30. * |_ GrandChild1
  31. * |_ GrandChild2
  32. */
  33. m_entityMap[RootEntityName] = CreateEditorEntity(RootEntityName, AZ::EntityId());
  34. m_entityMap[ChildEntityName] = CreateEditorEntity(ChildEntityName, m_entityMap[RootEntityName]);
  35. m_entityMap[GrandChild1EntityName] = CreateEditorEntity(GrandChild1EntityName, m_entityMap[ChildEntityName]);
  36. m_entityMap[GrandChild2EntityName] = CreateEditorEntity(GrandChild2EntityName, m_entityMap[ChildEntityName]);
  37. }
  38. AZ::EntityId ReadOnlyEntityFixture::CreateEditorEntity(const char* name, AZ::EntityId parentId)
  39. {
  40. AZ::Entity* entity = nullptr;
  41. UnitTest::CreateDefaultEditorEntity(name, &entity);
  42. // Parent
  43. AZ::TransformBus::Event(entity->GetId(), &AZ::TransformInterface::SetParent, parentId);
  44. return entity->GetId();
  45. }
  46. ReadOnlyHandlerAlwaysTrue::ReadOnlyHandlerAlwaysTrue()
  47. {
  48. auto editorEntityContextId = AzFramework::EntityContextId::CreateNull();
  49. EditorEntityContextRequestBus::BroadcastResult(editorEntityContextId, &EditorEntityContextRequests::GetEditorEntityContextId);
  50. ReadOnlyEntityQueryRequestBus::Handler::BusConnect(editorEntityContextId);
  51. }
  52. ReadOnlyHandlerAlwaysTrue::~ReadOnlyHandlerAlwaysTrue()
  53. {
  54. ReadOnlyEntityQueryRequestBus::Handler::BusDisconnect();
  55. if (auto readOnlyEntityQueryInterface = AZ::Interface<ReadOnlyEntityQueryInterface>::Get())
  56. {
  57. readOnlyEntityQueryInterface->RefreshReadOnlyStateForAllEntities();
  58. }
  59. }
  60. void ReadOnlyHandlerAlwaysTrue::IsReadOnly([[maybe_unused]] const AZ::EntityId& entityId, bool& isReadOnly)
  61. {
  62. isReadOnly = true;
  63. }
  64. ReadOnlyHandlerAlwaysFalse::ReadOnlyHandlerAlwaysFalse()
  65. {
  66. auto editorEntityContextId = AzFramework::EntityContextId::CreateNull();
  67. EditorEntityContextRequestBus::BroadcastResult(editorEntityContextId, &EditorEntityContextRequests::GetEditorEntityContextId);
  68. ReadOnlyEntityQueryRequestBus::Handler::BusConnect(editorEntityContextId);
  69. }
  70. ReadOnlyHandlerAlwaysFalse::~ReadOnlyHandlerAlwaysFalse()
  71. {
  72. ReadOnlyEntityQueryRequestBus::Handler::BusDisconnect();
  73. if (auto readOnlyEntityQueryInterface = AZ::Interface<ReadOnlyEntityQueryInterface>::Get())
  74. {
  75. readOnlyEntityQueryInterface->RefreshReadOnlyStateForAllEntities();
  76. }
  77. }
  78. ReadOnlyHandlerEntityId::ReadOnlyHandlerEntityId(AZ::EntityId entityId)
  79. : m_entityId(entityId)
  80. {
  81. auto editorEntityContextId = AzFramework::EntityContextId::CreateNull();
  82. EditorEntityContextRequestBus::BroadcastResult(editorEntityContextId, &EditorEntityContextRequests::GetEditorEntityContextId);
  83. ReadOnlyEntityQueryRequestBus::Handler::BusConnect(editorEntityContextId);
  84. }
  85. ReadOnlyHandlerEntityId::~ReadOnlyHandlerEntityId()
  86. {
  87. ReadOnlyEntityQueryRequestBus::Handler::BusDisconnect();
  88. if (auto readOnlyEntityQueryInterface = AZ::Interface<ReadOnlyEntityQueryInterface>::Get())
  89. {
  90. readOnlyEntityQueryInterface->RefreshReadOnlyStateForAllEntities();
  91. }
  92. }
  93. void ReadOnlyHandlerEntityId::IsReadOnly(const AZ::EntityId& entityId, bool& isReadOnly)
  94. {
  95. if (entityId == m_entityId)
  96. {
  97. isReadOnly = true;
  98. }
  99. }
  100. }