using System; using System.Reflection; using System.Collections.Generic; using DG.Tweening; using DevilLink2.Core; using DevilLink2.Gameplay; using DevilLink2.Presentation; using NUnit.Framework; using UnityEditor; using UnityEngine; using UnityEngine.UI; using Object = UnityEngine.Object; namespace DevilLink2.Tests { public sealed class InteractionVisualTests { // This shipped DOTween version skips Init outside Play Mode, but can still allocate tweens. // Scope only its initialized flag for synchronous EditMode tests; no editor update loop, // scene component, real clock, KillAll, or changes to third-party sources are required. private sealed class EditModeTweenScope : IDisposable { private readonly FieldInfo initialized = typeof(DOTween).GetField("initialized", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); private readonly bool previous; private readonly List owned = new List(); public EditModeTweenScope() { Assert.That(initialized, Is.Not.Null, "DOTween initialization flag must match the installed DLL."); previous = (bool)initialized.GetValue(null); initialized.SetValue(null, true); } public Tween Create(TweenCallback completed = null) { float value = 0; var tween = DOTween.To(() => value, x => value = x, 1, 10) .SetUpdate(UpdateType.Manual).SetRecyclable(false).OnComplete(completed); owned.Add(tween); return tween; } public void Dispose() { try { foreach (var tween in owned) tween.Kill(false); } finally { initialized.SetValue(null, previous); } } } private sealed class Clock : IMonotonicClock { public double Seconds => 0; } private static GameObject TileRoot() => Object.Instantiate(AssetDatabase.LoadAssetAtPath("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab")); private static void Field(TileView tile, string name, object value) => typeof(TileView).GetField(name, BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tile, value); [Test] public void SelectedAndHintNeverResizeOrTintArtwork() { var root = TileRoot(); try { var tile = root.GetComponent(); tile.Bind(new Tile(1, TileSymbol.imp, 0, 0)); var rect = (RectTransform)root.transform; var size = rect.sizeDelta; var position = rect.anchoredPosition; tile.SetHighlighted(true); tile.SetHinted(true); tile.SetHighlighted(false); tile.SetHinted(false); Assert.That(rect.sizeDelta, Is.EqualTo(size)); Assert.That(rect.anchoredPosition, Is.EqualTo(position)); Assert.That(rect.localScale, Is.EqualTo(Vector3.one)); Assert.That(tile.Artwork.color, Is.EqualTo(Color.white)); Assert.That(tile.IsHinted, Is.False); } finally { Object.DestroyImmediate(root); } } [Test] public void HintCanExpireWhileSelectionRemainsVisible() { var root = TileRoot(); try { var tile = root.GetComponent(); tile.Bind(new Tile(1, TileSymbol.imp, 0, 0)); tile.SetHinted(true); tile.SetHighlighted(true); tile.SetHinted(false); Assert.That(root.GetComponent().color, Is.EqualTo((Color)new Color32(225,190,100,255))); tile.SetHighlighted(false); Assert.That(root.GetComponent().color, Is.EqualTo((Color)new Color32(55,56,77,255))); } finally { Object.DestroyImmediate(root); } } [Test] public void AcceptedRemovalNotifiesOnceEvenWithReentrantRequest() { var root = TileRoot(); var tile = root.GetComponent(); tile.Bind(new Tile(1,TileSymbol.imp,0,0)); int notifications = 0; tile.PlayRemoval(() => { notifications++; tile.PlayRemoval(() => notifications++); }); Assert.That(notifications, Is.EqualTo(1)); Assert.That(root == null, Is.True); } [Test] public void DisableKillsOnlyOwnedTweensAndSuppressesStaleCompletion() { using (var scope = new EditModeTweenScope()) { var root = TileRoot(); int callbacks = 0; int killed = 0; var unrelated = scope.Create(); var tile = root.GetComponent(); try { tile.Bind(new Tile(1,TileSymbol.imp,0,0)); var tween = scope.Create(() => callbacks++).OnKill(() => killed++); Field(tile,"stateTween",tween); Field(tile,"removalCompleted",(Action)(() => callbacks++)); root.SetActive(false); // Non-ExecuteAlways MonoBehaviours don't reliably receive native lifecycle messages // in EditMode. Exercise the exact production callback explicitly (it is idempotent). typeof(TileView).GetMethod("OnDisable",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly, null,Type.EmptyTypes,null).Invoke(tile,null); Assert.That(tween.IsActive(), Is.False); Assert.That(killed, Is.EqualTo(1)); Assert.That(unrelated.IsActive(), Is.True); tile.FinishVisuals(); Assert.That(callbacks, Is.Zero); Assert.That(typeof(TileView).GetField("stateTween",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(tile),Is.Null); Assert.That(typeof(TileView).GetField("removalCompleted",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(tile),Is.Null); } finally { Object.DestroyImmediate(root); } } } [Test] public void RebindKillsOldAnimationAndResetsVisualTransform() { using (var scope = new EditModeTweenScope()) { var root = TileRoot(); int staleCallbacks = 0; var unrelated = scope.Create(); try { var tile = root.GetComponent(); tile.Bind(new Tile(1,TileSymbol.imp,0,0)); var tween = scope.Create(() => staleCallbacks++); Field(tile,"removalTween",tween); Field(tile,"removalCompleted",(Action)(() => staleCallbacks++)); root.transform.localScale = Vector3.one * .86f; root.AddComponent().alpha = .1f; tile.Bind(new Tile(2,TileSymbol.ring,0,0)); Assert.That(tween.IsActive(), Is.False); Assert.That(unrelated.IsActive(), Is.True); Assert.That(staleCallbacks, Is.Zero); Assert.That(typeof(TileView).GetField("removalCompleted",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(tile),Is.Null); Assert.That(root.transform.localScale, Is.EqualTo(Vector3.one)); Assert.That(root.GetComponent().alpha, Is.EqualTo(1)); Assert.That(tile.TileId, Is.EqualTo(2)); } finally { Object.DestroyImmediate(root); } } } [TestCase(0)] [TestCase(1)] [TestCase(2)] public void PathsRenderAndExpireWithoutChangingSuppliedPoints(int turns) { var root = new GameObject("Paths",typeof(RectTransform),typeof(BoardPathRenderer)); var board = new GameObject("Board",typeof(RectTransform)); try { var rect=(RectTransform)board.transform; var layout=BoardLayoutCalculator.Calculate(4,8); rect.sizeDelta=layout.Size; GridPosition[] points = turns==0 ? new[]{new GridPosition(0,0),new GridPosition(0,3)} : turns==1 ? new[]{new GridPosition(0,0),new GridPosition(0,3),new GridPosition(3,3)} : new[]{new GridPosition(0,0),new GridPosition(-1,0),new GridPosition(-1,3),new GridPosition(0,3)}; var copy=(GridPosition[])points.Clone(); var paths=root.GetComponent(); paths.Show(points,rect,layout); paths.AdvanceVisuals(BoardPathRenderer.DrawSeconds); using(var mesh=new VertexHelper()) { typeof(BoardPathRenderer).GetMethod("OnPopulateMesh",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly, null,new[]{typeof(VertexHelper)},null).Invoke(paths,new object[]{mesh}); Assert.That(mesh.currentVertCount,Is.GreaterThanOrEqualTo(4)); var vertex=new UIVertex(); mesh.PopulateUIVertex(ref vertex,0); Assert.That(vertex.color,Is.EqualTo((Color32)new Color(239f/255,214f/255,157f/255,1))); } CollectionAssert.AreEqual(copy,points); Assert.That(paths.raycastTarget,Is.False); Assert.That(paths.maskable,Is.False); paths.AdvanceVisuals(1); Assert.That(paths.ActivePathCount,Is.Zero); } finally { Object.DestroyImmediate(root); Object.DestroyImmediate(board); } } [Test] public void MissingPathResourcesAndDisableLeaveNoResidualStrokes() { var root=new GameObject("Paths",typeof(RectTransform),typeof(BoardPathRenderer)); try { var path=root.GetComponent(); path.Show(null,null,default); Assert.That(path.ActivePathCount,Is.Zero); path.Show(new[]{new GridPosition(0,0),new GridPosition(0,1)},(RectTransform)root.transform,BoardLayoutCalculator.Calculate(4,8)); root.SetActive(false); Assert.That(path.ActivePathCount,Is.Zero); } finally { Object.DestroyImmediate(root); } } [TestCase(false)] [TestCase(true)] public void PauseThenResumeOrRestartClearsPathsAndIsolatesOldSession(bool restart) { var root=new GameObject("Lifecycle",typeof(RectTransform)); using(var flow=new GameFlowController(GameMode.Classic,new ModeProgress(),new PlayerEconomy(),new Clock())) try { var content=new GameObject("Content",typeof(RectTransform)); content.transform.SetParent(root.transform,false); var board=new GameObject("Board",typeof(RectTransform)); board.transform.SetParent(content.transform,false); var view=root.AddComponent(); view.Configure(AssetDatabase.LoadAssetAtPath("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab").GetComponent(), (RectTransform)root.transform,(RectTransform)content.transform,(RectTransform)board.transform); var controller=root.AddComponent(); controller.Initialize(view,flow); flow.Start(); var old=flow.Runtime; new BoardMoveFinder().TryFindMove(view.Data,out var pair); flow.Click(pair.First.Row,pair.First.Column); flow.Click(pair.Second.Row,pair.Second.Column); var paths=root.GetComponentInChildren(); Assert.That(paths.ActivePathCount,Is.EqualTo(1)); flow.Pause(); Assert.That(paths.ActivePathCount,Is.Zero); if(restart) { flow.RestartCurrentLevel(); var fresh=view.Data; old.Update(); Assert.That(view.Data,Is.SameAs(fresh)); Assert.That(view.VisibleTileCount,Is.EqualTo(32)); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); } else { var current=view.Data; flow.Resume(); Assert.That(view.Data,Is.SameAs(current)); Assert.That(view.VisibleTileCount,Is.EqualTo(30)); } flow.ReturnToLobby(); Assert.That(view.VisibleTileCount,Is.Zero); Assert.That(paths.ActivePathCount,Is.Zero); } finally { Object.DestroyImmediate(root); } } } }