| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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<Canvas>().renderMode=RenderMode.ScreenSpaceOverlay;
- var scaler=root.GetComponent<CanvasScaler>(); 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<SafeAreaAdapter>();
- 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<GameObject>("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab");
- view=viewport.gameObject.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(),viewport,content,board);
- bootstrap=root.AddComponent<BoardDemoBootstrap>(); 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<StartScreen>(true).Length,Is.EqualTo(1)); Assert.That(frame.GetComponentsInChildren<LobbyScreen>(true).Length,Is.EqualTo(1));
- Assert.That(root.GetComponentsInChildren<Canvas>(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<LobbyScreen>(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<UIScreen>(true).Length,Is.EqualTo(11));
- Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
- }
- [Test] public void HiddenPagesDisableCanvasGroupInputAndReservedEntriesAreUnavailable()
- {
- ui.ShowLobby(); var hidden=ui.StartScreen.GetComponent<CanvasGroup>(); Assert.That(hidden.blocksRaycasts || hidden.interactable,Is.False);
- var unavailable=ui.Lobby.GetComponentsInChildren<Button>(true).Where(b=>b.name.EndsWith("Unavailable")).ToArray();
- Assert.That(unavailable.Length,Is.EqualTo(0)); Assert.That(unavailable.All(b=>!b.interactable),Is.True);
- }
- [Test] public void SafeAreaUsesExistingAdapterAndDesignControlsFitTheirPage()
- {
- Assert.That(root.GetComponentsInChildren<SafeAreaAdapter>(true).Length,Is.EqualTo(1));
- var adapter=root.GetComponentInChildren<SafeAreaAdapter>(); adapter.Apply(); var safe=(RectTransform)adapter.transform;
- if(Screen.width>0 && Screen.height>0)
- { Assert.That(safe.anchorMin.x,Is.EqualTo(Screen.safeArea.xMin/Screen.width).Within(.001)); Assert.That(safe.anchorMax.y,Is.EqualTo(Screen.safeArea.yMax/Screen.height).Within(.001)); }
- foreach(var card in ui.Lobby.Cards)
- { var r=(RectTransform)card.transform; Assert.That(r.anchoredPosition.x+r.sizeDelta.x,Is.LessThanOrEqualTo(430)); Assert.That(-r.anchoredPosition.y+r.sizeDelta.y,Is.LessThanOrEqualTo(850)); }
- Assert.That(ui.StartScreen.StartButton.GetComponent<RectTransform>().sizeDelta,Is.EqualTo(new Vector2(232,56)));
- }
- [Test] public void ReturningAndReenteringModeCreatesFreshBoardWithoutChangingPlayerData()
- {
- ui.ShowLobby(); ui.SelectMode(GameMode.Classic); var old=bootstrap.Session.Board; ui.ShowLobby(); ui.SelectMode(GameMode.Zen);
- Assert.That(bootstrap.Session.Board,Is.Not.SameAs(old)); Assert.That(player.Economy.Coins,Is.EqualTo(9876)); Assert.That(player.Economy.SpinTickets,Is.EqualTo(7));
- Assert.That(player.Progress.GetHighestCleared(GameMode.Classic),Is.EqualTo(2));
- }
- [Test] public void RepeatedStartNavigationDoesNotReplayPageChange()
- {
- int changed=0;ui.PageChanged+=page=>changed++;
- ui.StartScreen.StartButton.onClick.Invoke();ui.StartScreen.StartButton.onClick.Invoke();
- Assert.That(changed,Is.EqualTo(1));Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby));
- Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
- }
- [Test] public void SettingsLegalAndHelpUseCachedTransitionPanels()
- {
- ui.ShowLobby();ui.OpenSettings();var settings=ui.Settings;
- settings.ShowLegal(false);Assert.That(UITransition.IsVisible(settings.LegalPanel),Is.True);
- settings.BackFromLegal();settings.ShowLegal(true);settings.BackFromLegal();
- settings.HelpButton.onClick.Invoke();Assert.That(ui.Help.IsVisible,Is.True);Assert.That(settings.IsVisible,Is.False);
- ui.CloseHelp();Assert.That(ui.Settings,Is.SameAs(settings));Assert.That(settings.IsVisible,Is.True);
- Assert.That(settings.LegalPanel.GetComponents<UITransition>().Length,Is.EqualTo(1));
- Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
- }
- }
- }
|