| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using NUnit.Framework;
- using TMPro;
- using UnityEngine;
- using UnityEngine.TestTools;
- using UnityEngine.UI;
- using Object=UnityEngine.Object;
- namespace HexaCrazy.Tests
- {
- public class MotionFeedbackTests
- {
- HexGameApp app;string folder;int source,target,drop;
- [UnitySetUp] public IEnumerator Setup()
- {
- folder=Path.Combine(Path.GetTempPath(),"HexaCrazyMotionTests",Guid.NewGuid().ToString("N"));
- app=new GameObject("MotionTestApp").AddComponent<HexGameApp>();app.UseReviewProfile(Path.Combine(folder,"save.json"));
- app.Profile.Data.reduceMotion=false;app.Profile.Data.tutorial=1;
- if(Object.FindAnyObjectByType<AudioListener>()==null)app.gameObject.AddComponent<AudioListener>();
- app.GetComponent<HexAudio>().Music=false;app.GetComponent<HexAudio>().Sound=false;
- yield return new WaitForSecondsRealtime(.95f);
- }
- [UnityTearDown] public IEnumerator Cleanup(){Object.Destroy(app.gameObject);yield return null;if(Directory.Exists(folder))Directory.Delete(folder,true);}
- void Fixture(int center=3,int mode=0)
- {
- app.StartLevel(mode,1,true);foreach(var c in app.Game.Cells){c.tiles.Clear();c.need=0;}
- source=Index(-1,0);target=Index(0,0);drop=Index(1,0);
- app.Game.Cells[source].tiles.AddRange(new[]{1,1,0,0,0});app.Game.Cells[target].tiles.AddRange(Enumerable.Repeat(0,center));
- app.Game.Hand[0]=new HandGroup(0,1);app.SelectHand(0);
- }
- int Index(int q,int r)=>app.Game.Cells.FindIndex(c=>c.q==q&&c.r==r);
- HexStackView View(int cell)=>app.Root.GetComponentsInChildren<HexStackView>().First(v=>v.CellIndex==cell);
- IEnumerator Until(Func<bool> condition,float timeout=5)
- {
- float end=Time.realtimeSinceStartup+timeout;while(!condition()&&Time.realtimeSinceStartup<end)yield return null;
- Assert.True(condition(),"Timed out waiting for animation state");
- }
- [UnityTest] public IEnumerator MergeRemovesEachSourceTileAtLaunchAndAddsItOnlyOnArrival()
- {
- Fixture();yield return null;var before=View(source);int identity=before.GetInstanceID();
- Assert.AreEqual(1,before.HiddenRunCount);CollectionAssert.AreEqual(new[]{1,1,0,0,0},before.Colors);
- var hidden=before.transform.Find("BuriedRun_0");Assert.True(hidden.gameObject.activeSelf);
- Assert.AreSame(HexUI.Sprite(HexUI.Symbols[1]),hidden.Find("OriginalSymbol").GetComponent<Image>().sprite);
- app.TapCell(drop);yield return Until(()=>app.AnimationStep==StepKind.Merge);
- var sourceCounts=new HashSet<int>();var targetCounts=new HashSet<int>();float deadline=Time.realtimeSinceStartup+4;
- while(app.Busy&&Time.realtimeSinceStartup<deadline)
- {
- sourceCounts.Add(View(source).TileCount);targetCounts.Add(View(target).TileCount);
- 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");
- if(View(source).TileCount==4){Assert.AreEqual(3,View(target).TileCount);Assert.AreEqual(1,app.FlyingTileCount);}
- yield return null;
- }
- Assert.False(app.Busy);CollectionAssert.IsSubsetOf(new[]{4,3,2},sourceCounts);CollectionAssert.IsSubsetOf(new[]{3,4,5,6,7},targetCounts);
- Assert.AreEqual(identity,View(source).GetInstanceID(),"Merging must reuse the existing stack view");
- CollectionAssert.AreEqual(new[]{1,1},View(source).Colors);Assert.AreEqual(0,View(source).HiddenRunCount);Assert.AreEqual(0,app.FlyingTileCount);
- }
- [UnityTest] public IEnumerator ClearFliesToProgressBeforeScoreGainAndFloatThenEasesTheBar()
- {
- // Timed mode calls UpdateHud every frame; it must not expose the rules' ahead-of-animation score.
- Fixture(5,1);app.TapCell(drop);yield return Until(()=>app.AnimationStep==StepKind.Clear);
- var bar=app.Root.GetComponentInChildren<HexScoreBar>();Assert.AreEqual(74,app.Game.Score);Assert.AreEqual(2,bar.TargetScore);
- yield return new WaitForSecondsRealtime(.23f);Assert.Greater(app.FlyingTileCount,0);Assert.AreEqual(2,bar.TargetScore);
- yield return Until(()=>bar.TargetScore==74);Assert.AreEqual(0,app.FlyingTileCount);Assert.Less(bar.DisplayedScore,74);
- Assert.True(app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="Gain"&&t.text=="+72"));
- Assert.False(app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="ToastText"&&t.text.Contains("72")));
- yield return new WaitForSecondsRealtime(.6f);Assert.AreEqual(74,bar.DisplayedScore,.01f);
- Assert.AreEqual(74f/app.Game.Spec.target,app.Root.GetComponentsInChildren<HexProgress>().First(f=>f.name=="ScoreFill").Value,.001f);
- }
- [UnityTest] public IEnumerator BuriedClearPreservesOtherTypesAndChainShowsItsMultiplier()
- {
- Fixture();app.Game.Cells[source].tiles.Clear();app.Game.Cells[target].tiles.Clear();
- app.Game.Cells[target].tiles.AddRange(Enumerable.Repeat(0,8));app.Game.Cells[target].tiles.AddRange(new[]{2,2});
- app.Game.Cells[source].tiles.AddRange(Enumerable.Repeat(0,7));app.Game.Hand[0]=new HandGroup(1,1);
- app.TapCell(drop);yield return Until(()=>app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="Chain"&&t.text=="CHAIN ×2"));
- CollectionAssert.AreEqual(new[]{2,2},View(target).Colors);Assert.AreEqual(0,View(source).TileCount);Assert.AreEqual(182,app.Game.Score);
- Assert.True(app.Root.GetComponentsInChildren<TextMeshProUGUI>().Any(t=>t.name=="Gain"&&t.text=="+120"));
- yield return Until(()=>!app.Busy);Assert.AreEqual(0,app.FlyingTileCount);
- }
- [UnityTest] public IEnumerator PauseFreezesFlyingTilesAndLeavingCancelsAllPresentation()
- {
- Fixture();app.TapCell(drop);yield return Until(()=>app.AnimationStep==StepKind.Merge&&app.FlyingTileCount>0);
- app.OpenPanel("pause");int count=View(source).TileCount;var flight=app.Root.GetComponentsInChildren<RectTransform>().First(r=>r.name=="FlyingTile");Vector2 pos=flight.anchoredPosition;
- var bar=app.Root.GetComponentInChildren<HexScoreBar>();float score=bar.DisplayedScore;
- yield return new WaitForSecondsRealtime(.2f);Assert.AreEqual(count,View(source).TileCount);Assert.AreEqual(pos,flight.anchoredPosition);Assert.AreEqual(score,bar.DisplayedScore);
- app.ClosePanel();yield return new WaitForSecondsRealtime(.1f);Assert.AreNotEqual(pos,flight.anchoredPosition);
- app.LeaveGame();yield return new WaitForSecondsRealtime(.6f);Assert.AreEqual("Lobby",app.ScreenName);Assert.AreEqual(0,app.FlyingTileCount);Assert.IsNull(app.AnimationStep);
- Assert.False(app.Root.GetComponentsInChildren<RectTransform>().Any(r=>r.name=="FlyingTile"||r.name=="ScoreFloat"));
- }
- }
- }
|