UIScreenTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using DevilLink2.Core;
  6. using DevilLink2.Gameplay;
  7. using DevilLink2.Persistence;
  8. using DevilLink2.Presentation;
  9. using DevilLink2.UI;
  10. using NUnit.Framework;
  11. using UnityEditor;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using Object=UnityEngine.Object;
  15. namespace DevilLink2.Tests
  16. {
  17. public sealed partial class UIScreenTests
  18. {
  19. private sealed class Utc : IUtcClock { public DateTimeOffset UtcNow=>new DateTimeOffset(2026,11,3,12,0,0,TimeSpan.Zero); }
  20. private GameObject root; private RectTransform frame; private BoardView view; private BoardDemoBootstrap bootstrap;
  21. private UIManager ui; private SavedPlayer player; private string directory;
  22. [SetUp] public void Setup()
  23. {
  24. directory=Path.Combine(Path.GetTempPath(),"DevilLink2.UITests",Guid.NewGuid().ToString("N"));
  25. var saves=new LocalSaveService(new FileSaveStorage(directory)); saves.Save(new SaveData{Coins=9876,SpinTickets=7,ClassicProgress=2,TimeAttackProgress=3,ZenProgress=19});
  26. player=new SavedPlayer(saves,new Utc());
  27. root=new GameObject("UITestCanvas",typeof(RectTransform),typeof(Canvas),typeof(CanvasScaler),typeof(GraphicRaycaster));
  28. root.GetComponent<Canvas>().renderMode=RenderMode.ScreenSpaceOverlay;
  29. var scaler=root.GetComponent<CanvasScaler>(); scaler.uiScaleMode=CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution=new Vector2(430,932);
  30. var safe=UIBuilder.Rect("SafeArea",root.transform,0,0,430,932); safe.gameObject.AddComponent<SafeAreaAdapter>();
  31. frame=UIBuilder.Rect("PortraitFrame",safe,0,0,430,932);
  32. var viewport=UIBuilder.Rect("BoardViewport",frame,6,60,418,860);
  33. var content=UIBuilder.Rect("Content",viewport,0,0,418,860); var board=UIBuilder.Rect("Board",content,0,0,418,385);
  34. var prefab=AssetDatabase.LoadAssetAtPath<GameObject>("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab");
  35. view=viewport.gameObject.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(),viewport,content,board);
  36. bootstrap=root.AddComponent<BoardDemoBootstrap>(); bootstrap.Configure(view);
  37. ui=UIBuilder.Build(frame,bootstrap,view); ui.Initialize(player,bootstrap);
  38. Canvas.ForceUpdateCanvases();
  39. }
  40. [TearDown] public void Cleanup()
  41. {
  42. Object.DestroyImmediate(root); player.Dispose();
  43. string expected=Path.GetFullPath(Path.Combine(Path.GetTempPath(),"DevilLink2.UITests"))+Path.DirectorySeparatorChar;
  44. string resolved=Path.GetFullPath(directory);
  45. if(!resolved.StartsWith(expected,StringComparison.OrdinalIgnoreCase) || !Guid.TryParseExact(Path.GetFileName(resolved),"N",out _))throw new InvalidOperationException("Invalid test cleanup path.");
  46. if(Directory.Exists(resolved))Directory.Delete(resolved,true);
  47. }
  48. [Test] public void StartToLobbyReusesPagesAndDoesNotCreateSession()
  49. {
  50. Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Start)); Assert.That(bootstrap.Session,Is.Null);
  51. ui.StartScreen.StartButton.onClick.Invoke(); ui.StartScreen.StartButton.onClick.Invoke();
  52. Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby)); Assert.That(bootstrap.Session,Is.Null);
  53. Assert.That(frame.GetComponentsInChildren<StartScreen>(true).Length,Is.EqualTo(1)); Assert.That(frame.GetComponentsInChildren<LobbyScreen>(true).Length,Is.EqualTo(1));
  54. Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1)); Assert.That(UIBuilder.Build(frame,bootstrap,view),Is.SameAs(ui));
  55. }
  56. [TestCase(GameMode.Classic,3)] [TestCase(GameMode.TimeAttack,4)] [TestCase(GameMode.Zen,20)]
  57. public void EachCardStartsOnlyOneRealSessionWithIndependentProgress(GameMode mode,int level)
  58. {
  59. ui.ShowLobby(); var card=ui.Lobby.Cards.Single(c=>c.Mode==mode); card.Button.onClick.Invoke();
  60. var session=bootstrap.Session; var expected=LevelCatalog.Get(mode,level);
  61. Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Gameplay)); Assert.That(session.Mode,Is.EqualTo(mode)); Assert.That(session.Definition,Is.SameAs(expected));
  62. Assert.That(view.Data,Is.SameAs(session.Board)); Assert.That(view.VisibleTileCount,Is.EqualTo(expected.Columns*expected.Rows));
  63. Assert.That(view.Data.Columns,Is.EqualTo(expected.Columns)); Assert.That(view.Data.Rows,Is.EqualTo(expected.Rows));
  64. Assert.That(bootstrap.Flow.Runtime.Timer.InitialTime,Is.EqualTo(expected.TimeLimitSeconds)); Assert.That(bootstrap.Flow.Runtime.Timer.HasTimer,Is.EqualTo(expected.HasTimer));
  65. Assert.That(ui.GameTitle.text,Does.Contain(UIManager.ModeName(mode))); Assert.That(ui.GameTitle.text,Does.Contain("LEVEL "+level));
  66. card.Button.onClick.Invoke(); Assert.That(ui.SelectMode(mode),Is.False); Assert.That(bootstrap.Session,Is.SameAs(session));
  67. ui.LobbyButton.onClick.Invoke(); Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby));
  68. }
  69. [Test] public void LobbyReflectsRealBalancesAndIndependentProgress()
  70. {
  71. ui.ShowLobby(); Assert.That(ui.Lobby.CoinsLabel.text,Is.EqualTo("9,876")); Assert.That(ui.Lobby.TicketsLabel.text,Is.EqualTo("7"));
  72. 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"));
  73. player.Economy.TryCredit(Guid.NewGuid(),100,"test income"); Assert.That(ui.Lobby.CoinsLabel.text,Is.EqualTo("9,976"));
  74. }
  75. [Test] public void RepeatedInitializationAndLobbyVisitsDoNotDuplicateSubscriptions()
  76. {
  77. var field=typeof(PlayerEconomy).GetField("PersistentStateChanged",BindingFlags.NonPublic|BindingFlags.Instance);
  78. int before=((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length;
  79. for(int i=0;i<10;i++) { ui.Initialize(player,bootstrap); ui.ShowLobby(); }
  80. Assert.That(((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length,Is.EqualTo(before));
  81. Assert.That(frame.GetComponentsInChildren<LobbyScreen>(true).Length,Is.EqualTo(1));
  82. }
  83. [Test] public void DestroyAndRebuildShellDetachesEventsAndRetainsOriginalBoardObject()
  84. {
  85. var field=typeof(PlayerEconomy).GetField("PersistentStateChanged",BindingFlags.NonPublic|BindingFlags.Instance);
  86. int subscribed=((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length; var boardObject=view.gameObject;
  87. Object.DestroyImmediate(ui.gameObject);
  88. Assert.That(((Delegate)field.GetValue(player.Economy)).GetInvocationList().Length,Is.EqualTo(subscribed-1)); Assert.That(boardObject,Is.Not.Null);
  89. ui=UIBuilder.Build(frame,bootstrap,view); ui.Initialize(player,bootstrap); ui.ShowLobby(); ui.SelectMode(GameMode.Classic);
  90. Assert.That(view.gameObject,Is.SameAs(boardObject)); Assert.That(frame.GetComponentsInChildren<UIScreen>(true).Length,Is.EqualTo(11));
  91. Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
  92. }
  93. [Test] public void HiddenPagesDisableCanvasGroupInputAndReservedEntriesAreUnavailable()
  94. {
  95. ui.ShowLobby(); var hidden=ui.StartScreen.GetComponent<CanvasGroup>(); Assert.That(hidden.blocksRaycasts || hidden.interactable,Is.False);
  96. var unavailable=ui.Lobby.GetComponentsInChildren<Button>(true).Where(b=>b.name.EndsWith("Unavailable")).ToArray();
  97. Assert.That(unavailable.Length,Is.EqualTo(0)); Assert.That(unavailable.All(b=>!b.interactable),Is.True);
  98. }
  99. [Test] public void SafeAreaUsesExistingAdapterAndDesignControlsFitTheirPage()
  100. {
  101. Assert.That(root.GetComponentsInChildren<SafeAreaAdapter>(true).Length,Is.EqualTo(1));
  102. var adapter=root.GetComponentInChildren<SafeAreaAdapter>(); adapter.Apply(); var safe=(RectTransform)adapter.transform;
  103. if(Screen.width>0 && Screen.height>0)
  104. { 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)); }
  105. foreach(var card in ui.Lobby.Cards)
  106. { 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)); }
  107. Assert.That(ui.StartScreen.StartButton.GetComponent<RectTransform>().sizeDelta,Is.EqualTo(new Vector2(232,56)));
  108. }
  109. [Test] public void ReturningAndReenteringModeCreatesFreshBoardWithoutChangingPlayerData()
  110. {
  111. ui.ShowLobby(); ui.SelectMode(GameMode.Classic); var old=bootstrap.Session.Board; ui.ShowLobby(); ui.SelectMode(GameMode.Zen);
  112. 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));
  113. Assert.That(player.Progress.GetHighestCleared(GameMode.Classic),Is.EqualTo(2));
  114. }
  115. [Test] public void RepeatedStartNavigationDoesNotReplayPageChange()
  116. {
  117. int changed=0;ui.PageChanged+=page=>changed++;
  118. ui.StartScreen.StartButton.onClick.Invoke();ui.StartScreen.StartButton.onClick.Invoke();
  119. Assert.That(changed,Is.EqualTo(1));Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby));
  120. Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
  121. }
  122. [Test] public void SettingsLegalAndHelpUseCachedTransitionPanels()
  123. {
  124. ui.ShowLobby();ui.OpenSettings();var settings=ui.Settings;
  125. settings.ShowLegal(false);Assert.That(UITransition.IsVisible(settings.LegalPanel),Is.True);
  126. settings.BackFromLegal();settings.ShowLegal(true);settings.BackFromLegal();
  127. settings.HelpButton.onClick.Invoke();Assert.That(ui.Help.IsVisible,Is.True);Assert.That(settings.IsVisible,Is.False);
  128. ui.CloseHelp();Assert.That(ui.Settings,Is.SameAs(settings));Assert.That(settings.IsVisible,Is.True);
  129. Assert.That(settings.LegalPanel.GetComponents<UITransition>().Length,Is.EqualTo(1));
  130. Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
  131. }
  132. }
  133. }