Files
mrgameeng/tests/MrGameEng.Core.Tests/SceneManagerTests.cs
T
Leonid PershinandClaude Fable 5 501d81e19f
CI / build-test (push) Failing after 1m7s
Fix engine-wide code review findings
Collisions: init bucket heads to -1 (QueryAabb hung before the first
rebuild), reset query stamps on truncated QueryAabb (later queries
silently dropped entities), inside-origin raycasts hit at fraction 0 for
circles too, exactly-touching boxes now pair like touching circles.

Graphics: render into the letterbox viewport so the picture matches
ScreenToWorld/WorldToScreen instead of stretching; Y-sort by the
transform pivot rather than the quad center; lock-free snapshot
LayerRegistry (parallel submit read it unsynchronized); validate
InitialCapacity; warn when UseRenderer2D drops options of a later scene.

Core: scenes are explicitly single-use (re-loading threw silently
duplicated systems/entities before — now it throws), Scene.RegisterUnload
for per-scene resources, a switch requested during the reveal phase
covers again instead of hard-swapping, borderless fullscreen
(HardwareModeSwitch off), InputCapture service for input-suppressing
overlays, host disposes the transition renderer and IDisposable services
on shutdown.

Input: game input reads as released while InputCapture is held; mouse
position and wheel freeze so deltas stay zero.

DevConsole: holds InputCapture while open (typing no longer drives the
camera), Revision increments only under the lock, quoted command
arguments, history capped at 256.

UI: scene Desktop skips Myra input processing while the console is open
(clicks no longer fall through), is disposed on scene unload, and Myra
init no longer depends on a process-static flag.

Audio: validate channel count/sample rate before stopping the previous
track, empty looped oggs no longer hang FillBuffers, the instance stops
when a non-looping track drains (IsPlaying was stuck true).

Atlases: metadata v2 stores per-source size+mtime snapshots, so
timestamp-preserving copies and renames invalidate correctly; loader
checks the version and disposes pages on partial load failure; shared
pages never exceed a non-POT MaxPageSize; oversized items pack first
onto exact-size pages instead of splitting an open shared page; the CLI
validates numeric options.

Assets.Generator: file names are escaped in XML docs and string
literals, members no longer collide with the enclosing class (CS0542),
and the Assets root is resolved against build_property.projectdir so
nested "Assets" directories do not shift region paths.

Pathfinding: queries throw when the grid was resized after construction;
generation stamps survive int overflow.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 21:18:08 +03:00

153 lines
4.3 KiB
C#

using MrGameEng.Core;
using Xunit;
namespace MrGameEng.Core.Tests;
public class SceneManagerTests
{
private sealed class TrackingScene : Scene
{
public int LoadCount;
public int UnloadCount;
public int UpdateCount;
protected override void OnLoad() => LoadCount++;
protected override void OnUnload() => UnloadCount++;
public override void Update(GameClock clock)
{
UpdateCount++;
base.Update(clock);
}
}
[Fact]
public void Switch_IsDeferred_UntilNextUpdate()
{
var context = new EngineContext();
var scene = new TrackingScene();
context.Scenes.Switch(scene);
Assert.Null(context.Scenes.Current);
Assert.Equal(0, scene.LoadCount);
context.Scenes.Update(context.Clock);
Assert.Same(scene, context.Scenes.Current);
Assert.Equal(1, scene.LoadCount);
Assert.Equal(1, scene.UpdateCount);
Assert.True(scene.IsLoaded);
}
[Fact]
public void Switch_UnloadsPreviousScene_AndLoadsNext()
{
var context = new EngineContext();
var first = new TrackingScene();
var second = new TrackingScene();
context.Scenes.Switch(first);
context.Scenes.Update(context.Clock);
context.Scenes.Switch(second);
context.Scenes.Update(context.Clock);
Assert.Equal(1, first.UnloadCount);
Assert.False(first.IsLoaded);
Assert.Same(second, context.Scenes.Current);
Assert.Equal(1, second.LoadCount);
}
[Fact]
public void Switch_ToNull_UnloadsCurrentScene()
{
var context = new EngineContext();
var scene = new TrackingScene();
context.Scenes.Switch(scene);
context.Scenes.Update(context.Clock);
context.Scenes.Switch(null);
context.Scenes.Update(context.Clock);
Assert.Null(context.Scenes.Current);
Assert.Equal(1, scene.UnloadCount);
}
[Fact]
public void Update_WithoutScene_DoesNothing()
{
var context = new EngineContext();
context.Scenes.Update(context.Clock);
context.Scenes.Draw(context.Clock);
Assert.Null(context.Scenes.Current);
}
private sealed class CallbackScene(Action<Scene> onLoad) : Scene
{
protected override void OnLoad() => onLoad(this);
}
[Fact]
public void Switch_BackToLoadedSceneInstance_Throws()
{
var context = new EngineContext();
var first = new TrackingScene();
var second = new TrackingScene();
context.Scenes.Switch(first);
context.Scenes.Update(context.Clock);
context.Scenes.Switch(second);
context.Scenes.Update(context.Clock);
// Повторная загрузка молча задвоила бы системы и сущности — должен быть отказ.
context.Scenes.Switch(first);
Assert.Throws<InvalidOperationException>(() => context.Scenes.Update(context.Clock));
}
[Fact]
public void RegisterUnload_RunsOnUnload_InReverseOrder()
{
var context = new EngineContext();
var order = new List<int>();
var scene = new CallbackScene(s =>
{
s.RegisterUnload(() => order.Add(1));
s.RegisterUnload(() => order.Add(2));
});
context.Scenes.Switch(scene);
context.Scenes.Update(context.Clock);
Assert.Empty(order);
context.Scenes.Switch(null);
context.Scenes.Update(context.Clock);
Assert.Equal([2, 1], order);
}
private sealed class DisposableService : IDisposable
{
public int DisposeCount;
public void Dispose() => DisposeCount++;
}
[Fact]
public void DisposeServices_DisposesEachServiceOnce_AndSkipsExcept()
{
var registry = new ServiceRegistry();
var service = new DisposableService();
var host = new DisposableService();
registry.Add(service);
registry.Add<IDisposable>(host); // host зарегистрирован, но его освобождает вызывающий
registry.DisposeServices(except: host);
Assert.Equal(1, service.DisposeCount);
Assert.Equal(0, host.DisposeCount);
Assert.Null(registry.GetOrDefault<DisposableService>());
}
}