A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections;
3using UnityEditor;
4using UnityEditorInternal;
5
6namespace UnityEngine.TestTools
7{
8 /// <summary>
9 /// WaitForDomainReload is an <see cref="IEditModeTestYieldInstruction"/> that you can yield in Edit Mode tests. It delays the execution of scripts until after an incoming domain reload. If the domain reload results in a script compilation failure, then it throws an exception.
10 /// </summary>
11 public class WaitForDomainReload : IEditModeTestYieldInstruction
12 {
13 /// <summary>
14 /// Create a new instance of the `WaitForDomainReload` yield instruction.
15 /// <example>
16 /// <code>
17 /// [UnitySetUp]
18 /// public IEnumerator SetUp()
19 /// {
20 /// File.Copy("Resources/MyDll.dll", @"Assets/MyDll.dll", true); // Trigger a domain reload.
21 /// AssetDatabase.Refresh();
22 /// yield return new WaitForDomainReload();
23 /// }
24 /// </code>
25 /// </example>
26 /// </summary>
27 public WaitForDomainReload()
28 {
29 ExpectDomainReload = true;
30 }
31
32 /// <summary>
33 /// Returns true if the instruction expects a domain reload to occur.
34 /// </summary>
35 public bool ExpectDomainReload { get; }
36 /// <summary>
37 /// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
38 /// </summary>
39 public bool ExpectedPlaymodeState { get; }
40
41 /// <summary>
42 /// Perform the multi step action of waiting for a domain reload.
43 /// </summary>
44 /// <returns>An IEnumerator with steps.</returns>
45 /// <exception cref="Exception">Throws an exception if script compilation failed or if the expected domain reload did not occur.</exception>
46 public IEnumerator Perform()
47 {
48 EditorApplication.UnlockReloadAssemblies();
49
50 while (InternalEditorUtility.IsScriptReloadRequested() || EditorApplication.isCompiling)
51 {
52 yield return null;
53 }
54
55 // Add this point the domain reload should have occured and stopped any further progress on the instruction.
56 EditorApplication.LockReloadAssemblies();
57 throw new Exception(
58 EditorUtility.scriptCompilationFailed
59 ? "Script compilation failed"
60 : "Expected domain reload, but it did not occur");
61 }
62 }
63}