BoardPresentationTests.cs 6.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Linq;
  2. using DevilLink2.Core;
  3. using DevilLink2.Presentation;
  4. using NUnit.Framework;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace DevilLink2.Tests
  9. {
  10. public sealed class BoardPresentationTests
  11. {
  12. private const string PrefabPath = "Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab";
  13. [TestCase(4, 8, 42)] [TestCase(6, 8, 42)] [TestCase(10, 14, 23)] [TestCase(8, 15, 21)]
  14. public void LayoutMatchesHtmlAndDoesNotOverlap(int columns, int rows, int expectedSize)
  15. {
  16. var layout = BoardLayoutCalculator.Calculate(columns, rows);
  17. Assert.That(layout.TileSize, Is.EqualTo(expectedSize));
  18. Assert.That(layout.Size.x, Is.LessThanOrEqualTo(418));
  19. Assert.That(layout.Size.y, Is.LessThanOrEqualTo(385));
  20. var first = layout.TilePosition(0, 0); var next = layout.TilePosition(1, 1);
  21. Assert.That(next.x - first.x, Is.EqualTo(expectedSize + 2));
  22. Assert.That(first.y - next.y, Is.EqualTo(expectedSize + 2));
  23. Assert.That(first.x, Is.EqualTo(16)); Assert.That(first.y, Is.EqualTo(-16));
  24. }
  25. [TestCase(TileSymbol.imp, "IMP")] [TestCase(TileSymbol.trident, "TRI")]
  26. [TestCase(TileSymbol.ring, "RNG")] [TestCase(TileSymbol.potion, "POT")]
  27. [TestCase(TileSymbol.candle, "CND")] [TestCase(TileSymbol.star, "STR")]
  28. [TestCase(TileSymbol.piggy, "PIG")] [TestCase(TileSymbol.A, "A")]
  29. [TestCase(TileSymbol.Q, "Q")] [TestCase(TileSymbol.J, "J")]
  30. [TestCase(TileSymbol.T10, "10")] [TestCase(TileSymbol.N9, "9")]
  31. public void DisplayNamesDoNotChangeCoreSymbols(TileSymbol symbol, string expected)
  32. { Assert.That(TileView.SymbolText(symbol), Is.EqualTo(expected)); }
  33. [TestCase(4, 8)] [TestCase(6, 8)] [TestCase(10, 14)] [TestCase(8, 15)]
  34. public void RealCoreBoardCreatesOneSquareViewPerTileAndRefreshes(int columns, int rows)
  35. {
  36. var root = new GameObject("PresentationTest", typeof(RectTransform));
  37. try
  38. {
  39. var viewport = (RectTransform)root.transform; viewport.sizeDelta = new Vector2(418, 385);
  40. var scroll = root.AddComponent<ScrollRect>(); scroll.viewport = viewport; scroll.horizontal = false;
  41. var content = (RectTransform)new GameObject("Content", typeof(RectTransform)).transform; content.SetParent(viewport, false);
  42. content.anchorMin = new Vector2(0, 1); content.anchorMax = Vector2.one; content.pivot = new Vector2(.5f, 1);
  43. scroll.content = content;
  44. var boardRoot = (RectTransform)new GameObject("Board", typeof(RectTransform)).transform; boardRoot.SetParent(content, false);
  45. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
  46. Assert.That(prefab, Is.Not.Null);
  47. var view = root.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(), viewport, content, boardRoot);
  48. var board = BoardGenerator.Generate(columns, rows, 2026); view.Show(board);
  49. Assert.That(view.Data, Is.SameAs(board)); Assert.That(view.VisibleTileCount, Is.EqualTo(columns * rows));
  50. var tileViews = boardRoot.GetComponentsInChildren<TileView>();
  51. Assert.That(tileViews.Length, Is.EqualTo(board.RemainingTileCount));
  52. Assert.That(tileViews.Select(t => t.TileId).Distinct().Count(), Is.EqualTo(tileViews.Length));
  53. for (int r = 0; r < rows; r++) for (int c = 0; c < columns; c++)
  54. {
  55. var tile = board.GetTile(r, c); var tileView = tileViews.Single(t => t.TileId == tile.Id);
  56. Assert.That(tileView.DisplayText, Is.EqualTo(TileView.SymbolText(tile.Symbol)));
  57. var rect = (RectTransform)tileView.transform;
  58. Assert.That(rect.sizeDelta.x, Is.EqualTo(rect.sizeDelta.y));
  59. Assert.That(rect.anchoredPosition, Is.EqualTo(BoardLayoutCalculator.Calculate(columns, rows).TilePosition(r, c)));
  60. Assert.That(rect.anchoredPosition.x + rect.rect.width, Is.LessThanOrEqualTo(boardRoot.rect.width));
  61. Assert.That(tileView.GetComponent<Button>(), Is.Null);
  62. Assert.That(tileView.GetComponentsInChildren<Graphic>().All(g => !g.raycastTarget), Is.True);
  63. }
  64. Assert.That(scroll.horizontal, Is.False);
  65. // A narrow viewport scales both axes uniformly, never stretching cells.
  66. viewport.sizeDelta = new Vector2(180, 100); Canvas.ForceUpdateCanvases(); view.Refresh();
  67. Assert.That(boardRoot.rect.width * boardRoot.localScale.x, Is.LessThanOrEqualTo(180.001f));
  68. Assert.That(boardRoot.localScale.x, Is.EqualTo(boardRoot.localScale.y));
  69. Assert.That(scroll.vertical, Is.True);
  70. board.RemoveTile(0, 0); view.Refresh();
  71. Assert.That(view.VisibleTileCount, Is.EqualTo(board.RemainingTileCount));
  72. Assert.That(boardRoot.childCount, Is.EqualTo(board.RemainingTileCount));
  73. view.Clear(); Assert.That(boardRoot.childCount, Is.Zero);
  74. }
  75. finally { Object.DestroyImmediate(root); }
  76. }
  77. [Test]
  78. public void BootstrapGeneratesDefaultClassicBoardFromCore()
  79. {
  80. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
  81. Assert.That(prefab, Is.Not.Null);
  82. var root = new GameObject("BootstrapTest", typeof(RectTransform));
  83. try
  84. {
  85. var viewport = (RectTransform)root.transform; viewport.sizeDelta = new Vector2(418, 385);
  86. var content = (RectTransform)new GameObject("Content", typeof(RectTransform)).transform; content.SetParent(root.transform, false);
  87. var boardRoot = (RectTransform)new GameObject("Board", typeof(RectTransform)).transform; boardRoot.SetParent(content, false);
  88. var view = root.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(), viewport, content, boardRoot);
  89. var bootstrap = root.AddComponent<BoardDemoBootstrap>(); bootstrap.Configure(view); bootstrap.Initialize();
  90. Assert.That(view.Data.Columns, Is.EqualTo(4)); Assert.That(view.Data.Rows, Is.EqualTo(8));
  91. Assert.That(view.VisibleTileCount, Is.EqualTo(32));
  92. }
  93. finally { Object.DestroyImmediate(root); }
  94. }
  95. }
  96. }