BsEditorTestSuite.cpp 23 KB

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