2
0

BsEditorTestSuite.cpp 19 KB

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