| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using System;
- using System.Collections;
- using System.IO;
- using System.Linq;
- using NUnit.Framework;
- using TMPro;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.TestTools;
- using UnityEngine.UI;
- using Object=UnityEngine.Object;
- namespace HexaCrazy.Tests
- {
- public class GameFlowTests
- {
- HexGameApp app;string directory;
- [UnitySetUp] public IEnumerator Setup()
- {
- directory=Path.Combine(Path.GetTempPath(),"HexaCrazyPlayTests",Guid.NewGuid().ToString("N"));
- app=new GameObject("TestGameApp").AddComponent<HexGameApp>();app.UseReviewProfile(Path.Combine(directory,"save.json"));app.Profile.Data.reduceMotion=true;
- 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(directory))Directory.Delete(directory,true);}
- Button Button(string name)=>app.Root.GetComponentsInChildren<Button>().First(b=>b.name==name);
- [UnityTest] public IEnumerator PlatformOpenStopsMusicAndPersistsTheSetting()
- {
- var audio = app.GetComponent<HexAudio>();
- var bgm = app.GetComponents<AudioSource>().Single(source => source.loop);
- AppRuntimesEvents.RaisePlatformOpen();
- Button("GameStart").onClick.Invoke();
- Assert.IsFalse(audio.Music);
- Assert.IsFalse(new LocalProfile(app.Profile.Path).Data.music);
- app.OpenPanel("settings");
- Button("Toggle_0").onClick.Invoke();
- yield return null;
- yield return null;
- Assert.IsTrue(audio.Music);
- Assert.Greater(bgm.volume, 0f);
- bool sound = audio.Sound;
- AppRuntimesEvents.RaisePlatformOpen();
- AppRuntimesEvents.RaisePlatformOpen();
- Assert.IsFalse(audio.Music);
- Assert.IsFalse(bgm.isPlaying);
- Assert.AreEqual(0f, bgm.volume);
- Assert.AreEqual("OFF", Button("Toggle_0").transform.Find("Switch/Value").GetComponent<TMP_Text>().text);
- var saved = new LocalProfile(app.Profile.Path).Data;
- Assert.IsFalse(saved.music);
- Assert.AreEqual(sound, audio.Sound);
- Assert.AreEqual(sound, saved.sound);
- }
- [UnityTest] public IEnumerator WheelTurnsUnderFixedPointerAndStopsOnAwardedPrize()
- {
- app.Profile.Data.reduceMotion = false;
- app.Profile.Data.tickets = 1;
- app.ShowLobby();
- app.OpenPanel("draw");
- Button("Spin").onClick.Invoke();
- var spin = Button("Spin");
- var frame = spin.transform.parent;
- var wheel = frame.Find("WheelAperture/PrizeWheel");
- var rim = frame.Find("WheelRim");
- var rimStart = rim.rotation;
- var pointer = frame.Find("PrizeIndicator");
- var start = wheel.localRotation;
- var pointerStart = pointer.rotation;
- var hubStart = spin.transform.rotation;
- Assert.False(spin.interactable);
- spin.onClick.Invoke();
- Assert.AreEqual(1, app.Profile.Data.tickets, "Repeated input during a free spin must not spend a ticket.");
- yield return new WaitForSecondsRealtime(.35f);
- Assert.Greater(Quaternion.Angle(start, wheel.localRotation), 1f);
- Assert.Less(Quaternion.Angle(rimStart, rim.rotation), .01f);
- var aperture = wheel.GetComponentInParent<Mask>();
- Assert.NotNull(aperture, "The spinning artwork must be clipped inside the stationary rim.");
- Assert.False(rim.IsChildOf(aperture.transform));
- Assert.Less(Quaternion.Angle(pointerStart, pointer.rotation), .01f);
- Assert.Less(Quaternion.Angle(hubStart, spin.transform.rotation), .01f);
- Assert.AreEqual(1f, spin.image.canvasRenderer.GetColor().a, .001f);
- float deadline = Time.realtimeSinceStartup + 5;
- yield return new WaitUntil(() => Button("Spin").interactable || Time.realtimeSinceStartup >= deadline);
- Assert.True(Button("Spin").interactable);
- wheel = Button("Spin").transform.parent.Find("WheelAperture/PrizeWheel");
- var winner = Enumerable.Range(0, wheel.childCount).Select(i => wheel.GetChild(i))
- .OrderByDescending(wedge => wedge.position.y).First();
- Assert.AreEqual(wheel.position.x, winner.position.x, .1f, "The winning sector should be centered at twelve o'clock.");
- var award = app.Root.GetComponentsInChildren<TextMeshProUGUI>().First(t => t.name == "ToastText");
- Assert.AreEqual("WON " + winner.GetComponentInChildren<TextMeshProUGUI>().text, award.text);
- app.Profile.Data.reduceMotion = true;
- Button("Spin").onClick.Invoke();
- yield return new WaitForSecondsRealtime(.2f);
- spin = Button("Spin");
- Assert.Zero(app.Profile.Data.tickets);
- Assert.False(spin.interactable);
- Assert.AreEqual(1f, spin.image.canvasRenderer.GetColor().a, .001f);
- }
- [UnityTest] public IEnumerator RewardedButtonsHandleUnavailableSkippedAndRepeatedCallbacks()
- {
- var ad=app.gameObject.AddComponent<TestRewardedAds>();app.Ads=ad;
- app.ShowLobby();app.OpenPanel("checkin");Button("Footer_CLAIM DAY 1").onClick.Invoke();
- Button("AdCheckin").onClick.Invoke();ad.Close();
- Assert.AreEqual(1700,app.Profile.Data.coins);
- Button("AdCheckin").onClick.Invoke();ad.Earn();ad.Earn();ad.Close();
- Assert.AreEqual(1900,app.Profile.Data.coins);Assert.False(app.Profile.CanClaimCheckinBonus);
- app.ClosePanel();app.OpenPanel("draw");const string button="Footer_WATCH AD · +1 TICKET";
- ad.Available=false;Button(button).onClick.Invoke();Assert.False(ad.IsShowing);
- Assert.False(Button(button).interactable);
- Assert.AreEqual(1,ad.Retries);
- ad.SetStatus(HexaCrazy.Ads.RewardedAdStatus.NetworkError);yield return null;
- Assert.True(Button(button).interactable);
- StringAssert.Contains("NETWORK ERROR",Button(button).GetComponentInChildren<TextMeshProUGUI>().text);
- Button(button).onClick.Invoke();Assert.AreEqual(2,ad.Retries);Assert.AreEqual(0,app.Profile.Data.tickets);
- ad.SetStatus(HexaCrazy.Ads.RewardedAdStatus.NoFill);yield return null;
- StringAssert.Contains("NO AD AVAILABLE",Button(button).GetComponentInChildren<TextMeshProUGUI>().text);
- ad.SetStatus(HexaCrazy.Ads.RewardedAdStatus.Ready);yield return null;
- StringAssert.Contains("WATCH AD",Button(button).GetComponentInChildren<TextMeshProUGUI>().text);
- ad.Available=true;Button(button).onClick.Invoke();
- var oldEarn=ad.Earn;var oldClose=ad.Close;
- Button(button).onClick.Invoke();Assert.AreSame(oldEarn,ad.Earn);
- ad.Close();oldEarn();Assert.AreEqual(0,app.Profile.Data.tickets);
- for(int i=1;i<=2;i++)
- {
- Button(button).onClick.Invoke();oldClose();Assert.True(ad.IsShowing);
- ad.Earn();ad.Earn();ad.Close();
- Assert.AreEqual(i,new LocalProfile(app.Profile.Path).Data.tickets);
- }
- Assert.True(app.Profile.FreeDraw);
- Button("Spin").onClick.Invoke();Assert.AreEqual(2,app.Profile.Data.tickets);
- yield return null;
- }
- [UnityTest] public IEnumerator TimeoutDuringMoveCanReviveOnCloseAndWinOnce()
- {
- var ad=app.gameObject.AddComponent<TestRewardedAds>();app.Ads=ad;
- app.Profile.Data.reduceMotion=false;app.Profile.Data.tutorial=1;
- app.StartLevel(1,1,true);var round=app.Game;
- round.Hand[0]=new HandGroup(0,1);round.Hand[1]=round.Hand[2]=null;
- Assert.True(app.PlaceAt(0,0));round.Time=0;
- yield return new WaitUntil(()=>round.Over);
- Assert.AreEqual(2,round.Score);Assert.True(round.Hand.All(h=>h!=null));
- Button("AdRevive").onClick.Invoke();ad.Close();Assert.True(round.Over);Assert.False(round.ReviveUsed);
- Button("AdRevive").onClick.Invoke();ad.Earn();Assert.True(round.Over);ad.Close();
- Assert.False(round.Over);Assert.AreSame(round,app.Game);Assert.AreEqual(30,round.Time);
- Assert.True(round.ReviveUsed);Assert.IsNull(app.Modal);Assert.False(app.Busy);
- app.Profile.Data.reduceMotion=true;round.Score=round.Spec.target-2;
- round.Hand[0]=new HandGroup(1,1);Assert.True(app.PlaceAt(0,1));
- Assert.AreEqual(1,app.Profile.Data.totalWins);Assert.True(round.Over);
- ad.Close();Assert.AreEqual(1,app.Profile.Data.totalWins);
- Assert.False(app.Modal.GetComponentsInChildren<Button>().Any(b=>b.name=="AdRevive"));
- }
- [UnityTest] public IEnumerator FullBoardOffersOnlyOneRevivePerRound()
- {
- var ad=app.gameObject.AddComponent<TestRewardedAds>();app.Ads=ad;
- app.StartLevel(0,1,true);
- foreach(var c in app.Game.Cells)c.tiles.Add(0);
- app.UseItem(1);Assert.True(app.Game.Over);
- Button("AdRevive").onClick.Invoke();ad.Earn();ad.Close();
- Assert.AreEqual(3,app.Game.Cells.Count(c=>c.tiles.Count==0));
- foreach(var c in app.Game.Cells.Where(c=>c.tiles.Count==0))c.tiles.Add(0);
- app.Profile.Data.items[1]++;app.UseItem(1);Assert.True(app.Game.Over);
- Assert.False(app.Modal.GetComponentsInChildren<Button>().Any(b=>b.name=="AdRevive"));
- Assert.NotNull(Button("Footer_RETRY"));yield return null;
- }
- Vector2 Position(RectTransform rect)=>RectTransformUtility.WorldToScreenPoint(null,rect.position);
- [UnityTest] public IEnumerator StartLobbyModesAndPanelsWork()
- {
- Assert.AreEqual("Start",app.ScreenName);Button("GameStart").onClick.Invoke();Assert.AreEqual("Lobby",app.ScreenName);
- Button("CoinBalance").onClick.Invoke();
- Assert.AreEqual("Modal_shop", app.Modal.name);
- app.ClosePanel();
- Button("NextMode").onClick.Invoke();Button("Play").onClick.Invoke();Assert.AreEqual(1,app.Game.Spec.mode);
- Button("Pause").onClick.Invoke();Assert.True(app.Paused);Button("Resume").onClick.Invoke();Assert.False(app.Paused);
- app.LeaveGame();foreach(string panel in new[]{"shop","checkin","draw","help","profile","settings"}){app.OpenPanel(panel);yield return null;Assert.NotNull(app.Modal);app.ClosePanel();Assert.IsNull(app.Modal);}
- }
- [UnityTest] public IEnumerator RealDragHandlersSurviveBeginDragAndFinishTutorial()
- {
- app.StartLevel(0,1);yield return null;
- var source=app.Root.GetComponentsInChildren<HandDrag>().First(h=>h.slot==0);
- int center=app.Game.Cells.FindIndex(c=>c.q==0&&c.r==0);int target=app.Game.Neighbors(center)[0];
- var e=new PointerEventData(EventSystem.current){position=Position((RectTransform)source.transform)};
- ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.beginDragHandler);yield return null;
- Assert.True(source!=null,"BeginDrag must not destroy its own receiver");
- e.position=Position((RectTransform)Button("Cell_"+target).transform);
- ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.dragHandler);ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.endDragHandler);yield return null;
- Assert.AreEqual(62,app.Game.Score);Assert.AreEqual(1,app.Profile.Data.tutorial);Assert.False(app.Busy);
- Canvas.ForceUpdateCanvases();var fill=app.Root.GetComponentInChildren<HexProgress>();Assert.Greater(fill.Value,0);Assert.Greater(fill.canvasRenderer.GetMesh().vertexCount,0,"Score progress must have a visible mesh");
- }
- [UnityTest] public IEnumerator DownwardDragReleasedOverHandSlotLeavesAllThreeSlotsUsable()
- {
- app.Profile.Data.tutorial = 1;
- app.StartLevel(0, 6);
- yield return null;
- var effects = app.Root.Find("Effects");
- var hand = app.Game.Hand.ToArray();
- foreach (int slot in new[] { 2, 0, 1 })
- {
- var source = app.Root.GetComponentsInChildren<HandDrag>().Single(h => h.slot == slot);
- var rect = (RectTransform)source.transform;
- var e = new PointerEventData(EventSystem.current) { position = Position(rect), eligibleForClick = true };
- ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.pointerDownHandler);
- e.position = RectTransformUtility.WorldToScreenPoint(null, rect.TransformPoint(Vector2.down * rect.rect.height * .4f));
- ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.beginDragHandler);
- e.dragging = true;
- ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.dragHandler);
- Assert.AreEqual(1, effects.childCount, "Each slot must be able to start a fresh drag");
- // uGUI sends a same-target click before endDrag if the gesture is still eligible.
- if (e.eligibleForClick)
- ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.pointerClickHandler);
- ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.endDragHandler);
- yield return null;
- Assert.AreEqual(0, effects.childCount, "Releasing below the tile must remove the preview");
- Assert.AreEqual(-1, app.SelectedSlot);
- CollectionAssert.AreEqual(hand, app.Game.Hand);
- source = app.Root.GetComponentsInChildren<HandDrag>().Single(h => h.slot == slot);
- var tap = new PointerEventData(EventSystem.current) { position = Position((RectTransform)source.transform) };
- ExecuteEvents.Execute(source.gameObject, tap, ExecuteEvents.pointerDownHandler);
- ExecuteEvents.Execute(source.gameObject, tap, ExecuteEvents.pointerClickHandler);
- Assert.AreEqual(slot, app.SelectedSlot);
- }
- }
- [UnityTest] public IEnumerator InvalidDropPreservesHandAndShopRefreshesInventory()
- {
- app.StartLevel(0,1,true);yield return null;var group=app.Game.Hand[0];app.BeginHandDrag(0,Vector2.zero);app.EndHandDrag(new Vector2(-100,-100));Assert.AreSame(group,app.Game.Hand[0]);
- app.Game.Cells[0].tiles.Add(0);yield return null;
- var source=app.Root.GetComponentsInChildren<HandDrag>().First(h=>h.slot==0);
- var e=new PointerEventData(EventSystem.current){position=Position((RectTransform)source.transform)};
- ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.beginDragHandler);
- e.position=Position((RectTransform)Button("Cell_0").transform);ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.endDragHandler);
- yield return null;Assert.AreSame(group,app.Game.Hand[0]);
- ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.pointerDownHandler);ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.pointerClickHandler);
- Assert.AreEqual(-1,app.SelectedSlot,"The next tap after an occupied-cell drop must not be swallowed");
- app.OpenPanel("shop");Button("BuyItem_0").onClick.Invoke();app.ClosePanel();yield return null;
- Assert.AreEqual(700,app.Profile.Data.coins);Assert.AreEqual("2",Button("UseItem_0").GetComponentsInChildren<TextMeshProUGUI>().First(t=>t.name=="Count").text);
- }
- [UnityTest] public IEnumerator TimerPausesAndTimeoutDoesNotPayRewards()
- {
- app.StartLevel(1,1,true);app.OpenPanel("pause");float before=app.Game.Time;yield return new WaitForSecondsRealtime(.2f);Assert.AreEqual(before,app.Game.Time,.01f);
- app.ClosePanel();app.Game.Time=.05f;yield return new WaitForSecondsRealtime(.15f);Assert.True(app.Game.Over);Assert.AreEqual(1500,app.Profile.Data.coins);Assert.AreEqual("Modal_result",app.Modal.name);
- }
- [UnityTest] public IEnumerator WinPaysOnceAndNextLevelAdvances()
- {
- app.StartLevel(0,1,true);app.Game.Score=app.Game.Spec.target-2;app.Game.Hand[0]=new HandGroup(0,1);
- Assert.True(app.PlaceAt(0,0));yield return null;Assert.True(app.Game.Over);Assert.AreEqual(1,app.Profile.Data.totalWins);Assert.AreEqual(1660,app.Profile.Data.coins);Assert.AreEqual(1,app.Profile.Data.tickets);
- Assert.False(app.PlaceAt(1,1));Assert.AreEqual(1660,app.Profile.Data.coins);Button("Footer_NEXT LEVEL").onClick.Invoke();Assert.AreEqual(2,app.Game.Spec.level);
- }
- [UnityTest] public IEnumerator AllLevelViewsStayInsideDesignCanvasAndTextRenders()
- {
- for(int mode=0;mode<3;mode++)foreach(int level in new[]{1,7,20})
- {
- app.StartLevel(mode,level,true);yield return null;Canvas.ForceUpdateCanvases();
- foreach(var b in app.Root.GetComponentsInChildren<Button>().Where(b=>b.name.StartsWith("Cell_")))
- {var rt=(RectTransform)b.transform;Assert.Greater(rt.anchoredPosition.x-rt.rect.width/2,0);Assert.Less(rt.anchoredPosition.x+rt.rect.width/2,750);Assert.Greater(-rt.anchoredPosition.y-rt.rect.height/2,250);Assert.Less(-rt.anchoredPosition.y+rt.rect.height/2,935);}
- foreach(var text in app.Root.GetComponentsInChildren<TextMeshProUGUI>().Where(t=>!string.IsNullOrEmpty(t.text)))
- {text.ForceMeshUpdate();Assert.Greater(text.textInfo.characterCount,0,"Empty text mesh: "+text.name+" / "+text.text);}
- }
- }
- [UnityTest] public IEnumerator CarouselAnimatesExistingCardsAndCanRetargetWithoutRebuilding()
- {
- app.Profile.Data.reduceMotion=false;app.ShowLobby();yield return null;
- var center=(RectTransform)Button("ModeCard0").transform;var next=(RectTransform)Button("ModeCard1").transform;int instance=center.GetInstanceID();
- var carousel=app.Root.GetComponentInChildren<HexLobbyCarousel>();float start=next.anchoredPosition.x;
- Button("NextMode").onClick.Invoke();Assert.True(carousel.IsAnimating);Assert.AreEqual(1,app.SelectedMode);
- yield return new WaitForSecondsRealtime(.08f);
- Assert.Less(next.anchoredPosition.x,start);Assert.Greater(next.anchoredPosition.x,375);Assert.AreEqual(instance,Button("ModeCard0").transform.GetInstanceID());
- Button("NextMode").onClick.Invoke();Button("NextMode").onClick.Invoke();Assert.AreEqual(0,app.SelectedMode);
- yield return new WaitForSecondsRealtime(.47f);
- Assert.False(carousel.IsAnimating);Assert.AreEqual(375,center.anchoredPosition.x,.01f);Assert.AreEqual(1,center.localScale.x,.001f);
- Assert.AreEqual(HexLobbyCarousel.SideScale,next.localScale.x,.001f);Assert.AreEqual(.5f,next.GetComponent<CanvasGroup>().alpha,.001f);
- Button("ModeCard0").onClick.Invoke();Assert.AreEqual("Game",app.ScreenName);Assert.AreEqual(0,app.Game.Spec.mode);
- }
- [UnityTest] public IEnumerator CarouselSwipeDotsAndPlayUseTheSelectedMode()
- {
- app.Profile.Data.cleared[1]=6;app.Profile.Data.cleared[2]=20;app.ShowLobby();yield return null;
- var carousel=app.Root.GetComponentInChildren<HexLobbyCarousel>();
- var e=new PointerEventData(EventSystem.current){position=Position((RectTransform)carousel.transform)};var start=e.position;
- ExecuteEvents.Execute(carousel.gameObject,e,ExecuteEvents.beginDragHandler);e.position=start+new Vector2(-150,0);ExecuteEvents.Execute(carousel.gameObject,e,ExecuteEvents.endDragHandler);
- Assert.AreEqual(1,app.SelectedMode);Button("Play").onClick.Invoke();Assert.AreEqual(1,app.Game.Spec.mode);Assert.AreEqual(7,app.Game.Spec.level);
- app.LeaveGame();Button("ModeDot2").onClick.Invoke();Assert.AreEqual("REPLAY",Button("Play").GetComponentInChildren<TextMeshProUGUI>().text);
- Button("Play").onClick.Invoke();Assert.AreEqual(2,app.Game.Spec.mode);Assert.AreEqual(20,app.Game.Spec.level);
- }
- [UnityTest] public IEnumerator SpriteMeshesKeepTheirNativeAspectAcrossScreensAndPanels()
- {
- app.ShowLobby();yield return null;CheckImageMeshes();
- app.StartLevel(2,1,true);yield return null;CheckImageMeshes();
- foreach(var panel in new[]{"shop","draw","profile","settings"}){app.OpenPanel(panel);yield return null;CheckImageMeshes();app.ClosePanel();}
- }
- void CheckImageMeshes()
- {
- Canvas.ForceUpdateCanvases();
- foreach(var im in app.Root.GetComponentsInChildren<Image>().Where(i=>i.sprite!=null))
- {
- if(HexUI.Resizable(im.sprite.name)){Assert.AreEqual(Image.Type.Sliced,im.type,im.name);Assert.Greater(im.sprite.border.sqrMagnitude,0);continue;}
- Assert.True(im.preserveAspect,im.name+" must retain native aspect");var mesh=im.canvasRenderer.GetMesh();if(mesh==null||mesh.vertexCount==0)continue;
- var v=mesh.vertices;float w=v.Max(p=>p.x)-v.Min(p=>p.x),h=v.Max(p=>p.y)-v.Min(p=>p.y);
- // Unity trims transparent sprite padding. Compare geometry to sampled texels,
- // not the full source rectangle, so the test measures actual deformation.
- var uv=mesh.uv;float pixelsWide=(uv.Max(p=>p.x)-uv.Min(p=>p.x))*im.mainTexture.width,pixelsHigh=(uv.Max(p=>p.y)-uv.Min(p=>p.y))*im.mainTexture.height;
- Assert.AreEqual(pixelsWide/pixelsHigh,w/h,.025f,"Stretched mesh: "+im.name);
- }
- }
- }
- }
|