AWSGameLiftClientSystemComponentTest.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <AzCore/Component/ComponentApplication.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <AWSGameLiftClientFixture.h>
  13. #include <AWSGameLiftClientManager.h>
  14. #include <AWSGameLiftClientLocalTicketTracker.h>
  15. #include <AWSGameLiftClientSystemComponent.h>
  16. using namespace AWSGameLift;
  17. class AWSGameLiftClientManagerMock
  18. : public AWSGameLiftClientManager
  19. {
  20. public:
  21. AWSGameLiftClientManagerMock() = default;
  22. ~AWSGameLiftClientManagerMock() = default;
  23. MOCK_METHOD0(ActivateManager, void());
  24. MOCK_METHOD0(DeactivateManager, void());
  25. };
  26. class AWSGameLiftClientLocalTicketTrackerMock
  27. : public AWSGameLiftClientLocalTicketTracker
  28. {
  29. public:
  30. AWSGameLiftClientLocalTicketTrackerMock() = default;
  31. ~AWSGameLiftClientLocalTicketTrackerMock() = default;
  32. MOCK_METHOD0(ActivateTracker, void());
  33. MOCK_METHOD0(DeactivateTracker, void());
  34. };
  35. class TestAWSGameLiftClientSystemComponent
  36. : public AWSGameLiftClientSystemComponent
  37. {
  38. public:
  39. AZ_CLASS_ALLOCATOR(TestAWSGameLiftClientSystemComponent, AZ::SystemAllocator)
  40. TestAWSGameLiftClientSystemComponent()
  41. {
  42. m_gameliftClientManagerMockPtr = nullptr;
  43. m_gameliftClientTicketTrackerMockPtr = nullptr;
  44. }
  45. ~TestAWSGameLiftClientSystemComponent()
  46. {
  47. m_gameliftClientManagerMockPtr = nullptr;
  48. m_gameliftClientTicketTrackerMockPtr = nullptr;
  49. }
  50. void SetUpMockManager()
  51. {
  52. AZStd::unique_ptr<AWSGameLiftClientManagerMock> gameliftClientManagerMock =
  53. AZStd::make_unique<AWSGameLiftClientManagerMock>();
  54. m_gameliftClientManagerMockPtr = gameliftClientManagerMock.get();
  55. SetGameLiftClientManager(AZStd::move(gameliftClientManagerMock));
  56. AZStd::unique_ptr<AWSGameLiftClientLocalTicketTrackerMock> gameliftClientTicketTrackerMock =
  57. AZStd::make_unique<AWSGameLiftClientLocalTicketTrackerMock>();
  58. m_gameliftClientTicketTrackerMockPtr = gameliftClientTicketTrackerMock.get();
  59. SetGameLiftClientTicketTracker(AZStd::move(gameliftClientTicketTrackerMock));
  60. }
  61. AWSGameLiftClientManagerMock* m_gameliftClientManagerMockPtr;
  62. AWSGameLiftClientLocalTicketTrackerMock* m_gameliftClientTicketTrackerMockPtr;
  63. };
  64. class AWSCoreSystemComponentMock
  65. : public AZ::Component
  66. {
  67. public:
  68. AZ_COMPONENT(AWSCoreSystemComponentMock, "{52DB1342-30C6-412F-B7CC-B23F8B0629EA}");
  69. static void Reflect(AZ::ReflectContext* context)
  70. {
  71. AZ_UNUSED(context);
  72. }
  73. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  74. {
  75. provided.push_back(AZ_CRC_CE("AWSCoreService"));
  76. }
  77. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  78. {
  79. AZ_UNUSED(incompatible);
  80. }
  81. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  82. {
  83. AZ_UNUSED(required);
  84. }
  85. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  86. {
  87. AZ_UNUSED(dependent);
  88. }
  89. AWSCoreSystemComponentMock() = default;
  90. ~AWSCoreSystemComponentMock() = default;
  91. void Init() override {}
  92. void Activate() override {}
  93. void Deactivate() override {}
  94. };
  95. class AWSGameLiftClientSystemComponentTest
  96. : public AWSGameLiftClientFixture
  97. {
  98. protected:
  99. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  100. AZStd::unique_ptr<AZ::BehaviorContext> m_behaviorContext;
  101. AZStd::unique_ptr<AZ::ComponentDescriptor> m_coreComponentDescriptor;
  102. AZStd::unique_ptr<AZ::ComponentDescriptor> m_gameliftClientComponentDescriptor;
  103. void SetUp() override
  104. {
  105. AWSGameLiftClientFixture::SetUp();
  106. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  107. m_serializeContext->CreateEditContext();
  108. m_behaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
  109. m_coreComponentDescriptor.reset(AWSCoreSystemComponentMock::CreateDescriptor());
  110. m_gameliftClientComponentDescriptor.reset(TestAWSGameLiftClientSystemComponent::CreateDescriptor());
  111. m_gameliftClientComponentDescriptor->Reflect(m_serializeContext.get());
  112. m_gameliftClientComponentDescriptor->Reflect(m_behaviorContext.get());
  113. m_entity = aznew AZ::Entity();
  114. m_coreSystemComponent = AZStd::make_unique<AWSCoreSystemComponentMock>();
  115. m_entity->AddComponent(m_coreSystemComponent.get());
  116. m_gameliftClientSystemComponent = AZStd::make_unique<TestAWSGameLiftClientSystemComponent>();
  117. m_gameliftClientSystemComponent->SetUpMockManager();
  118. m_entity->AddComponent(m_gameliftClientSystemComponent.get());
  119. }
  120. void TearDown() override
  121. {
  122. m_entity->RemoveComponent(m_gameliftClientSystemComponent.get());
  123. m_gameliftClientSystemComponent.reset();
  124. m_entity->RemoveComponent(m_coreSystemComponent.get());
  125. m_coreSystemComponent.reset();
  126. delete m_entity;
  127. m_entity = nullptr;
  128. m_gameliftClientComponentDescriptor.reset();
  129. m_coreComponentDescriptor.reset();
  130. m_behaviorContext.reset();
  131. m_serializeContext.reset();
  132. AWSGameLiftClientFixture::TearDown();
  133. }
  134. public:
  135. AZStd::unique_ptr<AWSCoreSystemComponentMock> m_coreSystemComponent;
  136. AZStd::unique_ptr<TestAWSGameLiftClientSystemComponent> m_gameliftClientSystemComponent;
  137. AZ::Entity* m_entity;
  138. };
  139. TEST_F(AWSGameLiftClientSystemComponentTest, ActivateDeactivate_Call_GameLiftClientManagerGetsInvoked)
  140. {
  141. m_entity->Init();
  142. EXPECT_CALL(*(m_gameliftClientSystemComponent->m_gameliftClientManagerMockPtr), ActivateManager()).Times(1);
  143. EXPECT_CALL(*(m_gameliftClientSystemComponent->m_gameliftClientTicketTrackerMockPtr), ActivateTracker()).Times(1);
  144. m_entity->Activate();
  145. EXPECT_CALL(*(m_gameliftClientSystemComponent->m_gameliftClientManagerMockPtr), DeactivateManager()).Times(1);
  146. EXPECT_CALL(*(m_gameliftClientSystemComponent->m_gameliftClientTicketTrackerMockPtr), DeactivateTracker()).Times(1);
  147. m_entity->Deactivate();
  148. }