UnitTests.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. string oldScene = Scene.ActiveSceneUUID;
  216. Scene.Clear();
  217. try
  218. {
  219. // Simple scene save & load
  220. {
  221. {
  222. // unitTest4Scene_0.prefab:
  223. // so0 (Comp1)
  224. // - so0_0
  225. // - so0_1 (Comp1)
  226. // - so0_1_0 (Comp1)
  227. // so1 (Comp2)
  228. // - so1_0
  229. SceneObject so0 = new SceneObject("so0");
  230. SceneObject so1 = new SceneObject("so1");
  231. SceneObject so0_0 = new SceneObject("so0_0");
  232. SceneObject so0_1 = new SceneObject("so0_1");
  233. SceneObject so1_0 = new SceneObject("so1_0");
  234. SceneObject so0_1_0 = new SceneObject("so0_1_0");
  235. so0_0.Parent = so0;
  236. so0_1.Parent = so0;
  237. so1_0.Parent = so1;
  238. so0_1_0.Parent = so0_1;
  239. so0_1_0.LocalPosition = new Vector3(10.0f, 15.0f, 20.0f);
  240. so0_1.LocalPosition = new Vector3(1.0f, 2.0f, 3.0f);
  241. so1_0.LocalPosition = new Vector3(0, 123.0f, 0.0f);
  242. UT1_Component1 comp0 = so0.AddComponent<UT1_Component1>();
  243. UT1_Component2 comp1 = so1.AddComponent<UT1_Component2>();
  244. UT1_Component1 comp1_1 = so0_1.AddComponent<UT1_Component1>();
  245. UT1_Component1 comp0_1_0 = so0_1_0.AddComponent<UT1_Component1>();
  246. comp0.otherSO = so0_1_0;
  247. comp0.otherComponent = comp1;
  248. comp1_1.b = "originalValue2";
  249. comp0_1_0.b = "testValue";
  250. comp0_1_0.otherSO = so0;
  251. comp0_1_0.otherComponent2 = comp0;
  252. EditorApplication.SaveScene("unitTest4Scene_0.prefab");
  253. }
  254. {
  255. EditorApplication.LoadScene("unitTest4Scene_0.prefab");
  256. SceneObject sceneRoot = Scene.Root;
  257. SceneObject so0 = sceneRoot.FindChild("so0", false);
  258. SceneObject so1 = sceneRoot.FindChild("so1", false);
  259. SceneObject so0_0 = so0.FindChild("so0_0", false);
  260. SceneObject so0_1 = so0.FindChild("so0_1", false);
  261. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  262. Assert(so0_0 != null);
  263. Assert(so0_1 != null);
  264. Assert(so0_1_0 != null);
  265. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  266. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  267. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  268. Assert(comp0 != null);
  269. Assert(comp1 != null);
  270. Assert(comp0_1_0 != null);
  271. Assert(comp0_1_0.b == "testValue");
  272. Assert(comp0.otherSO == so0_1_0);
  273. Assert(comp0.otherComponent == comp1);
  274. Assert(comp0_1_0.otherSO == so0);
  275. Assert(comp0_1_0.otherComponent2 == comp0);
  276. }
  277. }
  278. Debug.Log("Passed stage 1");
  279. // Load & save a scene that contains a prefab and references its objects
  280. {
  281. {
  282. // unitTest4Scene_1.prefab:
  283. // parentSO0
  284. // - [unitTest4Scene_0.prefab]
  285. // parentSO1
  286. // - parentSO1_0 (Comp1)
  287. Scene.Clear();
  288. SceneObject parentSO0 = new SceneObject("parentSO0", false);
  289. SceneObject parentSO1 = new SceneObject("parentSO1", false);
  290. SceneObject parentSO1_0 = new SceneObject("parentSO1_0", false);
  291. parentSO1_0.Parent = parentSO1;
  292. parentSO0.LocalPosition = new Vector3(50.0f, 50.0f, 50.0f);
  293. UT1_Component1 parentComp1_0 = parentSO1_0.AddComponent<UT1_Component1>();
  294. Prefab scene0Prefab = ProjectLibrary.Load<Prefab>("unitTest4Scene_0.prefab");
  295. SceneObject prefabInstance = scene0Prefab.Instantiate();
  296. prefabInstance.Parent = parentSO0;
  297. prefabInstance.LocalPosition = Vector3.Zero;
  298. SceneObject so0 = prefabInstance.FindChild("so0", false);
  299. SceneObject so1 = prefabInstance.FindChild("so1", false);
  300. SceneObject so0_1 = so0.FindChild("so0_1", false);
  301. SceneObject so1_0 = so1.FindChild("so1_0", false);
  302. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  303. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  304. parentComp1_0.otherSO = so1_0;
  305. parentComp1_0.otherComponent2 = comp0_1_0;
  306. EditorApplication.SaveScene("unitTest4Scene_1.prefab");
  307. }
  308. {
  309. EditorApplication.LoadScene("unitTest4Scene_1.prefab");
  310. SceneObject parentSO0 = Scene.Root.FindChild("parentSO0", false);
  311. SceneObject parentSO1 = Scene.Root.FindChild("parentSO1", false);
  312. SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);
  313. UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent<UT1_Component1>();
  314. SceneObject prefabInstance = parentSO0.GetChild(0);
  315. SceneObject so0 = prefabInstance.FindChild("so0", false);
  316. SceneObject so1 = prefabInstance.FindChild("so1", false);
  317. SceneObject so0_1 = so0.FindChild("so0_1", false);
  318. SceneObject so1_0 = so1.FindChild("so1_0", false);
  319. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  320. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  321. Assert(parentComp1_0.otherSO == so1_0);
  322. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  323. }
  324. }
  325. Debug.Log("Passed stage 2");
  326. // Modify prefab, reload the scene and ensure it is updated with modified prefab
  327. {
  328. {
  329. // unitTest4Scene_0.prefab:
  330. // so0
  331. // - so0_1 (Comp1)
  332. // - so0_1_0 (Comp1)
  333. // so1 (Comp1, Comp2)
  334. // - so1_0
  335. // - so1_1
  336. Scene.Load("unitTest4Scene_0.prefab");
  337. SceneObject sceneRoot = Scene.Root;
  338. SceneObject so0 = sceneRoot.FindChild("so0", false);
  339. SceneObject so0_0 = so0.FindChild("so0_0", false);
  340. SceneObject so0_1 = so0.FindChild("so0_1", false);
  341. SceneObject so1 = sceneRoot.FindChild("so1", false);
  342. SceneObject so1_0 = so1.FindChild("so1_0", false);
  343. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  344. SceneObject so1_1 = new SceneObject("so1_1");
  345. so1_1.Parent = so1;
  346. so0.RemoveComponent<UT1_Component1>();
  347. UT1_Component1 comp1 = so1.AddComponent<UT1_Component1>();
  348. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  349. so0_0.Destroy();
  350. comp1.otherSO = so1_0;
  351. comp1.otherComponent2 = comp0_1_0;
  352. comp0_1_0.otherSO = so1_1;
  353. comp0_1_0.otherComponent2 = comp1;
  354. comp0_1_0.a = 123;
  355. comp0_1_0.b = "modifiedValue";
  356. so1.Name = "so1_modified";
  357. so1.LocalPosition = new Vector3(0, 999.0f, 0.0f);
  358. EditorApplication.SaveScene("unitTest4Scene_0.prefab");
  359. }
  360. {
  361. EditorApplication.LoadScene("unitTest4Scene_1.prefab");
  362. SceneObject parentSO0 = Scene.Root.FindChild("parentSO0", false);
  363. SceneObject parentSO1 = Scene.Root.FindChild("parentSO1", false);
  364. SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);
  365. UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent<UT1_Component1>();
  366. SceneObject prefabInstance = parentSO0.GetChild(0);
  367. SceneObject so0 = prefabInstance.FindChild("so0", false);
  368. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  369. SceneObject so0_0 = so0.FindChild("so0_0", false);
  370. SceneObject so0_1 = so0.FindChild("so0_1", false);
  371. SceneObject so1_0 = so1.FindChild("so1_0", false);
  372. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  373. SceneObject so1_1 = so1.FindChild("so1_1", false);
  374. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  375. UT1_Component1 comp1 = so1.GetComponent<UT1_Component1>();
  376. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  377. Assert(parentComp1_0.otherSO == so1_0);
  378. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  379. Assert(so1_1 != null);
  380. Assert(so0_0 == null);
  381. Assert(comp0 == null);
  382. Assert(comp0_1_0.otherSO == so1_1);
  383. Assert(comp0_1_0.otherComponent2 == comp1);
  384. Assert(comp0_1_0.a == 123);
  385. Assert(comp0_1_0.b == "modifiedValue");
  386. Assert(comp1.otherSO == so1_0);
  387. Assert(comp1.otherComponent2 == comp0_1_0);
  388. Assert(MathEx.ApproxEquals(so1.LocalPosition.y, 999.0f));
  389. }
  390. }
  391. Debug.Log("Passed stage 3");
  392. // Make instance specific changes to the prefab, modify the prefab itself and ensure
  393. // both changes persist
  394. {
  395. // Create new scene referencing the prefab and make instance modifications
  396. {
  397. // unitTest4Scene_2.prefab:
  398. // parent2SO0
  399. // - [unitTest4Scene_0.prefab]
  400. // parent2SO1
  401. // - parent2SO1_0 (Comp1)
  402. // unitTest4Scene_0.prefab (unitTest4Scene_2.prefab instance):
  403. // so0 (Comp1(INSTANCE))
  404. // - so0_0 (INSTANCE)
  405. // - so0_1 (Comp1)
  406. // - so0_1_0 (Comp1)
  407. // so1 (Comp2)
  408. // - so1_0
  409. Scene.Clear();
  410. SceneObject parent2SO0 = new SceneObject("parent2SO0");
  411. SceneObject parent2SO1 = new SceneObject("parent2SO1");
  412. SceneObject parent2SO1_0 = new SceneObject("parent2SO1_0");
  413. parent2SO1_0.Parent = parent2SO1;
  414. UT1_Component1 parentComp1_0 = parent2SO1_0.AddComponent<UT1_Component1>();
  415. Prefab scene0Prefab = ProjectLibrary.Load<Prefab>("unitTest4Scene_0.prefab");
  416. SceneObject prefabInstance = scene0Prefab.Instantiate();
  417. prefabInstance.Parent = parent2SO0;
  418. SceneObject so0 = prefabInstance.FindChild("so0", false);
  419. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  420. SceneObject so0_1 = so0.FindChild("so0_1", false);
  421. SceneObject so1_0 = so1.FindChild("so1_0", false);
  422. SceneObject so1_1 = so1.FindChild("so1_1", false);
  423. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  424. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  425. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  426. UT1_Component1 comp0_1 = so0_1.GetComponent<UT1_Component1>();
  427. SceneObject so0_0 = new SceneObject("so0_0");
  428. so0_0.Parent = so0;
  429. UT1_Component1 comp0 = so0.AddComponent<UT1_Component1>();
  430. so1.RemoveComponent<UT1_Component1>();
  431. so1_1.Destroy();
  432. comp0.otherSO = so0_1_0;
  433. comp0.otherComponent = comp1;
  434. parentComp1_0.otherSO = so1_0;
  435. parentComp1_0.otherComponent2 = comp0_1_0;
  436. comp0_1_0.otherSO = parent2SO1_0;
  437. comp0_1_0.otherComponent2 = parentComp1_0;
  438. comp0_1_0.b = "instanceValue";
  439. comp0_1.b = "instanceValue2";
  440. EditorApplication.SaveScene("unitTest4Scene_2.prefab");
  441. }
  442. Debug.Log("Passed stage 4.1");
  443. // Reload the scene and ensure instance modifications remain
  444. {
  445. EditorApplication.LoadScene("unitTest4Scene_2.prefab");
  446. SceneObject root = Scene.Root;
  447. SceneObject parent2SO0 = root.FindChild("parent2SO0", false);
  448. SceneObject parent2SO1 = root.FindChild("parent2SO1", false);
  449. SceneObject parent2SO1_0 = parent2SO1.FindChild("parent2SO1_0", false);
  450. SceneObject prefabInstance = parent2SO0.GetChild(0);
  451. SceneObject so0 = prefabInstance.FindChild("so0", false);
  452. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  453. SceneObject so0_0 = so0.FindChild("so0_0", false);
  454. SceneObject so0_1 = so0.FindChild("so0_1", false);
  455. SceneObject so1_0 = so1.FindChild("so1_0", false);
  456. SceneObject so1_1 = so1.FindChild("so1_1", false);
  457. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  458. UT1_Component1 parentComp1_0 = parent2SO1_0.GetComponent<UT1_Component1>();
  459. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  460. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  461. UT1_Component1 comp11 = so1.GetComponent<UT1_Component1>();
  462. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  463. UT1_Component1 comp0_1 = so0_1.GetComponent<UT1_Component1>();
  464. Assert(so0_0 != null);
  465. Assert(comp0 != null);
  466. Assert(so1_1 == null);
  467. Assert(comp11 == null);
  468. Assert(comp0.otherSO == so0_1_0);
  469. Assert(comp0.otherComponent == comp1);
  470. Assert(parentComp1_0.otherSO == so1_0);
  471. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  472. Debug.Log(comp0_1_0.otherSO == null);
  473. if (comp0_1_0.otherSO != null)
  474. Debug.Log(comp0_1_0.otherSO.InstanceId + " - " + parent2SO1_0.InstanceId);
  475. Assert(comp0_1_0.otherSO == parent2SO1_0);
  476. Assert(comp0_1_0.otherComponent2 == parentComp1_0);
  477. Assert(comp0_1_0.b == "instanceValue");
  478. Assert(comp0_1.b == "instanceValue2");
  479. }
  480. Debug.Log("Passed stage 4.2");
  481. // Load original scene and ensure instance modifications didn't influence it
  482. {
  483. EditorApplication.LoadScene("unitTest4Scene_1.prefab");
  484. SceneObject parentSO0 = Scene.Root.FindChild("parentSO0", false);
  485. SceneObject parentSO1 = Scene.Root.FindChild("parentSO1", false);
  486. SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);
  487. UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent<UT1_Component1>();
  488. SceneObject prefabInstance = parentSO0.GetChild(0);
  489. SceneObject so0 = prefabInstance.FindChild("so0", false);
  490. SceneObject so1 = prefabInstance.FindChild("so1_modified", false);
  491. SceneObject so0_0 = so0.FindChild("so0_0", false);
  492. SceneObject so0_1 = so0.FindChild("so0_1", false);
  493. SceneObject so1_0 = so1.FindChild("so1_0", false);
  494. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  495. SceneObject so1_1 = so1.FindChild("so1_1", false);
  496. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  497. UT1_Component1 comp1 = so1.GetComponent<UT1_Component1>();
  498. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  499. UT1_Component1 comp0_1 = so0_1.GetComponent<UT1_Component1>();
  500. Assert(parentComp1_0.otherSO == so1_0);
  501. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  502. Assert(so1_1 != null);
  503. Assert(so0_0 == null);
  504. Assert(comp0 == null);
  505. Assert(comp0_1_0.otherSO == so1_1);
  506. Assert(comp0_1_0.otherComponent2 == comp1);
  507. Assert(comp0_1_0.a == 123);
  508. Assert(comp0_1_0.b == "modifiedValue");
  509. Assert(comp1.otherSO == so1_0);
  510. Assert(comp1.otherComponent2 == comp0_1_0);
  511. Assert(comp0_1.b == "originalValue2");
  512. Assert(MathEx.ApproxEquals(so1.LocalPosition.y, 999.0f));
  513. }
  514. Debug.Log("Passed stage 4.3");
  515. // Modify prefab and ensure both prefab and instance modifications remain
  516. {
  517. // unitTest4Scene_0.prefab:
  518. // so0 (Comp1)
  519. // - so0_1
  520. // - so0_1_0 (Comp1)
  521. // so1 (Comp1, Comp2)
  522. // - so1_1
  523. // - so1_2 (Comp1)
  524. // unitTest4Scene_0.prefab (unitTest4Scene_2.prefab instance):
  525. // so0 (Comp1)
  526. // - so0_0
  527. // - so0_1 (Comp1)
  528. // - so0_1_0 (Comp1)
  529. // so1 (Comp2)
  530. // - so1_2 (Comp1)
  531. Scene.Load("unitTest4Scene_0.prefab");
  532. SceneObject sceneRoot = Scene.Root;
  533. SceneObject so0 = sceneRoot.FindChild("so0", false);
  534. SceneObject so0_1 = so0.FindChild("so0_1", false);
  535. SceneObject so1 = sceneRoot.FindChild("so1_modified", false);
  536. SceneObject so1_0 = so1.FindChild("so1_0", false);
  537. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  538. SceneObject so1_2 = new SceneObject("so1_2");
  539. so1_2.Parent = so1;
  540. so0.AddComponent<UT1_Component1>();
  541. so0_1.RemoveComponent<UT1_Component1>();
  542. so1_0.Destroy();
  543. UT1_Component1 comp3 = so1_2.AddComponent<UT1_Component1>();
  544. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  545. comp0_1_0.b = "modifiedValueAgain";
  546. so1.Name = "so1_modifiedAgain";
  547. comp3.otherSO = so0_1;
  548. comp3.otherComponent2 = comp0_1_0;
  549. EditorApplication.SaveScene("unitTest4Scene_0.prefab");
  550. }
  551. Debug.Log("Passed stage 4.4");
  552. // Reload the scene and ensure both instance and prefab modifications remain
  553. {
  554. EditorApplication.LoadScene("unitTest4Scene_2.prefab");
  555. SceneObject root = Scene.Root;
  556. SceneObject parent2SO0 = root.FindChild("parent2SO0", false);
  557. SceneObject parent2SO1 = root.FindChild("parent2SO1", false);
  558. SceneObject parent2SO1_0 = parent2SO1.FindChild("parent2SO1_0", false);
  559. SceneObject prefabInstance = parent2SO0.GetChild(0);
  560. SceneObject so0 = prefabInstance.FindChild("so0", false);
  561. SceneObject so1 = prefabInstance.FindChild("so1_modifiedAgain", false);
  562. SceneObject so0_0 = so0.FindChild("so0_0", false);
  563. SceneObject so0_1 = so0.FindChild("so0_1", false);
  564. SceneObject so1_0 = so1.FindChild("so1_0", false);
  565. SceneObject so1_1 = so1.FindChild("so1_1", false);
  566. SceneObject so1_2 = so1.FindChild("so1_2", false);
  567. SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);
  568. UT1_Component1 parentComp1_0 = parent2SO1_0.GetComponent<UT1_Component1>();
  569. UT1_Component1 comp0 = so0.GetComponent<UT1_Component1>();
  570. UT1_Component2 comp1 = so1.GetComponent<UT1_Component2>();
  571. UT1_Component1 comp11 = so1.GetComponent<UT1_Component1>();
  572. UT1_Component1 comp0_1_0 = so0_1_0.GetComponent<UT1_Component1>();
  573. UT1_Component1 comp3 = so1_2.AddComponent<UT1_Component1>();
  574. // Check instance modifications (they should override any prefab modifications)
  575. Assert(so0_0 != null);
  576. Assert(comp0 != null);
  577. Assert(so1_1 == null);
  578. Assert(comp11 == null);
  579. Assert(comp0.otherSO == so0_1_0);
  580. Assert(comp0.otherComponent == comp1);
  581. Assert(parentComp1_0.otherSO == so1_0);
  582. Assert(parentComp1_0.otherComponent2 == comp0_1_0);
  583. Assert(comp0_1_0.otherSO == parent2SO1_0);
  584. Assert(comp0_1_0.otherComponent2 == parentComp1_0);
  585. Assert(comp0_1_0.b == "instanceValue");
  586. // Check prefab modifications
  587. Assert(so1_0 == null);
  588. Assert(so1.Name == "so1_modifiedAgain");
  589. Assert(comp3.otherSO == so0_1);
  590. Assert(comp3.otherComponent2 == comp0_1_0);
  591. }
  592. Debug.Log("Passed stage 4.5");
  593. }
  594. }
  595. catch
  596. {
  597. PrintSceneState();
  598. throw;
  599. }
  600. finally
  601. {
  602. if (!string.IsNullOrEmpty(oldScene))
  603. Scene.Load(ProjectLibrary.GetPath(oldScene));
  604. else
  605. Scene.Clear();
  606. ProjectLibrary.Delete("unitTest4Scene_0.prefab");
  607. ProjectLibrary.Delete("unitTest4Scene_1.prefab");
  608. ProjectLibrary.Delete("unitTest4Scene_2.prefab");
  609. }
  610. }
  611. /// <summary>
  612. /// Runs all tests.
  613. /// </summary>
  614. static void RunTests()
  615. {
  616. UnitTest1_ManagedSerialization();
  617. UnitTest2_SerializableProperties();
  618. UnitTest3_ManagedDiff();
  619. UnitTest4_Prefabs();
  620. }
  621. [MethodImpl(MethodImplOptions.InternalCall)]
  622. private static extern void Internal_UT1_GameObjectClone(SceneObject so);
  623. [MethodImpl(MethodImplOptions.InternalCall)]
  624. private static extern void Internal_UT3_GenerateDiff(UT_DiffObj oldObj, UT_DiffObj newObj);
  625. [MethodImpl(MethodImplOptions.InternalCall)]
  626. private static extern void Internal_UT3_ApplyDiff(UT_DiffObj obj);
  627. }
  628. }