BsEditorTestSuite.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Testing/BsEditorTestSuite.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "UndoRedo/BsCmdDeleteSO.h"
  6. #include "UndoRedo/BsUndoRedo.h"
  7. #include "Reflection/BsRTTIType.h"
  8. #include "Private/RTTI/BsGameObjectRTTI.h"
  9. #include "Serialization/BsBinarySerializer.h"
  10. #include "Serialization/BsMemorySerializer.h"
  11. #include "Serialization/BsBinaryDiff.h"
  12. #include "Scene/BsPrefab.h"
  13. #include "Resources/BsResources.h"
  14. #include "Scene/BsPrefabDiff.h"
  15. #include "FileSystem/BsFileSystem.h"
  16. #include "Scene/BsSceneManager.h"
  17. #include "Scene/BsSerializedSceneObject.h"
  18. namespace bs
  19. {
  20. class TestComponentARTTI : public RTTIType<TestComponentA, Component, TestComponentARTTI>
  21. {
  22. private:
  23. HSceneObject& getRef1(TestComponentA* obj) { return obj->ref1; }
  24. void setRef1(TestComponentA* obj, HSceneObject& val) { obj->ref1 = val; }
  25. HComponent& getRef2(TestComponentA* obj) { return obj->ref2; }
  26. void setRef2(TestComponentA* obj, HComponent& val) { obj->ref2 = val; }
  27. public:
  28. TestComponentARTTI()
  29. {
  30. addReflectableField("ref1", 0, &TestComponentARTTI::getRef1, &TestComponentARTTI::setRef1);
  31. addReflectableField("ref2", 1, &TestComponentARTTI::getRef2, &TestComponentARTTI::setRef2);
  32. }
  33. const String& getRTTIName() override
  34. {
  35. static String name = "TestComponentA";
  36. return name;
  37. }
  38. UINT32 getRTTIId() override
  39. {
  40. return TID_TestComponentA;
  41. }
  42. SPtr<IReflectable> newRTTIObject() override
  43. {
  44. return GameObjectRTTI::createGameObject<TestComponentA>();
  45. }
  46. };
  47. class TestComponentBRTTI : public RTTIType<TestComponentB, Component, TestComponentBRTTI>
  48. {
  49. private:
  50. HSceneObject& getRef1(TestComponentB* obj) { return obj->ref1; }
  51. void setRef1(TestComponentB* obj, HSceneObject& val) { obj->ref1 = val; }
  52. String& getVal1(TestComponentB* obj) { return obj->val1; }
  53. void setVal1(TestComponentB* obj, String& val) { obj->val1 = val; }
  54. public:
  55. TestComponentBRTTI()
  56. {
  57. addReflectableField("ref1", 0, &TestComponentBRTTI::getRef1, &TestComponentBRTTI::setRef1);
  58. addPlainField("val1", 1, &TestComponentBRTTI::getVal1, &TestComponentBRTTI::setVal1);
  59. }
  60. const String& getRTTIName() override
  61. {
  62. static String name = "TestComponentB";
  63. return name;
  64. }
  65. UINT32 getRTTIId() override
  66. {
  67. return TID_TestComponentB;
  68. }
  69. SPtr<IReflectable> newRTTIObject() override
  70. {
  71. return GameObjectRTTI::createGameObject<TestComponentB>();
  72. }
  73. };
  74. TestComponentA::TestComponentA(const HSceneObject& parent)
  75. :Component(parent)
  76. {}
  77. RTTITypeBase* TestComponentA::getRTTIStatic()
  78. {
  79. return TestComponentARTTI::instance();
  80. }
  81. RTTITypeBase* TestComponentA::getRTTI() const
  82. {
  83. return TestComponentA::getRTTIStatic();
  84. }
  85. TestComponentB::TestComponentB(const HSceneObject& parent)
  86. :Component(parent)
  87. {}
  88. RTTITypeBase* TestComponentB::getRTTIStatic()
  89. {
  90. return TestComponentBRTTI::instance();
  91. }
  92. RTTITypeBase* TestComponentB::getRTTI() const
  93. {
  94. return TestComponentB::getRTTIStatic();
  95. }
  96. struct TestObjectB : IReflectable
  97. {
  98. UINT32 intA = 100;
  99. String strA = "100";
  100. /************************************************************************/
  101. /* RTTI */
  102. /************************************************************************/
  103. public:
  104. friend class TestObjectBRTTI;
  105. static RTTITypeBase* getRTTIStatic();
  106. RTTITypeBase* getRTTI() const override;
  107. };
  108. struct TestObjectA : IReflectable
  109. {
  110. TestObjectA()
  111. {
  112. arrStrA = { "10", "11", "12" };
  113. arrStrB = { "13", "14", "15" };
  114. arrStrC = { "16", "17", "18" };
  115. arrObjA = { TestObjectB(), TestObjectB(), TestObjectB() };
  116. arrObjB = { TestObjectB(), TestObjectB(), TestObjectB() };
  117. arrObjPtrA = { bs_shared_ptr_new<TestObjectB>(), bs_shared_ptr_new<TestObjectB>(), bs_shared_ptr_new<TestObjectB>() };
  118. arrObjPtrB = { bs_shared_ptr_new<TestObjectB>(), bs_shared_ptr_new<TestObjectB>(), bs_shared_ptr_new<TestObjectB>() };
  119. }
  120. UINT32 intA = 5;
  121. String strA = "5";
  122. String strB = "7";
  123. TestObjectB objA;
  124. TestObjectB objB;
  125. SPtr<TestObjectB> objPtrA = bs_shared_ptr_new<TestObjectB>();
  126. SPtr<TestObjectB> objPtrB = bs_shared_ptr_new<TestObjectB>();
  127. SPtr<TestObjectB> objPtrC = bs_shared_ptr_new<TestObjectB>();
  128. SPtr<TestObjectB> objPtrD = nullptr;
  129. Vector<String> arrStrA;
  130. Vector<String> arrStrB;
  131. Vector<String> arrStrC;
  132. Vector<TestObjectB> arrObjA;
  133. Vector<TestObjectB> arrObjB;
  134. Vector<SPtr<TestObjectB>> arrObjPtrA;
  135. Vector<SPtr<TestObjectB>> arrObjPtrB;
  136. /************************************************************************/
  137. /* RTTI */
  138. /************************************************************************/
  139. public:
  140. friend class TestObjectARTTI;
  141. static RTTITypeBase* getRTTIStatic();
  142. RTTITypeBase* getRTTI() const override;
  143. };
  144. class TestObjectARTTI : public RTTIType < TestObjectA, IReflectable, TestObjectARTTI >
  145. {
  146. private:
  147. BS_BEGIN_RTTI_MEMBERS
  148. BS_RTTI_MEMBER_PLAIN(intA, 0)
  149. BS_RTTI_MEMBER_PLAIN(strA, 1)
  150. BS_RTTI_MEMBER_PLAIN(strB, 2)
  151. BS_RTTI_MEMBER_REFL(objA, 3)
  152. BS_RTTI_MEMBER_REFL(objB, 4)
  153. BS_RTTI_MEMBER_REFLPTR(objPtrA, 5)
  154. BS_RTTI_MEMBER_REFLPTR(objPtrB, 6)
  155. BS_RTTI_MEMBER_REFLPTR(objPtrC, 7)
  156. BS_RTTI_MEMBER_REFLPTR(objPtrD, 8)
  157. BS_RTTI_MEMBER_PLAIN_ARRAY(arrStrA, 9)
  158. BS_RTTI_MEMBER_PLAIN_ARRAY(arrStrB, 10)
  159. BS_RTTI_MEMBER_PLAIN_ARRAY(arrStrC, 11)
  160. BS_RTTI_MEMBER_REFL_ARRAY(arrObjA, 12)
  161. BS_RTTI_MEMBER_REFL_ARRAY(arrObjB, 13)
  162. BS_RTTI_MEMBER_REFLPTR_ARRAY(arrObjPtrA, 14)
  163. BS_RTTI_MEMBER_REFLPTR_ARRAY(arrObjPtrB, 15)
  164. BS_END_RTTI_MEMBERS
  165. public:
  166. TestObjectARTTI()
  167. :mInitMembers(this)
  168. { }
  169. const String& getRTTIName() override
  170. {
  171. static String name = "TestObjectA";
  172. return name;
  173. }
  174. UINT32 getRTTIId() override
  175. {
  176. return TID_TestObjectA;
  177. }
  178. SPtr<IReflectable> newRTTIObject() override
  179. {
  180. return bs_shared_ptr_new<TestObjectA>();
  181. }
  182. };
  183. class TestObjectBRTTI : public RTTIType < TestObjectB, IReflectable, TestObjectBRTTI >
  184. {
  185. private:
  186. BS_BEGIN_RTTI_MEMBERS
  187. BS_RTTI_MEMBER_PLAIN(intA, 0)
  188. BS_RTTI_MEMBER_PLAIN(strA, 1)
  189. BS_END_RTTI_MEMBERS
  190. public:
  191. TestObjectBRTTI()
  192. :mInitMembers(this)
  193. { }
  194. const String& getRTTIName() override
  195. {
  196. static String name = "TestObjectB";
  197. return name;
  198. }
  199. UINT32 getRTTIId() override
  200. {
  201. return TID_TestObjectB;
  202. }
  203. SPtr<IReflectable> newRTTIObject() override
  204. {
  205. return bs_shared_ptr_new<TestObjectB>();
  206. }
  207. };
  208. RTTITypeBase* TestObjectB::getRTTIStatic()
  209. {
  210. return TestObjectBRTTI::instance();
  211. }
  212. RTTITypeBase* TestObjectB::getRTTI() const
  213. {
  214. return TestObjectB::getRTTIStatic();
  215. }
  216. RTTITypeBase* TestObjectA::getRTTIStatic()
  217. {
  218. return TestObjectARTTI::instance();
  219. }
  220. RTTITypeBase* TestObjectA::getRTTI() const
  221. {
  222. return TestObjectA::getRTTIStatic();
  223. }
  224. class TestComponentC : public Component
  225. {
  226. public:
  227. TestObjectA obj;
  228. /************************************************************************/
  229. /* COMPONENT OVERRIDES */
  230. /************************************************************************/
  231. protected:
  232. friend class SceneObject;
  233. TestComponentC(const HSceneObject& parent)
  234. :Component(parent)
  235. {}
  236. /************************************************************************/
  237. /* RTTI */
  238. /************************************************************************/
  239. public:
  240. friend class TestComponentCRTTI;
  241. static RTTITypeBase* getRTTIStatic();
  242. RTTITypeBase* getRTTI() const override;
  243. protected:
  244. TestComponentC() {} // Serialization only
  245. };
  246. class TestComponentD : public Component
  247. {
  248. public:
  249. TestObjectB obj;
  250. /************************************************************************/
  251. /* COMPONENT OVERRIDES */
  252. /************************************************************************/
  253. protected:
  254. friend class SceneObject;
  255. TestComponentD(const HSceneObject& parent)
  256. :Component(parent)
  257. {}
  258. /************************************************************************/
  259. /* RTTI */
  260. /************************************************************************/
  261. public:
  262. friend class TestComponentDRTTI;
  263. static RTTITypeBase* getRTTIStatic();
  264. virtual RTTITypeBase* getRTTI() const override;
  265. protected:
  266. TestComponentD() {} // Serialization only
  267. };
  268. class TestComponentCRTTI : public RTTIType < TestComponentC, Component, TestComponentCRTTI >
  269. {
  270. private:
  271. BS_BEGIN_RTTI_MEMBERS
  272. BS_RTTI_MEMBER_REFL(obj, 0)
  273. BS_END_RTTI_MEMBERS
  274. public:
  275. TestComponentCRTTI()
  276. :mInitMembers(this)
  277. { }
  278. const String& getRTTIName() override
  279. {
  280. static String name = "TestComponentC";
  281. return name;
  282. }
  283. UINT32 getRTTIId() override
  284. {
  285. return TID_TestComponentC;
  286. }
  287. SPtr<IReflectable> newRTTIObject() override
  288. {
  289. return GameObjectRTTI::createGameObject<TestComponentC>();
  290. }
  291. };
  292. class TestComponentDRTTI : public RTTIType < TestComponentD, Component, TestComponentDRTTI >
  293. {
  294. private:
  295. BS_BEGIN_RTTI_MEMBERS
  296. BS_RTTI_MEMBER_REFL(obj, 0)
  297. BS_END_RTTI_MEMBERS
  298. public:
  299. TestComponentDRTTI()
  300. :mInitMembers(this)
  301. { }
  302. const String& getRTTIName() override
  303. {
  304. static String name = "TestComponentD";
  305. return name;
  306. }
  307. UINT32 getRTTIId() override
  308. {
  309. return TID_TestComponentD;
  310. }
  311. SPtr<IReflectable> newRTTIObject() override
  312. {
  313. return GameObjectRTTI::createGameObject<TestComponentD>();
  314. }
  315. };
  316. RTTITypeBase* TestComponentC::getRTTIStatic()
  317. {
  318. return TestComponentCRTTI::instance();
  319. }
  320. RTTITypeBase* TestComponentC::getRTTI() const
  321. {
  322. return TestComponentC::getRTTIStatic();
  323. }
  324. RTTITypeBase* TestComponentD::getRTTIStatic()
  325. {
  326. return TestComponentDRTTI::instance();
  327. }
  328. RTTITypeBase* TestComponentD::getRTTI() const
  329. {
  330. return TestComponentD::getRTTIStatic();
  331. }
  332. EditorTestSuite::EditorTestSuite()
  333. {
  334. BS_ADD_TEST(EditorTestSuite::SceneObjectRecord_UndoRedo);
  335. BS_ADD_TEST(EditorTestSuite::SceneObjectDelete_UndoRedo);
  336. BS_ADD_TEST(EditorTestSuite::BinaryDiff);
  337. BS_ADD_TEST(EditorTestSuite::TestPrefabComplex);
  338. BS_ADD_TEST(EditorTestSuite::TestPrefabDiff);
  339. BS_ADD_TEST(EditorTestSuite::TestFrameAlloc);
  340. }
  341. void EditorTestSuite::SceneObjectRecord_UndoRedo()
  342. {
  343. HSceneObject so0_0 = SceneObject::create("so0_0");
  344. HSceneObject so1_0 = SceneObject::create("so1_0");
  345. HSceneObject so1_1 = SceneObject::create("so1_1");
  346. HSceneObject so2_0 = SceneObject::create("so2_0");
  347. so1_0->setParent(so0_0);
  348. so1_1->setParent(so0_0);
  349. so2_0->setParent(so1_0);
  350. GameObjectHandle<TestComponentA> cmpA1_1 = so1_1->addComponent<TestComponentA>();
  351. GameObjectHandle<TestComponentB> cmpB1_1 = so0_0->addComponent<TestComponentB>();
  352. HSceneObject soExternal = SceneObject::create("soExternal");
  353. GameObjectHandle<TestComponentA> cmpExternal = soExternal->addComponent<TestComponentA>();
  354. cmpA1_1->ref1 = so2_0;
  355. cmpA1_1->ref2 = static_object_cast<Component>(cmpB1_1);
  356. cmpB1_1->ref1 = soExternal;
  357. cmpB1_1->val1 = "InitialValue";
  358. cmpExternal->ref1 = so1_1;
  359. cmpExternal->ref2 = static_object_cast<Component>(cmpA1_1);
  360. auto serializedSO = bs_shared_ptr_new<SerializedSceneObject>(so0_0);
  361. cmpB1_1->val1 = "ModifiedValue";
  362. so0_0->setName("modified");
  363. serializedSO->restore();
  364. BS_TEST_ASSERT(!so0_0.isDestroyed());
  365. BS_TEST_ASSERT(!so1_0.isDestroyed());
  366. BS_TEST_ASSERT(!so1_1.isDestroyed());
  367. BS_TEST_ASSERT(!so2_0.isDestroyed());
  368. BS_TEST_ASSERT(!cmpA1_1.isDestroyed());
  369. BS_TEST_ASSERT(!cmpB1_1.isDestroyed());
  370. BS_TEST_ASSERT(!cmpA1_1->ref1.isDestroyed());
  371. BS_TEST_ASSERT(!cmpA1_1->ref2.isDestroyed());
  372. BS_TEST_ASSERT(!cmpB1_1->ref1.isDestroyed());
  373. BS_TEST_ASSERT(!cmpExternal->ref1.isDestroyed());
  374. BS_TEST_ASSERT(!cmpExternal->ref2.isDestroyed());
  375. BS_TEST_ASSERT(cmpB1_1->val1 == "InitialValue");
  376. BS_TEST_ASSERT(so0_0->getName() == "so0_0");
  377. so0_0->destroy();
  378. soExternal->destroy();
  379. }
  380. void EditorTestSuite::SceneObjectDelete_UndoRedo()
  381. {
  382. HSceneObject so0_0 = SceneObject::create("so0_0");
  383. HSceneObject so1_0 = SceneObject::create("so1_0");
  384. HSceneObject so1_1 = SceneObject::create("so1_1");
  385. HSceneObject so2_0 = SceneObject::create("so2_0");
  386. so1_0->setParent(so0_0);
  387. so1_1->setParent(so0_0);
  388. so2_0->setParent(so1_0);
  389. GameObjectHandle<TestComponentA> cmpA1_1 = so1_1->addComponent<TestComponentA>();
  390. GameObjectHandle<TestComponentB> cmpB1_1 = so1_1->addComponent<TestComponentB>();
  391. HSceneObject soExternal = SceneObject::create("soExternal");
  392. GameObjectHandle<TestComponentA> cmpExternal = soExternal->addComponent<TestComponentA>();
  393. cmpA1_1->ref1 = so2_0;
  394. cmpA1_1->ref2 = static_object_cast<Component>(cmpB1_1);
  395. cmpB1_1->ref1 = soExternal;
  396. cmpB1_1->val1 = "InitialValue";
  397. cmpExternal->ref1 = so1_1;
  398. cmpExternal->ref2 = static_object_cast<Component>(cmpA1_1);
  399. CmdDeleteSO::execute(so0_0);
  400. UndoRedo::instance().undo();
  401. BS_TEST_ASSERT(!so0_0.isDestroyed());
  402. BS_TEST_ASSERT(!so1_0.isDestroyed());
  403. BS_TEST_ASSERT(!so1_1.isDestroyed());
  404. BS_TEST_ASSERT(!so2_0.isDestroyed());
  405. BS_TEST_ASSERT(!cmpA1_1.isDestroyed());
  406. BS_TEST_ASSERT(!cmpB1_1.isDestroyed());
  407. BS_TEST_ASSERT(!cmpA1_1->ref1.isDestroyed());
  408. BS_TEST_ASSERT(!cmpA1_1->ref2.isDestroyed());
  409. BS_TEST_ASSERT(!cmpB1_1->ref1.isDestroyed());
  410. BS_TEST_ASSERT(!cmpExternal->ref1.isDestroyed());
  411. BS_TEST_ASSERT(!cmpExternal->ref2.isDestroyed());
  412. BS_TEST_ASSERT(cmpB1_1->val1 == "InitialValue");
  413. so0_0->destroy();
  414. soExternal->destroy();
  415. }
  416. void EditorTestSuite::BinaryDiff()
  417. {
  418. SPtr<TestObjectA> orgObj = bs_shared_ptr_new<TestObjectA>();
  419. SPtr<TestObjectA> newObj = bs_shared_ptr_new<TestObjectA>();
  420. newObj->intA = 995;
  421. newObj->strA = "potato";
  422. newObj->arrStrB = { "orange", "carrot" };
  423. newObj->arrStrC[2] = "banana";
  424. newObj->objB.intA = 9940;
  425. newObj->objPtrB->strA = "kiwi";
  426. newObj->objPtrC = nullptr;
  427. newObj->objPtrD = bs_shared_ptr_new<TestObjectB>();
  428. newObj->arrObjB[1].strA = "strawberry";
  429. newObj->arrObjPtrB[0]->intA = 99100;
  430. SPtr<SerializedObject> orgSerialized = SerializedObject::create(*orgObj.get());
  431. SPtr<SerializedObject> newSerialized = SerializedObject::create(*newObj.get());
  432. IDiff& diffHandler = orgObj->getRTTI()->getDiffHandler();
  433. SPtr<SerializedObject> objDiff = diffHandler.generateDiff(orgSerialized, newSerialized);
  434. diffHandler.applyDiff(orgObj, objDiff, nullptr);
  435. BS_TEST_ASSERT(orgObj->intA == newObj->intA);
  436. BS_TEST_ASSERT(orgObj->strA == newObj->strA);
  437. BS_TEST_ASSERT(orgObj->strB == newObj->strB);
  438. BS_TEST_ASSERT(orgObj->objA.intA == newObj->objA.intA);
  439. BS_TEST_ASSERT(orgObj->objB.intA == newObj->objB.intA);
  440. BS_TEST_ASSERT(orgObj->objPtrA->strA == newObj->objPtrA->strA);
  441. BS_TEST_ASSERT(orgObj->objPtrB->strA == newObj->objPtrB->strA);
  442. BS_TEST_ASSERT(orgObj->objPtrD->strA == newObj->objPtrD->strA);
  443. BS_TEST_ASSERT(orgObj->objPtrC == newObj->objPtrC);
  444. BS_TEST_ASSERT(orgObj->arrStrA.size() == newObj->arrStrA.size());
  445. for (UINT32 i = 0; i < (UINT32)orgObj->arrStrA.size(); i++)
  446. BS_TEST_ASSERT(orgObj->arrStrA[i] == newObj->arrStrA[i]);
  447. BS_TEST_ASSERT(orgObj->arrStrB.size() == newObj->arrStrB.size());
  448. for (UINT32 i = 0; i < (UINT32)orgObj->arrStrB.size(); i++)
  449. BS_TEST_ASSERT(orgObj->arrStrB[i] == newObj->arrStrB[i]);
  450. BS_TEST_ASSERT(orgObj->arrStrC.size() == newObj->arrStrC.size());
  451. for (UINT32 i = 0; i < (UINT32)orgObj->arrStrC.size(); i++)
  452. BS_TEST_ASSERT(orgObj->arrStrC[i] == newObj->arrStrC[i]);
  453. BS_TEST_ASSERT(orgObj->arrObjA.size() == newObj->arrObjA.size());
  454. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjA.size(); i++)
  455. BS_TEST_ASSERT(orgObj->arrObjA[i].strA == newObj->arrObjA[i].strA);
  456. BS_TEST_ASSERT(orgObj->arrObjB.size() == newObj->arrObjB.size());
  457. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjB.size(); i++)
  458. BS_TEST_ASSERT(orgObj->arrObjB[i].strA == newObj->arrObjB[i].strA);
  459. BS_TEST_ASSERT(orgObj->arrObjPtrA.size() == newObj->arrObjPtrA.size());
  460. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjPtrA.size(); i++)
  461. BS_TEST_ASSERT(orgObj->arrObjPtrA[i]->intA == newObj->arrObjPtrA[i]->intA);
  462. BS_TEST_ASSERT(orgObj->arrObjPtrB.size() == newObj->arrObjPtrB.size());
  463. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjPtrB.size(); i++)
  464. BS_TEST_ASSERT(orgObj->arrObjPtrB[i]->intA == newObj->arrObjPtrB[i]->intA);
  465. }
  466. void EditorTestSuite::TestPrefabComplex()
  467. {
  468. HSceneObject aDeleteMe = SceneObject::create("A");
  469. HSceneObject box = SceneObject::create("Box");
  470. box->addComponent<TestComponentA>();
  471. HSceneObject light = SceneObject::create("Directional light");
  472. light->addComponent<TestComponentA>();
  473. HSceneObject sceneRoot = gSceneManager().getMainScene()->getRoot();
  474. HPrefab scenePrefab = Prefab::create(sceneRoot, true);
  475. HSceneObject bDeleteMe = SceneObject::create("B");
  476. HSceneObject _1 = SceneObject::create("1");
  477. scenePrefab->update(sceneRoot);
  478. aDeleteMe->destroy();
  479. bDeleteMe->destroy();
  480. HPrefab targetBoxPrefab = Prefab::create(_1, false);
  481. HSceneObject camera = SceneObject::create("Camera");
  482. camera->addComponent<TestComponentA>();
  483. scenePrefab->update(sceneRoot);
  484. HSceneObject target = SceneObject::create("Target");
  485. _1->setParent(target);
  486. HSceneObject _3 = targetBoxPrefab->instantiate();
  487. _3->setName("3");
  488. _3->setParent(target);
  489. HSceneObject _10 = targetBoxPrefab->instantiate();
  490. _10->setParent(target);
  491. _10->setName("10");
  492. // Ensure multiple instances of the same prefab don't have the same ID
  493. BS_TEST_ASSERT(_1->getLinkId() != _3->getLinkId() && _1->getLinkId() != _10->getLinkId());
  494. // Ensure new instances of a prefab have -1 root link ID
  495. BS_TEST_ASSERT(_3->getLinkId() == (UINT32)-1 && _10->getLinkId() == (UINT32)-1);
  496. scenePrefab->update(sceneRoot);
  497. _1->addComponent<TestComponentA>();
  498. _3->addComponent<TestComponentA>();
  499. _10->addComponent<TestComponentA>();
  500. _1->addComponent<TestComponentB>();
  501. _3->addComponent<TestComponentB>();
  502. _10->addComponent<TestComponentB>();
  503. _1->breakPrefabLink();
  504. _3->breakPrefabLink();
  505. _10->breakPrefabLink();
  506. HPrefab targetPrefab = Prefab::create(target, false);
  507. target->addComponent<TestComponentA>();
  508. scenePrefab->update(sceneRoot);
  509. targetPrefab->update(target);
  510. box->destroy();
  511. light->destroy();
  512. camera->destroy();
  513. target->destroy();
  514. }
  515. void EditorTestSuite::TestPrefabDiff()
  516. {
  517. HSceneObject root = SceneObject::create("root");
  518. HSceneObject so0 = SceneObject::create("so0");
  519. HSceneObject so1 = SceneObject::create("so1");
  520. HSceneObject so2 = SceneObject::create("so2");
  521. HSceneObject so0_0 = SceneObject::create("so0_0");
  522. HSceneObject so0_1 = SceneObject::create("so0_1");
  523. HSceneObject so1_0 = SceneObject::create("so1_0");
  524. HSceneObject so1_1 = SceneObject::create("so1_1");
  525. HSceneObject so1_2 = SceneObject::create("so1_2");
  526. HSceneObject so2_0 = SceneObject::create("so2_0");
  527. so0->setParent(root);
  528. so1->setParent(root);
  529. so2->setParent(root);
  530. so0_0->setParent(so0);
  531. so0_1->setParent(so0);
  532. so1_0->setParent(so1);
  533. so1_1->setParent(so1);
  534. so1_2->setParent(so1);
  535. so2_0->setParent(so2);
  536. GameObjectHandle<TestComponentC> cmp0 = so0->addComponent<TestComponentC>();
  537. GameObjectHandle<TestComponentC> cmp0_1_A = so0_1->addComponent<TestComponentC>();
  538. GameObjectHandle<TestComponentD> cmp0_1_B = so0_1->addComponent<TestComponentD>();
  539. GameObjectHandle<TestComponentD> cmp1 = so1->addComponent<TestComponentD>();
  540. GameObjectHandle<TestComponentD> cmp1_2 = so1_2->addComponent<TestComponentD>();
  541. GameObjectHandle<TestComponentD> cmp2 = so2->addComponent<TestComponentD>();
  542. Path prefabPath = Path::combine(FileSystem::getTempDirectoryPath(), "testprefab.asset");
  543. HPrefab prefab = Prefab::create(root);
  544. gResources().save(prefab, prefabPath, true);
  545. // Perform modifications
  546. GameObjectHandle<TestComponentC> cmp1_3;
  547. GameObjectHandle<TestComponentD> cmp3;
  548. HSceneObject so1_3, so2_1, so3;
  549. {
  550. cmp0->obj.strA = "banana";
  551. so0_0->destroy();
  552. cmp0_1_A->destroy();
  553. so1_3 = SceneObject::create("so1_2");
  554. so1_3->setParent(so1);
  555. cmp1_3 = so1_3->addComponent<TestComponentC>();
  556. cmp1_3->obj.intA = 999;
  557. so1_0->setName("apple");
  558. so1_1->destroy();
  559. cmp1_2->destroy();
  560. cmp1_2 = so1_2->addComponent<TestComponentD>();
  561. cmp1_2->obj.strA = "orange";
  562. so2_1 = SceneObject::create("so2_1");
  563. so2_1->setParent(so2);
  564. so2_0->addComponent<TestComponentD>();
  565. so3 = SceneObject::create("so3");
  566. so3->setParent(root);
  567. cmp3 = so3->addComponent<TestComponentD>();
  568. }
  569. SPtr<PrefabDiff> prefabDiff = PrefabDiff::create(prefab->_getRoot(), root);
  570. prefab = gResources().load<Prefab>(prefabPath);
  571. HSceneObject newRoot = prefab->instantiate();
  572. prefabDiff->apply(newRoot);
  573. // Compare and assert
  574. BS_TEST_ASSERT(root->getNumChildren() == newRoot->getNumChildren());
  575. HSceneObject nso0 = newRoot->getChild(0);
  576. GameObjectHandle<TestComponentC> ncmp0 = nso0->getComponent<TestComponentC>();
  577. BS_TEST_ASSERT(cmp0->obj.strA == ncmp0->obj.strA);
  578. BS_TEST_ASSERT(so0->getNumChildren() == nso0->getNumChildren());
  579. HSceneObject nso0_1 = nso0->getChild(0);
  580. GameObjectHandle<TestComponentD> ncmp0_1 = nso0_1->getComponent<TestComponentD>();
  581. BS_TEST_ASSERT(ncmp0_1 != nullptr);
  582. HSceneObject nso1 = newRoot->getChild(1);
  583. BS_TEST_ASSERT(so1->getNumChildren() == nso1->getNumChildren());
  584. HSceneObject nso1_0 = nso1->getChild(0);
  585. BS_TEST_ASSERT(so1_0->getName() == nso1_0->getName());
  586. HSceneObject nso1_2 = nso1->getChild(1);
  587. GameObjectHandle<TestComponentD> ncmp1_2 = nso1_2->getComponent<TestComponentD>();
  588. BS_TEST_ASSERT(cmp1_2->obj.strA == ncmp1_2->obj.strA);
  589. HSceneObject nso1_3 = nso1->getChild(2);
  590. GameObjectHandle<TestComponentC> ncmp1_3 = nso1_3->getComponent<TestComponentC>();
  591. BS_TEST_ASSERT(cmp1_3->obj.intA == ncmp1_3->obj.intA);
  592. HSceneObject nso2 = newRoot->getChild(2);
  593. BS_TEST_ASSERT(so2->getNumChildren() == nso2->getNumChildren());
  594. HSceneObject nso2_0 = nso2->getChild(0);
  595. GameObjectHandle<TestComponentD> ncmp2_0 = nso2_0->getComponent<TestComponentD>();
  596. BS_TEST_ASSERT(ncmp2_0 != nullptr);
  597. HSceneObject nso3 = newRoot->getChild(3);
  598. GameObjectHandle<TestComponentD> ncmp3 = nso3->getComponent<TestComponentD>();
  599. BS_TEST_ASSERT(ncmp3 != nullptr);
  600. root->destroy();
  601. newRoot->destroy();
  602. }
  603. void EditorTestSuite::TestFrameAlloc()
  604. {
  605. FrameAlloc alloc(128);
  606. alloc.markFrame();
  607. UINT8* a1 = alloc.alloc(5);
  608. UINT8* a2 = alloc.alloc(10);
  609. UINT8* a3 = alloc.alloc(130);
  610. UINT8* a4 = alloc.alloc(5);
  611. alloc.free(a1);
  612. alloc.free(a2);
  613. alloc.free(a3);
  614. alloc.free(a4);
  615. alloc.clear();
  616. alloc.markFrame();
  617. UINT8* a5 = alloc.alloc(5);
  618. UINT8* a6 = alloc.alloc(10);
  619. UINT8* a7 = alloc.alloc(130);
  620. UINT8* a8 = alloc.alloc(5);
  621. alloc.free(a5);
  622. alloc.free(a6);
  623. alloc.free(a7);
  624. alloc.free(a8);
  625. alloc.markFrame();
  626. UINT8* a9 = alloc.alloc(5);
  627. UINT8* a10 = alloc.alloc(10);
  628. UINT8* a11 = alloc.alloc(130);
  629. UINT8* a12 = alloc.alloc(5);
  630. alloc.free(a9);
  631. alloc.free(a10);
  632. alloc.free(a11);
  633. alloc.free(a12);
  634. alloc.clear();
  635. alloc.clear();
  636. UINT8* a13 = alloc.alloc(5);
  637. alloc.free(a13);
  638. alloc.clear();
  639. }
  640. }