using System; using System.Linq; using System.Reflection; using DevilLink2.Gameplay; using DevilLink2.UI; using NUnit.Framework; using UnityEditor; using UnityEngine; using UnityEngine.UI; using Object = UnityEngine.Object; namespace DevilLink2.Tests { public sealed partial class UIScreenTests { private const string ArtRoot = "Assets/DevilLink2/Art/UI/Lobby/UI11A/"; [TestCase("Background", "OpaqueSceneCrops/Backgrounds/Lobby_Castle_Opaque.png")] [TestCase("Logo", "ReferenceNeedsRedraw/Branding/Logo_DevilLink2_Reference.png")] [TestCase("Panel", "ReferenceNeedsRedraw/Panels/Panel_OrnatePurple_Reference.png")] [TestCase("Coins", "ReferenceNeedsRedraw/ResourceBars/ResourceBar_Coin_2000_Reference.png")] [TestCase("Tickets", "ReferenceNeedsRedraw/ResourceBars/ResourceBar_Ticket_0_Reference.png")] [TestCase("Classic", "ReferenceNeedsRedraw/ModeCards/Mode_Classic_Reference.png")] [TestCase("TimeAttack", "ReferenceNeedsRedraw/ModeCards/Mode_TimeAttack_Reference.png")] [TestCase("Zen", "ReferenceNeedsRedraw/ModeCards/Mode_Zen_Reference.png")] [TestCase("Daily", "ReferenceNeedsRedraw/Icons/Icon_Gift_Reference.png")] [TestCase("Wheel", "ReferenceNeedsRedraw/Navigation/Nav_Wheel_Reference.png")] [TestCase("Shop", "ReferenceNeedsRedraw/Navigation/Nav_Shop_Reference.png")] [TestCase("Profile", "ReferenceNeedsRedraw/Navigation/Nav_Profile_Reference.png")] [TestCase("Settings", "ReferenceNeedsRedraw/Navigation/Nav_Settings_Reference.png")] public void LobbyCatalogReferencesExactApprovedSprites(string field, string path) { var catalog = Resources.Load("DevilLink2LobbyArt"); Assert.That(catalog, Is.Not.Null); var sprite = (Sprite)typeof(LobbyArtCatalog).GetField(field).GetValue(catalog); Assert.That(sprite, Is.Not.Null); Assert.That(AssetDatabase.GetAssetPath(sprite), Is.EqualTo(ArtRoot + path)); var importer = (TextureImporter)AssetImporter.GetAtPath(ArtRoot + path); Assert.That(importer.textureType, Is.EqualTo(TextureImporterType.Sprite)); Assert.That(importer.spriteImportMode, Is.EqualTo(SpriteImportMode.Single)); Assert.That(importer.alphaIsTransparency && !importer.mipmapEnabled, Is.True); } [Test] public void MissingSpriteCreatesNoImageAndKeepsExistingControls() { int count = ui.Lobby.GetComponentsInChildren(true).Length; Assert.That(LobbyArtView.Picture("Missing", ui.Lobby.transform, null, 0, 0, 30, 30), Is.Null); Assert.That(ui.Lobby.GetComponentsInChildren(true).Length, Is.EqualTo(count)); Assert.That(ui.Lobby.CoinButton, Is.Not.Null); } private LobbyScreen FallbackLobby(LobbyArtCatalog catalog) { var method = typeof(UIBuilder).GetMethod("Lobby", BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(RectTransform), typeof(LobbyArtCatalog) }, null); return (LobbyScreen)method.Invoke(null, new object[] { frame, catalog }); } [Test] public void MissingWholeCatalogPreservesOriginalReadableLobby() { var fallback = FallbackLobby(null); fallback.Refresh(player); Assert.That(fallback.CoinsLabel.enabled, Is.True); Assert.That(fallback.CoinsLabel.text, Is.EqualTo("9,876")); Assert.That(fallback.Cards.All(c => c.Button && c.ProgressLabel), Is.True); } [Test] public void MissingModeArtHasReadableCompactFallbackAndSameModeEvents() { var empty = ScriptableObject.CreateInstance(); try { var fallback = FallbackLobby(empty); fallback.SetVisible(true); fallback.Refresh(player); int calls = 0; fallback.ModeRequested += _ => calls++; foreach (var card in fallback.Cards) { Assert.That(card.transform.Find("FallbackModeName").GetComponent().text, Is.EqualTo(UIManager.ModeName(card.Mode))); card.Button.onClick.Invoke(); } Assert.That(calls, Is.EqualTo(3)); } finally { Object.DestroyImmediate(empty); } } [Test] public void ReinstallAndReopenDoNotDuplicateLobbyArtworkOrControls() { var view = ui.Lobby.GetComponent(); int count = ui.Lobby.transform.childCount; for(int i=0;i<5;i++) { Assert.That(LobbyArtView.Install(ui.Lobby, null), Is.SameAs(view)); ui.ShowLobby(); ui.Lobby.SetVisible(false); ui.ShowLobby(); } Assert.That(ui.Lobby.transform.childCount, Is.EqualTo(count)); Assert.That(ui.Lobby.GetComponents().Length, Is.EqualTo(1)); } [Test] public void CoinTMPReadsActualEconomyAndUpdatesImmediately() { ui.ShowLobby(); var art = ui.Lobby.GetComponent(); Assert.That(art.Coins.text, Is.EqualTo("9,876")); player.Economy.TryCredit(Guid.NewGuid(), 23, "visual test"); Assert.That(art.Coins.text, Is.EqualTo("9,899")); Assert.That(ui.Lobby.CoinsLabel.enabled, Is.False); } [Test] public void TicketTMPReadsActualEconomyAndPlusOpensExistingWheel() { WheelSetup(1,7); ui.CloseWheel(); var art = ui.Lobby.GetComponent(); Assert.That(art.Tickets.text, Is.EqualTo("7")); ui.Lobby.TicketButton.onClick.Invoke(); Assert.That(ui.Wheel.IsVisible, Is.True); ui.Wheel.Spin(Guid.NewGuid()); FinishWheel(); ui.Wheel.Spin(Guid.NewGuid()); Assert.That(player.Economy.SpinTickets, Is.EqualTo(6)); Assert.That(art.Tickets.text, Is.EqualTo("6")); } [TestCase(0,2)] [TestCase(1,3)] [TestCase(2,19)] public void ArtModeCardsKeepIndependentDynamicProgress(int index,int cleared) { ui.ShowLobby(); Assert.That(ui.Lobby.Cards[index].ProgressLabel.text, Does.Contain("CLEARED " + cleared + "/20")); Assert.That(ui.Lobby.Cards[index].transform.Find("ModeArtwork"), Is.Not.Null); } [TestCase(0)] [TestCase(1)] [TestCase(2)] [TestCase(3)] [TestCase(4)] public void ArtNavigationUsesExistingCachedFunctionalPanel(int index) { ui.ShowLobby(); var buttons = new[] { ui.Lobby.DailyButton, ui.Lobby.WheelButton, ui.Lobby.ShopButton, ui.Lobby.ProfileButton, ui.Lobby.SettingsButton }; buttons[index].onClick.Invoke(); var panels = new UIScreen[] { ui.Daily, ui.Wheel, ui.Shop, ui.Profile, ui.Settings }; Assert.That(panels[index].IsVisible, Is.True); Assert.That(panels.Count(p=>p.IsVisible), Is.EqualTo(1)); Assert.That(buttons[index].transform.Find("NavigationArtwork"), Is.Not.Null); Assert.That(buttons[index].transform.Find("Label").gameObject.activeSelf, Is.True); } [TestCase(430,932)] [TestCase(320,568)] [TestCase(390,844)] [TestCase(768,1024)] public void LobbyDesignFitsDifferentPortraitViewportsWithoutStretch(float width,float height) { float scale = ScreenLayout.FitScale(width,height); Assert.That(430*scale, Is.LessThanOrEqualTo(width+.01f)); Assert.That(932*scale, Is.LessThanOrEqualTo(height+.01f)); foreach(var card in ui.Lobby.Cards) { var rect=(RectTransform)card.transform; Assert.That(rect.sizeDelta, Is.EqualTo(new Vector2(130,286))); Assert.That(rect.anchoredPosition.x+rect.sizeDelta.x, Is.LessThanOrEqualTo(430)); Assert.That(-rect.anchoredPosition.y+rect.sizeDelta.y, Is.LessThan(836)); } } [Test] public void DecorativeSpritesPreserveAspectAlphaAndNeverStealInput() { var images=ui.Lobby.GetComponentsInChildren(true).Where(x=>x.sprite).ToArray(); Assert.That(images.Length, Is.GreaterThanOrEqualTo(13)); foreach(var image in images) { Assert.That(image.preserveAspect, Is.True); Assert.That(image.raycastTarget, Is.False); Assert.That(image.color, Is.EqualTo(Color.white)); } } [Test] public void BackgroundUsesCoverCropInsideDedicatedViewport() { var background=ui.Lobby.GetComponent().Background; var fitter=background.GetComponent(); Assert.That(fitter.aspectMode, Is.EqualTo(AspectRatioFitter.AspectMode.EnvelopeParent)); var viewport = (RectTransform)background.transform.parent; Canvas.ForceUpdateCanvases(); float spriteRatio = background.sprite.rect.width / background.sprite.rect.height; Assert.That(viewport.rect.size, Is.EqualTo(new Vector2(430,932))); Assert.That(fitter.aspectRatio, Is.EqualTo(spriteRatio).Within(.001f)); float scale = Mathf.Max(viewport.rect.width / background.sprite.rect.width, viewport.rect.height / background.sprite.rect.height); var expected = background.sprite.rect.size * scale; Assert.That(background.rectTransform.rect.width, Is.EqualTo(expected.x).Within(.01f)); Assert.That(background.rectTransform.rect.height, Is.EqualTo(expected.y).Within(.01f)); Assert.That(background.rectTransform.rect.width, Is.GreaterThanOrEqualTo(viewport.rect.width-.01f)); Assert.That(background.rectTransform.rect.height, Is.GreaterThanOrEqualTo(viewport.rect.height-.01f)); Assert.That(background.rectTransform.rect.width/background.rectTransform.rect.height, Is.EqualTo(spriteRatio).Within(.001f)); Assert.That(background.rectTransform.anchoredPosition, Is.EqualTo(Vector2.zero)); Assert.That(background.transform.parent.GetComponent(), Is.Not.Null); Assert.That(ui.Lobby.GetComponent(), Is.Null); } } }