Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions Assets/Tests/Editor/DomainReloadDisableScopeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using System.IO;

using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;

using io.github.hatayama.UnityCliLoop.FirstPartyTools;

namespace io.github.hatayama.UnityCliLoop.Tests.Editor
{
/// <summary>
/// Test fixture that verifies Enter Play Mode settings recovery for DomainReloadDisableScope.
/// </summary>
public sealed class DomainReloadDisableScopeTests
{
private static readonly string MarkerFilePath = DomainReloadDisableScopeRecovery.MarkerFilePathForTests;
private static readonly string TempFilePath = DomainReloadDisableScopeRecovery.TempFilePathForTests;

private bool _originalEnabled;
private EnterPlayModeOptions _originalOptions;
private bool _markerFileExisted;
private string _markerFileContent;
private bool _tempFileExisted;
private string _tempFileContent;

[SetUp]
public void SetUp()
{
_originalEnabled = EditorSettings.enterPlayModeOptionsEnabled;
_originalOptions = EditorSettings.enterPlayModeOptions;
_markerFileExisted = File.Exists(MarkerFilePath);
_markerFileContent = _markerFileExisted ? File.ReadAllText(MarkerFilePath) : string.Empty;
_tempFileExisted = File.Exists(TempFilePath);
_tempFileContent = _tempFileExisted ? File.ReadAllText(TempFilePath) : string.Empty;

DomainReloadDisableScope.ResetActiveScopeCountForTests();
DomainReloadDisableScopeRecovery.ClearPendingRestoreForTests();
}

[TearDown]
public void TearDown()
{
EditorSettings.enterPlayModeOptionsEnabled = _originalEnabled;
EditorSettings.enterPlayModeOptions = _originalOptions;
DomainReloadDisableScope.ResetActiveScopeCountForTests();
DomainReloadDisableScopeRecovery.ClearPendingRestoreForTests();
RestoreFile(MarkerFilePath, _markerFileExisted, _markerFileContent);
RestoreFile(TempFilePath, _tempFileExisted, _tempFileContent);
}

[Test]
public void Dispose_RestoresOriginalSettingsAndDeletesMarkerFile()
{
// Verifies that a completed scope restores settings and removes its recovery marker.
SetEnterPlayModeSettings(false, EnterPlayModeOptions.None);

using (DomainReloadDisableScope scope = new DomainReloadDisableScope())
{
Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload));
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.True);
}

Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None));
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False);
}

[Test]
public void Dispose_KeepsDomainReloadDisabled_UntilLastNestedScopeDisposes()
{
// Verifies that nested scopes restore settings only after the final scope exits.
SetEnterPlayModeSettings(false, EnterPlayModeOptions.None);

DomainReloadDisableScope outerScope = new DomainReloadDisableScope();
DomainReloadDisableScope innerScope = new DomainReloadDisableScope();

innerScope.Dispose();

Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload));
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.True);

outerScope.Dispose();

Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None));
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False);
}

[Test]
public void RestoreIfPending_RestoresOriginalSettings_WhenScopeWasAbandoned()
{
// Verifies that a marker left by an abandoned scope can restore the original settings.
SetEnterPlayModeSettings(false, EnterPlayModeOptions.None);

DomainReloadDisableScope scope = new DomainReloadDisableScope();
Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload));

DomainReloadDisableScopeRecovery.RestoreIfPending();

Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None));
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False);
System.GC.KeepAlive(scope);
}

[Test]
public void Constructor_RestoresStaleMarker_BeforeSavingNewRunMarker()
{
// Verifies that stale recovery state is consumed before the next run records its baseline.
SetEnterPlayModeSettings(false, EnterPlayModeOptions.None);

DomainReloadDisableScope abandonedScope = new DomainReloadDisableScope();
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.True);
DomainReloadDisableScope.RecoverAbandonedScopeBeforeNewRun();

DomainReloadDisableScope nextScope = new DomainReloadDisableScope();
DomainReloadDisableScopeRecoveryData markerData = DomainReloadDisableScopeRecovery.ReadMarkerDataForTests();

Assert.That(markerData.originalOptionsEnabled, Is.False);
Assert.That(markerData.originalOptions, Is.EqualTo((int)EnterPlayModeOptions.None));

nextScope.Dispose();

Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None));
Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False);
System.GC.KeepAlive(abandonedScope);
}

[Test]
public void RestoreIfPending_LeavesNoMarkerFile_AfterSuccessfulRestore()
{
// Verifies that successful recovery deletes the marker instead of saving a cleared state.
SetEnterPlayModeSettings(false, EnterPlayModeOptions.None);
DomainReloadDisableScope scope = new DomainReloadDisableScope();

DomainReloadDisableScopeRecovery.RestoreIfPending();

Assert.That(File.Exists(MarkerFilePath), Is.False);
Assert.That(File.Exists(TempFilePath), Is.False);
System.GC.KeepAlive(scope);
}

[Test]
public void RestoreIfPending_RejectsUnsupportedEnterPlayModeOptionBits()
{
// Verifies that corrupted markers cannot write unsupported option bits into EditorSettings.
SetEnterPlayModeSettings(true, EnterPlayModeOptions.DisableDomainReload);
string json = "{ \"originalOptionsEnabled\": false, \"originalOptions\": 1024 }";
File.WriteAllText(MarkerFilePath, json);

InvalidOperationException exception =
Assert.Throws<InvalidOperationException>(() => DomainReloadDisableScopeRecovery.RestoreIfPending());

Assert.That(exception.Message, Does.Contain("supported Enter Play Mode option bits"));
Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True);
Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload));
Assert.That(File.Exists(MarkerFilePath), Is.False);
}

[Test]
public void SaveCurrentSettings_ThrowsAndKeepsExistingMarker_WhenMarkerAlreadyExists()
{
// Verifies that saving refuses to replace an unrecovered marker.
string directoryPath = Path.GetDirectoryName(MarkerFilePath);
if (!string.IsNullOrEmpty(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

string existingJson = "{ \"originalOptionsEnabled\": true, \"originalOptions\": 1 }";
File.WriteAllText(MarkerFilePath, existingJson);

LogAssert.Expect(LogType.Assert, "recovery marker must be restored before saving a new marker");
InvalidOperationException exception =
Assert.Throws<InvalidOperationException>(() => DomainReloadDisableScopeRecovery.SaveCurrentSettings());

Assert.That(exception.Message, Does.Contain("must be restored before saving"));
Assert.That(File.ReadAllText(MarkerFilePath), Is.EqualTo(existingJson));
Assert.That(File.Exists(TempFilePath), Is.False);
}

private static void SetEnterPlayModeSettings(bool enabled, EnterPlayModeOptions options)
{
EditorSettings.enterPlayModeOptionsEnabled = enabled;
EditorSettings.enterPlayModeOptions = options;
}

private static void RestoreFile(string filePath, bool fileExisted, string fileContent)
{
if (!fileExisted)
{
return;
}

string directoryPath = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

File.WriteAllText(filePath, fileContent);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editor/DomainReloadDisableScopeTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnityCLILoop.FirstPartyTools.Editor")]
[assembly: InternalsVisibleTo("UnityCLILoop.FirstPartyTools.RunTests.TestFramework.Editor")]
[assembly: InternalsVisibleTo("UnityCLILoop.Tests.Editor")]
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,61 @@ namespace io.github.hatayama.UnityCliLoop.FirstPartyTools
/// </summary>
public class DomainReloadDisableScope : IDisposable
{
private readonly bool _originalEnabled;
private readonly EnterPlayModeOptions _originalOptions;
private static int _activeScopeCount;
private bool _disposed;

public DomainReloadDisableScope()
{
_originalEnabled = EditorSettings.enterPlayModeOptionsEnabled;
_originalOptions = EditorSettings.enterPlayModeOptions;
if (_activeScopeCount == 0)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
{
DomainReloadDisableScopeRecovery.RestoreIfPending();
DomainReloadDisableScopeRecovery.SaveCurrentSettings();
}

_activeScopeCount++;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

EditorSettings.enterPlayModeOptionsEnabled = true;
EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.DisableDomainReload;
}

public void Dispose()
{
EditorSettings.enterPlayModeOptionsEnabled = _originalEnabled;
EditorSettings.enterPlayModeOptions = _originalOptions;
if (_disposed)
{
return;
}

_disposed = true;
System.Diagnostics.Debug.Assert(_activeScopeCount > 0, "active scope count must be positive before dispose");
_activeScopeCount--;

if (_activeScopeCount == 0)
{
DomainReloadDisableScopeRecovery.RestoreIfPending();
}
}

/// <summary>
/// Resets stale scope state before a new PlayMode test run starts.
/// </summary>
internal static void RecoverAbandonedScopeBeforeNewRun()
{
if (_activeScopeCount == 0)
{
return;
}

if (!DomainReloadDisableScopeRecovery.HasPendingRestore())
{
return;
}

_activeScopeCount = 0;
}

internal static void ResetActiveScopeCountForTests()
{
_activeScopeCount = 0;
}
}
}
}
Loading
Loading