LobbyArtTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using DevilLink2.Gameplay;
  5. using DevilLink2.UI;
  6. using NUnit.Framework;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using Object = UnityEngine.Object;
  11. namespace DevilLink2.Tests
  12. {
  13. public sealed partial class UIScreenTests
  14. {
  15. private const string ArtRoot = "Assets/DevilLink2/Art/UI/Lobby/UI11A/";
  16. [TestCase("Background", "OpaqueSceneCrops/Backgrounds/Lobby_Castle_Opaque.png")]
  17. [TestCase("Logo", "ReferenceNeedsRedraw/Branding/Logo_DevilLink2_Reference.png")]
  18. [TestCase("Panel", "ReferenceNeedsRedraw/Panels/Panel_OrnatePurple_Reference.png")]
  19. [TestCase("Coins", "ReferenceNeedsRedraw/ResourceBars/ResourceBar_Coin_2000_Reference.png")]
  20. [TestCase("Tickets", "ReferenceNeedsRedraw/ResourceBars/ResourceBar_Ticket_0_Reference.png")]
  21. [TestCase("Classic", "ReferenceNeedsRedraw/ModeCards/Mode_Classic_Reference.png")]
  22. [TestCase("TimeAttack", "ReferenceNeedsRedraw/ModeCards/Mode_TimeAttack_Reference.png")]
  23. [TestCase("Zen", "ReferenceNeedsRedraw/ModeCards/Mode_Zen_Reference.png")]
  24. [TestCase("Daily", "ReferenceNeedsRedraw/Icons/Icon_Gift_Reference.png")]
  25. [TestCase("Wheel", "ReferenceNeedsRedraw/Navigation/Nav_Wheel_Reference.png")]
  26. [TestCase("Shop", "ReferenceNeedsRedraw/Navigation/Nav_Shop_Reference.png")]
  27. [TestCase("Profile", "ReferenceNeedsRedraw/Navigation/Nav_Profile_Reference.png")]
  28. [TestCase("Settings", "ReferenceNeedsRedraw/Navigation/Nav_Settings_Reference.png")]
  29. public void LobbyCatalogReferencesExactApprovedSprites(string field, string path)
  30. {
  31. var catalog = Resources.Load<LobbyArtCatalog>("DevilLink2LobbyArt");
  32. Assert.That(catalog, Is.Not.Null);
  33. var sprite = (Sprite)typeof(LobbyArtCatalog).GetField(field).GetValue(catalog);
  34. Assert.That(sprite, Is.Not.Null);
  35. Assert.That(AssetDatabase.GetAssetPath(sprite), Is.EqualTo(ArtRoot + path));
  36. var importer = (TextureImporter)AssetImporter.GetAtPath(ArtRoot + path);
  37. Assert.That(importer.textureType, Is.EqualTo(TextureImporterType.Sprite));
  38. Assert.That(importer.spriteImportMode, Is.EqualTo(SpriteImportMode.Single));
  39. Assert.That(importer.alphaIsTransparency && !importer.mipmapEnabled, Is.True);
  40. }
  41. [Test] public void MissingSpriteCreatesNoImageAndKeepsExistingControls()
  42. {
  43. int count = ui.Lobby.GetComponentsInChildren<Image>(true).Length;
  44. Assert.That(LobbyArtView.Picture("Missing", ui.Lobby.transform, null, 0, 0, 30, 30), Is.Null);
  45. Assert.That(ui.Lobby.GetComponentsInChildren<Image>(true).Length, Is.EqualTo(count));
  46. Assert.That(ui.Lobby.CoinButton, Is.Not.Null);
  47. }
  48. private LobbyScreen FallbackLobby(LobbyArtCatalog catalog)
  49. {
  50. var method = typeof(UIBuilder).GetMethod("Lobby", BindingFlags.NonPublic | BindingFlags.Static,
  51. null, new[] { typeof(RectTransform), typeof(LobbyArtCatalog) }, null);
  52. return (LobbyScreen)method.Invoke(null, new object[] { frame, catalog });
  53. }
  54. [Test] public void MissingWholeCatalogPreservesOriginalReadableLobby()
  55. {
  56. var fallback = FallbackLobby(null); fallback.Refresh(player);
  57. Assert.That(fallback.CoinsLabel.enabled, Is.True);
  58. Assert.That(fallback.CoinsLabel.text, Is.EqualTo("9,876"));
  59. Assert.That(fallback.Cards.All(c => c.Button && c.ProgressLabel), Is.True);
  60. }
  61. [Test] public void MissingModeArtHasReadableCompactFallbackAndSameModeEvents()
  62. {
  63. var empty = ScriptableObject.CreateInstance<LobbyArtCatalog>();
  64. try
  65. {
  66. var fallback = FallbackLobby(empty); fallback.SetVisible(true); fallback.Refresh(player);
  67. int calls = 0; fallback.ModeRequested += _ => calls++;
  68. foreach (var card in fallback.Cards)
  69. {
  70. Assert.That(card.transform.Find("FallbackModeName").GetComponent<Text>().text,
  71. Is.EqualTo(UIManager.ModeName(card.Mode)));
  72. card.Button.onClick.Invoke();
  73. }
  74. Assert.That(calls, Is.EqualTo(3));
  75. }
  76. finally { Object.DestroyImmediate(empty); }
  77. }
  78. [Test] public void ReinstallAndReopenDoNotDuplicateLobbyArtworkOrControls()
  79. {
  80. var view = ui.Lobby.GetComponent<LobbyArtView>(); int count = ui.Lobby.transform.childCount;
  81. for(int i=0;i<5;i++)
  82. {
  83. Assert.That(LobbyArtView.Install(ui.Lobby, null), Is.SameAs(view));
  84. ui.ShowLobby(); ui.Lobby.SetVisible(false); ui.ShowLobby();
  85. }
  86. Assert.That(ui.Lobby.transform.childCount, Is.EqualTo(count));
  87. Assert.That(ui.Lobby.GetComponents<LobbyArtView>().Length, Is.EqualTo(1));
  88. }
  89. [Test] public void CoinTMPReadsActualEconomyAndUpdatesImmediately()
  90. {
  91. ui.ShowLobby(); var art = ui.Lobby.GetComponent<LobbyArtView>();
  92. Assert.That(art.Coins.text, Is.EqualTo("9,876"));
  93. player.Economy.TryCredit(Guid.NewGuid(), 23, "visual test");
  94. Assert.That(art.Coins.text, Is.EqualTo("9,899"));
  95. Assert.That(ui.Lobby.CoinsLabel.enabled, Is.False);
  96. }
  97. [Test] public void TicketTMPReadsActualEconomyAndPlusOpensExistingWheel()
  98. {
  99. WheelSetup(1,7); ui.CloseWheel(); var art = ui.Lobby.GetComponent<LobbyArtView>();
  100. Assert.That(art.Tickets.text, Is.EqualTo("7"));
  101. ui.Lobby.TicketButton.onClick.Invoke(); Assert.That(ui.Wheel.IsVisible, Is.True);
  102. ui.Wheel.Spin(Guid.NewGuid()); FinishWheel(); ui.Wheel.Spin(Guid.NewGuid());
  103. Assert.That(player.Economy.SpinTickets, Is.EqualTo(6));
  104. Assert.That(art.Tickets.text, Is.EqualTo("6"));
  105. }
  106. [TestCase(0,2)] [TestCase(1,3)] [TestCase(2,19)]
  107. public void ArtModeCardsKeepIndependentDynamicProgress(int index,int cleared)
  108. {
  109. ui.ShowLobby(); Assert.That(ui.Lobby.Cards[index].ProgressLabel.text, Does.Contain("CLEARED " + cleared + "/20"));
  110. Assert.That(ui.Lobby.Cards[index].transform.Find("ModeArtwork"), Is.Not.Null);
  111. }
  112. [TestCase(0)] [TestCase(1)] [TestCase(2)] [TestCase(3)] [TestCase(4)]
  113. public void ArtNavigationUsesExistingCachedFunctionalPanel(int index)
  114. {
  115. ui.ShowLobby(); var buttons = new[] { ui.Lobby.DailyButton, ui.Lobby.WheelButton, ui.Lobby.ShopButton, ui.Lobby.ProfileButton, ui.Lobby.SettingsButton };
  116. buttons[index].onClick.Invoke();
  117. var panels = new UIScreen[] { ui.Daily, ui.Wheel, ui.Shop, ui.Profile, ui.Settings };
  118. Assert.That(panels[index].IsVisible, Is.True);
  119. Assert.That(panels.Count(p=>p.IsVisible), Is.EqualTo(1));
  120. Assert.That(buttons[index].transform.Find("NavigationArtwork"), Is.Not.Null);
  121. Assert.That(buttons[index].transform.Find("Label").gameObject.activeSelf, Is.True);
  122. }
  123. [TestCase(430,932)] [TestCase(320,568)] [TestCase(390,844)] [TestCase(768,1024)]
  124. public void LobbyDesignFitsDifferentPortraitViewportsWithoutStretch(float width,float height)
  125. {
  126. float scale = ScreenLayout.FitScale(width,height);
  127. Assert.That(430*scale, Is.LessThanOrEqualTo(width+.01f));
  128. Assert.That(932*scale, Is.LessThanOrEqualTo(height+.01f));
  129. foreach(var card in ui.Lobby.Cards)
  130. {
  131. var rect=(RectTransform)card.transform;
  132. Assert.That(rect.sizeDelta, Is.EqualTo(new Vector2(130,286)));
  133. Assert.That(rect.anchoredPosition.x+rect.sizeDelta.x, Is.LessThanOrEqualTo(430));
  134. Assert.That(-rect.anchoredPosition.y+rect.sizeDelta.y, Is.LessThan(836));
  135. }
  136. }
  137. [Test] public void DecorativeSpritesPreserveAspectAlphaAndNeverStealInput()
  138. {
  139. var images=ui.Lobby.GetComponentsInChildren<Image>(true).Where(x=>x.sprite).ToArray();
  140. Assert.That(images.Length, Is.GreaterThanOrEqualTo(13));
  141. foreach(var image in images)
  142. { Assert.That(image.preserveAspect, Is.True); Assert.That(image.raycastTarget, Is.False); Assert.That(image.color, Is.EqualTo(Color.white)); }
  143. }
  144. [Test] public void BackgroundUsesCoverCropInsideDedicatedViewport()
  145. {
  146. var background=ui.Lobby.GetComponent<LobbyArtView>().Background;
  147. var fitter=background.GetComponent<AspectRatioFitter>();
  148. Assert.That(fitter.aspectMode, Is.EqualTo(AspectRatioFitter.AspectMode.EnvelopeParent));
  149. var viewport = (RectTransform)background.transform.parent;
  150. Canvas.ForceUpdateCanvases();
  151. float spriteRatio = background.sprite.rect.width / background.sprite.rect.height;
  152. Assert.That(viewport.rect.size, Is.EqualTo(new Vector2(430,932)));
  153. Assert.That(fitter.aspectRatio, Is.EqualTo(spriteRatio).Within(.001f));
  154. float scale = Mathf.Max(viewport.rect.width / background.sprite.rect.width,
  155. viewport.rect.height / background.sprite.rect.height);
  156. var expected = background.sprite.rect.size * scale;
  157. Assert.That(background.rectTransform.rect.width, Is.EqualTo(expected.x).Within(.01f));
  158. Assert.That(background.rectTransform.rect.height, Is.EqualTo(expected.y).Within(.01f));
  159. Assert.That(background.rectTransform.rect.width, Is.GreaterThanOrEqualTo(viewport.rect.width-.01f));
  160. Assert.That(background.rectTransform.rect.height, Is.GreaterThanOrEqualTo(viewport.rect.height-.01f));
  161. Assert.That(background.rectTransform.rect.width/background.rectTransform.rect.height,
  162. Is.EqualTo(spriteRatio).Within(.001f));
  163. Assert.That(background.rectTransform.anchoredPosition, Is.EqualTo(Vector2.zero));
  164. Assert.That(background.transform.parent.GetComponent<RectMask2D>(), Is.Not.Null);
  165. Assert.That(ui.Lobby.GetComponent<RectMask2D>(), Is.Null);
  166. }
  167. }
  168. }