/* * 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 #include #include #include #include #include #include namespace Multiplayer { using namespace testing; using namespace ::UnitTest; class LocalRpcTests : public NetworkEntityTests { public: void SetUp() override { NetworkEntityTests::SetUp(); ON_CALL(*m_mockMultiplayer, GetAgentType()).WillByDefault(Return(MultiplayerAgentType::ClientServer)); m_rpcTesterDescriptor.reset(MultiplayerTest::RpcUnitTesterComponent::CreateDescriptor()); m_rpcTesterDescriptor->Reflect(m_serializeContext.get()); ConfigureEntity(NetEntityRole::Authority); } void ConfigureEntity(NetEntityRole local) { m_root = AZStd::make_unique(1, "entity", NetEntityId{ 1 }, EntityInfo::Role::Root); PopulateNetworkEntity(*m_root); SetupEntity(m_root->m_entity, m_root->m_netId, local); m_root->m_entity->Activate(); // For a local client-server entity replicators are NOT created. } void TearDown() override { m_root.reset(); m_rpcTesterDescriptor.reset(); NetworkEntityTests::TearDown(); } void PopulateNetworkEntity(const EntityInfo& entityInfo) { entityInfo.m_entity->CreateComponent(); entityInfo.m_entity->CreateComponent(); entityInfo.m_entity->CreateComponent(); entityInfo.m_entity->CreateComponent(); } AZStd::unique_ptr m_root; AZStd::unique_ptr m_rpcTesterDescriptor; }; TEST_F(LocalRpcTests, LocalRpcServerToAuthority) { const auto component = m_root->m_entity->FindComponent(); component->RPC_ServerToAuthority(); m_networkEntityManager->DispatchLocalDeferredRpcMessages(); EXPECT_EQ(component->GetTestController()->m_serverToAuthorityCalls, 1); } TEST_F(LocalRpcTests, LocalRpcAuthorityToClients) { const auto component = m_root->m_entity->FindComponent(); component->GetTestController()->RPC_AuthorityToClient(); m_networkEntityManager->DispatchLocalDeferredRpcMessages(); EXPECT_EQ(component->m_authorityToClientCalls, 1); } TEST_F(LocalRpcTests, LocalRpcAuthorityToAutonomous) { m_root->m_entity->FindComponent()->EnablePlayerHostAutonomy(true); const auto component = m_root->m_entity->FindComponent(); component->GetTestController()->RPC_AuthorityToAutonomous(); m_networkEntityManager->DispatchLocalDeferredRpcMessages(); EXPECT_EQ(component->GetTestController()->m_authorityToAutonomousCalls, 1); } TEST_F(LocalRpcTests, LocalRpcAutonomousToAuthority) { m_root->m_entity->FindComponent()->EnablePlayerHostAutonomy(true); const auto component = m_root->m_entity->FindComponent(); component->GetTestController()->RPC_AutonomousToAuthority(); m_networkEntityManager->DispatchLocalDeferredRpcMessages(); EXPECT_EQ(component->GetTestController()->m_autonomousToAuthorityCalls, 1); } }