ClientHierarchyTests.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <CommonHierarchySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Console/Console.h>
  11. #include <AzCore/Name/Name.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/UnitTest/UnitTest.h>
  14. #include <AzTest/AzTest.h>
  15. #include <Multiplayer/Components/NetBindComponent.h>
  16. #include <Multiplayer/Components/NetworkHierarchyChildComponent.h>
  17. #include <Multiplayer/NetworkEntity/EntityReplication/EntityReplicator.h>
  18. #include <Multiplayer/NetworkInput/NetworkInputArray.h>
  19. #include <Source/NetworkEntity/NetworkEntityManager.h>
  20. namespace Multiplayer
  21. {
  22. using namespace testing;
  23. using namespace ::UnitTest;
  24. /*
  25. * Test NetBindComponent creation / destruction without activation. This must work before more complicated tests.
  26. */
  27. TEST_F(HierarchyTests, On_Client_NetBindComponent_Init)
  28. {
  29. auto multiplayer = AZ::Interface<IMultiplayer>::Get();
  30. auto networkEntityManager = multiplayer->GetNetworkEntityManager();
  31. auto netTracker = networkEntityManager->GetNetworkEntityTracker();
  32. // At the start, there shouldn't be anything in the Network Entity Manager.
  33. EXPECT_EQ(networkEntityManager->GetEntityCount(), 0);
  34. AZStd::unique_ptr<AZ::Entity> entity = AZStd::make_unique<AZ::Entity>();
  35. AZ::Entity* rawEntityPtr = entity.get();
  36. auto netBindComponent = entity->CreateComponent<NetBindComponent>();
  37. SetupEntity(entity, NetEntityId{ 1 }, NetEntityRole::Client);
  38. // After the netBindComponent is initialized, it should appear in the NetworkEntityManager and the NetworkEntityTracker.
  39. EXPECT_EQ(networkEntityManager->GetEntityCount(), 1);
  40. EXPECT_EQ(netBindComponent, netTracker->GetNetBindComponent(rawEntityPtr));
  41. StopEntity(entity);
  42. entity.reset();
  43. // After the entity is destroyed, it should no longer appear in the NetworkEntityManager and the NetworkEntityTracker.
  44. // This should be true whether or not the entity was ever activated.
  45. EXPECT_EQ(networkEntityManager->GetEntityCount(), 0);
  46. EXPECT_EQ(netTracker->GetNetBindComponent(rawEntityPtr), nullptr);
  47. }
  48. /*
  49. * Test NetBindComponent activation. This must work before more complicated tests.
  50. */
  51. TEST_F(HierarchyTests, On_Client_NetBindComponent_Activate)
  52. {
  53. auto multiplayer = AZ::Interface<IMultiplayer>::Get();
  54. auto networkEntityManager = multiplayer->GetNetworkEntityManager();
  55. auto netTracker = networkEntityManager->GetNetworkEntityTracker();
  56. // At the start, there shouldn't be anything in the Network Entity Manager.
  57. EXPECT_EQ(networkEntityManager->GetEntityCount(), 0);
  58. AZStd::unique_ptr<AZ::Entity> entity = AZStd::make_unique<AZ::Entity>();
  59. AZ::Entity* rawEntityPtr = entity.get();
  60. entity->CreateComponent<NetBindComponent>();
  61. SetupEntity(entity, NetEntityId{ 1 }, NetEntityRole::Client);
  62. entity->Activate();
  63. StopEntity(entity);
  64. entity->Deactivate();
  65. entity.reset();
  66. // After the entity is destroyed, it should no longer appear in the NetworkEntityManager and the NetworkEntityTracker.
  67. // This should be true whether or not the entity was ever activated.
  68. EXPECT_EQ(networkEntityManager->GetEntityCount(), 0);
  69. EXPECT_EQ(netTracker->GetNetBindComponent(rawEntityPtr), nullptr);
  70. }
  71. /*
  72. * Hierarchy test - a child entity on a client delaying activation until its hierarchical parent has been activated
  73. */
  74. TEST_F(HierarchyTests, On_Client_EntityReplicator_DontActivate_BeforeParent)
  75. {
  76. // Create a child entity that will be tested for activation inside a hierarchy
  77. AZStd::unique_ptr<AZ::Entity> childEntity = AZStd::make_unique<AZ::Entity>();
  78. CreateEntityWithChildHierarchy(childEntity);
  79. SetupEntity(childEntity, NetEntityId{ 2 }, NetEntityRole::Client);
  80. // child entity is not activated on purpose here, we are about to test conditional activation check
  81. // we need a parent-id value to be present in NetworkTransformComponent (which is in client mode and doesn't have a controller)
  82. SetParentIdOnNetworkTransform(childEntity, NetEntityId{ 1 });
  83. SetHierarchyRootFieldOnNetworkHierarchyChild<NetworkHierarchyChildComponent>(childEntity, NetEntityId{ 1 });
  84. // Create an entity replicator for the child entity
  85. const NetworkEntityHandle childHandle(childEntity.get(), m_networkEntityTracker.get());
  86. EntityReplicator entityReplicator(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, childHandle);
  87. entityReplicator.Initialize(childHandle);
  88. // Entity replicator should not be ready to activate the entity because its parent does not exist
  89. EXPECT_EQ(entityReplicator.IsReadyToActivate(), false);
  90. }
  91. TEST_F(HierarchyTests, On_Client_EntityReplicator_DontActivate_Inner_Root_Before_Top_Root)
  92. {
  93. // Create a child entity that will be tested for activation inside a hierarchy
  94. AZStd::unique_ptr<AZ::Entity> innerRootEntity = AZStd::make_unique<AZ::Entity>();
  95. CreateEntityWithRootHierarchy(innerRootEntity);
  96. SetupEntity(innerRootEntity, NetEntityId{ 2 }, NetEntityRole::Client);
  97. // child entity is not activated on purpose here, we are about to test conditional activation check
  98. // we need a parent-id value to be present in NetworkTransformComponent (which is in client mode and doesn't have a controller)
  99. SetParentIdOnNetworkTransform(innerRootEntity, NetEntityId{ 1 });
  100. SetHierarchyRootFieldOnNetworkHierarchyChild<NetworkHierarchyRootComponent>(innerRootEntity, NetEntityId{ 1 });
  101. // Create an entity replicator for the child entity
  102. const NetworkEntityHandle innerRootHandle(innerRootEntity.get(), m_networkEntityTracker.get());
  103. EntityReplicator entityReplicator(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, innerRootHandle);
  104. entityReplicator.Initialize(innerRootHandle);
  105. // Entity replicator should not be ready to activate the entity because its parent does not exist
  106. EXPECT_EQ(entityReplicator.IsReadyToActivate(), false);
  107. }
  108. TEST_F(HierarchyTests, On_Client_Not_In_Hierarchy_EntityReplicator_Ignores_Parent)
  109. {
  110. // Create a child entity that will be tested for activation inside a hierarchy
  111. AZStd::unique_ptr<AZ::Entity> childEntity = AZStd::make_unique<AZ::Entity>();
  112. CreateEntityWithChildHierarchy(childEntity);
  113. SetupEntity(childEntity, NetEntityId{ 2 }, NetEntityRole::Client);
  114. // child entity is not activated on purpose here, we are about to test conditional activation check
  115. // we need a parent-id value to be present in NetworkTransformComponent (which is in client mode and doesn't have a controller)
  116. SetParentIdOnNetworkTransform(childEntity, NetEntityId{ 1 });
  117. SetHierarchyRootFieldOnNetworkHierarchyChild<NetworkHierarchyChildComponent>(childEntity, InvalidNetEntityId);
  118. // Create an entity replicator for the child entity
  119. const NetworkEntityHandle childHandle(childEntity.get(), m_networkEntityTracker.get());
  120. EntityReplicator entityReplicator(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, childHandle);
  121. entityReplicator.Initialize(childHandle);
  122. // Entity replicator should not be ready to activate the entity because its parent does not exist
  123. EXPECT_EQ(entityReplicator.IsReadyToActivate(), true);
  124. }
  125. /*
  126. * Hierarchy test - a child entity on a client allowing activation when its hierarchical parent is active
  127. */
  128. TEST_F(HierarchyTests, On_Client_EntityReplicator_Activates_AfterParent)
  129. {
  130. AZStd::unique_ptr<AZ::Entity> childEntity = AZStd::make_unique<AZ::Entity>();
  131. CreateEntityWithChildHierarchy(childEntity);
  132. SetupEntity(childEntity, NetEntityId{ 2 }, NetEntityRole::Client);
  133. // we need a parent-id value to be present in NetworkTransformComponent (which is in client mode and doesn't have a controller)
  134. SetParentIdOnNetworkTransform(childEntity, NetEntityId{ 1 });
  135. SetHierarchyRootFieldOnNetworkHierarchyChild<NetworkHierarchyChildComponent>(childEntity, NetEntityId{ 1 });
  136. // Create an entity replicator for the child entity
  137. const NetworkEntityHandle childHandle(childEntity.get(), m_networkEntityTracker.get());
  138. EntityReplicator childEntityReplicator(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, childHandle);
  139. childEntityReplicator.Initialize(childHandle);
  140. // Now let's create a parent entity and activate it
  141. AZStd::unique_ptr<AZ::Entity> parentEntity = AZStd::make_unique<AZ::Entity>();
  142. CreateEntityWithRootHierarchy(parentEntity);
  143. SetupEntity(parentEntity, NetEntityId{ 1 }, NetEntityRole::Client);
  144. // Create an entity replicator for the parent entity
  145. const NetworkEntityHandle parentHandle(parentEntity.get(), m_networkEntityTracker.get());
  146. ON_CALL(*m_mockNetworkEntityManager, GetEntity(_)).WillByDefault(Return(parentHandle));
  147. EntityReplicator parentEntityReplicator(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, parentHandle);
  148. parentEntityReplicator.Initialize(parentHandle);
  149. parentEntity->Activate();
  150. // The child should be ready to be activated
  151. EXPECT_EQ(childEntityReplicator.IsReadyToActivate(), true);
  152. StopEntity(parentEntity);
  153. parentEntity->Deactivate();
  154. }
  155. /*
  156. * Parent -> Child
  157. */
  158. class ClientSimpleHierarchyTests : public HierarchyTests
  159. {
  160. public:
  161. const NetEntityId RootNetEntityId = NetEntityId{ 1 };
  162. const NetEntityId ChildNetEntityId = NetEntityId{ 2 };
  163. void SetUp() override
  164. {
  165. HierarchyTests::SetUp();
  166. m_root = AZStd::make_unique<EntityInfo>(1, "root", RootNetEntityId, EntityInfo::Role::Root);
  167. m_child = AZStd::make_unique<EntityInfo>(2, "child", ChildNetEntityId, EntityInfo::Role::Child);
  168. CreateSimpleHierarchy(*m_root, *m_child);
  169. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
  170. // now the two entities are under one hierarchy
  171. }
  172. void TearDown() override
  173. {
  174. m_child.reset();
  175. m_root.reset();
  176. HierarchyTests::TearDown();
  177. }
  178. void CreateSimpleHierarchy(EntityInfo& root, EntityInfo& child)
  179. {
  180. PopulateHierarchicalEntity(root);
  181. SetupEntity(root.m_entity, root.m_netId, NetEntityRole::Autonomous);
  182. PopulateHierarchicalEntity(child);
  183. SetupEntity(child.m_entity, child.m_netId, NetEntityRole::Autonomous);
  184. // we need a parent-id value to be present in NetworkTransformComponent (which is in client mode and doesn't have a controller)
  185. SetParentIdOnNetworkTransform(child.m_entity, root.m_netId);
  186. SetHierarchyRootFieldOnNetworkHierarchyChild<NetworkHierarchyChildComponent>(child.m_entity, root.m_netId);
  187. // Create an entity replicator for the child entity
  188. const NetworkEntityHandle childHandle(child.m_entity.get(), m_networkEntityTracker.get());
  189. child.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, childHandle);
  190. child.m_replicator->Initialize(childHandle);
  191. // Create an entity replicator for the root entity
  192. const NetworkEntityHandle rootHandle(root.m_entity.get(), m_networkEntityTracker.get());
  193. root.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, rootHandle);
  194. root.m_replicator->Initialize(rootHandle);
  195. root.m_entity->Activate();
  196. child.m_entity->Activate();
  197. }
  198. void SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(const AZStd::unique_ptr<AZ::Entity>& entity, NetEntityId value)
  199. {
  200. /* Derived from NetworkHierarchyChildComponent.AutoComponent.xml */
  201. constexpr int totalBits = 1 /*NetworkHierarchyChildComponentInternal::AuthorityToClientDirtyEnum::Count*/;
  202. constexpr int inHierarchyBit = 0 /*NetworkHierarchyChildComponentInternal::AuthorityToClientDirtyEnum::hierarchyRoot_DirtyFlag*/;
  203. ReplicationRecord currentRecord(NetEntityRole::Client);
  204. currentRecord.m_authorityToClient.AddBits(totalBits);
  205. currentRecord.m_authorityToClient.SetBit(inHierarchyBit, true);
  206. constexpr uint32_t bufferSize = 100;
  207. AZStd::array<uint8_t, bufferSize> buffer = {};
  208. NetworkInputSerializer inSerializer(buffer.begin(), bufferSize);
  209. ISerializer& serializer = inSerializer;
  210. serializer.Serialize(value, "hierarchyRoot"); // Derived from NetworkHierarchyChildComponent.AutoComponent.xml
  211. NetworkOutputSerializer outSerializer(buffer.begin(), bufferSize);
  212. ReplicationRecord notifyRecord = currentRecord;
  213. entity->FindComponent<NetworkHierarchyChildComponent>()->SerializeStateDeltaMessage(currentRecord, outSerializer);
  214. entity->FindComponent<NetworkHierarchyChildComponent>()->NotifyStateDeltaChanges(notifyRecord);
  215. }
  216. AZStd::unique_ptr<EntityInfo> m_root;
  217. AZStd::unique_ptr<EntityInfo> m_child;
  218. };
  219. TEST_F(ClientSimpleHierarchyTests, Client_Activates_Hierarchy_From_Network_Fields)
  220. {
  221. EXPECT_EQ(
  222. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchyRoot(),
  223. InvalidNetEntityId
  224. );
  225. EXPECT_EQ(
  226. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
  227. RootNetEntityId
  228. );
  229. EXPECT_EQ(
  230. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalRoot(),
  231. m_root->m_entity.get()
  232. );
  233. EXPECT_EQ(
  234. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
  235. 2
  236. );
  237. if (m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 2)
  238. {
  239. EXPECT_EQ(
  240. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
  241. m_root->m_entity.get()
  242. );
  243. EXPECT_EQ(
  244. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
  245. m_child->m_entity.get()
  246. );
  247. }
  248. }
  249. TEST_F(ClientSimpleHierarchyTests, Client_Detaches_Child_When_Server_Detaches)
  250. {
  251. // simulate server detaching child entity
  252. SetParentIdOnNetworkTransform(m_child->m_entity, InvalidNetEntityId);
  253. SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(m_child->m_entity, InvalidNetEntityId);
  254. EXPECT_EQ(
  255. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
  256. InvalidNetEntityId
  257. );
  258. EXPECT_EQ(
  259. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalRoot(),
  260. nullptr
  261. );
  262. }
  263. TEST_F(ClientSimpleHierarchyTests, Client_Sends_NetworkHierarchy_Updated_Event_On_Child_Detached_On_Server)
  264. {
  265. MockNetworkHierarchyCallbackHandler mock;
  266. EXPECT_CALL(mock, OnNetworkHierarchyUpdated(m_root->m_entity->GetId()));
  267. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->BindNetworkHierarchyChangedEventHandler(mock.m_changedHandler);
  268. // simulate server detaching a child entity
  269. SetParentIdOnNetworkTransform(m_child->m_entity, InvalidNetEntityId);
  270. SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(m_child->m_entity, InvalidNetEntityId);
  271. }
  272. TEST_F(ClientSimpleHierarchyTests, Client_Sends_NetworkHierarchy_Leave_Event_On_Child_Detached_On_Server)
  273. {
  274. MockNetworkHierarchyCallbackHandler mock;
  275. EXPECT_CALL(mock, OnNetworkHierarchyLeave);
  276. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->BindNetworkHierarchyLeaveEventHandler(mock.m_leaveHandler);
  277. // simulate server detaching a child entity
  278. SetParentIdOnNetworkTransform(m_child->m_entity, InvalidNetEntityId);
  279. SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(m_child->m_entity, InvalidNetEntityId);
  280. }
  281. TEST_F(ClientSimpleHierarchyTests, ChildHasOwningConnectionIdOfParent)
  282. {
  283. // disconnect and assign new connection ids
  284. SetParentIdOnNetworkTransform(m_child->m_entity, InvalidNetEntityId);
  285. SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(m_child->m_entity, InvalidNetEntityId);
  286. m_root->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 1 });
  287. m_child->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 2 });
  288. const ConnectionId previousConnectionId = m_child->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId();
  289. // re-attach, child's owning connection id should then be root's connection id
  290. SetParentIdOnNetworkTransform(m_child->m_entity, RootNetEntityId);
  291. SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(m_child->m_entity, RootNetEntityId);
  292. EXPECT_EQ(
  293. m_child->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
  294. m_root->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
  295. );
  296. // detach, the child should roll back to his previous owning connection id
  297. SetParentIdOnNetworkTransform(m_child->m_entity, InvalidNetEntityId);
  298. SetHierarchyRootFieldOnNetworkHierarchyChildOnClient(m_child->m_entity, InvalidNetEntityId);
  299. EXPECT_EQ(
  300. m_child->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
  301. previousConnectionId
  302. );
  303. }
  304. /*
  305. * Parent -> Child -> ChildOfChild
  306. */
  307. class ClientDeepHierarchyTests : public ClientSimpleHierarchyTests
  308. {
  309. public:
  310. const NetEntityId ChildOfChildNetEntityId = NetEntityId{ 3 };
  311. void SetUp() override
  312. {
  313. ClientSimpleHierarchyTests::SetUp();
  314. m_childOfChild = AZStd::make_unique<EntityInfo>((3), "child of child", ChildOfChildNetEntityId, EntityInfo::Role::Child);
  315. CreateDeepHierarchyOnClient(*m_childOfChild);
  316. m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child->m_entity->GetId());
  317. }
  318. void TearDown() override
  319. {
  320. m_childOfChild.reset();
  321. ClientSimpleHierarchyTests::TearDown();
  322. }
  323. void CreateDeepHierarchyOnClient(EntityInfo& childOfChild)
  324. {
  325. PopulateHierarchicalEntity(childOfChild);
  326. SetupEntity(childOfChild.m_entity, childOfChild.m_netId, NetEntityRole::Autonomous);
  327. // we need a parent-id value to be present in NetworkTransformComponent (which is in client mode and doesn't have a controller)
  328. SetParentIdOnNetworkTransform(childOfChild.m_entity, m_childOfChild->m_netId);
  329. SetHierarchyRootFieldOnNetworkHierarchyChild<NetworkHierarchyChildComponent>(childOfChild.m_entity, m_root->m_netId);
  330. // Create an entity replicator for the child entity
  331. const NetworkEntityHandle childOfChildHandle(childOfChild.m_entity.get(), m_networkEntityTracker.get());
  332. childOfChild.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Authority, childOfChildHandle);
  333. childOfChild.m_replicator->Initialize(childOfChildHandle);
  334. childOfChild.m_entity->Activate();
  335. }
  336. AZStd::unique_ptr<EntityInfo> m_childOfChild;
  337. };
  338. TEST_F(ClientDeepHierarchyTests, Client_Activates_Hierarchy_From_Network_Fields)
  339. {
  340. EXPECT_EQ(
  341. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchyRoot(),
  342. InvalidNetEntityId
  343. );
  344. EXPECT_EQ(
  345. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
  346. RootNetEntityId
  347. );
  348. EXPECT_EQ(
  349. m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
  350. RootNetEntityId
  351. );
  352. EXPECT_EQ(
  353. m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalRoot(),
  354. m_root->m_entity.get()
  355. );
  356. EXPECT_EQ(
  357. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
  358. 3
  359. );
  360. if (m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 3)
  361. {
  362. EXPECT_EQ(
  363. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
  364. m_root->m_entity.get()
  365. );
  366. EXPECT_EQ(
  367. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
  368. m_child->m_entity.get()
  369. );
  370. EXPECT_EQ(
  371. m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
  372. m_childOfChild->m_entity.get()
  373. );
  374. }
  375. }
  376. TEST_F(ClientDeepHierarchyTests, CreateProcessInputTest)
  377. {
  378. using MultiplayerTest::TestMultiplayerComponent;
  379. using MultiplayerTest::TestMultiplayerComponentController;
  380. using MultiplayerTest::TestMultiplayerComponentNetworkInput;
  381. auto* rootNetBind = m_root->m_entity->FindComponent<NetBindComponent>();
  382. NetworkInputArray inputArray(rootNetBind->GetEntityHandle());
  383. NetworkInput& input = inputArray[0];
  384. const float deltaTime = 0.16f;
  385. rootNetBind->CreateInput(input, deltaTime);
  386. auto ValidateCreatedInput = [](const NetworkInput& input, const HierarchyTests::EntityInfo& entityInfo)
  387. {
  388. // Validate test input for the root entity's TestMultiplayerComponent
  389. auto* testInput = input.FindComponentInput<TestMultiplayerComponentNetworkInput>();
  390. EXPECT_NE(testInput, nullptr);
  391. auto* testMultiplayerComponent = entityInfo.m_entity->FindComponent<TestMultiplayerComponent>();
  392. EXPECT_NE(testMultiplayerComponent, nullptr);
  393. EXPECT_EQ(testInput->m_ownerId, testMultiplayerComponent->GetId());
  394. };
  395. // Validate root input
  396. ValidateCreatedInput(input, *m_root);
  397. // Validate children input
  398. {
  399. NetworkHierarchyRootComponentNetworkInput* rootHierarchyInput = input.FindComponentInput<NetworkHierarchyRootComponentNetworkInput>();
  400. const AZStd::vector<NetworkInputChild>& childInputs = rootHierarchyInput->m_childInputs;
  401. EXPECT_EQ(childInputs.size(), 2);
  402. ValidateCreatedInput(childInputs[0].GetNetworkInput(), *m_child);
  403. ValidateCreatedInput(childInputs[1].GetNetworkInput(), *m_childOfChild);
  404. }
  405. // Test ProcessInput
  406. {
  407. NetEntityIdSet inputProcessedEntities;
  408. size_t processInputCallCounter = 0;
  409. auto processInputCallback = [&inputProcessedEntities, &processInputCallCounter](
  410. NetEntityId netEntityId, [[maybe_unused]] Multiplayer::NetworkInput& input, [[maybe_unused]] float deltaTime)
  411. {
  412. inputProcessedEntities.insert(netEntityId);
  413. processInputCallCounter++;
  414. };
  415. // Set the callbacks for processing input. This allows us to inspect how many times the input was processed
  416. // and which entity's controller was invoked.
  417. m_root->m_entity->FindComponent<TestMultiplayerComponent>()->m_processInputCallback = processInputCallback;
  418. m_child->m_entity->FindComponent<TestMultiplayerComponent>()->m_processInputCallback = processInputCallback;
  419. m_childOfChild->m_entity->FindComponent<TestMultiplayerComponent>()->m_processInputCallback = processInputCallback;
  420. rootNetBind->ProcessInput(input, deltaTime);
  421. EXPECT_EQ(processInputCallCounter, 3);
  422. EXPECT_EQ(inputProcessedEntities,
  423. NetEntityIdSet({ m_root->m_netId, m_child->m_netId, m_childOfChild->m_netId }));
  424. }
  425. }
  426. }