InteractionVisualTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using DG.Tweening;
  5. using DevilLink2.Core;
  6. using DevilLink2.Gameplay;
  7. using DevilLink2.Presentation;
  8. using NUnit.Framework;
  9. using UnityEditor;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. using Object = UnityEngine.Object;
  13. namespace DevilLink2.Tests
  14. {
  15. public sealed class InteractionVisualTests
  16. {
  17. // This shipped DOTween version skips Init outside Play Mode, but can still allocate tweens.
  18. // Scope only its initialized flag for synchronous EditMode tests; no editor update loop,
  19. // scene component, real clock, KillAll, or changes to third-party sources are required.
  20. private sealed class EditModeTweenScope : IDisposable
  21. {
  22. private readonly FieldInfo initialized = typeof(DOTween).GetField("initialized", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  23. private readonly bool previous;
  24. private readonly List<Tween> owned = new List<Tween>();
  25. public EditModeTweenScope()
  26. {
  27. Assert.That(initialized, Is.Not.Null, "DOTween initialization flag must match the installed DLL.");
  28. previous = (bool)initialized.GetValue(null); initialized.SetValue(null, true);
  29. }
  30. public Tween Create(TweenCallback completed = null)
  31. {
  32. float value = 0;
  33. var tween = DOTween.To(() => value, x => value = x, 1, 10)
  34. .SetUpdate(UpdateType.Manual).SetRecyclable(false).OnComplete(completed);
  35. owned.Add(tween); return tween;
  36. }
  37. public void Dispose()
  38. {
  39. try { foreach (var tween in owned) tween.Kill(false); }
  40. finally { initialized.SetValue(null, previous); }
  41. }
  42. }
  43. private sealed class Clock : IMonotonicClock { public double Seconds => 0; }
  44. private static GameObject TileRoot() => Object.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab"));
  45. private static void Field(TileView tile, string name, object value) => typeof(TileView).GetField(name, BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tile, value);
  46. [Test] public void SelectedAndHintNeverResizeOrTintArtwork()
  47. {
  48. var root = TileRoot();
  49. try {
  50. var tile = root.GetComponent<TileView>(); tile.Bind(new Tile(1, TileSymbol.imp, 0, 0));
  51. var rect = (RectTransform)root.transform; var size = rect.sizeDelta; var position = rect.anchoredPosition;
  52. tile.SetHighlighted(true); tile.SetHinted(true); tile.SetHighlighted(false); tile.SetHinted(false);
  53. Assert.That(rect.sizeDelta, Is.EqualTo(size)); Assert.That(rect.anchoredPosition, Is.EqualTo(position));
  54. Assert.That(rect.localScale, Is.EqualTo(Vector3.one)); Assert.That(tile.Artwork.color, Is.EqualTo(Color.white));
  55. Assert.That(tile.IsHinted, Is.False);
  56. } finally { Object.DestroyImmediate(root); }
  57. }
  58. [Test] public void HintCanExpireWhileSelectionRemainsVisible()
  59. {
  60. var root = TileRoot();
  61. try {
  62. var tile = root.GetComponent<TileView>(); tile.Bind(new Tile(1, TileSymbol.imp, 0, 0));
  63. tile.SetHinted(true); tile.SetHighlighted(true); tile.SetHinted(false);
  64. Assert.That(root.GetComponent<Image>().color, Is.EqualTo((Color)new Color32(225,190,100,255)));
  65. tile.SetHighlighted(false);
  66. Assert.That(root.GetComponent<Image>().color, Is.EqualTo((Color)new Color32(55,56,77,255)));
  67. } finally { Object.DestroyImmediate(root); }
  68. }
  69. [Test] public void AcceptedRemovalNotifiesOnceEvenWithReentrantRequest()
  70. {
  71. var root = TileRoot(); var tile = root.GetComponent<TileView>(); tile.Bind(new Tile(1,TileSymbol.imp,0,0));
  72. int notifications = 0;
  73. tile.PlayRemoval(() => { notifications++; tile.PlayRemoval(() => notifications++); });
  74. Assert.That(notifications, Is.EqualTo(1)); Assert.That(root == null, Is.True);
  75. }
  76. [Test] public void DisableKillsOnlyOwnedTweensAndSuppressesStaleCompletion()
  77. {
  78. using (var scope = new EditModeTweenScope())
  79. {
  80. var root = TileRoot(); int callbacks = 0; int killed = 0;
  81. var unrelated = scope.Create(); var tile = root.GetComponent<TileView>();
  82. try {
  83. tile.Bind(new Tile(1,TileSymbol.imp,0,0));
  84. var tween = scope.Create(() => callbacks++).OnKill(() => killed++);
  85. Field(tile,"stateTween",tween); Field(tile,"removalCompleted",(Action)(() => callbacks++));
  86. root.SetActive(false);
  87. // Non-ExecuteAlways MonoBehaviours don't reliably receive native lifecycle messages
  88. // in EditMode. Exercise the exact production callback explicitly (it is idempotent).
  89. typeof(TileView).GetMethod("OnDisable",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly,
  90. null,Type.EmptyTypes,null).Invoke(tile,null);
  91. Assert.That(tween.IsActive(), Is.False); Assert.That(killed, Is.EqualTo(1));
  92. Assert.That(unrelated.IsActive(), Is.True);
  93. tile.FinishVisuals(); Assert.That(callbacks, Is.Zero);
  94. Assert.That(typeof(TileView).GetField("stateTween",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(tile),Is.Null);
  95. Assert.That(typeof(TileView).GetField("removalCompleted",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(tile),Is.Null);
  96. } finally { Object.DestroyImmediate(root); }
  97. }
  98. }
  99. [Test] public void RebindKillsOldAnimationAndResetsVisualTransform()
  100. {
  101. using (var scope = new EditModeTweenScope())
  102. {
  103. var root = TileRoot(); int staleCallbacks = 0;
  104. var unrelated = scope.Create();
  105. try {
  106. var tile = root.GetComponent<TileView>(); tile.Bind(new Tile(1,TileSymbol.imp,0,0));
  107. var tween = scope.Create(() => staleCallbacks++);
  108. Field(tile,"removalTween",tween); Field(tile,"removalCompleted",(Action)(() => staleCallbacks++));
  109. root.transform.localScale = Vector3.one * .86f;
  110. root.AddComponent<CanvasGroup>().alpha = .1f;
  111. tile.Bind(new Tile(2,TileSymbol.ring,0,0));
  112. Assert.That(tween.IsActive(), Is.False); Assert.That(unrelated.IsActive(), Is.True);
  113. Assert.That(staleCallbacks, Is.Zero);
  114. Assert.That(typeof(TileView).GetField("removalCompleted",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(tile),Is.Null);
  115. Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one));
  116. Assert.That(root.GetComponent<CanvasGroup>().alpha, Is.EqualTo(1)); Assert.That(tile.TileId, Is.EqualTo(2));
  117. } finally { Object.DestroyImmediate(root); }
  118. }
  119. }
  120. [TestCase(0)] [TestCase(1)] [TestCase(2)]
  121. public void PathsRenderAndExpireWithoutChangingSuppliedPoints(int turns)
  122. {
  123. var root = new GameObject("Paths",typeof(RectTransform),typeof(BoardPathRenderer));
  124. var board = new GameObject("Board",typeof(RectTransform));
  125. try {
  126. var rect=(RectTransform)board.transform; var layout=BoardLayoutCalculator.Calculate(4,8); rect.sizeDelta=layout.Size;
  127. GridPosition[] points = turns==0 ? new[]{new GridPosition(0,0),new GridPosition(0,3)} :
  128. turns==1 ? new[]{new GridPosition(0,0),new GridPosition(0,3),new GridPosition(3,3)} :
  129. new[]{new GridPosition(0,0),new GridPosition(-1,0),new GridPosition(-1,3),new GridPosition(0,3)};
  130. var copy=(GridPosition[])points.Clone(); var paths=root.GetComponent<BoardPathRenderer>();
  131. paths.Show(points,rect,layout); paths.AdvanceVisuals(BoardPathRenderer.DrawSeconds);
  132. using(var mesh=new VertexHelper()) {
  133. typeof(BoardPathRenderer).GetMethod("OnPopulateMesh",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly,
  134. null,new[]{typeof(VertexHelper)},null).Invoke(paths,new object[]{mesh});
  135. Assert.That(mesh.currentVertCount,Is.GreaterThanOrEqualTo(4));
  136. var vertex=new UIVertex(); mesh.PopulateUIVertex(ref vertex,0);
  137. Assert.That(vertex.color,Is.EqualTo((Color32)new Color(239f/255,214f/255,157f/255,1)));
  138. }
  139. CollectionAssert.AreEqual(copy,points); Assert.That(paths.raycastTarget,Is.False); Assert.That(paths.maskable,Is.False);
  140. paths.AdvanceVisuals(1); Assert.That(paths.ActivePathCount,Is.Zero);
  141. } finally { Object.DestroyImmediate(root); Object.DestroyImmediate(board); }
  142. }
  143. [Test] public void MissingPathResourcesAndDisableLeaveNoResidualStrokes()
  144. {
  145. var root=new GameObject("Paths",typeof(RectTransform),typeof(BoardPathRenderer));
  146. try {
  147. var path=root.GetComponent<BoardPathRenderer>(); path.Show(null,null,default);
  148. Assert.That(path.ActivePathCount,Is.Zero);
  149. path.Show(new[]{new GridPosition(0,0),new GridPosition(0,1)},(RectTransform)root.transform,BoardLayoutCalculator.Calculate(4,8));
  150. root.SetActive(false); Assert.That(path.ActivePathCount,Is.Zero);
  151. } finally { Object.DestroyImmediate(root); }
  152. }
  153. [TestCase(false)] [TestCase(true)]
  154. public void PauseThenResumeOrRestartClearsPathsAndIsolatesOldSession(bool restart)
  155. {
  156. var root=new GameObject("Lifecycle",typeof(RectTransform));
  157. using(var flow=new GameFlowController(GameMode.Classic,new ModeProgress(),new PlayerEconomy(),new Clock()))
  158. try {
  159. var content=new GameObject("Content",typeof(RectTransform)); content.transform.SetParent(root.transform,false);
  160. var board=new GameObject("Board",typeof(RectTransform)); board.transform.SetParent(content.transform,false);
  161. var view=root.AddComponent<BoardView>();
  162. view.Configure(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab").GetComponent<TileView>(),
  163. (RectTransform)root.transform,(RectTransform)content.transform,(RectTransform)board.transform);
  164. var controller=root.AddComponent<BoardInteractionController>(); controller.Initialize(view,flow); flow.Start();
  165. var old=flow.Runtime; new BoardMoveFinder().TryFindMove(view.Data,out var pair);
  166. flow.Click(pair.First.Row,pair.First.Column); flow.Click(pair.Second.Row,pair.Second.Column);
  167. var paths=root.GetComponentInChildren<BoardPathRenderer>(); Assert.That(paths.ActivePathCount,Is.EqualTo(1));
  168. flow.Pause(); Assert.That(paths.ActivePathCount,Is.Zero);
  169. if(restart) {
  170. flow.RestartCurrentLevel(); var fresh=view.Data; old.Update();
  171. Assert.That(view.Data,Is.SameAs(fresh)); Assert.That(view.VisibleTileCount,Is.EqualTo(32));
  172. Assert.That(flow.Runtime.Score.TotalScore,Is.Zero);
  173. } else {
  174. var current=view.Data; flow.Resume(); Assert.That(view.Data,Is.SameAs(current));
  175. Assert.That(view.VisibleTileCount,Is.EqualTo(30));
  176. }
  177. flow.ReturnToLobby(); Assert.That(view.VisibleTileCount,Is.Zero); Assert.That(paths.ActivePathCount,Is.Zero);
  178. } finally { Object.DestroyImmediate(root); }
  179. }
  180. }
  181. }