1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381 |
- /*
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <CommonHierarchySetup.h>
- #include <MockInterfaces.h>
- #include <AzCore/Component/Entity.h>
- #include <AzCore/Console/Console.h>
- #include <AzCore/Name/Name.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/UnitTest/TestTypes.h>
- #include <AzCore/UnitTest/UnitTest.h>
- #include <AzFramework/Components/TransformComponent.h>
- #include <AzNetworking/Serialization/StringifySerializer.h>
- #include <AzTest/AzTest.h>
- #include <Multiplayer/Components/NetBindComponent.h>
- #include <Multiplayer/Components/NetworkHierarchyChildComponent.h>
- #include <Multiplayer/Components/NetworkHierarchyRootComponent.h>
- #include <Multiplayer/NetworkEntity/EntityReplication/EntityReplicator.h>
- namespace Multiplayer
- {
- using namespace testing;
- using namespace ::UnitTest;
- /*
- * Parent -> Child
- */
- class ServerSimpleHierarchyTests : public HierarchyTests
- {
- public:
- void SetUp() override
- {
- HierarchyTests::SetUp();
- m_root = AZStd::make_unique<EntityInfo>(1, "root", NetEntityId{ 1 }, EntityInfo::Role::Root);
- m_child = AZStd::make_unique<EntityInfo>(2, "child", NetEntityId{ 2 }, EntityInfo::Role::Child);
- CreateSimpleHierarchy(*m_root, *m_child);
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- // now the two entities are under one hierarchy
- }
- void TearDown() override
- {
- m_child.reset();
- m_root.reset();
- HierarchyTests::TearDown();
- }
- void CreateSimpleHierarchy(EntityInfo& root, EntityInfo& child)
- {
- PopulateHierarchicalEntity(root);
- SetupEntity(root.m_entity, root.m_netId, NetEntityRole::Authority);
- PopulateHierarchicalEntity(child);
- SetupEntity(child.m_entity, child.m_netId, NetEntityRole::Authority);
- // Create an entity replicator for the child entity
- const NetworkEntityHandle childHandle(child.m_entity.get(), m_networkEntityTracker.get());
- child.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, childHandle);
- child.m_replicator->Initialize(childHandle);
- // Create an entity replicator for the root entity
- const NetworkEntityHandle rootHandle(root.m_entity.get(), m_networkEntityTracker.get());
- root.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, rootHandle);
- root.m_replicator->Initialize(rootHandle);
- root.m_entity->Activate();
- child.m_entity->Activate();
- }
- AZStd::unique_ptr<EntityInfo> m_root;
- AZStd::unique_ptr<EntityInfo> m_child;
- };
- TEST_F(ServerSimpleHierarchyTests, Server_Sets_Appropriate_Network_Fields_For_Clients)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- NetEntityId{ 1 }
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Root_Is_Top_Level_Root)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- false
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Child_Has_Root_Set)
- {
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- NetEntityId{ 1 }
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Child_Has_Root_Cleared_On_Detach)
- {
- // now detach the child
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Root_Has_Child_Reference)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Root_Has_Child_References_Removed_On_Detach)
- {
- // now detach the child
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 1
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Root_Deactivates_Child_Has_No_References_To_Root)
- {
- StopEntity(m_root->m_entity);
- m_root->m_entity->Deactivate();
- m_root->m_entity.reset();
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Child_Deactivates_Root_Has_No_References_To_Child)
- {
- m_child.reset();
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 1
- );
- }
- TEST_F(ServerSimpleHierarchyTests, Root_Deactivates_IsHierarchyEnabled_Is_False)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchyEnabled(),
- true
- );
- StopEntity(m_root->m_entity);
- m_root->m_entity->Deactivate();
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchyEnabled(),
- false
- );
- m_root->m_entity.reset();
- }
- TEST_F(ServerSimpleHierarchyTests, Child_Deactivates_IsHierarchyEnabled_Is_False)
- {
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->IsHierarchyEnabled(),
- true
- );
- StopEntity(m_child->m_entity);
- m_child->m_entity->Deactivate();
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->IsHierarchyEnabled(),
- false
- );
- m_child->m_entity.reset();
- }
- TEST_F(ServerSimpleHierarchyTests, ChildPointsToRootAfterReattachment)
- {
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- m_root->m_entity->FindComponent<NetBindComponent>()->GetNetEntityId()
- );
- }
- TEST_F(ServerSimpleHierarchyTests, ChildHasOwningConnectionIdOfParent)
- {
- // disconnect and assign new connection ids
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- m_root->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 1 });
- m_child->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 2 });
- const ConnectionId previousConnectionId = m_child->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId();
- // re-attach, child's owning connection id should then be root's connection id
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- m_root->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
- );
- // detach, the child should roll back to his previous owning connection id
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- previousConnectionId
- );
- }
- /*
- * Parent -> Child -> ChildOfChild
- */
- class ServerDeepHierarchyTests : public HierarchyTests
- {
- public:
- const NetEntityId RootNetEntityId = NetEntityId{ 1 };
- const NetEntityId ChildNetEntityId = NetEntityId{ 2 };
- const NetEntityId ChildOfChildNetEntityId = NetEntityId{ 3 };
- void SetUp() override
- {
- HierarchyTests::SetUp();
- m_root = AZStd::make_unique<EntityInfo>((1), "root", RootNetEntityId, EntityInfo::Role::Root);
- m_child = AZStd::make_unique<EntityInfo>((2), "child", ChildNetEntityId, EntityInfo::Role::Child);
- m_childOfChild = AZStd::make_unique<EntityInfo>((3), "child of child", ChildOfChildNetEntityId, EntityInfo::Role::Child);
- CreateDeepHierarchy(*m_root, *m_child, *m_childOfChild);
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child->m_entity->GetId());
- // now the entities are under one hierarchy
- }
- void TearDown() override
- {
- m_childOfChild.reset();
- m_child.reset();
- m_root.reset();
- HierarchyTests::TearDown();
- }
- AZStd::unique_ptr<EntityInfo> m_root;
- AZStd::unique_ptr<EntityInfo> m_child;
- AZStd::unique_ptr<EntityInfo> m_childOfChild;
- };
- TEST_F(ServerDeepHierarchyTests, Root_Is_Top_Level_Root)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- false
- );
- }
- TEST_F(ServerDeepHierarchyTests, Root_Has_Child_References)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- if (m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 3)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
- m_root->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
- m_child->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
- m_childOfChild->m_entity.get()
- );
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalEntities()[2],
- m_childOfChild->m_entity.get()
- );
- }
- }
- TEST_F(ServerDeepHierarchyTests, Root_Has_Child_Of_Child_Reference_Removed_On_Detach)
- {
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- }
- TEST_F(ServerDeepHierarchyTests, Root_Has_All_References_Removed_On_Detach_Of_Mid_Child)
- {
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 1
- );
- }
- TEST_F(ServerDeepHierarchyTests, Root_Has_All_References_If_Mid_Child_Added_With_Child)
- {
- m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // reconnect
- m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerDeepHierarchyTests, Root_Has_All_References_If_Child_Of_Child_Added)
- {
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // reconnect
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerDeepHierarchyTests, Child_Of_Child_Points_To_Root_After_Attach)
- {
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // reconnect
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- RootNetEntityId
- );
- }
- TEST_F(ServerDeepHierarchyTests, All_New_Children_Point_To_Root_If_Mid_Child_Added_With_Child)
- {
- m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // reconnect
- m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- RootNetEntityId
- );
- EXPECT_EQ(
- m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- RootNetEntityId
- );
- }
- TEST_F(ServerDeepHierarchyTests, Children_Clear_Reference_To_Root_After_Mid_Child_Detached)
- {
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- EXPECT_EQ(
- m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- }
- TEST_F(ServerDeepHierarchyTests, Child_Of_Child_Clears_Reference_To_Root_After_Detached)
- {
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- }
- TEST_F(ServerDeepHierarchyTests, Root_Deactivates_Children_Have_No_References_To_Root)
- {
- m_root.reset();
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- EXPECT_EQ(
- m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- InvalidNetEntityId
- );
- }
- TEST_F(ServerDeepHierarchyTests, Child_Of_Child_Deactivates_Root_Removes_References_To_It)
- {
- m_childOfChild.reset();
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- }
- TEST_F(ServerDeepHierarchyTests, Testing_Limiting_Hierarchy_Maximum_Size)
- {
- uint32_t currentMaxLimit = 0;
- m_console->GetCvarValue<uint32_t>("bg_hierarchyEntityMaxLimit", currentMaxLimit);
- m_console->PerformCommand("bg_hierarchyEntityMaxLimit 2");
- // remake the hierarchy
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- m_console->PerformCommand((AZStd::string("bg_hierarchyEntityMaxLimit ") + AZStd::to_string(currentMaxLimit)).c_str());
- m_console->GetCvarValue<uint32_t>("bg_hierarchyEntityMaxLimit", currentMaxLimit);
- }
- TEST_F(ServerDeepHierarchyTests, ReattachMiddleChildRebuildInvokedTwice)
- {
- MockNetworkHierarchyCallbackHandler mock;
- EXPECT_CALL(mock, OnNetworkHierarchyUpdated(m_root->m_entity->GetId())).Times(2);
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->BindNetworkHierarchyChangedEventHandler(mock.m_changedHandler);
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- }
- TEST_F(ServerDeepHierarchyTests, SerializeHierarchyCorrection)
- {
- AzNetworking::StringifySerializer serializer;
- EXPECT_TRUE(m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->SerializeEntityCorrection(serializer));
- }
- /*
- * Parent -> Child -> Child Of Child
- * -> Child2 -> Child Of Child2
- * -> Child2 Of Child2
- */
- class ServerBranchedHierarchyTests : public HierarchyTests
- {
- public:
- const NetEntityId RootNetEntityId = NetEntityId{ 1 };
- const NetEntityId ChildNetEntityId = NetEntityId{ 2 };
- const NetEntityId ChildOfChildNetEntityId = NetEntityId{ 3 };
- const NetEntityId Child2NetEntityId = NetEntityId{ 4 };
- const NetEntityId ChildOfChild2NetEntityId = NetEntityId{ 5 };
- const NetEntityId Child2OfChild2NetEntityId = NetEntityId{ 6 };
- void SetUp() override
- {
- HierarchyTests::SetUp();
- m_root = AZStd::make_unique<EntityInfo>((1), "root", RootNetEntityId, EntityInfo::Role::Root);
- m_child = AZStd::make_unique<EntityInfo>((2), "child", ChildNetEntityId, EntityInfo::Role::Child);
- m_childOfChild = AZStd::make_unique<EntityInfo>((3), "child of child", ChildOfChildNetEntityId, EntityInfo::Role::Child);
- m_child2 = AZStd::make_unique<EntityInfo>((4), "child2", Child2NetEntityId, EntityInfo::Role::Child);
- m_childOfChild2 = AZStd::make_unique<EntityInfo>((5), "child of child2", ChildOfChild2NetEntityId, EntityInfo::Role::Child);
- m_child2OfChild2 = AZStd::make_unique<EntityInfo>((6), "child2 of child2", Child2OfChild2NetEntityId, EntityInfo::Role::Child);
- CreateBranchedHierarchy(*m_root, *m_child, *m_childOfChild,
- *m_child2, *m_childOfChild2, *m_child2OfChild2);
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child2->m_entity->GetId());
- m_child2OfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child2->m_entity->GetId());
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child->m_entity->GetId());
- // now the entities are under one hierarchy
- }
- void TearDown() override
- {
- m_child2OfChild2.reset();
- m_childOfChild2.reset();
- m_child2.reset();
- m_childOfChild.reset();
- m_child.reset();
- m_root.reset();
- HierarchyTests::TearDown();
- }
- void CreateBranchedHierarchy(EntityInfo& root, EntityInfo& child, EntityInfo& childOfChild,
- EntityInfo& child2, EntityInfo& childOfChild2, EntityInfo& child2OfChild2)
- {
- PopulateHierarchicalEntity(root);
- PopulateHierarchicalEntity(child);
- PopulateHierarchicalEntity(childOfChild);
- PopulateHierarchicalEntity(child2);
- PopulateHierarchicalEntity(childOfChild2);
- PopulateHierarchicalEntity(child2OfChild2);
- SetupEntity(root.m_entity, root.m_netId, NetEntityRole::Authority);
- SetupEntity(child.m_entity, child.m_netId, NetEntityRole::Authority);
- SetupEntity(childOfChild.m_entity, childOfChild.m_netId, NetEntityRole::Authority);
- SetupEntity(child2.m_entity, child2.m_netId, NetEntityRole::Authority);
- SetupEntity(childOfChild2.m_entity, childOfChild2.m_netId, NetEntityRole::Authority);
- SetupEntity(child2OfChild2.m_entity, child2OfChild2.m_netId, NetEntityRole::Authority);
- // Create entity replicators
- const NetworkEntityHandle childOfChild2Handle(childOfChild2.m_entity.get(), m_networkEntityTracker.get());
- childOfChild.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, childOfChild2Handle);
- childOfChild.m_replicator->Initialize(childOfChild2Handle);
- const NetworkEntityHandle child2OfChild2Handle(child2OfChild2.m_entity.get(), m_networkEntityTracker.get());
- childOfChild.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, child2OfChild2Handle);
- childOfChild.m_replicator->Initialize(child2OfChild2Handle);
- const NetworkEntityHandle child2Handle(child2.m_entity.get(), m_networkEntityTracker.get());
- child.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, child2Handle);
- child.m_replicator->Initialize(child2Handle);
- const NetworkEntityHandle childOfChildHandle(childOfChild.m_entity.get(), m_networkEntityTracker.get());
- childOfChild.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, childOfChildHandle);
- childOfChild.m_replicator->Initialize(childOfChildHandle);
- const NetworkEntityHandle childHandle(child.m_entity.get(), m_networkEntityTracker.get());
- child.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, childHandle);
- child.m_replicator->Initialize(childHandle);
- const NetworkEntityHandle rootHandle(root.m_entity.get(), m_networkEntityTracker.get());
- root.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, rootHandle);
- root.m_replicator->Initialize(rootHandle);
- root.m_entity->Activate();
- child.m_entity->Activate();
- childOfChild.m_entity->Activate();
- child2.m_entity->Activate();
- childOfChild2.m_entity->Activate();
- child2OfChild2.m_entity->Activate();
- }
- AZStd::unique_ptr<EntityInfo> m_root;
- AZStd::unique_ptr<EntityInfo> m_child;
- AZStd::unique_ptr<EntityInfo> m_childOfChild;
- AZStd::unique_ptr<EntityInfo> m_child2;
- AZStd::unique_ptr<EntityInfo> m_childOfChild2;
- AZStd::unique_ptr<EntityInfo> m_child2OfChild2;
- };
- TEST_F(ServerBranchedHierarchyTests, Sanity_Check)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- if (m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 6)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
- m_root->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
- m_child->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
- m_child2->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[3],
- m_childOfChild->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[4],
- m_child2OfChild2->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[5],
- m_childOfChild2->m_entity.get()
- );
- }
- }
- TEST_F(ServerBranchedHierarchyTests, Detach_Child_While_Child2_Remains_Attached)
- {
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 4
- );
- if (m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 4)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
- m_root->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
- m_child2->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
- m_child2OfChild2->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[3],
- m_childOfChild2->m_entity.get()
- );
- }
- EXPECT_EQ(
- m_child2->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalRoot(),
- m_root->m_entity.get()
- );
- EXPECT_EQ(
- m_child->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalRoot(),
- nullptr
- );
- EXPECT_EQ(
- m_childOfChild->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchicalRoot(),
- nullptr
- );
- }
- TEST_F(ServerBranchedHierarchyTests, Detach_Child_Then_Attach_To_Child2)
- {
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child2->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- /*
- * Sets up 2 deep hierarchies.
- */
- class ServerHierarchyOfHierarchyTests : public ServerDeepHierarchyTests
- {
- public:
- const NetEntityId Root2NetEntityId = NetEntityId{ 4 };
- const NetEntityId Child2NetEntityId = NetEntityId{ 5 };
- const NetEntityId ChildOfChild2NetEntityId = NetEntityId{ 6 };
- void SetUp() override
- {
- ServerDeepHierarchyTests::SetUp();
- m_root2 = AZStd::make_unique<EntityInfo>((4), "root 2", Root2NetEntityId, EntityInfo::Role::Root);
- m_child2 = AZStd::make_unique<EntityInfo>((5), "child 2", Child2NetEntityId, EntityInfo::Role::Child);
- m_childOfChild2 = AZStd::make_unique<EntityInfo>((6), "child of child 2", ChildOfChild2NetEntityId, EntityInfo::Role::Child);
- CreateDeepHierarchy(*m_root2, *m_child2, *m_childOfChild2);
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root2->m_entity->GetId());
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child2->m_entity->GetId());
- // now the entities are under one hierarchy
- }
- void TearDown() override
- {
- m_childOfChild2.reset();
- m_child2.reset();
- m_root2.reset();
- ServerDeepHierarchyTests::TearDown();
- }
- AZStd::unique_ptr<EntityInfo> m_root2;
- AZStd::unique_ptr<EntityInfo> m_child2;
- AZStd::unique_ptr<EntityInfo> m_childOfChild2;
- };
- TEST_F(ServerHierarchyOfHierarchyTests, Hierarchies_Are_Not_Related)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- if (m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 3)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
- m_root->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
- m_child->m_entity.get()
- );
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
- m_childOfChild->m_entity.get()
- );
- }
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- if (m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 3)
- {
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
- m_root2->m_entity.get()
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
- m_child2->m_entity.get()
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
- m_childOfChild2->m_entity.get()
- );
- }
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Is_Not_Top_Level_Root)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- false
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- true
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_References_All_When_Another_Hierarchy_Attached_At_Root)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_References_All_When_Another_Hierarchy_Attached_At_Child)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_References_All_When_Another_Hierarchy_Attached_At_Child_Of_Child)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_References_Top_Root_When_Another_Hierarchy_Attached_At_Root)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- true
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchyRoot(),
- RootNetEntityId
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_References_Top_Root_When_Another_Hierarchy_Attached_At_Child)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- true
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchyRoot(),
- RootNetEntityId
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_References_Top_Root_When_Another_Hierarchy_Attached_At_Child_Of_Child)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- true
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchyRoot(),
- RootNetEntityId
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Doesnt_Keep_Child_References)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 0
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Has_Child_References_After_Detachment_From_Top_Root)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- // detach
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- if (m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size() == 3)
- {
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[0],
- m_root2->m_entity.get()
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[1],
- m_child2->m_entity.get()
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities()[2],
- m_childOfChild2->m_entity.get()
- );
- }
- }
- TEST_F(ServerHierarchyOfHierarchyTests, InnerChildrenPointToInnerRootAfterDetachmentFromTopRoot)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- // detach
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_child2->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- m_root2->m_entity->FindComponent<NetBindComponent>()->GetNetEntityId()
- );
- EXPECT_EQ(
- m_childOfChild2->m_entity->FindComponent<NetworkHierarchyChildComponent>()->GetHierarchyRoot(),
- m_root2->m_entity->FindComponent<NetBindComponent>()->GetNetEntityId()
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Has_Child_References_After_Detachment_From_Child_Of_Child)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Has_Child_References_After_Top_Root_Deactivates)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root.reset();
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Has_Child_References_After_Child_Of_Top_Root_Deactivates)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_child.reset();
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Has_Child_References_After_Child_Of_Child_Deactivates)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_childOfChild.reset();
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Stress_Test_Inner_Root_Has_Child_References_After_Detachment_From_Child_Of_Child)
- {
- for (int i = 0; i < 100; ++i)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- }
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_Updates_Child_References_After_Detachment_Of_Child_Of_Child_In_Inner_Hierarchy)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 5
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_Updates_Child_References_After_Attachment_Of_Child_Of_Child_In_Inner_Hierarchy)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // re-connect
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child2->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_Updates_Child_References_After_Child_Of_Child_Changed_Hierarchies)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // connect to a different hierarchy
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_Updates_Child_References_After_Detachment_Of_Child_In_Inner_Hierarchy)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 4
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Top_Root_Updates_Child_References_After_Child_Changed_Hierarchies)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- // connect to a different hierarchy
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 6
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Has_No_Child_References_After_All_Children_Moved_To_Another_Hierarchy)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- // detach
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 1
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Inner_Root_Child_Deactivated_Top_Root_Has_No_Child_Reference_To_It)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_child2.reset();
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 4
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, Testing_Limiting_Hierarchy_Maximum_Size)
- {
- uint32_t currentMaxLimit = 0;
- m_console->GetCvarValue<uint32_t>("bg_hierarchyEntityMaxLimit", currentMaxLimit);
- m_console->PerformCommand("bg_hierarchyEntityMaxLimit 2");
- // remake the top level hierarchy
- m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- m_console->PerformCommand((AZStd::string("bg_hierarchyEntityMaxLimit ") + AZStd::to_string(currentMaxLimit)).c_str());
- m_console->GetCvarValue<uint32_t>("bg_hierarchyEntityMaxLimit", currentMaxLimit);
- }
- TEST_F(ServerHierarchyOfHierarchyTests, InnerRootAndItsChildrenHaveOwningConnectionIdOfTopRoot)
- {
- // Assign new connection ids.
- m_root->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 1 });
- m_root2->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 2 });
- // Attach then inner hierarchy's owning connection id should then be top root's connection id.
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- m_root->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
- );
- EXPECT_EQ(
- m_child2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- m_root->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
- );
- EXPECT_EQ(
- m_childOfChild2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- m_root->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
- );
- }
- TEST_F(ServerHierarchyOfHierarchyTests, InnerRootAndItsChildrenHaveTheirOriginalOwningConnectionIdAfterDetachingFromTopRoot)
- {
- // Assign new connection ids.
- m_root->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 1 });
- m_root2->m_entity->FindComponent<NetBindComponent>()->SetOwningConnectionId(ConnectionId{ 2 });
- // Attach then inner hierarchy's owning connection id should then be top root's connection id.
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- // detach, inner hierarchy should roll back to his previous owning connection id
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- ConnectionId{ 2 }
- );
- EXPECT_EQ(
- m_child2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- m_root2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
- );
- EXPECT_EQ(
- m_childOfChild2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId(),
- m_root2->m_entity->FindComponent<NetBindComponent>()->GetOwningConnectionId()
- );
- }
- /*
- * Parent -> Child -> ChildOfChild (not marked as in a hierarchy)
- */
- class ServerMixedDeepHierarchyTests : public HierarchyTests
- {
- public:
- void SetUp() override
- {
- HierarchyTests::SetUp();
- m_root = AZStd::make_unique<EntityInfo>((1), "root", NetEntityId{ 1 }, EntityInfo::Role::Root);
- m_child = AZStd::make_unique<EntityInfo>((2), "child", NetEntityId{ 2 }, EntityInfo::Role::Child);
- m_childOfChild = AZStd::make_unique<EntityInfo>((3), "child of child", NetEntityId{ 3 }, EntityInfo::Role::None);
- CreateDeepHierarchy(*m_root, *m_child, *m_childOfChild);
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child->m_entity->GetId());
- // now the entities are under one hierarchy
- }
- void TearDown() override
- {
- m_childOfChild.reset();
- m_child.reset();
- m_root.reset();
- HierarchyTests::TearDown();
- }
- AZStd::unique_ptr<EntityInfo> m_root;
- AZStd::unique_ptr<EntityInfo> m_child;
- AZStd::unique_ptr<EntityInfo> m_childOfChild;
- };
- TEST_F(ServerMixedDeepHierarchyTests, Top_Root_Ignores_Non_Hierarchical_Entities)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- }
- TEST_F(ServerMixedDeepHierarchyTests, Detaching_Non_Hierarchical_Entity_Has_No_Effect_On_Top_Root)
- {
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- }
- TEST_F(ServerMixedDeepHierarchyTests, Attaching_Non_Hierarchical_Entity_Has_No_Effect_On_Top_Root)
- {
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- m_childOfChild->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- }
- /*
- * 1st hierarchy: Parent -> Child -> ChildOfChild (not marked as in a hierarchy)
- * 2nd hierarchy: Parent2 -> Child2 (not marked as in a hierarchy) -> ChildOfChild2
- */
- class ServerMixedHierarchyOfHierarchyTests : public ServerMixedDeepHierarchyTests
- {
- public:
- void SetUp() override
- {
- ServerMixedDeepHierarchyTests::SetUp();
- m_root2 = AZStd::make_unique<EntityInfo>((4), "root 2", NetEntityId{ 4 }, EntityInfo::Role::Root);
- m_child2 = AZStd::make_unique<EntityInfo>((5), "child 2", NetEntityId{ 5 }, EntityInfo::Role::None);
- m_childOfChild2 = AZStd::make_unique<EntityInfo>((6), "child of child 2", NetEntityId{ 6 }, EntityInfo::Role::Child);
- CreateDeepHierarchy(*m_root2, *m_child2, *m_childOfChild2);
- m_child2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root2->m_entity->GetId());
- m_childOfChild2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child2->m_entity->GetId());
- // now the entities are under one hierarchy
- }
- void TearDown() override
- {
- m_childOfChild2.reset();
- m_child2.reset();
- m_root2.reset();
- ServerMixedDeepHierarchyTests::TearDown();
- }
- AZStd::unique_ptr<EntityInfo> m_root2;
- AZStd::unique_ptr<EntityInfo> m_child2;
- AZStd::unique_ptr<EntityInfo> m_childOfChild2;
- };
- TEST_F(ServerMixedHierarchyOfHierarchyTests, Sanity_Check_Ingore_Children_Without_Hierarchy_Components)
- {
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 2
- );
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 1
- );
- }
- TEST_F(ServerMixedHierarchyOfHierarchyTests, Adding_Mixed_Hierarchy_Ingores_Children_Without_Hierarchy_Components)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
- EXPECT_EQ(
- m_root->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerMixedHierarchyOfHierarchyTests, Attaching_Hierarchy_To_Non_Hierarchical_Entity_Does_Not_Merge_Hierarchies)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->IsHierarchicalChild(),
- false
- );
- }
- /*
- * Sets up a hierarchy with 3 roots, 2 of them being inner roots.
- */
- class ServerHierarchyWithThreeRoots : public ServerHierarchyOfHierarchyTests
- {
- public:
- const NetEntityId Root3NetEntityId = NetEntityId{ 7 };
- const NetEntityId Child3NetEntityId = NetEntityId{ 8 };
- const NetEntityId ChildOfChild3NetEntityId = NetEntityId{ 9 };
- void SetUp() override
- {
- ServerHierarchyOfHierarchyTests::SetUp();
- m_root3 = AZStd::make_unique<EntityInfo>((7), "root 3", Root3NetEntityId, EntityInfo::Role::Root);
- m_child3 = AZStd::make_unique<EntityInfo>((8), "child 3", Child3NetEntityId, EntityInfo::Role::Child);
- m_childOfChild3 = AZStd::make_unique<EntityInfo>((9), "child of child 3", ChildOfChild3NetEntityId, EntityInfo::Role::Child);
- CreateDeepHierarchy(*m_root3, *m_child3, *m_childOfChild3);
- m_child3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root3->m_entity->GetId());
- m_childOfChild3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_child3->m_entity->GetId());
- // now the entities are under one hierarchy
- }
- void TearDown() override
- {
- m_childOfChild3.reset();
- m_child3.reset();
- m_root3.reset();
- ServerHierarchyOfHierarchyTests::TearDown();
- }
- AZStd::unique_ptr<EntityInfo> m_root3;
- AZStd::unique_ptr<EntityInfo> m_child3;
- AZStd::unique_ptr<EntityInfo> m_childOfChild3;
- };
- TEST_F(ServerHierarchyWithThreeRoots, Top_Root_Active_Then_Inner_Roots_Have_No_Child_References)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 0
- );
- EXPECT_EQ(
- m_root3->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 0
- );
- }
- TEST_F(ServerHierarchyWithThreeRoots, Top_Root_Deactivates_Inner_Roots_Have_Child_References)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root.reset();
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- EXPECT_EQ(
- m_root3->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyWithThreeRoots, Child_Of_Top_Root_Deactivates_Inner_Roots_Have_Child_References)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_child.reset();
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- EXPECT_EQ(
- m_root3->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyWithThreeRoots, Child_Of_Child_Of_Top_Root_Deactivates_Inner_Roots_Have_Child_References)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_childOfChild.reset();
- EXPECT_EQ(
- m_root2->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- EXPECT_EQ(
- m_root3->m_entity->FindComponent<NetworkHierarchyRootComponent>()->GetHierarchicalEntities().size(),
- 3
- );
- }
- TEST_F(ServerHierarchyWithThreeRoots, InnerRootLeftTopRootThenLastChildGetsJoinedEventOnce)
- {
- m_root2->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- m_root3->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_childOfChild->m_entity->GetId());
- MockNetworkHierarchyCallbackHandler mock;
- EXPECT_CALL(mock, OnNetworkHierarchyUpdated(m_root3->m_entity->GetId()));
- m_childOfChild3->m_entity->FindComponent<NetworkHierarchyChildComponent>()->BindNetworkHierarchyChangedEventHandler(mock.m_changedHandler);
- m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
- }
- }
|