ContainerEntityTests.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/FocusMode/EditorFocusModeFixture.h>
  9. namespace UnitTest
  10. {
  11. TEST_F(EditorFocusModeFixture, ContainerEntityRegister)
  12. {
  13. // Registering an entity is successful.
  14. auto outcome = m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CarEntityName]);
  15. EXPECT_TRUE(outcome.IsSuccess());
  16. // Restore default state for other tests.
  17. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CarEntityName]);
  18. }
  19. TEST_F(EditorFocusModeFixture, ContainerEntityRegisterTwice)
  20. {
  21. // Registering an entity twice fails.
  22. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CarEntityName]);
  23. auto outcome = m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CarEntityName]);
  24. EXPECT_FALSE(outcome.IsSuccess());
  25. // Restore default state for other tests.
  26. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CarEntityName]);
  27. }
  28. TEST_F(EditorFocusModeFixture, ContainerEntityUnregister)
  29. {
  30. // Unregistering a container entity is successful.
  31. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CarEntityName]);
  32. auto outcome = m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CarEntityName]);
  33. EXPECT_TRUE(outcome.IsSuccess());
  34. }
  35. TEST_F(EditorFocusModeFixture, ContainerEntityUnregisterRegularEntity)
  36. {
  37. // Unregistering an entity that was not previously registered fails.
  38. auto outcome = m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CarEntityName]);
  39. EXPECT_FALSE(outcome.IsSuccess());
  40. }
  41. TEST_F(EditorFocusModeFixture, ContainerEntityUnregisterTwice)
  42. {
  43. // Unregistering a container entity twice fails.
  44. auto outcome = m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CarEntityName]);
  45. EXPECT_FALSE(outcome.IsSuccess());
  46. }
  47. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOnRegularEntity)
  48. {
  49. // If a regular entity is passed, IsContainer returns false.
  50. // Note that we use a different entity than the tests above to validate a completely new EntityId.
  51. bool isContainer = m_containerEntityInterface->IsContainer(m_entityMap[SportsCarEntityName]);
  52. EXPECT_FALSE(isContainer);
  53. }
  54. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOnRegisteredContainer)
  55. {
  56. // If a container entity is passed, IsContainer returns true.
  57. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  58. bool isContainer = m_containerEntityInterface->IsContainer(m_entityMap[SportsCarEntityName]);
  59. EXPECT_TRUE(isContainer);
  60. // Restore default state for other tests.
  61. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  62. }
  63. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOnUnRegisteredContainer)
  64. {
  65. // If an entity that was previously a container but was then unregistered is passed, IsContainer returns false.
  66. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  67. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  68. bool isContainer = m_containerEntityInterface->IsContainer(m_entityMap[SportsCarEntityName]);
  69. EXPECT_FALSE(isContainer);
  70. }
  71. TEST_F(EditorFocusModeFixture, ContainerEntitySetContainerOpenOnRegularEntity)
  72. {
  73. // Setting a regular entity to open should return a failure.
  74. auto outcome = m_containerEntityInterface->SetContainerOpen(m_entityMap[StreetEntityName], true);
  75. EXPECT_FALSE(outcome.IsSuccess());
  76. }
  77. TEST_F(EditorFocusModeFixture, ContainerEntitySetContainerOpen)
  78. {
  79. // Set a container entity to open, and verify the operation was successful.
  80. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[StreetEntityName]);
  81. auto outcome = m_containerEntityInterface->SetContainerOpen(m_entityMap[StreetEntityName], true);
  82. EXPECT_TRUE(outcome.IsSuccess());
  83. // Restore default state for other tests.
  84. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[StreetEntityName]);
  85. }
  86. TEST_F(EditorFocusModeFixture, ContainerEntitySetContainerOpenTwice)
  87. {
  88. // Set a container entity to open twice, and verify that does not cause a failure (as intended).
  89. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[StreetEntityName]);
  90. m_containerEntityInterface->SetContainerOpen(m_entityMap[StreetEntityName], true);
  91. auto outcome = m_containerEntityInterface->SetContainerOpen(m_entityMap[StreetEntityName], true);
  92. EXPECT_TRUE(outcome.IsSuccess());
  93. // Restore default state for other tests.
  94. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[StreetEntityName]);
  95. }
  96. TEST_F(EditorFocusModeFixture, ContainerEntitySetContainerClosed)
  97. {
  98. // Set a container entity to closed, and verify the operation was successful.
  99. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[StreetEntityName]);
  100. auto outcome = m_containerEntityInterface->SetContainerOpen(m_entityMap[StreetEntityName], true);
  101. EXPECT_TRUE(outcome.IsSuccess());
  102. // Restore default state for other tests.
  103. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[StreetEntityName]);
  104. }
  105. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOpenOnRegularEntity)
  106. {
  107. // Query open state on a regular entity, and verify it returns true.
  108. // Open containers behave exactly as regular entities, so this is the expected return value.
  109. bool isOpen = m_containerEntityInterface->IsContainerOpen(m_entityMap[CityEntityName]);
  110. EXPECT_TRUE(isOpen);
  111. }
  112. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOpenOnDefaultContainerEntity)
  113. {
  114. // Query open state on a newly registered container entity, and verify it returns false.
  115. // Containers are registered closed by default.
  116. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CityEntityName]);
  117. bool isOpen = m_containerEntityInterface->IsContainerOpen(m_entityMap[CityEntityName]);
  118. EXPECT_FALSE(isOpen);
  119. // Restore default state for other tests.
  120. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CityEntityName]);
  121. }
  122. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOpenOnOpenContainerEntity)
  123. {
  124. // Query open state on a container entity that was opened, and verify it returns true.
  125. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CityEntityName]);
  126. m_containerEntityInterface->SetContainerOpen(m_entityMap[CityEntityName], true);
  127. bool isOpen = m_containerEntityInterface->IsContainerOpen(m_entityMap[CityEntityName]);
  128. EXPECT_TRUE(isOpen);
  129. // Restore default state for other tests.
  130. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CityEntityName]);
  131. }
  132. TEST_F(EditorFocusModeFixture, ContainerEntityIsContainerOpenOnClosedContainerEntity)
  133. {
  134. // Query open state on a container entity that was opened and then closed, and verify it returns false.
  135. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CityEntityName]);
  136. m_containerEntityInterface->SetContainerOpen(m_entityMap[CityEntityName], true);
  137. m_containerEntityInterface->SetContainerOpen(m_entityMap[CityEntityName], false);
  138. bool isOpen = m_containerEntityInterface->IsContainerOpen(m_entityMap[CityEntityName]);
  139. EXPECT_FALSE(isOpen);
  140. // Restore default state for other tests.
  141. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CityEntityName]);
  142. }
  143. TEST_F(EditorFocusModeFixture, ContainerEntityContainerOpenStateIsPreserved)
  144. {
  145. // Register an entity as container, open it, then unregister it.
  146. // When the entity is registered again, the open state should be preserved.
  147. // This behavior is necessary for the system to work alongside Prefab propagation refreshes.
  148. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CityEntityName]);
  149. m_containerEntityInterface->SetContainerOpen(m_entityMap[CityEntityName], true);
  150. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CityEntityName]);
  151. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[CityEntityName]);
  152. bool isOpen = m_containerEntityInterface->IsContainerOpen(m_entityMap[CityEntityName]);
  153. EXPECT_TRUE(isOpen);
  154. // Restore default state for other tests.
  155. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[CityEntityName]);
  156. }
  157. TEST_F(EditorFocusModeFixture, ContainerEntityClearSucceeds)
  158. {
  159. // The Clear function works if no container is registered.
  160. auto outcome = m_containerEntityInterface->Clear(m_editorEntityContextId);
  161. EXPECT_TRUE(outcome.IsSuccess());
  162. }
  163. TEST_F(EditorFocusModeFixture, ContainerEntityClearFailsIfContainersAreStillRegistered)
  164. {
  165. // The Clear function fails if a container is registered.
  166. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  167. auto outcome = m_containerEntityInterface->Clear(m_editorEntityContextId);
  168. EXPECT_FALSE(outcome.IsSuccess());
  169. // Restore default state for other tests.
  170. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  171. }
  172. TEST_F(EditorFocusModeFixture, ContainerEntityClearSucceedsIfContainersAreUnregistered)
  173. {
  174. // The Clear function fails if a container is registered.
  175. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  176. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  177. auto outcome = m_containerEntityInterface->Clear(m_editorEntityContextId);
  178. EXPECT_TRUE(outcome.IsSuccess());
  179. }
  180. TEST_F(EditorFocusModeFixture, ContainerEntityClearDeletesPreservedOpenStates)
  181. {
  182. // Register an entity as container, open it, unregister it, then call clear.
  183. // When the entity is registered again, the open state should not be preserved.
  184. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  185. m_containerEntityInterface->SetContainerOpen(m_entityMap[Passenger1EntityName], true);
  186. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  187. m_containerEntityInterface->Clear(m_editorEntityContextId);
  188. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  189. bool isOpen = m_containerEntityInterface->IsContainerOpen(m_entityMap[Passenger1EntityName]);
  190. EXPECT_FALSE(isOpen);
  191. // Restore default state for other tests.
  192. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[Passenger1EntityName]);
  193. }
  194. TEST_F(EditorFocusModeFixture, ContainerEntityFindHighestSelectableEntityWithNoContainers)
  195. {
  196. // When no containers are in the way, the function will just return the entityId that was passed to it.
  197. AZ::EntityId selectedEntityId = m_containerEntityInterface->FindHighestSelectableEntity(m_entityMap[Passenger2EntityName]);
  198. EXPECT_EQ(selectedEntityId, m_entityMap[Passenger2EntityName]);
  199. }
  200. TEST_F(EditorFocusModeFixture, ContainerEntityFindHighestSelectableEntityWithClosedContainer)
  201. {
  202. // If a closed container is an ancestor of the queried entity, the closed container is selected.
  203. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[SportsCarEntityName]); // Containers are closed by default
  204. AZ::EntityId selectedEntityId = m_containerEntityInterface->FindHighestSelectableEntity(m_entityMap[Passenger2EntityName]);
  205. EXPECT_EQ(selectedEntityId, m_entityMap[SportsCarEntityName]);
  206. // Restore default state for other tests.
  207. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  208. }
  209. TEST_F(EditorFocusModeFixture, ContainerEntityFindHighestSelectableEntityWithOpenContainer)
  210. {
  211. // If an open container is an ancestor of the queried entity, it is ignored.
  212. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  213. m_containerEntityInterface->SetContainerOpen(m_entityMap[SportsCarEntityName], true);
  214. AZ::EntityId selectedEntityId = m_containerEntityInterface->FindHighestSelectableEntity(m_entityMap[Passenger2EntityName]);
  215. EXPECT_EQ(selectedEntityId, m_entityMap[SportsCarEntityName]);
  216. // Restore default state for other tests.
  217. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  218. }
  219. TEST_F(EditorFocusModeFixture, ContainerEntityFindHighestSelectableEntityWithMultipleClosedContainers)
  220. {
  221. // If multiple closed containers are ancestors of the queried entity, the highest closed container is selected.
  222. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[StreetEntityName]);
  223. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  224. AZ::EntityId selectedEntityId = m_containerEntityInterface->FindHighestSelectableEntity(m_entityMap[Passenger2EntityName]);
  225. EXPECT_EQ(selectedEntityId, m_entityMap[StreetEntityName]);
  226. // Restore default state for other tests.
  227. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[StreetEntityName]);
  228. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  229. }
  230. TEST_F(EditorFocusModeFixture, ContainerEntityFindHighestSelectableEntityWithMultipleContainers)
  231. {
  232. // If multiple containers are ancestors of the queried entity, the highest closed container is selected.
  233. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[StreetEntityName]);
  234. m_containerEntityInterface->RegisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  235. m_containerEntityInterface->SetContainerOpen(m_entityMap[StreetEntityName], true);
  236. AZ::EntityId selectedEntityId = m_containerEntityInterface->FindHighestSelectableEntity(m_entityMap[Passenger2EntityName]);
  237. EXPECT_EQ(selectedEntityId, m_entityMap[StreetEntityName]);
  238. // Restore default state for other tests.
  239. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[StreetEntityName]);
  240. m_containerEntityInterface->UnregisterEntityAsContainer(m_entityMap[SportsCarEntityName]);
  241. }
  242. } // namespace UnitTest