ShopScreenTests.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Linq;
  3. using DevilLink2.Gameplay;
  4. using DevilLink2.Persistence;
  5. using DevilLink2.UI;
  6. using NUnit.Framework;
  7. using UnityEngine;
  8. namespace DevilLink2.Tests
  9. {
  10. public sealed partial class UIScreenTests
  11. {
  12. private void OpenShop() { ui.ShowLobby(); ui.Lobby.ShopButton.onClick.Invoke(); }
  13. [Test] public void ShopOpenClosePreservesPageAndInstances()
  14. {
  15. OpenShop(); ui.OpenShop(); Assert.That(ui.Shop.IsVisible,Is.True); Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Lobby));
  16. Assert.That(ui.Lobby.GetComponent<CanvasGroup>().interactable,Is.False);
  17. Assert.That(frame.GetComponentsInChildren<ShopScreen>(true).Length,Is.EqualTo(1));
  18. Assert.That(root.GetComponentsInChildren<Canvas>(true).Length,Is.EqualTo(1));
  19. ui.Shop.CloseButton.onClick.Invoke(); Assert.That(ui.Shop.IsVisible,Is.False); Assert.That(ui.Lobby.GetComponent<CanvasGroup>().interactable,Is.True);
  20. }
  21. [Test] public void CoinsEntryOpensShop() { ui.ShowLobby(); ui.Lobby.CoinButton.onClick.Invoke(); Assert.That(ui.Shop.IsVisible,Is.True); }
  22. [Test] public void ShopTabsSwitchAndReopenStartsWithItems()
  23. {
  24. OpenShop(); ui.Shop.RecordsButton.onClick.Invoke(); Assert.That(ui.Shop.CurrentTab,Is.EqualTo(ShopTab.CoinRecords));
  25. ui.Shop.ItemsButton.onClick.Invoke(); Assert.That(ui.Shop.CurrentTab,Is.EqualTo(ShopTab.Items));
  26. ui.Shop.SelectTab(ShopTab.CoinRecords); ui.CloseShop(); ui.OpenShop(); Assert.That(ui.Shop.CurrentTab,Is.EqualTo(ShopTab.Items));
  27. }
  28. [TestCase(ItemType.Hint,2000)] [TestCase(ItemType.Shuffle,5000)] [TestCase(ItemType.Time,3000)]
  29. public void ShopShowsActualPricesAndInventory(ItemType type,int price)
  30. {
  31. OpenShop(); Assert.That(ui.Shop.PriceLabels[(int)type].text,Is.EqualTo(price.ToString("N0",System.Globalization.CultureInfo.InvariantCulture)+" COINS"));
  32. Assert.That(ui.Shop.StockLabels[(int)type].text,Is.EqualTo("OWNED "+player.Economy.Inventory.GetCount(type)));
  33. }
  34. [TestCase(ItemType.Hint)] [TestCase(ItemType.Shuffle)] [TestCase(ItemType.Time)]
  35. public void ShopPurchaseUpdatesEconomyInventoryLedgerAndDisk(ItemType type)
  36. {
  37. OpenShop(); int stock=player.Economy.Inventory.GetCount(type); ui.Shop.BuyButtons[(int)type].onClick.Invoke();
  38. Assert.That(player.Economy.Coins,Is.EqualTo(9876-ShopService.Price(type))); Assert.That(player.Economy.Inventory.GetCount(type),Is.EqualTo(stock+1));
  39. Assert.That(ui.Shop.StockLabels[(int)type].text,Is.EqualTo("OWNED "+(stock+1)));
  40. Assert.That(player.Economy.Ledger.Entries.Single().Delta,Is.EqualTo(-ShopService.Price(type)));
  41. using(var restored=new SavedPlayer(new LocalSaveService(new FileSaveStorage(directory))))
  42. { Assert.That(restored.Economy.Coins,Is.EqualTo(player.Economy.Coins)); Assert.That(restored.Economy.Inventory.GetCount(type),Is.EqualTo(stock+1)); }
  43. }
  44. [Test] public void ShopInsufficientCoinsDoesNotCharge()
  45. {
  46. OpenShop(); Assert.That(ui.Shop.TryPurchase(ItemType.Shuffle,Guid.NewGuid()),Is.EqualTo(PurchaseStatus.Success));
  47. long before=player.Economy.Coins; int stock=player.Economy.Inventory.GetCount(ItemType.Shuffle);
  48. Assert.That(ui.Shop.TryPurchase(ItemType.Shuffle,Guid.NewGuid()),Is.EqualTo(PurchaseStatus.InsufficientCoins));
  49. Assert.That(player.Economy.Coins,Is.EqualTo(before)); Assert.That(player.Economy.Inventory.GetCount(ItemType.Shuffle),Is.EqualTo(stock));
  50. Assert.That(player.Economy.Ledger.Entries.Count,Is.EqualTo(1)); Assert.That(ui.Shop.FeedbackLabel.text,Does.Contain("NOT ENOUGH"));
  51. }
  52. [Test] public void ShopDuplicateOperationChargesOnceEvenAfterReopen()
  53. {
  54. OpenShop(); var id=Guid.NewGuid(); ui.Shop.TryPurchase(ItemType.Hint,id); ui.CloseShop(); ui.OpenShop();
  55. Assert.That(ui.Shop.TryPurchase(ItemType.Hint,id),Is.EqualTo(PurchaseStatus.DuplicateTransaction));
  56. Assert.That(player.Economy.Coins,Is.EqualTo(7876)); Assert.That(player.Economy.Ledger.Entries.Count,Is.EqualTo(1));
  57. }
  58. [Test] public void ShopRecordsUseLedgerOrderAndRetainedSummaries()
  59. {
  60. OpenShop(); player.Economy.TryCredit(Guid.NewGuid(),100,"income first"); ui.Shop.TryPurchase(ItemType.Hint,Guid.NewGuid());
  61. player.Economy.TryCredit(Guid.NewGuid(),300,"income newest"); ui.Shop.SelectTab(ShopTab.CoinRecords);
  62. string text=ui.Shop.RecordsText; Assert.That(text.IndexOf("income newest"),Is.LessThan(text.IndexOf("Shop: Hint")));
  63. Assert.That(text.IndexOf("Shop: Hint"),Is.LessThan(text.IndexOf("income first")));
  64. Assert.That(text,Does.Contain("UTC")); Assert.That(text,Does.Contain("INCOME +300")); Assert.That(text,Does.Contain("EXPENSE -2,000"));
  65. Assert.That(ui.Shop.SummaryLabel.text,Does.Contain("INCOME +400")); Assert.That(ui.Shop.SummaryLabel.text,Does.Contain("SPENT -2,000"));
  66. }
  67. [Test] public void ShopRetainsAtMost200RecordsAndDoesNotInventStarterGrant()
  68. {
  69. OpenShop(); for(int i=0;i<201;i++)player.Economy.TryCredit(Guid.NewGuid(),1,"record-"+i);
  70. Assert.That(player.Economy.Ledger.Entries.Count,Is.EqualTo(200)); Assert.That(ui.Shop.SummaryLabel.text,Does.Contain("INCOME +200"));
  71. Assert.That(ui.Shop.RecordsText,Does.Not.Contain("record-0\n")); Assert.That(ui.Shop.RecordsText,Does.Contain("record-200"));
  72. }
  73. [Test] public void ShopEmptyRecordsShowEmptyState()
  74. { OpenShop(); ui.Shop.SelectTab(ShopTab.CoinRecords); Assert.That(ui.Shop.RecordsText,Is.EqualTo("NO COIN RECORDS YET")); Assert.That(ui.Shop.SummaryLabel.text,Does.Contain("INCOME +0")); }
  75. [Test] public void ShopEscapesRecordMarkup()
  76. { OpenShop(); player.Economy.TryCredit(Guid.NewGuid(),1,"<size=99>test</size>"); Assert.That(ui.Shop.RecordsText,Does.Contain("&lt;size=99&gt;")); }
  77. [Test] public void ShopHiddenPurchaseAndUnderlyingModeInputAreRejected()
  78. { Assert.That(ui.Shop.TryPurchase(ItemType.Hint,Guid.NewGuid()),Is.Not.EqualTo(PurchaseStatus.Success)); OpenShop(); Assert.That(ui.SelectMode(GameMode.Classic),Is.False); Assert.That(bootstrap.Session,Is.Null); }
  79. [Test] public void ShopInGameDoesNotPauseOrReplaceBoard()
  80. {
  81. ui.ShowLobby(); ui.SelectMode(GameMode.Classic); var session=bootstrap.Session; var flow=bootstrap.Flow;
  82. ui.GameShopButton.onClick.Invoke(); Assert.That(ui.Shop.IsVisible,Is.True); Assert.That(flow.State,Is.EqualTo(GameFlowState.Running));
  83. double before=flow.Runtime.Timer.RemainingTime; flow.Runtime.Timer.Advance(.5);
  84. Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(before-.5)); ui.CloseShop();
  85. Assert.That(bootstrap.Session,Is.SameAs(session)); Assert.That(view.Data,Is.SameAs(session.Board)); Assert.That(ui.CurrentPage,Is.EqualTo(UIPage.Gameplay));
  86. }
  87. [Test] public void ShopClosesOnTimeoutAndCannotCoverResult()
  88. {
  89. ui.ShowLobby(); ui.SelectMode(GameMode.Classic); ui.OpenShop(); bootstrap.Flow.Runtime.Timer.Advance(1000);
  90. Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Defeat)); Assert.That(ui.Shop.IsVisible,Is.False); Assert.That(ui.Hud.Result.IsVisible,Is.True);
  91. }
  92. [Test] public void ShopRepeatedButtonDispatchInSameFrameDoesNotDoubleCharge()
  93. { OpenShop(); ui.Shop.BuyButtons[0].onClick.Invoke(); ui.Shop.BuyButtons[0].onClick.Invoke(); Assert.That(player.Economy.Coins,Is.EqualTo(7876)); }
  94. [Test] public void ShopRebuildDoesNotDuplicateGameplayEntry()
  95. {
  96. UnityEngine.Object.DestroyImmediate(ui.gameObject); ui=UIBuilder.Build(frame,bootstrap,view); ui.Initialize(player,bootstrap);
  97. Assert.That(ui.Gameplay.GetComponentsInChildren<UnityEngine.UI.Button>(true).Count(b=>b.name=="ShopEntry"),Is.EqualTo(1));
  98. }
  99. [Test] public void ShopRepeatedInitializationHasOnePurchaseListener()
  100. { OpenShop(); for(int i=0;i<5;i++)ui.Initialize(player,bootstrap); ui.Shop.BuyButtons[0].onClick.Invoke(); Assert.That(player.Economy.Coins,Is.EqualTo(7876)); }
  101. }
  102. }