EntityTests.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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/SerializeContextFixture.h>
  9. #include <AzCore/Asset/AssetManager.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Serialization/Utils.h>
  13. namespace UnitTest
  14. {
  15. // Used to verify that components are serialized on entities in a stable order, smallest to largest.
  16. // This component has the UUID with the smallest value.
  17. class SortOrderTestFirstComponent
  18. : public AZ::Component
  19. {
  20. public:
  21. AZ_COMPONENT(SortOrderTestFirstComponent, "{00000000-0000-0000-0000-000000000010}");
  22. ///////////////////////////////////////
  23. // Component overrides
  24. void Activate() override { }
  25. void Deactivate() override { }
  26. ///////////////////////////////////////
  27. static void Reflect(AZ::ReflectContext* reflection)
  28. {
  29. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  30. if (serializeContext)
  31. {
  32. serializeContext->Class<SortOrderTestFirstComponent, AZ::Component>();
  33. }
  34. }
  35. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  36. {
  37. services.push_back(AZ_CRC("SortOrderTestFirstService"));
  38. }
  39. };
  40. // This component has the UUID with the second smallest value.
  41. class SortOrderTestSecondComponent
  42. : public AZ::Component
  43. {
  44. public:
  45. AZ_COMPONENT(SortOrderTestSecondComponent, "{00000000-0000-0000-0000-000000000020}");
  46. ///////////////////////////////////////
  47. // Component overrides
  48. void Activate() override { }
  49. void Deactivate() override { }
  50. ///////////////////////////////////////
  51. static void Reflect(AZ::ReflectContext* reflection)
  52. {
  53. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  54. if (serializeContext)
  55. {
  56. serializeContext->Class<SortOrderTestSecondComponent, AZ::Component>();
  57. }
  58. }
  59. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  60. {
  61. services.push_back(AZ_CRC("SortOrderTestSecondService"));
  62. }
  63. };
  64. // This component has the UUID with the largest value.
  65. class SortOrderTestThirdComponent
  66. : public AZ::Component
  67. {
  68. public:
  69. AZ_COMPONENT(SortOrderTestThirdComponent, "{00000000-0000-0000-0000-000000000030}");
  70. ///////////////////////////////////////
  71. // Component overrides
  72. void Activate() override { }
  73. void Deactivate() override { }
  74. ///////////////////////////////////////
  75. static void Reflect(AZ::ReflectContext* reflection)
  76. {
  77. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  78. if (serializeContext)
  79. {
  80. serializeContext->Class<SortOrderTestThirdComponent, AZ::Component>();
  81. }
  82. }
  83. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  84. {
  85. services.push_back(AZ_CRC("SortOrderTestThirdService"));
  86. }
  87. };
  88. // Used to verify that components are always sorted after their dependencies.
  89. class SortOrderTestRequiresFirstComponent
  90. : public AZ::Component
  91. {
  92. public:
  93. // Purposely give this a UUID lower than its dependency.
  94. AZ_COMPONENT(SortOrderTestRequiresFirstComponent, "{00000000-0000-0000-0000-000000000001}");
  95. ///////////////////////////////////////
  96. // Component overrides
  97. void Activate() override { }
  98. void Deactivate() override { }
  99. ///////////////////////////////////////
  100. static void Reflect(AZ::ReflectContext* reflection)
  101. {
  102. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  103. if (serializeContext)
  104. {
  105. serializeContext->Class<SortOrderTestRequiresFirstComponent, AZ::Component>();
  106. }
  107. }
  108. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  109. {
  110. services.push_back(AZ_CRC("SortOrderTestFirstService"));
  111. }
  112. };
  113. // Used to verify that components are always sorted after their dependencies.
  114. class SortOrderTestRequiresSecondComponent
  115. : public AZ::Component
  116. {
  117. public:
  118. // Purposely give this a UUID lower than its dependency.
  119. AZ_COMPONENT(SortOrderTestRequiresSecondComponent, "{00000000-0000-0000-0000-000000000002}");
  120. ///////////////////////////////////////
  121. // Component overrides
  122. void Activate() override { }
  123. void Deactivate() override { }
  124. ///////////////////////////////////////
  125. static void Reflect(AZ::ReflectContext* reflection)
  126. {
  127. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  128. if (serializeContext)
  129. {
  130. serializeContext->Class<SortOrderTestRequiresSecondComponent, AZ::Component>();
  131. }
  132. }
  133. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  134. {
  135. services.push_back(AZ_CRC("SortOrderTestSecondService"));
  136. }
  137. };
  138. // Used to verify that components are always sorted after their dependencies.
  139. class SortOrderTestRequiresSecondAndThirdComponent
  140. : public AZ::Component
  141. {
  142. public:
  143. // Purposely give this a UUID between its dependencies.
  144. AZ_COMPONENT(SortOrderTestRequiresSecondAndThirdComponent, "{00000000-0000-0000-0000-000000000025}");
  145. ///////////////////////////////////////
  146. // Component overrides
  147. void Activate() override { }
  148. void Deactivate() override { }
  149. ///////////////////////////////////////
  150. static void Reflect(AZ::ReflectContext* reflection)
  151. {
  152. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  153. if (serializeContext)
  154. {
  155. serializeContext->Class<SortOrderTestRequiresSecondAndThirdComponent, AZ::Component>();
  156. }
  157. }
  158. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  159. {
  160. services.push_back(AZ_CRC("SortOrderTestThirdService"));
  161. services.push_back(AZ_CRC("SortOrderTestSecondService"));
  162. }
  163. };
  164. // Used to verify that components that do not provide services are sorted after components that do.
  165. class SortOrderTestNoService
  166. : public AZ::Component
  167. {
  168. public:
  169. // Purposely give this a UUID lower than the test components that provide services.
  170. AZ_COMPONENT(SortOrderTestNoService, "{00000000-0000-0000-0000-000000000003}");
  171. ///////////////////////////////////////
  172. // Component overrides
  173. void Activate() override { }
  174. void Deactivate() override { }
  175. ///////////////////////////////////////
  176. static void Reflect(AZ::ReflectContext* reflection)
  177. {
  178. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  179. if (serializeContext)
  180. {
  181. serializeContext->Class<SortOrderTestNoService, AZ::Component>();
  182. }
  183. }
  184. };
  185. // This component wraps the component base class, like GenericComponentWrapper.
  186. // GenericComponentWrapper is used in the editor for components that don't have specific editor representations.
  187. // Its usage depends on other editor systems being setup, so this is meant to simulate its usage.
  188. class SortOrderTestComponentWrapper
  189. : public AZ::Component
  190. {
  191. public:
  192. AZ_CLASS_ALLOCATOR(SortOrderTestComponentWrapper, AZ::SystemAllocator);
  193. AZ_RTTI(SortOrderTestComponentWrapper, "{00000000-0000-0000-0000-000000000011}", AZ::Component);
  194. SortOrderTestComponentWrapper() { }
  195. SortOrderTestComponentWrapper(AZ::Component* wrappedComponent)
  196. {
  197. if (wrappedComponent)
  198. {
  199. m_id = wrappedComponent->GetId();
  200. m_wrappedComponent = wrappedComponent;
  201. }
  202. }
  203. ~SortOrderTestComponentWrapper() override
  204. {
  205. delete m_wrappedComponent;
  206. m_wrappedComponent = nullptr;
  207. }
  208. AZ::Component* GetWrappedComponent() const { return m_wrappedComponent; }
  209. ///////////////////////////////////////
  210. // Component overrides
  211. void Activate() override { }
  212. void Deactivate() override { }
  213. AZ::TypeId GetUnderlyingComponentType() const override
  214. {
  215. if (m_wrappedComponent)
  216. {
  217. return m_wrappedComponent->RTTI_GetType();
  218. }
  219. return RTTI_GetType();
  220. }
  221. ///////////////////////////////////////
  222. static void Reflect(AZ::ReflectContext* reflection)
  223. {
  224. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  225. if (serializeContext)
  226. {
  227. serializeContext->Class<SortOrderTestComponentWrapper, AZ::Component>()
  228. ->Field("m_wrappedComponent", &SortOrderTestComponentWrapper::m_wrappedComponent);
  229. }
  230. }
  231. static AZ::ComponentDescriptor* CreateDescriptor();
  232. // Provide a service so that this component sorts with all other components that provide services.
  233. // This is used to test that the wrapped component sorts with what it wraps, and it is used
  234. // here to wrap components that provide services.
  235. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  236. {
  237. services.push_back(AZ_CRC("TestWrapperService"));
  238. }
  239. private:
  240. AZ::Component* m_wrappedComponent = nullptr;
  241. };
  242. // This is based on GenericComponentWrapperDescriptor, and it is meant to pass through services.
  243. class SortOrderComponentWrapperDescriptor
  244. : public AZ::ComponentDescriptorHelper<SortOrderTestComponentWrapper>
  245. {
  246. public:
  247. AZ_CLASS_ALLOCATOR(SortOrderComponentWrapperDescriptor, AZ::SystemAllocator);
  248. AZ_TYPE_INFO(SortOrderComponentWrapperDescriptor, "{58A6544E-9476-4A93-AB6E-768B7326494B}");
  249. AZ::ComponentDescriptor* GetTemplateDescriptor(const AZ::Component* instance) const
  250. {
  251. AZ::ComponentDescriptor* templateDescriptor = nullptr;
  252. const SortOrderTestComponentWrapper* wrapper = azrtti_cast<const SortOrderTestComponentWrapper*>(instance);
  253. if (wrapper && wrapper->GetWrappedComponent())
  254. {
  255. AZ::ComponentDescriptorBus::EventResult(
  256. templateDescriptor,
  257. wrapper->GetWrappedComponent()->RTTI_GetType(),
  258. &AZ::ComponentDescriptorBus::Events::GetDescriptor);
  259. }
  260. return templateDescriptor;
  261. }
  262. void Reflect(AZ::ReflectContext* reflection) const override
  263. {
  264. SortOrderTestComponentWrapper::Reflect(reflection);
  265. }
  266. void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided, const AZ::Component* instance) const override
  267. {
  268. const AZ::ComponentDescriptor* templateDescriptor = GetTemplateDescriptor(instance);
  269. if (templateDescriptor)
  270. {
  271. templateDescriptor->GetProvidedServices(provided, instance);
  272. }
  273. }
  274. void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent, const AZ::Component* instance) const override
  275. {
  276. const AZ::ComponentDescriptor* templateDescriptor = GetTemplateDescriptor(instance);
  277. if (templateDescriptor)
  278. {
  279. templateDescriptor->GetDependentServices(dependent, instance);
  280. }
  281. }
  282. void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required, const AZ::Component* instance) const override
  283. {
  284. const AZ::ComponentDescriptor* templateDescriptor = GetTemplateDescriptor(instance);
  285. if (templateDescriptor)
  286. {
  287. templateDescriptor->GetRequiredServices(required, instance);
  288. }
  289. }
  290. void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible, const AZ::Component* instance) const override
  291. {
  292. const AZ::ComponentDescriptor* templateDescriptor = GetTemplateDescriptor(instance);
  293. if (templateDescriptor)
  294. {
  295. templateDescriptor->GetIncompatibleServices(incompatible, instance);
  296. }
  297. }
  298. };
  299. AZ::ComponentDescriptor* SortOrderTestComponentWrapper::CreateDescriptor()
  300. {
  301. AZ::ComponentDescriptor* descriptor = nullptr;
  302. AZ::ComponentDescriptorBus::EventResult(
  303. descriptor, SortOrderTestComponentWrapper::RTTI_Type(), &AZ::ComponentDescriptorBus::Events::GetDescriptor);
  304. return descriptor ? descriptor : aznew SortOrderComponentWrapperDescriptor();
  305. }
  306. // Used to verify that components that accidentally provide the same service twice initialize correctly and ignore the duplication.
  307. class DuplicateProvidedServiceComponent
  308. : public AZ::Component
  309. {
  310. public:
  311. AZ_COMPONENT(DuplicateProvidedServiceComponent, "{D39D65A9-6A26-40A6-99FB-586E3AC14B56}");
  312. ///////////////////////////////////////
  313. // Component overrides
  314. void Activate() override { }
  315. void Deactivate() override { }
  316. ///////////////////////////////////////
  317. static void Reflect(AZ::ReflectContext* reflection)
  318. {
  319. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  320. if (serializeContext)
  321. {
  322. serializeContext->Class<DuplicateProvidedServiceComponent, AZ::Component>();
  323. }
  324. }
  325. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  326. {
  327. services.push_back(AZ_CRC("DuplicatedService"));
  328. services.push_back(AZ_CRC("DuplicatedService"));
  329. }
  330. };
  331. // Used to verify that components that accidentally provide the same service twice initialize correctly and ignore the duplication.
  332. // Previously, a crash occured when a component depended on a service that was provided multiple times by another component.
  333. class DependsOnDuplicateProvidedServiceComponent
  334. : public AZ::Component
  335. {
  336. public:
  337. AZ_COMPONENT(DependsOnDuplicateProvidedServiceComponent, "{1B78B608-AECB-44CE-9060-53A1998AB1D4}");
  338. ///////////////////////////////////////
  339. // Component overrides
  340. void Activate() override { }
  341. void Deactivate() override { }
  342. ///////////////////////////////////////
  343. static void Reflect(AZ::ReflectContext* reflection)
  344. {
  345. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  346. if (serializeContext)
  347. {
  348. serializeContext->Class<DependsOnDuplicateProvidedServiceComponent, AZ::Component>();
  349. }
  350. }
  351. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  352. {
  353. services.push_back(AZ_CRC("DuplicatedService"));
  354. }
  355. };
  356. // Used to verify that components that accidentally provide the same service twice initialize correctly and ignore the duplication.
  357. // Previously, a crash occured when a component required a service that was provided multiple times by another component.
  358. class RequiresDuplicateProvidedServiceComponent
  359. : public AZ::Component
  360. {
  361. public:
  362. AZ_COMPONENT(RequiresDuplicateProvidedServiceComponent, "{9AACE495-0E45-4DF0-B362-43CE12AE2F33}");
  363. ///////////////////////////////////////
  364. // Component overrides
  365. void Activate() override { }
  366. void Deactivate() override { }
  367. ///////////////////////////////////////
  368. static void Reflect(AZ::ReflectContext* reflection)
  369. {
  370. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  371. if (serializeContext)
  372. {
  373. serializeContext->Class<RequiresDuplicateProvidedServiceComponent, AZ::Component>();
  374. }
  375. }
  376. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  377. {
  378. services.push_back(AZ_CRC("DuplicatedService"));
  379. }
  380. };
  381. class EntityTests : public UnitTest::SerializeContextFixture
  382. {
  383. void SetUp() override
  384. {
  385. SerializeContextFixture::SetUp();
  386. m_sortFirstDescriptor = SortOrderTestFirstComponent::CreateDescriptor();
  387. m_sortFirstDescriptor->Reflect(m_serializeContext);
  388. m_sortSecondDescriptor = SortOrderTestSecondComponent::CreateDescriptor();
  389. m_sortSecondDescriptor->Reflect(m_serializeContext);
  390. m_sortThirdDescriptor = SortOrderTestThirdComponent::CreateDescriptor();
  391. m_sortThirdDescriptor->Reflect(m_serializeContext);
  392. m_sortFirstDependencyDescriptor = SortOrderTestRequiresFirstComponent::CreateDescriptor();
  393. m_sortFirstDependencyDescriptor->Reflect(m_serializeContext);
  394. m_sortSecondDependencyDescriptor = SortOrderTestRequiresSecondComponent::CreateDescriptor();
  395. m_sortSecondDependencyDescriptor->Reflect(m_serializeContext);
  396. m_sortSecondAndThirdDependencyDescriptor = SortOrderTestRequiresSecondAndThirdComponent::CreateDescriptor();
  397. m_sortSecondAndThirdDependencyDescriptor->Reflect(m_serializeContext);
  398. m_sortWrapperDescriptor = SortOrderTestComponentWrapper::CreateDescriptor();
  399. m_sortWrapperDescriptor->Reflect(m_serializeContext);
  400. m_sortNoServiceDescriptor = SortOrderTestNoService::CreateDescriptor();
  401. m_sortNoServiceDescriptor->Reflect(m_serializeContext);
  402. m_duplicateProvidedServiceComponentDescriptor = DuplicateProvidedServiceComponent::CreateDescriptor();
  403. m_duplicateProvidedServiceComponentDescriptor->Reflect(m_serializeContext);
  404. m_dependsOnDuplicateServiceComponentDescriptor = DependsOnDuplicateProvidedServiceComponent::CreateDescriptor();
  405. m_dependsOnDuplicateServiceComponentDescriptor->Reflect(m_serializeContext);
  406. m_requiresDuplicateServiceComponentDescriptor = RequiresDuplicateProvidedServiceComponent::CreateDescriptor();
  407. m_requiresDuplicateServiceComponentDescriptor->Reflect(m_serializeContext);
  408. AZ::Entity::Reflect(m_serializeContext);
  409. }
  410. void TearDown() override
  411. {
  412. m_requiresDuplicateServiceComponentDescriptor->ReleaseDescriptor();
  413. m_dependsOnDuplicateServiceComponentDescriptor->ReleaseDescriptor();
  414. m_duplicateProvidedServiceComponentDescriptor->ReleaseDescriptor();
  415. m_sortNoServiceDescriptor->ReleaseDescriptor();
  416. m_sortWrapperDescriptor->ReleaseDescriptor();
  417. m_sortSecondAndThirdDependencyDescriptor->ReleaseDescriptor();
  418. m_sortSecondDependencyDescriptor->ReleaseDescriptor();
  419. m_sortFirstDependencyDescriptor->ReleaseDescriptor();
  420. m_sortThirdDescriptor->ReleaseDescriptor();
  421. m_sortSecondDescriptor->ReleaseDescriptor();
  422. m_sortFirstDescriptor->ReleaseDescriptor();
  423. UnitTest::SerializeContextFixture::TearDown();
  424. }
  425. protected:
  426. // Make sure the component list is sorted, and has the expected number of components.
  427. void ValidateComponentList(const AZ::Entity& entity1, const AZ::Entity& entity2, int expectedComponentListSize)
  428. {
  429. const AZ::Entity::ComponentArrayType& components1 = entity1.GetComponents();
  430. const AZ::Entity::ComponentArrayType& components2 = entity2.GetComponents();
  431. EXPECT_EQ(components1.size(), expectedComponentListSize);
  432. EXPECT_EQ(components2.size(), expectedComponentListSize);
  433. for (int i = 0; i < expectedComponentListSize; ++i)
  434. {
  435. EXPECT_EQ(components1[i]->GetUnderlyingComponentType(), components2[i]->GetUnderlyingComponentType());
  436. }
  437. }
  438. AZ::ComponentDescriptor* m_sortFirstDescriptor = nullptr;
  439. AZ::ComponentDescriptor* m_sortSecondDescriptor = nullptr;
  440. AZ::ComponentDescriptor* m_sortThirdDescriptor = nullptr;
  441. AZ::ComponentDescriptor* m_sortFirstDependencyDescriptor = nullptr;
  442. AZ::ComponentDescriptor* m_sortSecondDependencyDescriptor = nullptr;
  443. AZ::ComponentDescriptor* m_sortSecondAndThirdDependencyDescriptor = nullptr;
  444. AZ::ComponentDescriptor* m_sortWrapperDescriptor = nullptr;
  445. AZ::ComponentDescriptor* m_sortNoServiceDescriptor = nullptr;
  446. AZ::ComponentDescriptor* m_duplicateProvidedServiceComponentDescriptor = nullptr;
  447. AZ::ComponentDescriptor* m_dependsOnDuplicateServiceComponentDescriptor = nullptr;
  448. AZ::ComponentDescriptor* m_requiresDuplicateServiceComponentDescriptor = nullptr;
  449. };
  450. TEST_F(EntityTests, EntityComponentList_OutOfOrderUUIDs_ComponentListIsSorted)
  451. {
  452. AZ::Entity entity1;
  453. entity1.CreateComponent<SortOrderTestFirstComponent>();
  454. entity1.CreateComponent<SortOrderTestSecondComponent>();
  455. entity1.CreateComponent<SortOrderTestThirdComponent>();
  456. entity1.EvaluateDependencies();
  457. AZ::Entity entity2;
  458. entity2.CreateComponent<SortOrderTestSecondComponent>();
  459. entity2.CreateComponent<SortOrderTestThirdComponent>();
  460. entity2.CreateComponent<SortOrderTestFirstComponent>();
  461. entity2.EvaluateDependencies();
  462. ValidateComponentList(entity1, entity2, 3);
  463. }
  464. TEST_F(EntityTests, EntityComponentList_OutOfOrderUUIDs_NoServiceComponentsAreSortedLast)
  465. {
  466. AZ::Entity entity1;
  467. entity1.CreateComponent<SortOrderTestFirstComponent>();
  468. entity1.CreateComponent<SortOrderTestSecondComponent>();
  469. entity1.CreateComponent<SortOrderTestNoService>();
  470. entity1.EvaluateDependencies();
  471. AZ::Entity entity2;
  472. entity2.CreateComponent<SortOrderTestSecondComponent>();
  473. entity2.CreateComponent<SortOrderTestNoService>();
  474. entity2.CreateComponent<SortOrderTestFirstComponent>();
  475. entity2.EvaluateDependencies();
  476. ValidateComponentList(entity1, entity2, 3);
  477. }
  478. TEST_F(EntityTests, EntityComponentList_DuplicateAndOutOfOrderUUIDs_ComponentListIsSorted)
  479. {
  480. AZ::Entity entity1;
  481. entity1.CreateComponent<SortOrderTestFirstComponent>();
  482. entity1.CreateComponent<SortOrderTestSecondComponent>();
  483. // Create a second copy of the second component, to verify that ordering works with duplicates.
  484. entity1.CreateComponent<SortOrderTestSecondComponent>();
  485. entity1.CreateComponent<SortOrderTestThirdComponent>();
  486. entity1.EvaluateDependencies();
  487. AZ::Entity entity2;
  488. entity2.CreateComponent<SortOrderTestThirdComponent>();
  489. entity2.CreateComponent<SortOrderTestSecondComponent>();
  490. // Create a second copy of the second component, to verify that ordering works with duplicates.
  491. entity2.CreateComponent<SortOrderTestSecondComponent>();
  492. entity2.CreateComponent<SortOrderTestFirstComponent>();
  493. entity2.EvaluateDependencies();
  494. ValidateComponentList(entity1, entity2, 4);
  495. }
  496. // Verify that the entity's component sorting uses the correct ID, the GetUnderlyingComponentType instead
  497. // of the component's base ID. This ensures that component wrappers like GenericComponentWrapper
  498. // will sort based on what they hold and not their own ID.
  499. TEST_F(EntityTests, EntityComponentList_WrappedOutOfOrderUUIDs_ComponentListIsSorted)
  500. {
  501. AZ::Entity entity1;
  502. entity1.CreateComponent<SortOrderTestFirstComponent>();
  503. entity1.CreateComponent<SortOrderTestSecondComponent>();
  504. entity1.CreateComponent<SortOrderTestThirdComponent>();
  505. entity1.EvaluateDependencies();
  506. AZ::Entity entity2;
  507. entity2.CreateComponent<SortOrderTestSecondComponent>();
  508. entity2.CreateComponent<SortOrderTestComponentWrapper>(aznew SortOrderTestThirdComponent());
  509. entity2.CreateComponent<SortOrderTestComponentWrapper>(aznew SortOrderTestFirstComponent());
  510. entity2.EvaluateDependencies();
  511. ValidateComponentList(entity1, entity2, 3);
  512. }
  513. TEST_F(EntityTests, EntityComponentList_OutOfOrderUUIDsWithDependencies_ComponentListIsSorted)
  514. {
  515. AZ::Entity entity1;
  516. entity1.CreateComponent<SortOrderTestFirstComponent>();
  517. entity1.CreateComponent<SortOrderTestSecondComponent>();
  518. entity1.CreateComponent<SortOrderTestThirdComponent>();
  519. entity1.CreateComponent<SortOrderTestRequiresFirstComponent>();
  520. entity1.CreateComponent<SortOrderTestRequiresSecondComponent>();
  521. entity1.CreateComponent<SortOrderTestRequiresSecondAndThirdComponent>();
  522. entity1.EvaluateDependencies();
  523. AZ::Entity entity2;
  524. entity2.CreateComponent<SortOrderTestSecondComponent>();
  525. entity2.CreateComponent<SortOrderTestRequiresSecondAndThirdComponent>();
  526. entity2.CreateComponent<SortOrderTestRequiresSecondComponent>();
  527. entity2.CreateComponent<SortOrderTestThirdComponent>();
  528. entity2.CreateComponent<SortOrderTestRequiresFirstComponent>();
  529. entity2.CreateComponent<SortOrderTestFirstComponent>();
  530. entity2.EvaluateDependencies();
  531. ValidateComponentList(entity1, entity2, 6);
  532. }
  533. TEST_F(EntityTests, EntityComponentList_WrappedOutOfOrderUUIDsWithDependencies_ComponentListIsSorted)
  534. {
  535. AZ::Entity entity1;
  536. entity1.CreateComponent<SortOrderTestFirstComponent>();
  537. entity1.CreateComponent<SortOrderTestSecondComponent>();
  538. entity1.CreateComponent<SortOrderTestThirdComponent>();
  539. entity1.CreateComponent<SortOrderTestRequiresFirstComponent>();
  540. entity1.CreateComponent<SortOrderTestRequiresSecondComponent>();
  541. entity1.CreateComponent<SortOrderTestRequiresSecondAndThirdComponent>();
  542. entity1.EvaluateDependencies();
  543. AZ::Entity entity2;
  544. entity2.CreateComponent<SortOrderTestSecondComponent>();
  545. entity2.CreateComponent<SortOrderTestRequiresSecondAndThirdComponent>();
  546. entity2.CreateComponent<SortOrderTestRequiresSecondComponent>();
  547. entity2.CreateComponent<SortOrderTestComponentWrapper>(aznew SortOrderTestThirdComponent());
  548. entity2.CreateComponent<SortOrderTestComponentWrapper>(aznew SortOrderTestRequiresFirstComponent());
  549. entity2.CreateComponent<SortOrderTestFirstComponent>();
  550. entity2.EvaluateDependencies();
  551. ValidateComponentList(entity1, entity2, 6);
  552. }
  553. TEST_F(EntityTests, EntityComponentList_ComponentWithDuplicateProvidedService_EntityInitializesCorrectly)
  554. {
  555. AZ::Entity entity;
  556. DuplicateProvidedServiceComponent* duplicateServiceComponent = aznew DuplicateProvidedServiceComponent();
  557. entity.AddComponent(duplicateServiceComponent);
  558. // No test condition here, EvaluateDependencies was previously crashing when duplicate services were provided.
  559. // The crash would be caught by the unit test system.
  560. entity.EvaluateDependencies();
  561. }
  562. TEST_F(EntityTests, EntityComponentList_ComponentDependingOnComponentWithDuplicateProvidedService_EntityInitializesCorrectly)
  563. {
  564. AZ::Entity entity;
  565. DuplicateProvidedServiceComponent* duplicateServiceComponent = aznew DuplicateProvidedServiceComponent();
  566. DependsOnDuplicateProvidedServiceComponent* dependantService = aznew DependsOnDuplicateProvidedServiceComponent();
  567. entity.AddComponent(duplicateServiceComponent);
  568. entity.AddComponent(dependantService);
  569. // No test condition here, EvaluateDependencies was previously crashing when duplicate services were provided.
  570. // The crash would be caught by the unit test system.
  571. entity.EvaluateDependencies();
  572. }
  573. TEST_F(EntityTests, EntityComponentList_ComponentRequiringComponentWithDuplicateProvidedService_EntityInitializesCorrectly)
  574. {
  575. AZ::Entity entity;
  576. DuplicateProvidedServiceComponent* duplicateServiceComponent = aznew DuplicateProvidedServiceComponent();
  577. RequiresDuplicateProvidedServiceComponent* dependantService = aznew RequiresDuplicateProvidedServiceComponent();
  578. entity.AddComponent(duplicateServiceComponent);
  579. entity.AddComponent(dependantService);
  580. // No test condition here, EvaluateDependencies was previously crashing when duplicate services were provided.
  581. // The crash would be caught by the unit test system.
  582. entity.EvaluateDependencies();
  583. }
  584. TEST_F(EntityTests, EntityIsMoveConstructed)
  585. {
  586. static_assert(!AZStd::is_copy_constructible<AZ::Entity>::value, "Entity is dangerous to copy construct.");
  587. static_assert(!AZStd::is_copy_assignable<AZ::Entity>::value, "Entity is dangerous to copy assign.");
  588. {
  589. AZ::Entity entity1;
  590. entity1.CreateComponent<SortOrderTestFirstComponent>();
  591. AZ::Entity entity2(AZStd::move(entity1));
  592. EXPECT_EQ(entity1.GetComponents().size(), 0);
  593. EXPECT_EQ(entity2.GetComponents().size(), 1);
  594. } // there will be a crash here if they go out of scope if they weren't properly moved.
  595. }
  596. TEST_F(EntityTests, EntityIsMoveAssigned)
  597. {
  598. {
  599. AZ::Entity entity1;
  600. entity1.CreateComponent<SortOrderTestFirstComponent>();
  601. AZ::Entity entity2;
  602. entity2 = AZStd::move(entity1);
  603. EXPECT_EQ(entity1.GetComponents().size(), 0);
  604. EXPECT_EQ(entity2.GetComponents().size(), 1);
  605. } // there will be a crash here if they go out of scope if they weren't properly moved.
  606. }
  607. } // namespace UnitTest