Interface.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/Interface/Interface.h>
  9. #include <AzCore/Memory/PoolAllocator.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. namespace UnitTest
  12. {
  13. class ITestSystem
  14. {
  15. public:
  16. AZ_RTTI(ITestSystem, "{E13935AC-5A72-432E-9E5D-72975CCC5CF9}");
  17. ITestSystem() = default;
  18. ITestSystem(ITestSystem&&) = delete;
  19. ITestSystem& operator=(ITestSystem&&) = delete;
  20. virtual ~ITestSystem() = default;
  21. virtual void SetNumber(uint32_t) = 0;
  22. virtual uint32_t GetNumber() const = 0;
  23. };
  24. class TestSystem
  25. : public ITestSystem
  26. {
  27. public:
  28. AZ::InterfaceRegisterOutcome Activate()
  29. {
  30. return AZ::Interface<ITestSystem>::Register(this);
  31. }
  32. AZ::InterfaceRegisterOutcome Deactivate()
  33. {
  34. return AZ::Interface<ITestSystem>::Unregister(this);
  35. }
  36. void SetNumber(uint32_t number) override
  37. {
  38. m_number = number;
  39. }
  40. uint32_t GetNumber() const override
  41. {
  42. return m_number;
  43. }
  44. private:
  45. uint32_t m_number = 0;
  46. };
  47. class TestSystemWithRegistrar
  48. : public AZ::Interface<ITestSystem>::Registrar
  49. {
  50. public:
  51. void SetNumber(uint32_t number) override
  52. {
  53. m_number = number;
  54. }
  55. uint32_t GetNumber() const override
  56. {
  57. return m_number;
  58. }
  59. private:
  60. uint32_t m_number = 0;
  61. };
  62. class InterfaceTest
  63. : public LeakDetectionFixture
  64. {
  65. };
  66. TEST_F(InterfaceTest, EmptyInterfaceTest)
  67. {
  68. ITestSystem* testSystemInterface = AZ::Interface<ITestSystem>::Get();
  69. EXPECT_EQ(testSystemInterface, nullptr);
  70. }
  71. TEST_F(InterfaceTest, EmptyAfterDisconnectTest)
  72. {
  73. TestSystem system;
  74. system.Activate();
  75. ITestSystem* valid = AZ::Interface<ITestSystem>::Get();
  76. EXPECT_NE(valid, nullptr);
  77. system.Deactivate();
  78. ITestSystem* invalid = AZ::Interface<ITestSystem>::Get();
  79. EXPECT_EQ(invalid, nullptr);
  80. }
  81. TEST_F(InterfaceTest, ValidInterfaceTest)
  82. {
  83. TestSystem system;
  84. system.Activate();
  85. {
  86. ITestSystem* testSystemInterface = AZ::Interface<ITestSystem>::Get();
  87. EXPECT_NE(testSystemInterface, nullptr);
  88. testSystemInterface->SetNumber(1);
  89. EXPECT_EQ(testSystemInterface->GetNumber(), 1);
  90. }
  91. system.Deactivate();
  92. }
  93. TEST_F(InterfaceTest, RegistrarTest)
  94. {
  95. ITestSystem* testSystemInterface = AZ::Interface<ITestSystem>::Get();
  96. EXPECT_EQ(testSystemInterface, nullptr);
  97. {
  98. TestSystemWithRegistrar system;
  99. testSystemInterface = AZ::Interface<ITestSystem>::Get();
  100. EXPECT_NE(testSystemInterface, nullptr);
  101. }
  102. testSystemInterface = AZ::Interface<ITestSystem>::Get();
  103. EXPECT_EQ(testSystemInterface, nullptr);
  104. }
  105. TEST_F(InterfaceTest, RegisterTwiceReturnsFailure)
  106. {
  107. TestSystem testSystem1;
  108. TestSystem testSystem2;
  109. EXPECT_TRUE(testSystem1.Activate());
  110. // This should fail.
  111. EXPECT_FALSE(testSystem2.Activate());
  112. // This should also fail.
  113. EXPECT_FALSE(testSystem2.Deactivate());
  114. EXPECT_TRUE(testSystem1.Deactivate());
  115. }
  116. TEST_F(InterfaceTest, RegisterMismatchTest)
  117. {
  118. TestSystem testSystem1;
  119. TestSystem testSystem2;
  120. EXPECT_TRUE(testSystem1.Activate());
  121. // This should fail.
  122. EXPECT_FALSE(testSystem2.Deactivate());
  123. EXPECT_TRUE(testSystem1.Deactivate());
  124. }
  125. }