AWSMetricsSystemComponentTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <AWSCoreBus.h>
  9. #include <AWSMetricsSystemComponent.h>
  10. #include <AWSMetricsGemMock.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Component/Entity.h>
  14. namespace AWSMetrics
  15. {
  16. class AWSMetricsSystemComponentMock
  17. : public AWSMetricsSystemComponent
  18. {
  19. public:
  20. AZ_CLASS_ALLOCATOR(AWSMetricsSystemComponentMock, AZ::SystemAllocator)
  21. void InitMock()
  22. {
  23. AWSMetricsSystemComponent::Init();
  24. }
  25. void ActivateMock()
  26. {
  27. AWSMetricsSystemComponent::Activate();
  28. }
  29. void DeactivateMock()
  30. {
  31. AWSMetricsSystemComponent::Deactivate();
  32. }
  33. AWSMetricsSystemComponentMock()
  34. {
  35. ON_CALL(*this, Init()).WillByDefault(testing::Invoke(this, &AWSMetricsSystemComponentMock::InitMock));
  36. ON_CALL(*this, Activate()).WillByDefault(testing::Invoke(this, &AWSMetricsSystemComponentMock::ActivateMock));
  37. ON_CALL(*this, Deactivate()).WillByDefault(testing::Invoke(this, &AWSMetricsSystemComponentMock::DeactivateMock));
  38. }
  39. MOCK_METHOD0(Init, void());
  40. MOCK_METHOD0(Activate, void());
  41. MOCK_METHOD0(Deactivate, void());
  42. };
  43. class AWSCoreSystemComponentMock
  44. : public AZ::Component
  45. {
  46. public:
  47. AZ_COMPONENT(AWSCoreSystemComponentMock, "{D1D84E43-66FA-470B-9762-AE253EF46F92}");
  48. static void Reflect(AZ::ReflectContext* context)
  49. {
  50. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  51. {
  52. serialize->Class<AWSCoreSystemComponentMock, AZ::Component>()
  53. ->Version(0)
  54. ;
  55. if (AZ::EditContext* ec = serialize->GetEditContext())
  56. {
  57. ec->Class<AWSCoreSystemComponentMock>("AWSCoreMock", "Adds core support for working with AWS")
  58. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  59. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  60. ;
  61. }
  62. }
  63. }
  64. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  65. {
  66. provided.push_back(AZ_CRC_CE("AWSCoreService"));
  67. }
  68. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  69. {
  70. AZ_UNUSED(incompatible);
  71. }
  72. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  73. {
  74. AZ_UNUSED(required);
  75. }
  76. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  77. {
  78. AZ_UNUSED(dependent);
  79. }
  80. void ActivateMock()
  81. {
  82. AWSCore::AWSCoreNotificationsBus::Broadcast(&AWSCore::AWSCoreNotifications::OnSDKInitialized);
  83. }
  84. AWSCoreSystemComponentMock()
  85. {
  86. ON_CALL(*this, Activate()).WillByDefault(testing::Invoke(this, &AWSCoreSystemComponentMock::ActivateMock));
  87. }
  88. ~AWSCoreSystemComponentMock() = default;
  89. MOCK_METHOD0(Init, void());
  90. MOCK_METHOD0(Activate, void());
  91. MOCK_METHOD0(Deactivate, void());
  92. };
  93. class AWSMetricsSystemComponentTest
  94. : public AWSMetricsGemAllocatorFixture
  95. {
  96. protected:
  97. void SetUp() override
  98. {
  99. AWSMetricsGemAllocatorFixture::SetUp();
  100. m_awsCoreComponentDescriptor.reset(AWSCoreSystemComponentMock::CreateDescriptor());
  101. m_componentDescriptor.reset(AWSMetricsSystemComponentMock::CreateDescriptor());
  102. m_awsCoreComponentDescriptor->Reflect(m_serializeContext.get());
  103. m_componentDescriptor->Reflect(m_serializeContext.get());
  104. m_entity = aznew AZ::Entity();
  105. m_awsCoreSystemsComponent = aznew testing::NiceMock<AWSCoreSystemComponentMock>();
  106. m_AWSMetricsSystemsComponent = aznew testing::NiceMock<AWSMetricsSystemComponentMock>();
  107. m_entity->AddComponent(m_awsCoreSystemsComponent);
  108. m_entity->AddComponent(m_AWSMetricsSystemsComponent);
  109. }
  110. void TearDown() override
  111. {
  112. m_entity->RemoveComponent(m_AWSMetricsSystemsComponent);
  113. m_entity->RemoveComponent(m_awsCoreSystemsComponent);
  114. delete m_AWSMetricsSystemsComponent;
  115. delete m_awsCoreSystemsComponent;
  116. delete m_entity;
  117. m_componentDescriptor.reset();
  118. m_awsCoreComponentDescriptor.reset();
  119. AWSMetricsGemAllocatorFixture::TearDown();
  120. }
  121. AZStd::unique_ptr<AZ::ComponentDescriptor> m_componentDescriptor;
  122. AZStd::unique_ptr<AZ::ComponentDescriptor> m_awsCoreComponentDescriptor;
  123. public:
  124. testing::NiceMock<AWSMetricsSystemComponentMock>* m_AWSMetricsSystemsComponent = nullptr;
  125. testing::NiceMock<AWSCoreSystemComponentMock>* m_awsCoreSystemsComponent = nullptr;
  126. AZ::Entity* m_entity = nullptr;
  127. };
  128. TEST_F(AWSMetricsSystemComponentTest, ActivateComponent_NewEntity_Success)
  129. {
  130. testing::Sequence s1, s2;
  131. EXPECT_CALL(*m_awsCoreSystemsComponent, Init()).Times(1).InSequence(s1);
  132. EXPECT_CALL(*m_AWSMetricsSystemsComponent, Init()).Times(1).InSequence(s1);
  133. EXPECT_CALL(*m_awsCoreSystemsComponent, Activate()).Times(1).InSequence(s1);
  134. EXPECT_CALL(*m_AWSMetricsSystemsComponent, Activate()).Times(1).InSequence(s1);
  135. EXPECT_CALL(*m_AWSMetricsSystemsComponent, Deactivate()).Times(1).InSequence(s2);
  136. EXPECT_CALL(*m_awsCoreSystemsComponent, Deactivate()).Times(1).InSequence(s2);
  137. // initialize component
  138. m_entity->Init();
  139. // activate component
  140. m_entity->Activate();
  141. // deactivate component
  142. m_entity->Deactivate();
  143. }
  144. }