UnitTests.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BansheeEngine;
  8. using DebugUnit = System.Diagnostics.Debug;
  9. namespace BansheeEditor
  10. {
  11. /// <summary>
  12. /// Contains various managed unit tests.
  13. /// </summary>
  14. class UnitTests
  15. {
  16. /// <summary>
  17. /// Triggers an exception when a unit test condition fails.
  18. /// </summary>
  19. /// <param name="success">True if the unit test condition succeeded, false otherwise.</param>
  20. static void Assert(bool success)
  21. {
  22. if (!success)
  23. throw new InvalidOperationException("Unit test failed.");
  24. }
  25. /// <summary>
  26. /// Tests managed object serialization and deserialization.
  27. /// </summary>
  28. static void UnitTest1_ManagedSerialization()
  29. {
  30. SceneObject otherSO = new SceneObject("OtherSO");
  31. UT1_Component2 dbgComponent2 = otherSO.AddComponent<UT1_Component2>();
  32. dbgComponent2.a2 = 33;
  33. SceneObject so = new SceneObject("TestSO");
  34. UT1_Component1 dbgComponent = so.AddComponent<UT1_Component1>();
  35. dbgComponent.a = 5;
  36. dbgComponent.b = "SomeTestVal";
  37. dbgComponent.complex.someValue = 19;
  38. dbgComponent.complex.anotherValue = "AnotherValue";
  39. dbgComponent.complex2.someValue2 = 21;
  40. dbgComponent.complex2.anotherValue2 = "AnotherValue2";
  41. dbgComponent.arrA = new int[5];
  42. dbgComponent.arrA[4] = 5;
  43. dbgComponent.arrB = new string[5];
  44. dbgComponent.arrB[4] = "ArrAnotherValue";
  45. dbgComponent.arrComplex = new UT1_SerzObj[5];
  46. dbgComponent.arrComplex[4].someValue = 99;
  47. dbgComponent.arrComplex[4].anotherValue = "ArrComplexAnotherValue";
  48. dbgComponent.arrComplex2 = new UT1_SerzCls[5];
  49. dbgComponent.arrComplex2[4] = new UT1_SerzCls();
  50. dbgComponent.arrComplex2[4].someValue2 = 101;
  51. dbgComponent.arrComplex2[4].anotherValue2 = "ArrComplex2AnotherValue";
  52. dbgComponent.listA = new List<int>();
  53. dbgComponent.listA.Add(5);
  54. dbgComponent.listB = new List<string>();
  55. dbgComponent.listB.Add("ListAnotherValue");
  56. dbgComponent.listB.Add(null);
  57. dbgComponent.listComplex = new List<UT1_SerzObj>();
  58. dbgComponent.listComplex.Add(new UT1_SerzObj());
  59. dbgComponent.listComplex.Add(new UT1_SerzObj(99, "ListComplexAnotherValue"));
  60. dbgComponent.listComplex2 = new List<UT1_SerzCls>();
  61. dbgComponent.listComplex2.Add(new UT1_SerzCls());
  62. dbgComponent.listComplex2[0].someValue2 = 101;
  63. dbgComponent.listComplex2[0].anotherValue2 = "ListComplexAnotherValue";
  64. dbgComponent.listComplex2.Add(null);
  65. dbgComponent.dictA = new Dictionary<int, string>();
  66. dbgComponent.dictA[5] = "value";
  67. dbgComponent.dictA[10] = "anotherValue";
  68. dbgComponent.dictB = new Dictionary<string, UT1_SerzObj>();
  69. dbgComponent.dictB["key1"] = new UT1_SerzObj(99, "DictComplexValue");
  70. dbgComponent.otherComponent = dbgComponent2;
  71. dbgComponent.otherSO = otherSO;
  72. Internal_UT1_GameObjectClone(so);
  73. System.Diagnostics.Debug.Assert(so.GetNumChildren() == 1);
  74. for (int i = 0; i < so.GetNumChildren(); i++)
  75. {
  76. SceneObject childSO = so.GetChild(i);
  77. UT1_Component1 otherComponent = childSO.GetComponent<UT1_Component1>();
  78. DebugUnit.Assert(otherComponent.a == 5);
  79. DebugUnit.Assert(otherComponent.b == "SomeTestVal");
  80. DebugUnit.Assert(otherComponent.complex.someValue == 19);
  81. DebugUnit.Assert(otherComponent.complex2.anotherValue2 == "AnotherValue2");
  82. DebugUnit.Assert(otherComponent.arrA[4] == 5);
  83. DebugUnit.Assert(otherComponent.arrB[4] == "ArrAnotherValue");
  84. DebugUnit.Assert(otherComponent.arrComplex[4].someValue == 99);
  85. DebugUnit.Assert(otherComponent.arrComplex2[4].anotherValue2 == "ArrComplex2AnotherValue");
  86. DebugUnit.Assert(otherComponent.listA[0] == 5);
  87. DebugUnit.Assert(otherComponent.listB[0] == "ListAnotherValue");
  88. DebugUnit.Assert(otherComponent.listComplex[1].someValue == 99);
  89. DebugUnit.Assert(otherComponent.listComplex2[0].anotherValue2 == "ListComplexAnotherValue");
  90. }
  91. so.Destroy();
  92. otherSO.Destroy();
  93. }
  94. /// <summary>
  95. /// Tests serializable properties used for inspection.
  96. /// </summary>
  97. static void UnitTest2_SerializableProperties()
  98. {
  99. SerializableObject obj = new SerializableObject(typeof(UT1_SerzCls), new UT1_SerzCls());
  100. SerializableProperty prop = obj.Fields[0].GetProperty();
  101. prop.SetValue(33);
  102. DebugUnit.Assert(prop.GetValue<int>() == 33);
  103. SerializableProperty prop2 = obj.Fields[2].GetProperty();
  104. UT1_SerzCls child = new UT1_SerzCls();
  105. child.anotherValue2 = "potato";
  106. prop2.SetValue<UT1_SerzCls>(child);
  107. DebugUnit.Assert(prop2.GetValue<UT1_SerzCls>() != null);
  108. DebugUnit.Assert(prop2.GetValue<UT1_SerzCls>().anotherValue2 == "potato");
  109. }
  110. /// <summary>
  111. /// Tests managed diff creation used by prefabs.
  112. /// </summary>
  113. static void UnitTest3_ManagedDiff()
  114. {
  115. UT_DiffObj original = new UT_DiffObj();
  116. UT_DiffObj modified = new UT_DiffObj();
  117. modified.plain2 = "banana";
  118. modified.complex = new UT_DiffChildObj();
  119. modified.complex2 = null;
  120. modified.complex3.plain2 = "tomato";
  121. modified.arrPlain1 = new[] {-1, -2, -3, -4};
  122. modified.arrPlain2[2] = "cherry";
  123. modified.arrComplex = new UT_DiffChildObj[3];
  124. modified.arrComplex2[0].plain1 = -10;
  125. modified.listPlain1[0] = -20;
  126. modified.listPlain2 = new List<string>();
  127. modified.listComplex = new List<UT_DiffChildObj>();
  128. modified.listComplex.Add(new UT_DiffChildObj());
  129. modified.listComplex2[1].plain2 = "orange";
  130. modified.dictPlain1.Remove(20);
  131. modified.dictPlain1[-30] = -30;
  132. modified.dictComplex = new Dictionary<int, UT_DiffChildObj>();
  133. modified.dictComplex[-40] = new UT_DiffChildObj();
  134. modified.dictComplex2[31].plain1 = -50;
  135. Internal_UT3_GenerateDiff(original, modified);
  136. Internal_UT3_ApplyDiff(original);
  137. DebugUnit.Assert(original.plain1 == modified.plain1);
  138. DebugUnit.Assert(original.plain2 == modified.plain2);
  139. DebugUnit.Assert(original.complex.plain2 == modified.complex.plain2);
  140. DebugUnit.Assert(original.complex2 == modified.complex2);
  141. DebugUnit.Assert(original.complex3.plain2 == modified.complex3.plain2);
  142. DebugUnit.Assert(original.arrPlain1.Length == modified.arrPlain1.Length);
  143. for (int i = 0; i < original.arrPlain1.Length; i++)
  144. DebugUnit.Assert(original.arrPlain1[i] == modified.arrPlain1[i]);
  145. for (int i = 0; i < original.arrPlain2.Length; i++)
  146. DebugUnit.Assert(original.arrPlain2[i] == modified.arrPlain2[i]);
  147. for (int i = 0; i < original.arrComplex.Length; i++)
  148. DebugUnit.Assert(original.arrComplex[i] == modified.arrComplex[i]);
  149. DebugUnit.Assert(original.arrComplex2[0].plain1 == modified.arrComplex2[0].plain1);
  150. for (int i = 0; i < original.listPlain1.Count; i++)
  151. DebugUnit.Assert(original.listPlain1[i] == modified.listPlain1[i]);
  152. DebugUnit.Assert(original.listPlain2.Count == modified.listPlain2.Count);
  153. for (int i = 0; i < original.listComplex.Count; i++)
  154. DebugUnit.Assert(original.listComplex[i].plain1 == modified.listComplex[i].plain1);
  155. DebugUnit.Assert(original.listComplex2[1].plain2 == modified.listComplex2[1].plain2);
  156. foreach (var entry in modified.dictPlain1)
  157. {
  158. if (!original.dictPlain1.ContainsKey(entry.Key))
  159. DebugUnit.Assert(false);
  160. DebugUnit.Assert(entry.Value == original.dictPlain1[entry.Key]);
  161. }
  162. foreach (var entry in modified.dictPlain2)
  163. {
  164. if (!original.dictPlain2.ContainsKey(entry.Key))
  165. DebugUnit.Assert(false);
  166. DebugUnit.Assert(entry.Value == original.dictPlain2[entry.Key]);
  167. }
  168. foreach (var entry in modified.dictComplex)
  169. {
  170. if (!original.dictComplex.ContainsKey(entry.Key))
  171. DebugUnit.Assert(false);
  172. DebugUnit.Assert(entry.Value.plain1 == original.dictComplex[entry.Key].plain1);
  173. }
  174. foreach (var entry in modified.dictComplex2)
  175. {
  176. if (!original.dictComplex2.ContainsKey(entry.Key))
  177. DebugUnit.Assert(false);
  178. DebugUnit.Assert(entry.Value.plain1 == original.dictComplex2[entry.Key].plain1);
  179. }
  180. }
  181. /// <summary>
  182. /// Tests saving, loading and updating of prefabs.
  183. /// </summary>
  184. private static void UnitTest4_Prefabs()
  185. {
  186. if (!EditorApplication.IsProjectLoaded)
  187. {
  188. Debug.LogWarning("Skipping unit test as no project is loaded.");
  189. return;
  190. }
  191. if (EditorApplication.IsSceneModified())
  192. {
  193. Debug.LogWarning("Cannot perform unit test as the current scene is modified.");
  194. return;
  195. }
  196. Action PrintSceneState = () =>
  197. {
  198. SceneObject root = Scene.Root;
  199. Stack<SceneObject> todo = new Stack<SceneObject>();
  200. todo.Push(root);
  201. StringBuilder output = new StringBuilder();
  202. while (todo.Count > 0)
  203. {
  204. SceneObject so = todo.Pop();
  205. int numChildren = so.GetNumChildren();
  206. for (int i = numChildren - 1; i >= 0; i--)
  207. {
  208. SceneObject child = so.GetChild(i);
  209. output.AppendLine(child.Name);
  210. todo.Push(child);
  211. }
  212. }
  213. Debug.Log(output);
  214. };
  215. // Disabled because it's a slow test, enable only when relevant (or when a build machine is set up)
  216. return;
  217. string oldScene = Scene.ActiveSceneUUID;
  218. Scene.Clear();
  219. try
  220. {
  221. // Simple scene save & load
  222. {
  223. {
  224. // unitTest4Scene_0.prefab:
  225. // so0 (Comp1)
  226. // - so0_0
  227. // - so0_1 (Comp1)
  228. // - so0_1_0 (Comp1)
  229. // so1 (Comp2)
  230. // - so1_0
  231. SceneObject so0 = new SceneObject("so0");
  232. SceneObject so1 = new SceneObject("so1");
  233. SceneObject so0_0 = new SceneObject("so0_0");
  234. SceneObject so0_1 = new SceneObject("so0_1");
  235. SceneObject so1_0 = new SceneObject("so1_0");
  236. SceneObject so0_1_0 = new SceneObject("so0_1_0");
  237. so0_0.Parent = so0;
  238. so0_1.Parent = so0;
  239. so1_0.Parent = so1;
  240. so0_1_0.Parent = so0_1;
  241. so0_1_0.LocalPosition = new Vector3(10.0f, 15.0f, 20.0f);
  242. so0_1.LocalPosition = new Vector3(1.0f, 2.0f, 3.0f);
  243. so1_0.LocalPosition = new Vector3(0, 123.0f, 0.0f);
  244. UT1_Component1 comp0 = so0.AddComponent<UT1_Component1>();
  245. UT1_Component2 comp1 = so1.AddComponent<UT1_Component2>();
  246. UT1_Component1 comp1_1 = so0_1.AddComponent<UT1_Component1>();
  247. UT1_Component1 comp0_1_0 = so0_1_0.AddComponent<UT1_Component1>();
  248. comp0.otherSO = so0_1_0;
  249. comp0.otherComponent = comp1;
  250. comp1_1.b = "originalValue2";
  251. comp0_1_0.b = "testValue";
  252. comp0_1_0.otherSO = so0;
  253. comp0_1_0.otherComponent2 = comp0;
  254. EditorApplication.SaveScene("unitTest4Scene_0.prefab");
  255. }
  256. {
  257. EditorApplication.LoadScene("unitTest4Scene_0.prefab");
  258. SceneObject sceneRoot = Scene.Root;
  259. SceneObject so0 = sceneRoot.FindChild("so0", false);
  260. SceneObject so1 = sceneRoot.FindChild("so1", false);
  261. SceneObject so0_0 = so0.FindChild("so0_0", false);
  262. SceneObject so0_1 = so0.FindChild("so0_1", false);
  263. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  264. Assert(so0_0 != null);
  265. Assert(so0_1 != null);
  266. Assert(so0_1_0 != null);
  267. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  268. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  269. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  270. Assert(comp0 != null);
  271. Assert(comp1 != null);
  272. Assert(comp0_1_0 != null);
  273. Assert(comp0_1_0.b == "testValue");
  274. Assert(comp0.otherSO == so0_1_0);
  275. Assert(comp0.otherComponent == comp1);
  276. Assert(comp0_1_0.otherSO == so0);
  277. Assert(comp0_1_0.otherComponent2 == comp0);
  278. }
  279. }
  280. Debug.Log("Passed stage 1");
  281. // Load & save a scene that contains a prefab and references its objects
  282. {
  283. {
  284. // unitTest4Scene_1.prefab:
  285. // parentSO0
  286. // - [unitTest4Scene_0.prefab]
  287. // parentSO1
  288. // - parentSO1_0 (Comp1)
  289. Scene.Clear();
  290. SceneObject parentSO0 = new SceneObject("parentSO0", false);
  291. SceneObject parentSO1 = new SceneObject("parentSO1", false);
  292. SceneObject parentSO1_0 = new SceneObject("parentSO1_0", false);
  293. parentSO1_0.Parent = parentSO1;
  294. parentSO0.LocalPosition = new Vector3(50.0f, 50.0f, 50.0f);
  295. UT1_Component1 parentComp1_0 = parentSO1_0.AddComponent<UT1_Component1>();
  296. Prefab scene0Prefab = ProjectLibrary.Load<Prefab>("unitTest4Scene_0.prefab");
  297. SceneObject prefabInstance = scene0Prefab.Instantiate();
  298. prefabInstance.Parent = parentSO0;
  299. prefabInstance.LocalPosition = Vector3.Zero;
  300. SceneObject so0 = prefabInstance.FindChild("so0", false);
  301. SceneObject so1 = prefabInstance.FindChild("so1", false);
  302. SceneObject so0_1 = so0.FindChild("so0_1", false);
  303. SceneObject so1_0 = so1.FindChild("so1_0", false);
  304. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  305. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  306. parentComp1_0.otherSO = so1_0;
  307. parentComp1_0.otherComponent2 = comp0_1_0;
  308. EditorApplication.SaveScene("unitTest4Scene_1.prefab");
  309. }
  310. {
  311. EditorApplication.LoadScene("unitTest4Scene_1.prefab");
  312. SceneObject parentSO0 = Scene.Root.FindChild("parentSO0", false);
  313. SceneObject parentSO1 = Scene.Root.FindChild("parentSO1", false);
  314. SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);
  315. UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent<UT1_Component1>();
  316. SceneObject prefabInstance = parentSO0.GetChild(0);
  317. SceneObject so0 = prefabInstance.FindChild("so0", false);
  318. SceneObject so1 = prefabInstance.FindChild("so1", false);
  319. SceneObject so0_1 = so0.FindChild("so0_1", false);
  320. SceneObject so1_0 = so1.FindChild("so1_0", false);
  321. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  322. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  323. Assert(parentComp1_0.otherSO == so1_0);
  324. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  325. }
  326. }
  327. Debug.Log("Passed stage 2");
  328. // Modify prefab, reload the scene and ensure it is updated with modified prefab
  329. {
  330. {
  331. // unitTest4Scene_0.prefab:
  332. // so0
  333. // - so0_1 (Comp1)
  334. // - so0_1_0 (Comp1)
  335. // so1 (Comp1, Comp2)
  336. // - so1_0
  337. // - so1_1
  338. Scene.Load("unitTest4Scene_0.prefab");
  339. SceneObject sceneRoot = Scene.Root;
  340. SceneObject so0 = sceneRoot.FindChild("so0", false);
  341. SceneObject so0_0 = so0.FindChild("so0_0", false);
  342. SceneObject so0_1 = so0.FindChild("so0_1", false);
  343. SceneObject so1 = sceneRoot.FindChild("so1", false);
  344. SceneObject so1_0 = so1.FindChild("so1_0", false);
  345. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  346. SceneObject so1_1 = new SceneObject("so1_1");
  347. so1_1.Parent = so1;
  348. so0.RemoveComponent<UT1_Component1>();
  349. UT1_Component1 comp1 = so1.AddComponent<UT1_Component1>();
  350. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  351. so0_0.Destroy();
  352. comp1.otherSO = so1_0;
  353. comp1.otherComponent2 = comp0_1_0;
  354. comp0_1_0.otherSO = so1_1;
  355. comp0_1_0.otherComponent2 = comp1;
  356. comp0_1_0.a = 123;
  357. comp0_1_0.b = "modifiedValue";
  358. so1.Name = "so1_modified";
  359. so1.LocalPosition = new Vector3(0, 999.0f, 0.0f);
  360. EditorApplication.SaveScene("unitTest4Scene_0.prefab");
  361. }
  362. {
  363. EditorApplication.LoadScene("unitTest4Scene_1.prefab");
  364. SceneObject parentSO0 = Scene.Root.FindChild("parentSO0", false);
  365. SceneObject parentSO1 = Scene.Root.FindChild("parentSO1", false);
  366. SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);
  367. UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent<UT1_Component1>();
  368. SceneObject prefabInstance = parentSO0.GetChild(0);
  369. SceneObject so0 = prefabInstance.FindChild("so0", false);
  370. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  371. SceneObject so0_0 = so0.FindChild("so0_0", false);
  372. SceneObject so0_1 = so0.FindChild("so0_1", false);
  373. SceneObject so1_0 = so1.FindChild("so1_0", false);
  374. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  375. SceneObject so1_1 = so1.FindChild("so1_1", false);
  376. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  377. UT1_Component1 comp1 = so1.GetComponent<UT1_Component1>();
  378. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  379. Assert(parentComp1_0.otherSO == so1_0);
  380. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  381. Assert(so1_1 != null);
  382. Assert(so0_0 == null);
  383. Assert(comp0 == null);
  384. Assert(comp0_1_0.otherSO == so1_1);
  385. Assert(comp0_1_0.otherComponent2 == comp1);
  386. Assert(comp0_1_0.a == 123);
  387. Assert(comp0_1_0.b == "modifiedValue");
  388. Assert(comp1.otherSO == so1_0);
  389. Assert(comp1.otherComponent2 == comp0_1_0);
  390. Assert(MathEx.ApproxEquals(so1.LocalPosition.y, 999.0f));
  391. }
  392. }
  393. Debug.Log("Passed stage 3");
  394. // Make instance specific changes to the prefab, modify the prefab itself and ensure
  395. // both changes persist
  396. {
  397. // Create new scene referencing the prefab and make instance modifications
  398. {
  399. // unitTest4Scene_2.prefab:
  400. // parent2SO0
  401. // - [unitTest4Scene_0.prefab]
  402. // parent2SO1
  403. // - parent2SO1_0 (Comp1)
  404. // unitTest4Scene_0.prefab (unitTest4Scene_2.prefab instance):
  405. // so0 (Comp1(INSTANCE))
  406. // - so0_0 (INSTANCE)
  407. // - so0_1 (Comp1)
  408. // - so0_1_0 (Comp1)
  409. // so1 (Comp2)
  410. // - so1_0
  411. Scene.Clear();
  412. SceneObject parent2SO0 = new SceneObject("parent2SO0");
  413. SceneObject parent2SO1 = new SceneObject("parent2SO1");
  414. SceneObject parent2SO1_0 = new SceneObject("parent2SO1_0");
  415. parent2SO1_0.Parent = parent2SO1;
  416. UT1_Component1 parentComp1_0 = parent2SO1_0.AddComponent<UT1_Component1>();
  417. Prefab scene0Prefab = ProjectLibrary.Load<Prefab>("unitTest4Scene_0.prefab");
  418. SceneObject prefabInstance = scene0Prefab.Instantiate();
  419. prefabInstance.Parent = parent2SO0;
  420. SceneObject so0 = prefabInstance.FindChild("so0", false);
  421. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  422. SceneObject so0_1 = so0.FindChild("so0_1", false);
  423. SceneObject so1_0 = so1.FindChild("so1_0", false);
  424. SceneObject so1_1 = so1.FindChild("so1_1", false);
  425. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  426. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  427. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  428. UT1_Component1 comp0_1 = so0_1.GetComponent<UT1_Component1>();
  429. SceneObject so0_0 = new SceneObject("so0_0");
  430. so0_0.Parent = so0;
  431. UT1_Component1 comp0 = so0.AddComponent<UT1_Component1>();
  432. so1.RemoveComponent<UT1_Component1>();
  433. so1_1.Destroy();
  434. comp0.otherSO = so0_1_0;
  435. comp0.otherComponent = comp1;
  436. parentComp1_0.otherSO = so1_0;
  437. parentComp1_0.otherComponent2 = comp0_1_0;
  438. comp0_1_0.otherSO = parent2SO1_0;
  439. comp0_1_0.otherComponent2 = parentComp1_0;
  440. comp0_1_0.b = "instanceValue";
  441. comp0_1.b = "instanceValue2";
  442. EditorApplication.SaveScene("unitTest4Scene_2.prefab");
  443. }
  444. Debug.Log("Passed stage 4.1");
  445. // Reload the scene and ensure instance modifications remain
  446. {
  447. EditorApplication.LoadScene("unitTest4Scene_2.prefab");
  448. SceneObject root = Scene.Root;
  449. SceneObject parent2SO0 = root.FindChild("parent2SO0", false);
  450. SceneObject parent2SO1 = root.FindChild("parent2SO1", false);
  451. SceneObject parent2SO1_0 = parent2SO1.FindChild("parent2SO1_0", false);
  452. SceneObject prefabInstance = parent2SO0.GetChild(0);
  453. SceneObject so0 = prefabInstance.FindChild("so0", false);
  454. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  455. SceneObject so0_0 = so0.FindChild("so0_0", false);
  456. SceneObject so0_1 = so0.FindChild("so0_1", false);
  457. SceneObject so1_0 = so1.FindChild("so1_0", false);
  458. SceneObject so1_1 = so1.FindChild("so1_1", false);
  459. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  460. UT1_Component1 parentComp1_0 = parent2SO1_0.GetComponent<UT1_Component1>();
  461. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  462. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  463. UT1_Component1 comp11 = so1.GetComponent<UT1_Component1>();
  464. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  465. UT1_Component1 comp0_1 = so0_1.GetComponent<UT1_Component1>();
  466. Assert(so0_0 != null);
  467. Assert(comp0 != null);
  468. Assert(so1_1 == null);
  469. Assert(comp11 == null);
  470. Assert(comp0.otherSO == so0_1_0);
  471. Assert(comp0.otherComponent == comp1);
  472. Assert(parentComp1_0.otherSO == so1_0);
  473. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  474. Debug.Log(comp0_1_0.otherSO == null);
  475. if (comp0_1_0.otherSO != null)
  476. Debug.Log(comp0_1_0.otherSO.InstanceId + " - " + parent2SO1_0.InstanceId);
  477. Assert(comp0_1_0.otherSO == parent2SO1_0);
  478. Assert(comp0_1_0.otherComponent2 == parentComp1_0);
  479. Assert(comp0_1_0.b == "instanceValue");
  480. Assert(comp0_1.b == "instanceValue2");
  481. }
  482. Debug.Log("Passed stage 4.2");
  483. // Load original scene and ensure instance modifications didn't influence it
  484. {
  485. EditorApplication.LoadScene("unitTest4Scene_1.prefab");
  486. SceneObject parentSO0 = Scene.Root.FindChild("parentSO0", false);
  487. SceneObject parentSO1 = Scene.Root.FindChild("parentSO1", false);
  488. SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);
  489. UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent<UT1_Component1>();
  490. SceneObject prefabInstance = parentSO0.GetChild(0);
  491. SceneObject so0 = prefabInstance.FindChild("so0", false);
  492. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  493. SceneObject so0_0 = so0.FindChild("so0_0", false);
  494. SceneObject so0_1 = so0.FindChild("so0_1", false);
  495. SceneObject so1_0 = so1.FindChild("so1_0", false);
  496. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  497. SceneObject so1_1 = so1.FindChild("so1_1", false);
  498. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  499. UT1_Component1 comp1 = so1.GetComponent<UT1_Component1>();
  500. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  501. UT1_Component1 comp0_1 = so0_1.GetComponent<UT1_Component1>();
  502. Assert(parentComp1_0.otherSO == so1_0);
  503. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  504. Assert(so1_1 != null);
  505. Assert(so0_0 == null);
  506. Assert(comp0 == null);
  507. Assert(comp0_1_0.otherSO == so1_1);
  508. Assert(comp0_1_0.otherComponent2 == comp1);
  509. Assert(comp0_1_0.a == 123);
  510. Assert(comp0_1_0.b == "modifiedValue");
  511. Assert(comp1.otherSO == so1_0);
  512. Assert(comp1.otherComponent2 == comp0_1_0);
  513. Assert(comp0_1.b == "originalValue2");
  514. Assert(MathEx.ApproxEquals(so1.LocalPosition.y, 999.0f));
  515. }
  516. Debug.Log("Passed stage 4.3");
  517. // Modify prefab and ensure both prefab and instance modifications remain
  518. {
  519. // unitTest4Scene_0.prefab:
  520. // so0 (Comp1)
  521. // - so0_1
  522. // - so0_1_0 (Comp1)
  523. // so1 (Comp1, Comp2)
  524. // - so1_1
  525. // - so1_2 (Comp1)
  526. // unitTest4Scene_0.prefab (unitTest4Scene_2.prefab instance):
  527. // so0 (Comp1)
  528. // - so0_0
  529. // - so0_1 (Comp1)
  530. // - so0_1_0 (Comp1)
  531. // so1 (Comp2)
  532. // - so1_2 (Comp1)
  533. Scene.Load("unitTest4Scene_0.prefab");
  534. SceneObject sceneRoot = Scene.Root;
  535. SceneObject so0 = sceneRoot.FindChild("so0", false);
  536. SceneObject so0_1 = so0.FindChild("so0_1", false);
  537. SceneObject so1 = sceneRoot.FindChild("so1_modified", false);
  538. SceneObject so1_0 = so1.FindChild("so1_0", false);
  539. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  540. SceneObject so1_2 = new SceneObject("so1_2");
  541. so1_2.Parent = so1;
  542. so0.AddComponent<UT1_Component1>();
  543. so0_1.RemoveComponent<UT1_Component1>();
  544. so1_0.Destroy();
  545. UT1_Component1 comp3 = so1_2.AddComponent<UT1_Component1>();
  546. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  547. comp0_1_0.b = "modifiedValueAgain";
  548. so1.Name = "so1_modifiedAgain";
  549. comp3.otherSO = so0_1;
  550. comp3.otherComponent2 = comp0_1_0;
  551. EditorApplication.SaveScene("unitTest4Scene_0.prefab");
  552. }
  553. Debug.Log("Passed stage 4.4");
  554. // Reload the scene and ensure both instance and prefab modifications remain
  555. {
  556. EditorApplication.LoadScene("unitTest4Scene_2.prefab");
  557. SceneObject root = Scene.Root;
  558. SceneObject parent2SO0 = root.FindChild("parent2SO0", false);
  559. SceneObject parent2SO1 = root.FindChild("parent2SO1", false);
  560. SceneObject parent2SO1_0 = parent2SO1.FindChild("parent2SO1_0", false);
  561. SceneObject prefabInstance = parent2SO0.GetChild(0);
  562. SceneObject so0 = prefabInstance.FindChild("so0", false);
  563. SceneObject so1 = prefabInstance.FindChild("so1_modifiedAgain", false);
  564. SceneObject so0_0 = so0.FindChild("so0_0", false);
  565. SceneObject so0_1 = so0.FindChild("so0_1", false);
  566. SceneObject so1_0 = so1.FindChild("so1_0", false);
  567. SceneObject so1_1 = so1.FindChild("so1_1", false);
  568. SceneObject so1_2 = so1.FindChild("so1_2", false);
  569. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  570. UT1_Component1 parentComp1_0 = parent2SO1_0.GetComponent<UT1_Component1>();
  571. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  572. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  573. UT1_Component1 comp11 = so1.GetComponent<UT1_Component1>();
  574. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  575. UT1_Component1 comp3 = so1_2.AddComponent<UT1_Component1>();
  576. // Check instance modifications (they should override any prefab modifications)
  577. Assert(so0_0 != null);
  578. Assert(comp0 != null);
  579. Assert(so1_1 == null);
  580. Assert(comp11 == null);
  581. Assert(comp0.otherSO == so0_1_0);
  582. Assert(comp0.otherComponent == comp1);
  583. Assert(parentComp1_0.otherSO == so1_0);
  584. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  585. Assert(comp0_1_0.otherSO == parent2SO1_0);
  586. Assert(comp0_1_0.otherComponent2 == parentComp1_0);
  587. Assert(comp0_1_0.b == "instanceValue");
  588. // Check prefab modifications
  589. Assert(so1_0 == null);
  590. Assert(so1.Name == "so1_modifiedAgain");
  591. Assert(comp3.otherSO == so0_1);
  592. Assert(comp3.otherComponent2 == comp0_1_0);
  593. }
  594. Debug.Log("Passed stage 4.5");
  595. }
  596. }
  597. catch
  598. {
  599. PrintSceneState();
  600. throw;
  601. }
  602. finally
  603. {
  604. if (!string.IsNullOrEmpty(oldScene))
  605. Scene.Load(ProjectLibrary.GetPath(oldScene));
  606. else
  607. Scene.Clear();
  608. ProjectLibrary.Delete("unitTest4Scene_0.prefab");
  609. ProjectLibrary.Delete("unitTest4Scene_1.prefab");
  610. ProjectLibrary.Delete("unitTest4Scene_2.prefab");
  611. }
  612. }
  613. /// <summary>
  614. /// Runs all tests.
  615. /// </summary>
  616. static void RunTests()
  617. {
  618. UnitTest1_ManagedSerialization();
  619. UnitTest2_SerializableProperties();
  620. UnitTest3_ManagedDiff();
  621. UnitTest4_Prefabs();
  622. }
  623. [MethodImpl(MethodImplOptions.InternalCall)]
  624. private static extern void Internal_UT1_GameObjectClone(SceneObject so);
  625. [MethodImpl(MethodImplOptions.InternalCall)]
  626. private static extern void Internal_UT3_GenerateDiff(UT_DiffObj oldObj, UT_DiffObj newObj);
  627. [MethodImpl(MethodImplOptions.InternalCall)]
  628. private static extern void Internal_UT3_ApplyDiff(UT_DiffObj obj);
  629. }
  630. }