using System; using System.IO; using System.Linq; using System.Reflection; using DevilLink2.Core; using DevilLink2.Gameplay; using DevilLink2.Persistence; using DevilLink2.Presentation; using DevilLink2.UI; using NUnit.Framework; using UnityEditor; using UnityEngine; using UnityEngine.UI; using Object=UnityEngine.Object; namespace DevilLink2.Tests { public sealed partial class UIScreenTests { private sealed class Utc : IUtcClock { public DateTimeOffset UtcNow=>new DateTimeOffset(2026,11,3,12,0,0,TimeSpan.Zero); } private GameObject root; private RectTransform frame; private BoardView view; private BoardDemoBootstrap bootstrap; private UIManager ui; private SavedPlayer player; private string directory; [SetUp] public void Setup() { directory=Path.Combine(Path.GetTempPath(),"DevilLink2.UITests",Guid.NewGuid().ToString("N")); var saves=new LocalSaveService(new FileSaveStorage(directory)); saves.Save(new SaveData{Coins=9876,SpinTickets=7,ClassicProgress=2,TimeAttackProgress=3,ZenProgress=19}); player=new SavedPlayer(saves,new Utc()); root=new GameObject("UITestCanvas",typeof(RectTransform),typeof(Canvas),typeof(CanvasScaler),typeof(GraphicRaycaster)); root.GetComponent().renderMode=RenderMode.ScreenSpaceOverlay; var scaler=root.GetComponent(); scaler.uiScaleMode=CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution=new Vector2(430,932); var safe=UIBuilder.Rect("SafeArea",root.transform,0,0,430,932); safe.gameObject.AddComponent(); frame=UIBuilder.Rect("PortraitFrame",safe,0,0,430,932); var viewport=UIBuilder.Rect("BoardViewport",frame,6,60,418,860); var content=UIBuilder.Rect("Content",viewport,0,0,418,860); var board=UIBuilder.Rect("Board",content,0,0,418,385); var prefab=AssetDatabase.LoadAssetAtPath("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab"); view=viewport.gameObject.AddComponent(); view.Configure(prefab.GetComponent(),viewport,content,board); bootstrap=root.AddComponent(); bootstrap.Configure(view); ui=UIBuilder.Build(frame,bootstrap,view); ui.Initialize(player,bootstrap); Canvas.ForceUpdateCanvases(); } [TearDown] public void Cleanup() { Object.DestroyImmediate(root); player.Dispose(); string expected=Path.GetFullPath(Path.Combine(Path.GetTempPath(),"DevilLink2.UITests"))+Path.DirectorySeparatorChar; string resolved=Path.GetFullPath(directory); if(!resolved.StartsWith(expected,StringComparison.OrdinalIgnoreCase) || !Guid.TryParseExact(Path.GetFileName(resolved),"N",out _))throw new InvalidOperationException("Invalid test cleanup path."); if(Directory.Exists(resolved))Directory.Delete(resolved,true); } [Test] public void StartToLobbyReusesPagesAndDoesNotCreateSession() { Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Start)); Assert.That(bootstrap.Session,Is.Null); ui.StartScreen.StartButton.onClick.Invoke(); ui.StartScreen.StartButton.onClick.Invoke(); Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby)); Assert.That(bootstrap.Session,Is.Null); Assert.That(frame.GetComponentsInChildren(true).Length,Is.EqualTo(1)); Assert.That(frame.GetComponentsInChildren(true).Length,Is.EqualTo(1)); Assert.That(root.GetComponentsInChildren(true).Length,Is.EqualTo(1)); Assert.That(UIBuilder.Build(frame,bootstrap,view),Is.SameAs(ui)); } [TestCase(GameMode.Classic,3)] [TestCase(GameMode.TimeAttack,4)] [TestCase(GameMode.Zen,20)] public void EachCardStartsOnlyOneRealSessionWithIndependentProgress(GameMode mode,int level) { ui.ShowLobby(); var card=ui.Lobby.Cards.Single(c=>c.Mode==mode); card.Button.onClick.Invoke(); var session=bootstrap.Session; var expected=LevelCatalog.Get(mode,level); Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Gameplay)); Assert.That(session.Mode,Is.EqualTo(mode)); Assert.That(session.Definition,Is.SameAs(expected)); Assert.That(view.Data,Is.SameAs(session.Board)); Assert.That(view.VisibleTileCount,Is.EqualTo(expected.Columns*expected.Rows)); Assert.That(view.Data.Columns,Is.EqualTo(expected.Columns)); Assert.That(view.Data.Rows,Is.EqualTo(expected.Rows)); Assert.That(bootstrap.Flow.Runtime.Timer.InitialTime,Is.EqualTo(expected.TimeLimitSeconds)); Assert.That(bootstrap.Flow.Runtime.Timer.HasTimer,Is.EqualTo(expected.HasTimer)); Assert.That(ui.GameTitle.text,Does.Contain(UIManager.ModeName(mode))); Assert.That(ui.GameTitle.text,Does.Contain("LEVEL "+level)); card.Button.onClick.Invoke(); Assert.That(ui.SelectMode(mode),Is.False); Assert.That(bootstrap.Session,Is.SameAs(session)); ui.LobbyButton.onClick.Invoke(); Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby)); } [Test] public void LobbyReflectsRealBalancesAndIndependentProgress() { ui.ShowLobby(); Assert.That(ui.Lobby.CoinsLabel.text,Is.EqualTo("9,876")); Assert.That(ui.Lobby.TicketsLabel.text,Is.EqualTo("7")); Assert.That(ui.Lobby.Cards[0].ProgressLabel.text,Does.Contain("CLEARED 2/20")); Assert.That(ui.Lobby.Cards[1].ProgressLabel.text,Does.Contain("CLEARED 3/20")); Assert.That(ui.Lobby.Cards[2].ProgressLabel.text,Does.Contain("CLEARED 19/20")); player.Economy.TryCredit(Guid.NewGuid(),100,"test income"); Assert.That(ui.Lobby.CoinsLabel.text,Is.EqualTo("9,976")); } [Test] public void RepeatedInitializationAndLobbyVisitsDoNotDuplicateSubscriptions() { var field=typeof(PlayerEconomy).GetField("PersistentStateChanged",BindingFlags.NonPublic|BindingFlags.Instance); int before=((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length; for(int i=0;i<10;i++) { ui.Initialize(player,bootstrap); ui.ShowLobby(); } Assert.That(((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length,Is.EqualTo(before)); Assert.That(frame.GetComponentsInChildren(true).Length,Is.EqualTo(1)); } [Test] public void DestroyAndRebuildShellDetachesEventsAndRetainsOriginalBoardObject() { var field=typeof(PlayerEconomy).GetField("PersistentStateChanged",BindingFlags.NonPublic|BindingFlags.Instance); int subscribed=((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length; var boardObject=view.gameObject; Object.DestroyImmediate(ui.gameObject); Assert.That(((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length,Is.EqualTo(subscribed-1)); Assert.That(boardObject,Is.Not.Null); ui=UIBuilder.Build(frame,bootstrap,view); ui.Initialize(player,bootstrap); ui.ShowLobby(); ui.SelectMode(GameMode.Classic); Assert.That(view.gameObject,Is.SameAs(boardObject)); Assert.That(frame.GetComponentsInChildren(true).Length,Is.EqualTo(11)); Assert.That(root.GetComponentsInChildren(true).Length,Is.EqualTo(1)); } [Test] public void HiddenPagesDisableCanvasGroupInputAndReservedEntriesAreUnavailable() { ui.ShowLobby(); var hidden=ui.StartScreen.GetComponent(); Assert.That(hidden.blocksRaycasts || hidden.interactable,Is.False); var unavailable=ui.Lobby.GetComponentsInChildren