ReadOnlyEntityTests.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace AzToolsFramework
  10. {
  11. TEST_F(ReadOnlyEntityFixture, NoHandlerEntityIsNotReadOnlyByDefault)
  12. {
  13. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  14. }
  15. TEST_F(ReadOnlyEntityFixture, SingleHandlerEntityIsReadOnly)
  16. {
  17. // Create a handler that sets all entities to read-only.
  18. ReadOnlyHandlerAlwaysTrue alwaysTrueHandler;
  19. // All entities should be marked read-only now.
  20. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[RootEntityName]));
  21. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  22. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild1EntityName]));
  23. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild2EntityName]));
  24. }
  25. TEST_F(ReadOnlyEntityFixture, SingleHandlerEntityIsNotReadOnly)
  26. {
  27. // Create a handler that sets all entities to read-only.
  28. ReadOnlyHandlerAlwaysFalse alwaysFalseHandler;
  29. // All entities should not be marked read-only now.
  30. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[RootEntityName]));
  31. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  32. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild1EntityName]));
  33. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild2EntityName]));
  34. }
  35. TEST_F(ReadOnlyEntityFixture, SingleHandlerWithLogic)
  36. {
  37. // Create a handler that sets just the child entity to read-only.
  38. ReadOnlyHandlerEntityId entityIdHandler(m_entityMap[ChildEntityName]);
  39. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[RootEntityName]));
  40. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  41. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild1EntityName]));
  42. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild2EntityName]));
  43. }
  44. TEST_F(ReadOnlyEntityFixture, TwoHandlersCanOverlap)
  45. {
  46. // Create two handlers that set different entities to read-only.
  47. ReadOnlyHandlerEntityId entityIdHandler1(m_entityMap[ChildEntityName]);
  48. ReadOnlyHandlerEntityId entityIdHandler2(m_entityMap[GrandChild2EntityName]);
  49. // Both entities should be marked as read-only, while others aren't.
  50. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[RootEntityName]));
  51. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  52. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild1EntityName]));
  53. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[GrandChild2EntityName]));
  54. }
  55. TEST_F(ReadOnlyEntityFixture, EnsureCacheIsRefreshedCorrectly)
  56. {
  57. // Verify the child entity is not marked as read-only
  58. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  59. // Create a handler that sets the child entity to read-only.
  60. ReadOnlyHandlerEntityId entityIdHandler(m_entityMap[ChildEntityName]);
  61. // Communicate to the ReadOnlyEntitySystemComponent that the read-only state for the child entity may have changed.
  62. // Note that this operation would usually be executed by the handler, hence the Query interface call.
  63. if (auto readOnlyEntityQueryInterface = AZ::Interface<ReadOnlyEntityQueryInterface>::Get())
  64. {
  65. readOnlyEntityQueryInterface->RefreshReadOnlyState({ m_entityMap[ChildEntityName] });
  66. }
  67. // Verify the child entity is marked as read-only
  68. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  69. }
  70. TEST_F(ReadOnlyEntityFixture, EnsureCacheIsClearedCorrectly)
  71. {
  72. {
  73. // Create a handler that sets the child entity to read-only.
  74. ReadOnlyHandlerEntityId entityIdHandler(m_entityMap[ChildEntityName]);
  75. // Verify the child entity is marked as read-only
  76. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  77. }
  78. // When the handler goes out of scope, it calls RefreshReadOnlyStateForAllEntities and refreshes the cache.
  79. // Verify the child entity is no longer marked as read-only
  80. EXPECT_FALSE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  81. }
  82. TEST_F(ReadOnlyEntityFixture, EnsureCacheIsClearedCorrectlyEvenIfUnchanged)
  83. {
  84. // Create a handler that sets all entities to read-only.
  85. ReadOnlyHandlerAlwaysTrue alwaysTrueHandler;
  86. {
  87. // Create a handler that sets the child entity to read-only.
  88. ReadOnlyHandlerEntityId entityIdHandler(m_entityMap[ChildEntityName]);
  89. // Verify the child entity is marked as read-only
  90. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  91. }
  92. // When the handler goes out of scope, it calls RefreshReadOnlyStateForAllEntities and refreshes the cache.
  93. // Verify the child entity is still marked as read-only
  94. EXPECT_TRUE(m_readOnlyEntityPublicInterface->IsReadOnly(m_entityMap[ChildEntityName]));
  95. }
  96. }