DataObjectTests.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <string>
  9. #include <gmock/gmock.h>
  10. #include <gtest/gtest.h>
  11. #include <AzCore/RTTI/TypeInfo.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <ScenePopulation/ScenePopulation/Containers/DataObject.h>
  14. namespace Containers = AZ::ScenePopulation::Containers;
  15. class ComplexObject
  16. {
  17. public:
  18. AZ_RTTI(ComplexObject, "{1969D4A3-3714-4E1C-816E-E066538A2A0B}")
  19. ComplexObject()
  20. : m_value0(42)
  21. , m_value1("test")
  22. {}
  23. explicit ComplexObject(int value0)
  24. : m_value0(value0)
  25. , m_value1("test")
  26. {}
  27. ComplexObject(int value0, const std::string& value1)
  28. : m_value0(value0)
  29. , m_value1(value1)
  30. {}
  31. bool operator==(const ComplexObject& rhs) const
  32. {
  33. return m_value0 == rhs.m_value0 && m_value1 == rhs.m_value1;
  34. }
  35. int m_value0;
  36. std::string m_value1;
  37. };
  38. class DestructionClass
  39. {
  40. public:
  41. AZ_RTTI(DestructionClass, "{6EFE6C0C-80E8-4D62-9705-A7315049DFBF}")
  42. ~DestructionClass()
  43. {
  44. Destructor();
  45. }
  46. MOCK_METHOD0(Destructor, void());
  47. };
  48. class BaseInterface
  49. {
  50. public:
  51. AZ_RTTI(BaseInterface, "{723F701D-B718-432B-AE67-78F999A64883}")
  52. };
  53. class SecondInterface
  54. {
  55. public:
  56. AZ_RTTI(SecondInterface, "{8D0420F1-C102-4615-94F3-9D39E0D3A272}")
  57. };
  58. class SingleInheritance
  59. : public BaseInterface
  60. {
  61. public:
  62. AZ_RTTI(SingleInheritance, "{2D5EA157-8137-4FD4-A08D-58941FB149B2}", BaseInterface);
  63. };
  64. class MultipleInheritance
  65. : public BaseInterface
  66. , public SecondInterface
  67. {
  68. public:
  69. AZ_RTTI(MultipleInheritance, "{277E1CAA-EB89-4820-B37E-B49922FD0DF9}", BaseInterface, SecondInterface);
  70. };
  71. class ReflectionClass
  72. {
  73. public:
  74. AZ_RTTI(ReflectionClass, "{81E5CB64-B879-440F-9659-9D45AA544144}")
  75. uint32_t testVar;
  76. MOCK_METHOD0(Reflect, void());
  77. static ReflectionClass* currentTestClass;
  78. static void Reflect(AZ::ReflectContext* context)
  79. {
  80. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  81. if (serializeContext)
  82. {
  83. serializeContext->
  84. Class<ReflectionClass>()->
  85. Version(1)->
  86. Field("testVar", &ReflectionClass::testVar);
  87. }
  88. currentTestClass->Reflect();
  89. }
  90. };
  91. ReflectionClass* ReflectionClass::currentTestClass = nullptr;
  92. class DataObjectTest
  93. : public ::testing::Test
  94. {
  95. public:
  96. DataObjectTest()
  97. {
  98. }
  99. };
  100. namespace TestNamespace
  101. {
  102. class TestClassInNamespace
  103. {
  104. public:
  105. AZ_RTTI(TestClassInNamespace, "{F6CF64ED-6255-4E93-A34D-8BE58546C12E}")
  106. };
  107. }
  108. // Create
  109. TEST_F(DataObjectTest, Create_CreateUninitializedPOD_ValidDataObject)
  110. {
  111. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
  112. EXPECT_NE(nullptr, result);
  113. }
  114. TEST_F(DataObjectTest, Create_CreateInitializedPOD_ValidDataObject)
  115. {
  116. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>(42);
  117. EXPECT_NE(nullptr, result);
  118. }
  119. TEST_F(DataObjectTest, Create_CreateUninitializedComplexObject_ValidDataObject)
  120. {
  121. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>();
  122. EXPECT_NE(nullptr, result);
  123. }
  124. TEST_F(DataObjectTest, Create_CreateComplexObjectWithSingleParamter_ValidDataObject)
  125. {
  126. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>(42);
  127. EXPECT_NE(nullptr, result);
  128. }
  129. TEST_F(DataObjectTest, Create_CreateComplexObjectWithMultipileParamters_ValidDataObject)
  130. {
  131. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>(42, "Test string");
  132. EXPECT_NE(nullptr, result);
  133. }
  134. // IsType
  135. TEST_F(DataObjectTest, IsType_GetPODType_PODIsRecognizedAsType)
  136. {
  137. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
  138. ASSERT_NE(nullptr, result);
  139. EXPECT_TRUE(result->IsType<int>());
  140. }
  141. TEST_F(DataObjectTest, IsType_GetComplexClassType_ComplexObjectIsRecognizedAsType)
  142. {
  143. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>();
  144. ASSERT_NE(nullptr, result);
  145. EXPECT_TRUE(result->IsType<ComplexObject>());
  146. }
  147. TEST_F(DataObjectTest, IsType_SingleInheritance_BaseInterfaceFoundFromDerivedClass)
  148. {
  149. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<SingleInheritance>();
  150. ASSERT_NE(nullptr, result);
  151. EXPECT_TRUE(result->IsType<BaseInterface>());
  152. }
  153. TEST_F(DataObjectTest, IsType_MultipleInheritance_BothInterfacesFoundFromDerivedClass)
  154. {
  155. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<MultipleInheritance>();
  156. ASSERT_NE(nullptr, result);
  157. EXPECT_TRUE(result->IsType<BaseInterface>());
  158. EXPECT_TRUE(result->IsType<SecondInterface>());
  159. }
  160. // GetTypeName
  161. TEST_F(DataObjectTest, GetTypeName_GetNameOfPOD_NameOfPODIsInt)
  162. {
  163. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
  164. ASSERT_NE(nullptr, result);
  165. EXPECT_STRCASEEQ("int", result->GetTypeName());
  166. }
  167. TEST_F(DataObjectTest, GetTypeName_GetNameOfComplexClass_NameIsComplexObject)
  168. {
  169. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>();
  170. ASSERT_NE(nullptr, result);
  171. EXPECT_STRCASEEQ("ComplexObject", result->GetTypeName());
  172. }
  173. TEST_F(DataObjectTest, GetTypeName_GetNameOfComplexClassInNamespace_TypeNameOfClassWithoutNamespace)
  174. {
  175. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<TestNamespace::TestClassInNamespace>();
  176. ASSERT_NE(nullptr, result);
  177. EXPECT_STRCASEEQ("TestClassInNamespace", result->GetTypeName());
  178. }
  179. TEST_F(DataObjectTest, GetTypeName_GetNameOfTypedefedType_NameOfOriginalType)
  180. {
  181. using U32 = int;
  182. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<U32>();
  183. ASSERT_NE(nullptr, result);
  184. EXPECT_STRCASEEQ("int", result->GetTypeName());
  185. }
  186. // DynamicCast
  187. TEST_F(DataObjectTest, DynamicCast_CastToGivenPOD_ValidPointer)
  188. {
  189. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
  190. ASSERT_NE(nullptr, result);
  191. EXPECT_NE(nullptr, result->DynamicCast<int*>());
  192. }
  193. TEST_F(DataObjectTest, DynamicCast_CastToInvalidType_Nullptr)
  194. {
  195. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>();
  196. ASSERT_NE(nullptr, result);
  197. EXPECT_EQ(nullptr, result->DynamicCast<float*>());
  198. }
  199. TEST_F(DataObjectTest, DynamicCast_GivenPODDataIsAccesible_SameValueAsStored)
  200. {
  201. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<int>(42);
  202. ASSERT_NE(nullptr, result);
  203. int* value = result->DynamicCast<int*>();
  204. EXPECT_EQ(42, *value);
  205. }
  206. TEST_F(DataObjectTest, DynamicCast_GivenComplextDataIsAccesible_SameValueAsStored)
  207. {
  208. ComplexObject comparison = ComplexObject(42, "test");
  209. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ComplexObject>(42, "test");
  210. ASSERT_NE(nullptr, result);
  211. ComplexObject* value = result->DynamicCast<ComplexObject*>();
  212. EXPECT_EQ(comparison, *value);
  213. }
  214. // Destruction
  215. TEST_F(DataObjectTest, Destruction_DestructorCalledOnConstructedObject_DestructorCalled)
  216. {
  217. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<DestructionClass>();
  218. ASSERT_NE(nullptr, result);
  219. DestructionClass* value = result->DynamicCast<DestructionClass*>();
  220. ASSERT_NE(nullptr, value);
  221. EXPECT_CALL(*value, Destructor()).Times(1);
  222. }
  223. // Reflect
  224. TEST_F(DataObjectTest, Reflect_ReflectIsCalledOnStoredObject_ReflectCalled)
  225. {
  226. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ReflectionClass>();
  227. ReflectionClass* value = result->DynamicCast<ReflectionClass*>();
  228. ASSERT_NE(nullptr, value);
  229. ReflectionClass::currentTestClass = value;
  230. EXPECT_CALL(*value, Reflect()).Times(1);
  231. AZ::SerializeContext context;
  232. result->ReflectData(&context);
  233. }
  234. TEST_F(DataObjectTest, Reflect_ReflectIsCalledMultipleTimesOnSameStoredObject_ReflectCalledOnceAndNowAssertsFromSerializeContext)
  235. {
  236. std::unique_ptr<Containers::DataObject> result = Containers::DataObject::Create<ReflectionClass>();
  237. ReflectionClass* value = result->DynamicCast<ReflectionClass*>();
  238. ASSERT_NE(nullptr, value);
  239. ReflectionClass::currentTestClass = value;
  240. EXPECT_CALL(*value, Reflect()).Times(1);
  241. AZ::SerializeContext context;
  242. result->ReflectData(&context);
  243. result->ReflectData(&context);
  244. }