using System;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;
namespace UnityEngine.TestTools
{
///
/// WaitForDomainReload is an 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.
///
public class WaitForDomainReload : IEditModeTestYieldInstruction
{
///
/// Create a new instance of the `WaitForDomainReload` yield instruction.
///
///
/// [UnitySetUp]
/// public IEnumerator SetUp()
/// {
/// File.Copy("Resources/MyDll.dll", @"Assets/MyDll.dll", true); // Trigger a domain reload.
/// AssetDatabase.Refresh();
/// yield return new WaitForDomainReload();
/// }
///
///
///
public WaitForDomainReload()
{
ExpectDomainReload = true;
}
///
/// Returns true if the instruction expects a domain reload to occur.
///
public bool ExpectDomainReload { get; }
///
/// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
///
public bool ExpectedPlaymodeState { get; }
///
/// Perform the multi step action of waiting for a domain reload.
///
/// An IEnumerator with steps.
/// Throws an exception if script compilation failed or if the expected domain reload did not occur.
public IEnumerator Perform()
{
EditorApplication.UnlockReloadAssemblies();
while (InternalEditorUtility.IsScriptReloadRequested() || EditorApplication.isCompiling)
{
yield return null;
}
// Add this point the domain reload should have occured and stopped any further progress on the instruction.
EditorApplication.LockReloadAssemblies();
throw new Exception(
EditorUtility.scriptCompilationFailed ?
"Script compilation failed" :
"Expected domain reload, but it did not occur");
}
}
}