MotionFeedbackTests.cs 7.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using NUnit.Framework;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.TestTools;
  10. using UnityEngine.UI;
  11. using Object=UnityEngine.Object;
  12. namespace HexaCrazy.Tests
  13. {
  14. public class MotionFeedbackTests
  15. {
  16. HexGameApp app;string folder;int source,target,drop;
  17. [UnitySetUp] public IEnumerator Setup()
  18. {
  19. folder=Path.Combine(Path.GetTempPath(),"HexaCrazyMotionTests",Guid.NewGuid().ToString("N"));
  20. app=new GameObject("MotionTestApp").AddComponent<HexGameApp>();app.UseReviewProfile(Path.Combine(folder,"save.json"));
  21. app.Profile.Data.reduceMotion=false;app.Profile.Data.tutorial=1;
  22. if(Object.FindAnyObjectByType<AudioListener>()==null)app.gameObject.AddComponent<AudioListener>();
  23. app.GetComponent<HexAudio>().Music=false;app.GetComponent<HexAudio>().Sound=false;
  24. yield return new WaitForSecondsRealtime(.95f);
  25. }
  26. [UnityTearDown] public IEnumerator Cleanup(){Object.Destroy(app.gameObject);yield return null;if(Directory.Exists(folder))Directory.Delete(folder,true);}
  27. void Fixture(int center=3,int mode=0)
  28. {
  29. app.StartLevel(mode,1,true);foreach(var c in app.Game.Cells){c.tiles.Clear();c.need=0;}
  30. source=Index(-1,0);target=Index(0,0);drop=Index(1,0);
  31. app.Game.Cells[source].tiles.AddRange(new[]{1,1,0,0,0});app.Game.Cells[target].tiles.AddRange(Enumerable.Repeat(0,center));
  32. app.Game.Hand[0]=new HandGroup(0,1);app.SelectHand(0);
  33. }
  34. int Index(int q,int r)=>app.Game.Cells.FindIndex(c=>c.q==q&&c.r==r);
  35. HexStackView View(int cell)=>app.Root.GetComponentsInChildren<HexStackView>().First(v=>v.CellIndex==cell);
  36. IEnumerator Until(Func<bool> condition,float timeout=5)
  37. {
  38. float end=Time.realtimeSinceStartup+timeout;while(!condition()&&Time.realtimeSinceStartup<end)yield return null;
  39. Assert.True(condition(),"Timed out waiting for animation state");
  40. }
  41. [UnityTest] public IEnumerator MergeRemovesEachSourceTileAtLaunchAndAddsItOnlyOnArrival()
  42. {
  43. Fixture();yield return null;var before=View(source);int identity=before.GetInstanceID();
  44. Assert.AreEqual(1,before.HiddenRunCount);CollectionAssert.AreEqual(new[]{1,1,0,0,0},before.Colors);
  45. var hidden=before.transform.Find("BuriedRun_0");Assert.True(hidden.gameObject.activeSelf);
  46. Assert.AreSame(HexUI.Sprite(HexUI.Symbols[1]),hidden.Find("OriginalSymbol").GetComponent<Image>().sprite);
  47. app.TapCell(drop);yield return Until(()=>app.AnimationStep==StepKind.Merge);
  48. var sourceCounts=new HashSet<int>();var targetCounts=new HashSet<int>();float deadline=Time.realtimeSinceStartup+4;
  49. while(app.Busy&&Time.realtimeSinceStartup<deadline)
  50. {
  51. sourceCounts.Add(View(source).TileCount);targetCounts.Add(View(target).TileCount);
  52. Assert.AreEqual(9,app.Root.GetComponentsInChildren<HexStackView>().Sum(v=>v.TileCount)+app.FlyingTileCount,"No tile may be drawn both on a stack and in flight, or disappear between launch and arrival");
  53. if(View(source).TileCount==4){Assert.AreEqual(3,View(target).TileCount);Assert.AreEqual(1,app.FlyingTileCount);}
  54. yield return null;
  55. }
  56. Assert.False(app.Busy);CollectionAssert.IsSubsetOf(new[]{4,3,2},sourceCounts);CollectionAssert.IsSubsetOf(new[]{3,4,5,6,7},targetCounts);
  57. Assert.AreEqual(identity,View(source).GetInstanceID(),"Merging must reuse the existing stack view");
  58. CollectionAssert.AreEqual(new[]{1,1},View(source).Colors);Assert.AreEqual(0,View(source).HiddenRunCount);Assert.AreEqual(0,app.FlyingTileCount);
  59. }
  60. [UnityTest] public IEnumerator ClearFliesToProgressBeforeScoreGainAndFloatThenEasesTheBar()
  61. {
  62. // Timed mode calls UpdateHud every frame; it must not expose the rules' ahead-of-animation score.
  63. Fixture(5,1);app.TapCell(drop);yield return Until(()=>app.AnimationStep==StepKind.Clear);
  64. var bar=app.Root.GetComponentInChildren<HexScoreBar>();Assert.AreEqual(74,app.Game.Score);Assert.AreEqual(2,bar.TargetScore);
  65. yield return new WaitForSecondsRealtime(.23f);Assert.Greater(app.FlyingTileCount,0);Assert.AreEqual(2,bar.TargetScore);
  66. yield return Until(()=>bar.TargetScore==74);Assert.AreEqual(0,app.FlyingTileCount);Assert.Less(bar.DisplayedScore,74);
  67. Assert.True(app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="Gain"&&t.text=="+72"));
  68. Assert.False(app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="ToastText"&&t.text.Contains("72")));
  69. yield return new WaitForSecondsRealtime(.6f);Assert.AreEqual(74,bar.DisplayedScore,.01f);
  70. Assert.AreEqual(74f/app.Game.Spec.target,app.Root.GetComponentsInChildren<HexProgress>().First(f=>f.name=="ScoreFill").Value,.001f);
  71. }
  72. [UnityTest] public IEnumerator BuriedClearPreservesOtherTypesAndChainShowsItsMultiplier()
  73. {
  74. Fixture();app.Game.Cells[source].tiles.Clear();app.Game.Cells[target].tiles.Clear();
  75. app.Game.Cells[target].tiles.AddRange(Enumerable.Repeat(0,8));app.Game.Cells[target].tiles.AddRange(new[]{2,2});
  76. app.Game.Cells[source].tiles.AddRange(Enumerable.Repeat(0,7));app.Game.Hand[0]=new HandGroup(1,1);
  77. app.TapCell(drop);yield return Until(()=>app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="Chain"&&t.text=="CHAIN ×2"));
  78. CollectionAssert.AreEqual(new[]{2,2},View(target).Colors);Assert.AreEqual(0,View(source).TileCount);Assert.AreEqual(182,app.Game.Score);
  79. Assert.True(app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="Gain"&&t.text=="+120"));
  80. yield return Until(()=>!app.Busy);Assert.AreEqual(0,app.FlyingTileCount);
  81. }
  82. [UnityTest] public IEnumerator PauseFreezesFlyingTilesAndLeavingCancelsAllPresentation()
  83. {
  84. Fixture();app.TapCell(drop);yield return Until(()=>app.AnimationStep==StepKind.Merge&&app.FlyingTileCount>0);
  85. app.OpenPanel("pause");int count=View(source).TileCount;var flight=app.Root.GetComponentsInChildren<RectTransform>().First(r=>r.name=="FlyingTile");Vector2 pos=flight.anchoredPosition;
  86. var bar=app.Root.GetComponentInChildren<HexScoreBar>();float score=bar.DisplayedScore;
  87. yield return new WaitForSecondsRealtime(.2f);Assert.AreEqual(count,View(source).TileCount);Assert.AreEqual(pos,flight.anchoredPosition);Assert.AreEqual(score,bar.DisplayedScore);
  88. app.ClosePanel();yield return new WaitForSecondsRealtime(.1f);Assert.AreNotEqual(pos,flight.anchoredPosition);
  89. app.LeaveGame();yield return new WaitForSecondsRealtime(.6f);Assert.AreEqual("Lobby",app.ScreenName);Assert.AreEqual(0,app.FlyingTileCount);Assert.IsNull(app.AnimationStep);
  90. Assert.False(app.Root.GetComponentsInChildren<RectTransform>().Any(r=>r.name=="FlyingTile"||r.name=="ScoreFloat"));
  91. }
  92. }
  93. }