LocalRpcTests.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <CommonNetworkEntitySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <RpcUnitTesterComponent.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Name/Name.h>
  12. #include <Multiplayer/Components/NetBindComponent.h>
  13. #include <Source/NetworkEntity/NetworkEntityManager.h>
  14. namespace Multiplayer
  15. {
  16. using namespace testing;
  17. using namespace ::UnitTest;
  18. class LocalRpcTests : public NetworkEntityTests
  19. {
  20. public:
  21. void SetUp() override
  22. {
  23. NetworkEntityTests::SetUp();
  24. ON_CALL(*m_mockMultiplayer, GetAgentType()).WillByDefault(Return(MultiplayerAgentType::ClientServer));
  25. m_rpcTesterDescriptor.reset(MultiplayerTest::RpcUnitTesterComponent::CreateDescriptor());
  26. m_rpcTesterDescriptor->Reflect(m_serializeContext.get());
  27. ConfigureEntity(NetEntityRole::Authority);
  28. }
  29. void ConfigureEntity(NetEntityRole local)
  30. {
  31. m_root = AZStd::make_unique<EntityInfo>(1, "entity", NetEntityId{ 1 }, EntityInfo::Role::Root);
  32. PopulateNetworkEntity(*m_root);
  33. SetupEntity(m_root->m_entity, m_root->m_netId, local);
  34. m_root->m_entity->Activate();
  35. // For a local client-server entity replicators are NOT created.
  36. }
  37. void TearDown() override
  38. {
  39. m_root.reset();
  40. m_rpcTesterDescriptor.reset();
  41. NetworkEntityTests::TearDown();
  42. }
  43. void PopulateNetworkEntity(const EntityInfo& entityInfo)
  44. {
  45. entityInfo.m_entity->CreateComponent<AzFramework::TransformComponent>();
  46. entityInfo.m_entity->CreateComponent<NetBindComponent>();
  47. entityInfo.m_entity->CreateComponent<NetworkTransformComponent>();
  48. entityInfo.m_entity->CreateComponent<MultiplayerTest::RpcUnitTesterComponent>();
  49. }
  50. AZStd::unique_ptr<EntityInfo> m_root;
  51. AZStd::unique_ptr<AZ::ComponentDescriptor> m_rpcTesterDescriptor;
  52. };
  53. TEST_F(LocalRpcTests, LocalRpcServerToAuthority)
  54. {
  55. const auto component = m_root->m_entity->FindComponent<MultiplayerTest::RpcUnitTesterComponent>();
  56. component->RPC_ServerToAuthority();
  57. m_networkEntityManager->DispatchLocalDeferredRpcMessages();
  58. EXPECT_EQ(component->GetTestController()->m_serverToAuthorityCalls, 1);
  59. }
  60. TEST_F(LocalRpcTests, LocalRpcAuthorityToClient)
  61. {
  62. const auto component = m_root->m_entity->FindComponent<MultiplayerTest::RpcUnitTesterComponent>();
  63. component->GetTestController()->RPC_AuthorityToClient();
  64. m_networkEntityManager->DispatchLocalDeferredRpcMessages();
  65. EXPECT_EQ(component->m_authorityToClientCalls, 1);
  66. }
  67. TEST_F(LocalRpcTests, LocalRpcAuthorityToAutonomous)
  68. {
  69. m_root->m_entity->FindComponent<NetBindComponent>()->EnablePlayerHostAutonomy(true);
  70. const auto component = m_root->m_entity->FindComponent<MultiplayerTest::RpcUnitTesterComponent>();
  71. component->GetTestController()->RPC_AuthorityToAutonomous();
  72. m_networkEntityManager->DispatchLocalDeferredRpcMessages();
  73. EXPECT_EQ(component->GetTestController()->m_authorityToAutonomousCalls, 1);
  74. }
  75. TEST_F(LocalRpcTests, LocalRpcAuthorityToRemoteAutonomous)
  76. {
  77. // Turn off player host autonomy.
  78. // This simulates a host machine (authority) sending an RPC to a remote autonomous client
  79. // For example, a local prediction player input correction RPC
  80. m_root->m_entity->FindComponent<NetBindComponent>()->EnablePlayerHostAutonomy(false);
  81. const auto component = m_root->m_entity->FindComponent<MultiplayerTest::RpcUnitTesterComponent>();
  82. component->GetTestController()->RPC_AuthorityToAutonomous();
  83. m_networkEntityManager->DispatchLocalDeferredRpcMessages();
  84. EXPECT_EQ(component->GetTestController()->m_authorityToAutonomousCalls, 0);
  85. }
  86. TEST_F(LocalRpcTests, LocalRpcAutonomousToAuthority)
  87. {
  88. m_root->m_entity->FindComponent<NetBindComponent>()->EnablePlayerHostAutonomy(true);
  89. const auto component = m_root->m_entity->FindComponent<MultiplayerTest::RpcUnitTesterComponent>();
  90. component->GetTestController()->RPC_AutonomousToAuthority();
  91. m_networkEntityManager->DispatchLocalDeferredRpcMessages();
  92. EXPECT_EQ(component->GetTestController()->m_autonomousToAuthorityCalls, 1);
  93. }
  94. }