Initial Commit

This commit is contained in:
Sebastian Cabrera 2021-08-02 05:44:37 -04:00
parent 53eb92e9af
commit 270ab7d11f
15341 changed files with 700234 additions and 0 deletions

View file

@ -0,0 +1,57 @@
using System;
using System.Collections;
using UnityEditor;
namespace UnityEngine.TestTools
{
/// <summary>
/// Implements <see cref="IEditModeTestYieldInstruction"/>. Creates a yield instruction to enter Play Mode.
/// </summary>
public class EnterPlayMode : IEditModeTestYieldInstruction
{
/// <summary>
/// Returns true if the instruction expects a domain reload to occur.
/// </summary>
public bool ExpectDomainReload { get; }
/// <summary>
/// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
/// </summary>
public bool ExpectedPlaymodeState { get; private set; }
/// <summary>
/// When creating an Editor test that uses the UnityTest attribute, use this to trigger the Editor to enter Play Mode.
/// Throws an exception if the Editor is already in Play Mode or if there is a script compilation error.
/// </summary>
/// <param name="expectDomainReload">A flag indication whether to expect a domain reload.</param>
public EnterPlayMode(bool expectDomainReload = true)
{
ExpectDomainReload = expectDomainReload;
}
/// <summary>
/// Performs the multi-step instructions of entering PlayMode.
/// </summary>
/// <returns>An IEnumerator with the async steps.</returns>
/// <exception cref="Exception">An exception is thrown if the editor is already in PlayMode or if script compilation failed.</exception>
public IEnumerator Perform()
{
if (EditorApplication.isPlaying)
{
throw new Exception("Editor is already in PlayMode");
}
if (EditorUtility.scriptCompilationFailed)
{
throw new Exception("Script compilation failed");
}
yield return null;
ExpectedPlaymodeState = true;
EditorApplication.UnlockReloadAssemblies();
EditorApplication.isPlaying = true;
while (!EditorApplication.isPlaying)
{
yield return null;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9bd5a110ed89025499ddee8c7e73778e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,48 @@
using System;
using System.Collections;
using UnityEditor;
namespace UnityEngine.TestTools
{
/// <summary>
/// Implements <see cref="IEditModeTestYieldInstruction"/>. A new instance of the class is a yield instruction to exit Play Mode.
/// </summary>
public class ExitPlayMode : IEditModeTestYieldInstruction
{
/// <summary>
/// Gets the value of ExpectDomainReload
/// </summary>
public bool ExpectDomainReload { get; }
/// <summary>
/// Gets the value of ExpectedPlaymodeState
/// </summary>
public bool ExpectedPlaymodeState { get; private set; }
/// <summary>
/// Sets ExpectDomainReload and ExpectedPlaymodeState to false.
/// </summary>
public ExitPlayMode()
{
ExpectDomainReload = false;
ExpectedPlaymodeState = false;
}
/// <summary>
/// Performs the multi-step instruction of exiting PlayMode.
/// </summary>
/// <returns>An IEnumerator with the async steps.</returns>
/// <exception cref="Exception">An exception is thrown if the editor is not in PlayMode.</exception>
public IEnumerator Perform()
{
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
throw new Exception("Editor is already in EditMode");
}
EditorApplication.isPlaying = false;
while (EditorApplication.isPlaying)
{
yield return null;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 408674d91d506a54aac9a7f07951c018
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,111 @@
using System;
using System.Collections;
using UnityEditor;
namespace UnityEngine.TestTools
{
/// <summary>
/// `RecompileScripts` is an <see cref="IEditModeTestYieldInstruction"/> that you can yield in Edit Mode tests. It lets you trigger a recompilation of scripts in the Unity Editor.
/// </summary>
public class RecompileScripts : IEditModeTestYieldInstruction
{
/// <summary>
/// Creates a new instance of the `RecompileScripts` yield instruction.
/// <example>
/// <code>
/// [UnitySetUp]
/// public IEnumerator SetUp()
/// {
/// using (var file = File.CreateText("Assets/temp/myScript.cs"))
/// {
/// file.Write("public class ATempClass { }");
/// }
/// AssetDatabase.Refresh();
/// yield return new RecompileScripts();
/// }
/// </code>
/// </example>
/// </summary>
public RecompileScripts() : this(true)
{
}
/// <summary>
/// Creates a new instance of the `RecompileScripts` yield instruction.
/// </summary>
/// <param name="expectScriptCompilation">This parameter indicates if you expect a script compilation to start (defaults to true). If a script compilation does not start and `expectScriptCompilation` is true, then it throws an exception.</param>
public RecompileScripts(bool expectScriptCompilation) : this(expectScriptCompilation, true)
{
}
/// <summary>
/// Creates a new instance of the `RecompileScripts` yield instruction.
/// </summary>
/// <param name="expectScriptCompilation">This parameter indicates if you expect a script compilation to start (defaults to true). If a script compilation does not start and `expectScriptCompilation` is `true`, then it throws an exception.</param>
/// <param name="expectScriptCompilationSuccess">This parameter indicates if you expect a script compilation to succeed. If not succeeded then an exception will be thrown.</param>
public RecompileScripts(bool expectScriptCompilation, bool expectScriptCompilationSuccess)
{
ExpectScriptCompilation = expectScriptCompilation;
ExpectScriptCompilationSuccess = expectScriptCompilationSuccess;
ExpectDomainReload = true;
}
/// <summary>
/// Returns true if the instruction expects a domain reload to occur.
/// </summary>
public bool ExpectDomainReload { get; private set; }
/// <summary>
/// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
/// </summary>
public bool ExpectedPlaymodeState { get; }
/// <summary>
/// Indicates whether a script compilation is expected.
/// </summary>
public bool ExpectScriptCompilation { get; private set; }
/// <summary>
/// Indicates whether the expected script compilation is expected to succeed.
/// </summary>
public bool ExpectScriptCompilationSuccess { get; private set; }
/// <summary>
/// The current active instance of the RecompileScripts yield instruction.
/// </summary>
public static RecompileScripts Current { get; private set; }
/// <summary>
/// Perform the multi step instruction of triggering a recompilation of scripts and waiting for its completion.
/// </summary>
/// <returns>An IEnumerator with the async steps.</returns>
/// <exception cref="Exception">Throws an exception if the editor does not need to recompile scripts or if the script compilation failed when expected to succeed.</exception>
public IEnumerator Perform()
{
Current = this;
// We need to yield, to give the test runner a chance to prepare for the domain reload
// If the script compilation happens very fast, then EditModeRunner.MoveNextAndUpdateYieldObject will not have a chance to set m_CurrentYieldObject
// This really should be fixed in EditModeRunner.MoveNextAndUpdateYieldObject
yield return null;
AssetDatabase.Refresh();
if (ExpectScriptCompilation && !EditorApplication.isCompiling)
{
Current = null;
throw new Exception("Editor does not need to recompile scripts");
}
EditorApplication.UnlockReloadAssemblies();
while (EditorApplication.isCompiling)
{
yield return null;
}
Current = null;
if (ExpectScriptCompilationSuccess && EditorUtility.scriptCompilationFailed)
{
EditorApplication.LockReloadAssemblies();
throw new Exception("Script compilation failed");
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9202fbba95ea8294cb5e718f028f21b0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,63 @@
using System;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;
namespace UnityEngine.TestTools
{
/// <summary>
/// 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.
/// </summary>
public class WaitForDomainReload : IEditModeTestYieldInstruction
{
/// <summary>
/// Create a new instance of the `WaitForDomainReload` yield instruction.
/// <example>
/// <code>
/// [UnitySetUp]
/// public IEnumerator SetUp()
/// {
/// File.Copy("Resources/MyDll.dll", @"Assets/MyDll.dll", true); // Trigger a domain reload.
/// AssetDatabase.Refresh();
/// yield return new WaitForDomainReload();
/// }
/// </code>
/// </example>
/// </summary>
public WaitForDomainReload()
{
ExpectDomainReload = true;
}
/// <summary>
/// Returns true if the instruction expects a domain reload to occur.
/// </summary>
public bool ExpectDomainReload { get;  }
/// <summary>
/// Returns true if the instruction expects the Unity Editor to be in **Play Mode**.
/// </summary>
public bool ExpectedPlaymodeState { get; }
/// <summary>
/// Perform the multi step action of waiting for a domain reload.
/// </summary>
/// <returns>An IEnumerator with steps.</returns>
/// <exception cref="Exception">Throws an exception if script compilation failed or if the expected domain reload did not occur.</exception>
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");
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5df3c21c5237c994db89660fbdfee07d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: