using System; using DevilLink2.Core; using DevilLink2.Gameplay; using DevilLink2.Interaction; using DevilLink2.Presentation; using NUnit.Framework; using UnityEditor; using UnityEngine; using Object = UnityEngine.Object; namespace DevilLink2.Tests { public sealed class RuntimePresentationTests { private sealed class Clock : IMonotonicClock { public double Seconds { get; set; } } private static BoardView View(GameObject root) { var viewport=(RectTransform)root.transform; viewport.sizeDelta=new Vector2(418,385); var content=(RectTransform)new GameObject("Content",typeof(RectTransform)).transform; content.SetParent(viewport,false); var board=(RectTransform)new GameObject("Board",typeof(RectTransform)).transform; board.SetParent(content,false); var prefab=AssetDatabase.LoadAssetAtPath("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab"); Assert.That(prefab,Is.Not.Null); var view=root.AddComponent(); view.Configure(prefab.GetComponent(),viewport,content,board); return view; } [Test] public void ReinitializingControllerDisposesOldSubscriptionsAndScoresExactlyOnce() { var root=new GameObject("RuntimeBridgeTest",typeof(RectTransform)); try { var view=View(root); var session=new GameSessionFactory().Create(GameMode.Classic,new ModeProgress(),2026); view.Show(session.Board); var controller=root.AddComponent(); var clock=new Clock(); controller.Initialize(view,session,clock); var previous=controller.Runtime; controller.Initialize(view,session,clock); var current=controller.Runtime; Assert.That(controller.State,Is.SameAs(current.Interaction)); Assert.That(current.Session.Board,Is.SameAs(view.Data)); Assert.That(previous.Timer.IsRunning,Is.False); Assert.That(previous.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(new BoardMoveFinder().TryFindMove(session.Board,out var pair),Is.True); current.Click(pair.First.Row,pair.First.Column); current.Click(pair.Second.Row,pair.Second.Column); Assert.That(current.Score.TotalScore,Is.EqualTo(100)); Assert.That(previous.Score.TotalScore,Is.Zero); clock.Seconds=.25; current.Update(); Assert.That(current.Score.TotalScore,Is.EqualTo(100)); } finally { Object.DestroyImmediate(root); } } [TestCase(GameMode.Classic,177)] [TestCase(GameMode.Zen,0)] public void ControllerForwardsInvalidMatchOnceAndRuntimeAppliesOnePenalty(GameMode mode,int expected) { var root=new GameObject("RuntimeInvalidBridgeTest",typeof(RectTransform)); try { var view=View(root); var session=new GameSessionFactory().Create(mode,new ModeProgress(),2026); view.Show(session.Board); var controller=root.AddComponent(); var clock=new Clock(); controller.Initialize(view,session,clock); controller.Initialize(view,session,clock); int notifications=0; controller.InvalidMatch += (a,b)=>notifications++; var board=session.Board; var paths=new LinkPathFinder(); bool tested=false; for(int r=0;r(); Assert.Throws(()=>controller.Initialize(view,session,new Clock())); Assert.That(controller.Runtime,Is.Null); } finally { Object.DestroyImmediate(root); } } [Test] public void BootstrapConnectsRealSessionToControllerRuntime() { var root=new GameObject("RuntimeBootstrapTest",typeof(RectTransform)); try { var view=View(root); var bootstrap=root.AddComponent(); bootstrap.Configure(view); bootstrap.Initialize(GameMode.TimeAttack,new ModeProgress()); var controller=root.GetComponent(); Assert.That(controller.Runtime.Session,Is.SameAs(bootstrap.Session)); Assert.That(view.Data,Is.SameAs(bootstrap.Session.Board)); Assert.That(controller.Runtime.Timer.InitialTime,Is.EqualTo(120)); Assert.That(view.VisibleTileCount,Is.EqualTo(48)); } finally { Object.DestroyImmediate(root); } } } }