UnitTests.cs 36 KB

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