UnitTests.cs 36 KB

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