BsEditorTestSuite.cpp 21 KB

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