BsEditorTestSuite.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsEditorTestSuite.h"
  4. #include "BsSceneObject.h"
  5. #include "BsCmdRecordSO.h"
  6. #include "BsCmdDeleteSO.h"
  7. #include "BsUndoRedo.h"
  8. #include "BsRTTIType.h"
  9. #include "BsGameObjectRTTI.h"
  10. #include "BsBinarySerializer.h"
  11. #include "BsMemorySerializer.h"
  12. #include "BsBinaryDiff.h"
  13. #include "BsPrefab.h"
  14. #include "BsResources.h"
  15. #include "BsPrefabDiff.h"
  16. #include "BsFrameAlloc.h"
  17. #include "BsFileSystem.h"
  18. namespace BansheeEngine
  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. virtual const String& getRTTIName() override
  34. {
  35. static String name = "TestComponentA";
  36. return name;
  37. }
  38. virtual UINT32 getRTTIId() override
  39. {
  40. return TID_TestComponentA;
  41. }
  42. virtual std::shared_ptr<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. virtual const String& getRTTIName() override
  61. {
  62. static String name = "TestComponentB";
  63. return name;
  64. }
  65. virtual UINT32 getRTTIId() override
  66. {
  67. return TID_TestComponentB;
  68. }
  69. virtual std::shared_ptr<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. virtual 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. virtual RTTITypeBase* getRTTI() const override;
  143. };
  144. class TestObjectARTTI : public RTTIType < TestObjectA, IReflectable, TestObjectARTTI >
  145. {
  146. private:
  147. BS_PLAIN_MEMBER(intA);
  148. BS_PLAIN_MEMBER(strA);
  149. BS_PLAIN_MEMBER(strB);
  150. BS_REFL_MEMBER(objA);
  151. BS_REFL_MEMBER(objB);
  152. BS_REFLPTR_MEMBER(objPtrA);
  153. BS_REFLPTR_MEMBER(objPtrB);
  154. BS_REFLPTR_MEMBER(objPtrC);
  155. BS_REFLPTR_MEMBER(objPtrD);
  156. BS_PLAIN_MEMBER_VEC(arrStrA);
  157. BS_PLAIN_MEMBER_VEC(arrStrB);
  158. BS_PLAIN_MEMBER_VEC(arrStrC);
  159. BS_REFL_MEMBER_VEC(arrObjA);
  160. BS_REFL_MEMBER_VEC(arrObjB);
  161. BS_REFLPTR_MEMBER_VEC(arrObjPtrA);
  162. BS_REFLPTR_MEMBER_VEC(arrObjPtrB);
  163. public:
  164. TestObjectARTTI()
  165. {
  166. BS_ADD_PLAIN_FIELD(intA, 0);
  167. BS_ADD_PLAIN_FIELD(strA, 1);
  168. BS_ADD_PLAIN_FIELD(strB, 2);
  169. BS_ADD_REFL_FIELD(objA, 3);
  170. BS_ADD_REFL_FIELD(objB, 4);
  171. BS_ADD_REFLPTR_FIELD(objPtrA, 5);
  172. BS_ADD_REFLPTR_FIELD(objPtrB, 6);
  173. BS_ADD_REFLPTR_FIELD(objPtrC, 7);
  174. BS_ADD_REFLPTR_FIELD(objPtrD, 8);
  175. BS_ADD_PLAIN_FIELD_ARR(arrStrA, 9);
  176. BS_ADD_PLAIN_FIELD_ARR(arrStrB, 10);
  177. BS_ADD_PLAIN_FIELD_ARR(arrStrC, 11);
  178. BS_ADD_REFL_FIELD_ARR(arrObjA, 12);
  179. BS_ADD_REFL_FIELD_ARR(arrObjB, 13);
  180. BS_ADD_REFLPTR_FIELD_ARR(arrObjPtrA, 14);
  181. BS_ADD_REFLPTR_FIELD_ARR(arrObjPtrB, 15);
  182. }
  183. virtual const String& getRTTIName() override
  184. {
  185. static String name = "TestObjectA";
  186. return name;
  187. }
  188. virtual UINT32 getRTTIId() override
  189. {
  190. return TID_TestObjectA;
  191. }
  192. virtual std::shared_ptr<IReflectable> newRTTIObject() override
  193. {
  194. return bs_shared_ptr_new<TestObjectA>();
  195. }
  196. };
  197. class TestObjectBRTTI : public RTTIType < TestObjectB, IReflectable, TestObjectBRTTI >
  198. {
  199. private:
  200. BS_PLAIN_MEMBER(intA);
  201. BS_PLAIN_MEMBER(strA);
  202. public:
  203. TestObjectBRTTI()
  204. {
  205. BS_ADD_PLAIN_FIELD(intA, 0);
  206. BS_ADD_PLAIN_FIELD(strA, 1);
  207. }
  208. virtual const String& getRTTIName() override
  209. {
  210. static String name = "TestObjectB";
  211. return name;
  212. }
  213. virtual UINT32 getRTTIId() override
  214. {
  215. return TID_TestObjectB;
  216. }
  217. virtual std::shared_ptr<IReflectable> newRTTIObject() override
  218. {
  219. return bs_shared_ptr_new<TestObjectB>();
  220. }
  221. };
  222. RTTITypeBase* TestObjectB::getRTTIStatic()
  223. {
  224. return TestObjectBRTTI::instance();
  225. }
  226. RTTITypeBase* TestObjectB::getRTTI() const
  227. {
  228. return TestObjectB::getRTTIStatic();
  229. }
  230. RTTITypeBase* TestObjectA::getRTTIStatic()
  231. {
  232. return TestObjectARTTI::instance();
  233. }
  234. RTTITypeBase* TestObjectA::getRTTI() const
  235. {
  236. return TestObjectA::getRTTIStatic();
  237. }
  238. class TestComponentC : public Component
  239. {
  240. public:
  241. TestObjectA obj;
  242. /************************************************************************/
  243. /* COMPONENT OVERRIDES */
  244. /************************************************************************/
  245. protected:
  246. friend class SceneObject;
  247. TestComponentC(const HSceneObject& parent)
  248. :Component(parent)
  249. {}
  250. /************************************************************************/
  251. /* RTTI */
  252. /************************************************************************/
  253. public:
  254. friend class TestComponentCRTTI;
  255. static RTTITypeBase* getRTTIStatic();
  256. virtual RTTITypeBase* getRTTI() const override;
  257. protected:
  258. TestComponentC() {} // Serialization only
  259. };
  260. class TestComponentD : public Component
  261. {
  262. public:
  263. TestObjectB obj;
  264. /************************************************************************/
  265. /* COMPONENT OVERRIDES */
  266. /************************************************************************/
  267. protected:
  268. friend class SceneObject;
  269. TestComponentD(const HSceneObject& parent)
  270. :Component(parent)
  271. {}
  272. /************************************************************************/
  273. /* RTTI */
  274. /************************************************************************/
  275. public:
  276. friend class TestComponentDRTTI;
  277. static RTTITypeBase* getRTTIStatic();
  278. virtual RTTITypeBase* getRTTI() const override;
  279. protected:
  280. TestComponentD() {} // Serialization only
  281. };
  282. class TestComponentCRTTI : public RTTIType < TestComponentC, Component, TestComponentCRTTI >
  283. {
  284. private:
  285. BS_REFL_MEMBER(obj)
  286. public:
  287. TestComponentCRTTI()
  288. {
  289. BS_ADD_REFL_FIELD(obj, 0);
  290. }
  291. virtual const String& getRTTIName() override
  292. {
  293. static String name = "TestComponentC";
  294. return name;
  295. }
  296. virtual UINT32 getRTTIId() override
  297. {
  298. return TID_TestComponentC;
  299. }
  300. virtual std::shared_ptr<IReflectable> newRTTIObject() override
  301. {
  302. return GameObjectRTTI::createGameObject<TestComponentC>();
  303. }
  304. };
  305. class TestComponentDRTTI : public RTTIType < TestComponentD, Component, TestComponentDRTTI >
  306. {
  307. private:
  308. BS_REFL_MEMBER(obj)
  309. public:
  310. TestComponentDRTTI()
  311. {
  312. BS_ADD_REFL_FIELD(obj, 0);
  313. }
  314. virtual const String& getRTTIName() override
  315. {
  316. static String name = "TestComponentD";
  317. return name;
  318. }
  319. virtual UINT32 getRTTIId() override
  320. {
  321. return TID_TestComponentD;
  322. }
  323. virtual std::shared_ptr<IReflectable> newRTTIObject() override
  324. {
  325. return GameObjectRTTI::createGameObject<TestComponentD>();
  326. }
  327. };
  328. RTTITypeBase* TestComponentC::getRTTIStatic()
  329. {
  330. return TestComponentCRTTI::instance();
  331. }
  332. RTTITypeBase* TestComponentC::getRTTI() const
  333. {
  334. return TestComponentC::getRTTIStatic();
  335. }
  336. RTTITypeBase* TestComponentD::getRTTIStatic()
  337. {
  338. return TestComponentDRTTI::instance();
  339. }
  340. RTTITypeBase* TestComponentD::getRTTI() const
  341. {
  342. return TestComponentD::getRTTIStatic();
  343. }
  344. EditorTestSuite::EditorTestSuite()
  345. {
  346. BS_ADD_TEST(EditorTestSuite::SceneObjectRecord_UndoRedo);
  347. BS_ADD_TEST(EditorTestSuite::SceneObjectDelete_UndoRedo);
  348. BS_ADD_TEST(EditorTestSuite::BinaryDiff);
  349. BS_ADD_TEST(EditorTestSuite::TestPrefabDiff);
  350. BS_ADD_TEST(EditorTestSuite::TestFrameAlloc)
  351. }
  352. void EditorTestSuite::SceneObjectRecord_UndoRedo()
  353. {
  354. HSceneObject so0_0 = SceneObject::create("so0_0");
  355. HSceneObject so1_0 = SceneObject::create("so1_0");
  356. HSceneObject so1_1 = SceneObject::create("so1_1");
  357. HSceneObject so2_0 = SceneObject::create("so2_0");
  358. so1_0->setParent(so0_0);
  359. so1_1->setParent(so0_0);
  360. so2_0->setParent(so1_0);
  361. GameObjectHandle<TestComponentA> cmpA1_1 = so1_1->addComponent<TestComponentA>();
  362. GameObjectHandle<TestComponentB> cmpB1_1 = so0_0->addComponent<TestComponentB>();
  363. HSceneObject soExternal = SceneObject::create("soExternal");
  364. GameObjectHandle<TestComponentA> cmpExternal = soExternal->addComponent<TestComponentA>();
  365. cmpA1_1->ref1 = so2_0;
  366. cmpA1_1->ref2 = cmpB1_1;
  367. cmpB1_1->ref1 = soExternal;
  368. cmpB1_1->val1 = "InitialValue";
  369. cmpExternal->ref1 = so1_1;
  370. cmpExternal->ref2 = cmpA1_1;
  371. CmdRecordSO::execute(so0_0);
  372. cmpB1_1->val1 = "ModifiedValue";
  373. so0_0->setName("modified");
  374. UndoRedo::instance().undo();
  375. BS_TEST_ASSERT(!so0_0.isDestroyed());
  376. BS_TEST_ASSERT(!so1_0.isDestroyed());
  377. BS_TEST_ASSERT(!so1_1.isDestroyed());
  378. BS_TEST_ASSERT(!so2_0.isDestroyed());
  379. BS_TEST_ASSERT(!cmpA1_1.isDestroyed());
  380. BS_TEST_ASSERT(!cmpB1_1.isDestroyed());
  381. BS_TEST_ASSERT(!cmpA1_1->ref1.isDestroyed());
  382. BS_TEST_ASSERT(!cmpA1_1->ref2.isDestroyed());
  383. BS_TEST_ASSERT(!cmpB1_1->ref1.isDestroyed());
  384. BS_TEST_ASSERT(!cmpExternal->ref1.isDestroyed());
  385. BS_TEST_ASSERT(!cmpExternal->ref2.isDestroyed());
  386. BS_TEST_ASSERT(cmpB1_1->val1 == "InitialValue");
  387. BS_TEST_ASSERT(so0_0->getName() == "so0_0");
  388. so0_0->destroy();
  389. soExternal->destroy();
  390. }
  391. void EditorTestSuite::SceneObjectDelete_UndoRedo()
  392. {
  393. HSceneObject so0_0 = SceneObject::create("so0_0");
  394. HSceneObject so1_0 = SceneObject::create("so1_0");
  395. HSceneObject so1_1 = SceneObject::create("so1_1");
  396. HSceneObject so2_0 = SceneObject::create("so2_0");
  397. so1_0->setParent(so0_0);
  398. so1_1->setParent(so0_0);
  399. so2_0->setParent(so1_0);
  400. GameObjectHandle<TestComponentA> cmpA1_1 = so1_1->addComponent<TestComponentA>();
  401. GameObjectHandle<TestComponentB> cmpB1_1 = so1_1->addComponent<TestComponentB>();
  402. HSceneObject soExternal = SceneObject::create("soExternal");
  403. GameObjectHandle<TestComponentA> cmpExternal = soExternal->addComponent<TestComponentA>();
  404. cmpA1_1->ref1 = so2_0;
  405. cmpA1_1->ref2 = cmpB1_1;
  406. cmpB1_1->ref1 = soExternal;
  407. cmpB1_1->val1 = "InitialValue";
  408. cmpExternal->ref1 = so1_1;
  409. cmpExternal->ref2 = cmpA1_1;
  410. CmdDeleteSO::execute(so0_0);
  411. UndoRedo::instance().undo();
  412. BS_TEST_ASSERT(!so0_0.isDestroyed());
  413. BS_TEST_ASSERT(!so1_0.isDestroyed());
  414. BS_TEST_ASSERT(!so1_1.isDestroyed());
  415. BS_TEST_ASSERT(!so2_0.isDestroyed());
  416. BS_TEST_ASSERT(!cmpA1_1.isDestroyed());
  417. BS_TEST_ASSERT(!cmpB1_1.isDestroyed());
  418. BS_TEST_ASSERT(!cmpA1_1->ref1.isDestroyed());
  419. BS_TEST_ASSERT(!cmpA1_1->ref2.isDestroyed());
  420. BS_TEST_ASSERT(!cmpB1_1->ref1.isDestroyed());
  421. BS_TEST_ASSERT(!cmpExternal->ref1.isDestroyed());
  422. BS_TEST_ASSERT(!cmpExternal->ref2.isDestroyed());
  423. BS_TEST_ASSERT(cmpB1_1->val1 == "InitialValue");
  424. so0_0->destroy();
  425. soExternal->destroy();
  426. }
  427. void EditorTestSuite::BinaryDiff()
  428. {
  429. SPtr<TestObjectA> orgObj = bs_shared_ptr_new<TestObjectA>();
  430. SPtr<TestObjectA> newObj = bs_shared_ptr_new<TestObjectA>();
  431. newObj->intA = 995;
  432. newObj->strA = "potato";
  433. newObj->arrStrB = { "orange", "carrot" };
  434. newObj->arrStrC[2] = "banana";
  435. newObj->objB.intA = 9940;
  436. newObj->objPtrB->strA = "kiwi";
  437. newObj->objPtrC = nullptr;
  438. newObj->objPtrD = bs_shared_ptr_new<TestObjectB>();
  439. newObj->arrObjB[1].strA = "strawberry";
  440. newObj->arrObjPtrB[0]->intA = 99100;
  441. MemorySerializer ms;
  442. UINT32 orgDataLength = 0;
  443. UINT8* orgData = ms.encode(orgObj.get(), orgDataLength);
  444. UINT32 newDataLength = 0;
  445. UINT8* newData = ms.encode(newObj.get(), newDataLength);
  446. BinarySerializer bs;
  447. SPtr<SerializedObject> orgSerialized = bs._decodeIntermediate(orgData, orgDataLength);
  448. SPtr<SerializedObject> newSerialized = bs._decodeIntermediate(newData, newDataLength);
  449. IDiff& diffHandler = orgObj->getRTTI()->getDiffHandler();
  450. SPtr<SerializedObject> objDiff = diffHandler.generateDiff(orgSerialized, newSerialized);
  451. diffHandler.applyDiff(orgObj, objDiff);
  452. bs_free(orgData);
  453. bs_free(newData);
  454. BS_TEST_ASSERT(orgObj->intA == newObj->intA);
  455. BS_TEST_ASSERT(orgObj->strA == newObj->strA);
  456. BS_TEST_ASSERT(orgObj->strB == newObj->strB);
  457. BS_TEST_ASSERT(orgObj->objA.intA == newObj->objA.intA);
  458. BS_TEST_ASSERT(orgObj->objB.intA == newObj->objB.intA);
  459. BS_TEST_ASSERT(orgObj->objPtrA->strA == newObj->objPtrA->strA);
  460. BS_TEST_ASSERT(orgObj->objPtrB->strA == newObj->objPtrB->strA);
  461. BS_TEST_ASSERT(orgObj->objPtrD->strA == newObj->objPtrD->strA);
  462. BS_TEST_ASSERT(orgObj->objPtrC == newObj->objPtrC);
  463. BS_TEST_ASSERT(orgObj->arrStrA.size() == newObj->arrStrA.size());
  464. for (UINT32 i = 0; i < (UINT32)orgObj->arrStrA.size(); i++)
  465. BS_TEST_ASSERT(orgObj->arrStrA[i] == newObj->arrStrA[i]);
  466. BS_TEST_ASSERT(orgObj->arrStrB.size() == newObj->arrStrB.size());
  467. for (UINT32 i = 0; i < (UINT32)orgObj->arrStrB.size(); i++)
  468. BS_TEST_ASSERT(orgObj->arrStrB[i] == newObj->arrStrB[i]);
  469. BS_TEST_ASSERT(orgObj->arrStrC.size() == newObj->arrStrC.size());
  470. for (UINT32 i = 0; i < (UINT32)orgObj->arrStrC.size(); i++)
  471. BS_TEST_ASSERT(orgObj->arrStrC[i] == newObj->arrStrC[i]);
  472. BS_TEST_ASSERT(orgObj->arrObjA.size() == newObj->arrObjA.size());
  473. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjA.size(); i++)
  474. BS_TEST_ASSERT(orgObj->arrObjA[i].strA == newObj->arrObjA[i].strA);
  475. BS_TEST_ASSERT(orgObj->arrObjB.size() == newObj->arrObjB.size());
  476. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjB.size(); i++)
  477. BS_TEST_ASSERT(orgObj->arrObjB[i].strA == newObj->arrObjB[i].strA);
  478. BS_TEST_ASSERT(orgObj->arrObjPtrA.size() == newObj->arrObjPtrA.size());
  479. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjPtrA.size(); i++)
  480. BS_TEST_ASSERT(orgObj->arrObjPtrA[i]->intA == newObj->arrObjPtrA[i]->intA);
  481. BS_TEST_ASSERT(orgObj->arrObjPtrB.size() == newObj->arrObjPtrB.size());
  482. for (UINT32 i = 0; i < (UINT32)orgObj->arrObjPtrB.size(); i++)
  483. BS_TEST_ASSERT(orgObj->arrObjPtrB[i]->intA == newObj->arrObjPtrB[i]->intA);
  484. }
  485. void EditorTestSuite::TestPrefabDiff()
  486. {
  487. HSceneObject root = SceneObject::create("root");
  488. HSceneObject so0 = SceneObject::create("so0");
  489. HSceneObject so1 = SceneObject::create("so1");
  490. HSceneObject so2 = SceneObject::create("so2");
  491. HSceneObject so0_0 = SceneObject::create("so0_0");
  492. HSceneObject so0_1 = SceneObject::create("so0_1");
  493. HSceneObject so1_0 = SceneObject::create("so1_0");
  494. HSceneObject so1_1 = SceneObject::create("so1_1");
  495. HSceneObject so1_2 = SceneObject::create("so1_2");
  496. HSceneObject so2_0 = SceneObject::create("so2_0");
  497. so0->setParent(root);
  498. so1->setParent(root);
  499. so2->setParent(root);
  500. so0_0->setParent(so0);
  501. so0_1->setParent(so0);
  502. so1_0->setParent(so1);
  503. so1_1->setParent(so1);
  504. so1_2->setParent(so1);
  505. so2_0->setParent(so2);
  506. GameObjectHandle<TestComponentC> cmp0 = so0->addComponent<TestComponentC>();
  507. GameObjectHandle<TestComponentC> cmp0_1_A = so0_1->addComponent<TestComponentC>();
  508. GameObjectHandle<TestComponentD> cmp0_1_B = so0_1->addComponent<TestComponentD>();
  509. GameObjectHandle<TestComponentD> cmp1 = so1->addComponent<TestComponentD>();
  510. GameObjectHandle<TestComponentD> cmp1_2 = so1_2->addComponent<TestComponentD>();
  511. GameObjectHandle<TestComponentD> cmp2 = so2->addComponent<TestComponentD>();
  512. Path prefabPath = Path::combine(FileSystem::getTempDirectoryPath(), "testprefab.asset");
  513. HPrefab prefab = Prefab::create(root);
  514. gResources().save(prefab, prefabPath, true);
  515. // Perform modifications
  516. GameObjectHandle<TestComponentC> cmp1_3;
  517. GameObjectHandle<TestComponentD> cmp3;
  518. HSceneObject so1_3, so2_1, so3;
  519. {
  520. cmp0->obj.strA = "banana";
  521. so0_0->destroy();
  522. cmp0_1_A->destroy();
  523. so1_3 = SceneObject::create("so1_2");
  524. so1_3->setParent(so1);
  525. cmp1_3 = so1_3->addComponent<TestComponentC>();
  526. cmp1_3->obj.intA = 999;
  527. so1_0->setName("apple");
  528. so1_1->destroy();
  529. cmp1_2->destroy();
  530. cmp1_2 = so1_2->addComponent<TestComponentD>();
  531. cmp1_2->obj.strA = "orange";
  532. so2_1 = SceneObject::create("so2_1");
  533. so2_1->setParent(so2);
  534. so2_0->addComponent<TestComponentD>();
  535. so3 = SceneObject::create("so3");
  536. so3->setParent(root);
  537. cmp3 = so3->addComponent<TestComponentD>();
  538. }
  539. SPtr<PrefabDiff> prefabDiff = PrefabDiff::create(prefab->_getRoot(), root);
  540. prefab = gResources().load<Prefab>(prefabPath);
  541. HSceneObject newRoot = prefab->instantiate();
  542. prefabDiff->apply(newRoot);
  543. // Compare and assert
  544. BS_TEST_ASSERT(root->getNumChildren() == newRoot->getNumChildren());
  545. HSceneObject nso0 = newRoot->getChild(0);
  546. GameObjectHandle<TestComponentC> ncmp0 = nso0->getComponent<TestComponentC>();
  547. BS_TEST_ASSERT(cmp0->obj.strA == ncmp0->obj.strA);
  548. BS_TEST_ASSERT(so0->getNumChildren() == nso0->getNumChildren());
  549. HSceneObject nso0_1 = nso0->getChild(0);
  550. GameObjectHandle<TestComponentC> ncmp0_1 = nso0_1->getComponent<TestComponentD>();
  551. BS_TEST_ASSERT(ncmp0_1 != nullptr);
  552. HSceneObject nso1 = newRoot->getChild(1);
  553. BS_TEST_ASSERT(so1->getNumChildren() == nso1->getNumChildren());
  554. HSceneObject nso1_0 = nso1->getChild(0);
  555. BS_TEST_ASSERT(so1_0->getName() == nso1_0->getName());
  556. HSceneObject nso1_2 = nso1->getChild(1);
  557. GameObjectHandle<TestComponentD> ncmp1_2 = nso1_2->getComponent<TestComponentD>();
  558. BS_TEST_ASSERT(cmp1_2->obj.strA == ncmp1_2->obj.strA);
  559. HSceneObject nso1_3 = nso1->getChild(2);
  560. GameObjectHandle<TestComponentC> ncmp1_3 = nso1_3->getComponent<TestComponentC>();
  561. BS_TEST_ASSERT(cmp1_3->obj.intA == ncmp1_3->obj.intA);
  562. HSceneObject nso2 = newRoot->getChild(2);
  563. BS_TEST_ASSERT(so2->getNumChildren() == nso2->getNumChildren());
  564. HSceneObject nso2_0 = nso2->getChild(0);
  565. GameObjectHandle<TestComponentD> ncmp2_0 = nso2_0->getComponent<TestComponentD>();
  566. BS_TEST_ASSERT(ncmp2_0 != nullptr);
  567. HSceneObject nso3 = newRoot->getChild(3);
  568. GameObjectHandle<TestComponentD> ncmp3 = nso3->getComponent<TestComponentD>();
  569. BS_TEST_ASSERT(ncmp3 != nullptr);
  570. root->destroy();
  571. newRoot->destroy();
  572. }
  573. void EditorTestSuite::TestFrameAlloc()
  574. {
  575. FrameAlloc alloc(128);
  576. alloc.markFrame();
  577. UINT8* a1 = alloc.alloc(5);
  578. UINT8* a2 = alloc.alloc(10);
  579. UINT8* a3 = alloc.alloc(130);
  580. UINT8* a4 = alloc.alloc(5);
  581. alloc.dealloc(a1);
  582. alloc.dealloc(a2);
  583. alloc.dealloc(a3);
  584. alloc.dealloc(a4);
  585. alloc.clear();
  586. alloc.markFrame();
  587. UINT8* a5 = alloc.alloc(5);
  588. UINT8* a6 = alloc.alloc(10);
  589. UINT8* a7 = alloc.alloc(130);
  590. UINT8* a8 = alloc.alloc(5);
  591. alloc.dealloc(a5);
  592. alloc.dealloc(a6);
  593. alloc.dealloc(a7);
  594. alloc.dealloc(a8);
  595. alloc.markFrame();
  596. UINT8* a9 = alloc.alloc(5);
  597. UINT8* a10 = alloc.alloc(10);
  598. UINT8* a11 = alloc.alloc(130);
  599. UINT8* a12 = alloc.alloc(5);
  600. alloc.dealloc(a9);
  601. alloc.dealloc(a10);
  602. alloc.dealloc(a11);
  603. alloc.dealloc(a12);
  604. alloc.clear();
  605. alloc.clear();
  606. UINT8* a13 = alloc.alloc(5);
  607. alloc.dealloc(a13);
  608. alloc.clear();
  609. }
  610. }