BsEditorTestSuite.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. namespace BansheeEngine
  8. {
  9. class TestComponentARTTI : public RTTIType<TestComponentA, Component, TestComponentARTTI>
  10. {
  11. private:
  12. HSceneObject& getRef1(TestComponentA* obj) { return obj->ref1; }
  13. void setRef1(TestComponentA* obj, HSceneObject& val) { obj->ref1 = val; }
  14. HComponent& getRef2(TestComponentA* obj) { return obj->ref2; }
  15. void setRef2(TestComponentA* obj, HComponent& val) { obj->ref2 = val; }
  16. public:
  17. TestComponentARTTI()
  18. {
  19. addReflectableField("ref1", 0, &TestComponentARTTI::getRef1, &TestComponentARTTI::setRef1);
  20. addReflectableField("ref2", 1, &TestComponentARTTI::getRef2, &TestComponentARTTI::setRef2);
  21. }
  22. virtual const String& getRTTIName()
  23. {
  24. static String name = "TestComponentA";
  25. return name;
  26. }
  27. virtual UINT32 getRTTIId()
  28. {
  29. return TID_TestComponentA;
  30. }
  31. virtual std::shared_ptr<IReflectable> newRTTIObject()
  32. {
  33. return GameObjectRTTI::createGameObject<TestComponentA>();
  34. }
  35. };
  36. class TestComponentBRTTI : public RTTIType<TestComponentB, Component, TestComponentBRTTI>
  37. {
  38. private:
  39. HSceneObject& getRef1(TestComponentB* obj) { return obj->ref1; }
  40. void setRef1(TestComponentB* obj, HSceneObject& val) { obj->ref1 = val; }
  41. String& getVal1(TestComponentB* obj) { return obj->val1; }
  42. void setVal1(TestComponentB* obj, String& val) { obj->val1 = val; }
  43. public:
  44. TestComponentBRTTI()
  45. {
  46. addReflectableField("ref1", 0, &TestComponentBRTTI::getRef1, &TestComponentBRTTI::setRef1);
  47. addPlainField("val1", 1, &TestComponentBRTTI::getVal1, &TestComponentBRTTI::setVal1);
  48. }
  49. virtual const String& getRTTIName()
  50. {
  51. static String name = "TestComponentB";
  52. return name;
  53. }
  54. virtual UINT32 getRTTIId()
  55. {
  56. return TID_TestComponentB;
  57. }
  58. virtual std::shared_ptr<IReflectable> newRTTIObject()
  59. {
  60. return GameObjectRTTI::createGameObject<TestComponentB>();
  61. }
  62. };
  63. TestComponentA::TestComponentA(const HSceneObject& parent)
  64. :Component(parent)
  65. {}
  66. RTTITypeBase* TestComponentA::getRTTIStatic()
  67. {
  68. return TestComponentARTTI::instance();
  69. }
  70. RTTITypeBase* TestComponentA::getRTTI() const
  71. {
  72. return TestComponentA::getRTTIStatic();
  73. }
  74. TestComponentB::TestComponentB(const HSceneObject& parent)
  75. :Component(parent)
  76. {}
  77. RTTITypeBase* TestComponentB::getRTTIStatic()
  78. {
  79. return TestComponentBRTTI::instance();
  80. }
  81. RTTITypeBase* TestComponentB::getRTTI() const
  82. {
  83. return TestComponentB::getRTTIStatic();
  84. }
  85. EditorTestSuite::EditorTestSuite()
  86. {
  87. BS_ADD_TEST(EditorTestSuite::SceneObjectRecord_UndoRedo)
  88. }
  89. void EditorTestSuite::SceneObjectRecord_UndoRedo()
  90. {
  91. HSceneObject so0_0 = SceneObject::create("so0_0");
  92. HSceneObject so1_0 = SceneObject::create("so1_0");
  93. HSceneObject so1_1 = SceneObject::create("so1_1");
  94. HSceneObject so2_0 = SceneObject::create("so2_0");
  95. so1_0->setParent(so0_0);
  96. so1_1->setParent(so0_0);
  97. so2_0->setParent(so1_0);
  98. GameObjectHandle<TestComponentA> cmpA1_1 = so1_1->addComponent<TestComponentA>();
  99. GameObjectHandle<TestComponentB> cmpB1_1 = so1_1->addComponent<TestComponentB>();
  100. HSceneObject soExternal = SceneObject::create("soExternal");
  101. GameObjectHandle<TestComponentA> cmpExternal = soExternal->addComponent<TestComponentA>();
  102. cmpA1_1->ref1 = so2_0;
  103. cmpA1_1->ref2 = cmpB1_1;
  104. cmpB1_1->ref1 = soExternal;
  105. cmpB1_1->val1 = "InitialValue";
  106. cmpExternal->ref1 = so1_1;
  107. cmpExternal->ref2 = cmpA1_1;
  108. CmdRecordSO::execute(so0_0);
  109. cmpB1_1->val1 = "ModifiedValue";
  110. UndoRedo::instance().undo();
  111. BS_TEST_ASSERT(!so0_0.isDestroyed());
  112. BS_TEST_ASSERT(!so1_0.isDestroyed());
  113. BS_TEST_ASSERT(!so1_1.isDestroyed());
  114. BS_TEST_ASSERT(!so2_0.isDestroyed());
  115. BS_TEST_ASSERT(!cmpA1_1.isDestroyed());
  116. BS_TEST_ASSERT(!cmpB1_1.isDestroyed());
  117. BS_TEST_ASSERT(!cmpA1_1->ref1.isDestroyed());
  118. BS_TEST_ASSERT(!cmpA1_1->ref2.isDestroyed());
  119. BS_TEST_ASSERT(!cmpB1_1->ref1.isDestroyed());
  120. BS_TEST_ASSERT(!cmpExternal->ref1.isDestroyed());
  121. BS_TEST_ASSERT(!cmpExternal->ref2.isDestroyed());
  122. BS_TEST_ASSERT(cmpB1_1->val1 == "InitialValue");
  123. }
  124. }