| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Linq;
- using DevilLink2.Core;
- using DevilLink2.Presentation;
- using NUnit.Framework;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.Tests
- {
- public sealed class BoardPresentationTests
- {
- private const string PrefabPath = "Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab";
- [TestCase(4, 8, 42)] [TestCase(6, 8, 42)] [TestCase(10, 14, 23)] [TestCase(8, 15, 21)]
- public void LayoutMatchesHtmlAndDoesNotOverlap(int columns, int rows, int expectedSize)
- {
- var layout = BoardLayoutCalculator.Calculate(columns, rows);
- Assert.That(layout.TileSize, Is.EqualTo(expectedSize));
- Assert.That(layout.Size.x, Is.LessThanOrEqualTo(418));
- Assert.That(layout.Size.y, Is.LessThanOrEqualTo(385));
- var first = layout.TilePosition(0, 0); var next = layout.TilePosition(1, 1);
- Assert.That(next.x - first.x, Is.EqualTo(expectedSize + 2));
- Assert.That(first.y - next.y, Is.EqualTo(expectedSize + 2));
- Assert.That(first.x, Is.EqualTo(16)); Assert.That(first.y, Is.EqualTo(-16));
- }
- [TestCase(TileSymbol.imp, "IMP")] [TestCase(TileSymbol.trident, "TRI")]
- [TestCase(TileSymbol.ring, "RNG")] [TestCase(TileSymbol.potion, "POT")]
- [TestCase(TileSymbol.candle, "CND")] [TestCase(TileSymbol.star, "STR")]
- [TestCase(TileSymbol.piggy, "PIG")] [TestCase(TileSymbol.A, "A")]
- [TestCase(TileSymbol.Q, "Q")] [TestCase(TileSymbol.J, "J")]
- [TestCase(TileSymbol.T10, "10")] [TestCase(TileSymbol.N9, "9")]
- public void DisplayNamesDoNotChangeCoreSymbols(TileSymbol symbol, string expected)
- { Assert.That(TileView.SymbolText(symbol), Is.EqualTo(expected)); }
- [TestCase(4, 8)] [TestCase(6, 8)] [TestCase(10, 14)] [TestCase(8, 15)]
- public void RealCoreBoardCreatesOneSquareViewPerTileAndRefreshes(int columns, int rows)
- {
- var root = new GameObject("PresentationTest", typeof(RectTransform));
- try
- {
- var viewport = (RectTransform)root.transform; viewport.sizeDelta = new Vector2(418, 385);
- var scroll = root.AddComponent<ScrollRect>(); scroll.viewport = viewport; scroll.horizontal = false;
- var content = (RectTransform)new GameObject("Content", typeof(RectTransform)).transform; content.SetParent(viewport, false);
- content.anchorMin = new Vector2(0, 1); content.anchorMax = Vector2.one; content.pivot = new Vector2(.5f, 1);
- scroll.content = content;
- var boardRoot = (RectTransform)new GameObject("Board", typeof(RectTransform)).transform; boardRoot.SetParent(content, false);
- var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
- Assert.That(prefab, Is.Not.Null);
- var view = root.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(), viewport, content, boardRoot);
- var board = BoardGenerator.Generate(columns, rows, 2026); view.Show(board);
- Assert.That(view.Data, Is.SameAs(board)); Assert.That(view.VisibleTileCount, Is.EqualTo(columns * rows));
- var tileViews = boardRoot.GetComponentsInChildren<TileView>();
- Assert.That(tileViews.Length, Is.EqualTo(board.RemainingTileCount));
- Assert.That(tileViews.Select(t => t.TileId).Distinct().Count(), Is.EqualTo(tileViews.Length));
- for (int r = 0; r < rows; r++) for (int c = 0; c < columns; c++)
- {
- var tile = board.GetTile(r, c); var tileView = tileViews.Single(t => t.TileId == tile.Id);
- Assert.That(tileView.DisplayText, Is.EqualTo(TileView.SymbolText(tile.Symbol)));
- var rect = (RectTransform)tileView.transform;
- Assert.That(rect.sizeDelta.x, Is.EqualTo(rect.sizeDelta.y));
- Assert.That(rect.anchoredPosition, Is.EqualTo(BoardLayoutCalculator.Calculate(columns, rows).TilePosition(r, c)));
- Assert.That(rect.anchoredPosition.x + rect.rect.width, Is.LessThanOrEqualTo(boardRoot.rect.width));
- Assert.That(tileView.GetComponent<Button>(), Is.Null);
- Assert.That(tileView.GetComponentsInChildren<Graphic>().All(g => !g.raycastTarget), Is.True);
- }
- Assert.That(scroll.horizontal, Is.False);
- // A narrow viewport scales both axes uniformly, never stretching cells.
- viewport.sizeDelta = new Vector2(180, 100); Canvas.ForceUpdateCanvases(); view.Refresh();
- Assert.That(boardRoot.rect.width * boardRoot.localScale.x, Is.LessThanOrEqualTo(180.001f));
- Assert.That(boardRoot.localScale.x, Is.EqualTo(boardRoot.localScale.y));
- Assert.That(scroll.vertical, Is.True);
- board.RemoveTile(0, 0); view.Refresh();
- Assert.That(view.VisibleTileCount, Is.EqualTo(board.RemainingTileCount));
- Assert.That(boardRoot.childCount, Is.EqualTo(board.RemainingTileCount));
- view.Clear(); Assert.That(boardRoot.childCount, Is.Zero);
- }
- finally { Object.DestroyImmediate(root); }
- }
- [Test]
- public void BootstrapGeneratesDefaultClassicBoardFromCore()
- {
- var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
- Assert.That(prefab, Is.Not.Null);
- var root = new GameObject("BootstrapTest", typeof(RectTransform));
- try
- {
- var viewport = (RectTransform)root.transform; viewport.sizeDelta = new Vector2(418, 385);
- var content = (RectTransform)new GameObject("Content", typeof(RectTransform)).transform; content.SetParent(root.transform, false);
- var boardRoot = (RectTransform)new GameObject("Board", typeof(RectTransform)).transform; boardRoot.SetParent(content, false);
- var view = root.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(), viewport, content, boardRoot);
- var bootstrap = root.AddComponent<BoardDemoBootstrap>(); bootstrap.Configure(view); bootstrap.Initialize();
- Assert.That(view.Data.Columns, Is.EqualTo(4)); Assert.That(view.Data.Rows, Is.EqualTo(8));
- Assert.That(view.VisibleTileCount, Is.EqualTo(32));
- }
- finally { Object.DestroyImmediate(root); }
- }
- }
- }
|