| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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<Tween> owned = new List<Tween>();
- 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<GameObject>("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<TileView>(); 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<TileView>(); tile.Bind(new Tile(1, TileSymbol.imp, 0, 0));
- tile.SetHinted(true); tile.SetHighlighted(true); tile.SetHinted(false);
- Assert.That(root.GetComponent<Image>().color, Is.EqualTo((Color)new Color32(225,190,100,255)));
- tile.SetHighlighted(false);
- Assert.That(root.GetComponent<Image>().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<TileView>(); 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<TileView>();
- 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<TileView>(); 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<CanvasGroup>().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<CanvasGroup>().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<BoardPathRenderer>();
- 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<BoardPathRenderer>(); 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<BoardView>();
- view.Configure(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab").GetComponent<TileView>(),
- (RectTransform)root.transform,(RectTransform)content.transform,(RectTransform)board.transform);
- var controller=root.AddComponent<BoardInteractionController>(); 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<BoardPathRenderer>(); 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); }
- }
- }
- }
|