GameFlowTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Linq;
  5. using NUnit.Framework;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. using UnityEngine.TestTools;
  10. using UnityEngine.UI;
  11. using Object=UnityEngine.Object;
  12. namespace HexaCrazy.Tests
  13. {
  14. public class GameFlowTests
  15. {
  16. HexGameApp app;string directory;
  17. [UnitySetUp] public IEnumerator Setup()
  18. {
  19. directory=Path.Combine(Path.GetTempPath(),"HexaCrazyPlayTests",Guid.NewGuid().ToString("N"));
  20. app=new GameObject("TestGameApp").AddComponent<HexGameApp>();app.UseReviewProfile(Path.Combine(directory,"save.json"));app.Profile.Data.reduceMotion=true;
  21. if(Object.FindAnyObjectByType<AudioListener>()==null)app.gameObject.AddComponent<AudioListener>();
  22. app.GetComponent<HexAudio>().Music=false;app.GetComponent<HexAudio>().Sound=false;
  23. yield return new WaitForSecondsRealtime(.95f);
  24. }
  25. [UnityTearDown] public IEnumerator Cleanup(){Object.Destroy(app.gameObject);yield return null;if(Directory.Exists(directory))Directory.Delete(directory,true);}
  26. Button Button(string name)=>app.Root.GetComponentsInChildren<Button>().First(b=>b.name==name);
  27. [UnityTest] public IEnumerator PlatformOpenStopsMusicAndPersistsTheSetting()
  28. {
  29. var audio = app.GetComponent<HexAudio>();
  30. var bgm = app.GetComponents<AudioSource>().Single(source => source.loop);
  31. AppRuntimesEvents.RaisePlatformOpen();
  32. Button("GameStart").onClick.Invoke();
  33. Assert.IsFalse(audio.Music);
  34. Assert.IsFalse(new LocalProfile(app.Profile.Path).Data.music);
  35. app.OpenPanel("settings");
  36. Button("Toggle_0").onClick.Invoke();
  37. yield return null;
  38. yield return null;
  39. Assert.IsTrue(audio.Music);
  40. Assert.Greater(bgm.volume, 0f);
  41. bool sound = audio.Sound;
  42. AppRuntimesEvents.RaisePlatformOpen();
  43. AppRuntimesEvents.RaisePlatformOpen();
  44. Assert.IsFalse(audio.Music);
  45. Assert.IsFalse(bgm.isPlaying);
  46. Assert.AreEqual(0f, bgm.volume);
  47. Assert.AreEqual("OFF", Button("Toggle_0").transform.Find("Switch/Value").GetComponent<TMP_Text>().text);
  48. var saved = new LocalProfile(app.Profile.Path).Data;
  49. Assert.IsFalse(saved.music);
  50. Assert.AreEqual(sound, audio.Sound);
  51. Assert.AreEqual(sound, saved.sound);
  52. }
  53. [UnityTest] public IEnumerator WheelTurnsUnderFixedPointerAndStopsOnAwardedPrize()
  54. {
  55. app.Profile.Data.reduceMotion = false;
  56. app.Profile.Data.tickets = 1;
  57. app.ShowLobby();
  58. app.OpenPanel("draw");
  59. Button("Spin").onClick.Invoke();
  60. var spin = Button("Spin");
  61. var frame = spin.transform.parent;
  62. var wheel = frame.Find("WheelAperture/PrizeWheel");
  63. var rim = frame.Find("WheelRim");
  64. var rimStart = rim.rotation;
  65. var pointer = frame.Find("PrizeIndicator");
  66. var start = wheel.localRotation;
  67. var pointerStart = pointer.rotation;
  68. var hubStart = spin.transform.rotation;
  69. Assert.False(spin.interactable);
  70. spin.onClick.Invoke();
  71. Assert.AreEqual(1, app.Profile.Data.tickets, "Repeated input during a free spin must not spend a ticket.");
  72. yield return new WaitForSecondsRealtime(.35f);
  73. Assert.Greater(Quaternion.Angle(start, wheel.localRotation), 1f);
  74. Assert.Less(Quaternion.Angle(rimStart, rim.rotation), .01f);
  75. var aperture = wheel.GetComponentInParent<Mask>();
  76. Assert.NotNull(aperture, "The spinning artwork must be clipped inside the stationary rim.");
  77. Assert.False(rim.IsChildOf(aperture.transform));
  78. Assert.Less(Quaternion.Angle(pointerStart, pointer.rotation), .01f);
  79. Assert.Less(Quaternion.Angle(hubStart, spin.transform.rotation), .01f);
  80. Assert.AreEqual(1f, spin.image.canvasRenderer.GetColor().a, .001f);
  81. float deadline = Time.realtimeSinceStartup + 5;
  82. yield return new WaitUntil(() => Button("Spin").interactable || Time.realtimeSinceStartup >= deadline);
  83. Assert.True(Button("Spin").interactable);
  84. wheel = Button("Spin").transform.parent.Find("WheelAperture/PrizeWheel");
  85. var winner = Enumerable.Range(0, wheel.childCount).Select(i => wheel.GetChild(i))
  86. .OrderByDescending(wedge => wedge.position.y).First();
  87. Assert.AreEqual(wheel.position.x, winner.position.x, .1f, "The winning sector should be centered at twelve o'clock.");
  88. var award = app.Root.GetComponentsInChildren<TextMeshProUGUI>().First(t => t.name == "ToastText");
  89. Assert.AreEqual("WON " + winner.GetComponentInChildren<TextMeshProUGUI>().text, award.text);
  90. app.Profile.Data.reduceMotion = true;
  91. Button("Spin").onClick.Invoke();
  92. yield return new WaitForSecondsRealtime(.2f);
  93. spin = Button("Spin");
  94. Assert.Zero(app.Profile.Data.tickets);
  95. Assert.False(spin.interactable);
  96. Assert.AreEqual(1f, spin.image.canvasRenderer.GetColor().a, .001f);
  97. }
  98. [UnityTest] public IEnumerator RewardedButtonsHandleUnavailableSkippedAndRepeatedCallbacks()
  99. {
  100. var ad=app.gameObject.AddComponent<TestRewardedAds>();app.Ads=ad;
  101. app.ShowLobby();app.OpenPanel("checkin");Button("Footer_CLAIM DAY 1").onClick.Invoke();
  102. Button("AdCheckin").onClick.Invoke();ad.Close();
  103. Assert.AreEqual(1700,app.Profile.Data.coins);
  104. Button("AdCheckin").onClick.Invoke();ad.Earn();ad.Earn();ad.Close();
  105. Assert.AreEqual(1900,app.Profile.Data.coins);Assert.False(app.Profile.CanClaimCheckinBonus);
  106. app.ClosePanel();app.OpenPanel("draw");const string button="Footer_WATCH AD · +1 TICKET";
  107. ad.Available=false;Button(button).onClick.Invoke();Assert.False(ad.IsShowing);
  108. Assert.False(Button(button).interactable);
  109. Assert.AreEqual(1,ad.Retries);
  110. ad.SetStatus(HexaCrazy.Ads.RewardedAdStatus.NetworkError);yield return null;
  111. Assert.True(Button(button).interactable);
  112. StringAssert.Contains("NETWORK ERROR",Button(button).GetComponentInChildren<TextMeshProUGUI>().text);
  113. Button(button).onClick.Invoke();Assert.AreEqual(2,ad.Retries);Assert.AreEqual(0,app.Profile.Data.tickets);
  114. ad.SetStatus(HexaCrazy.Ads.RewardedAdStatus.NoFill);yield return null;
  115. StringAssert.Contains("NO AD AVAILABLE",Button(button).GetComponentInChildren<TextMeshProUGUI>().text);
  116. ad.SetStatus(HexaCrazy.Ads.RewardedAdStatus.Ready);yield return null;
  117. StringAssert.Contains("WATCH AD",Button(button).GetComponentInChildren<TextMeshProUGUI>().text);
  118. ad.Available=true;Button(button).onClick.Invoke();
  119. var oldEarn=ad.Earn;var oldClose=ad.Close;
  120. Button(button).onClick.Invoke();Assert.AreSame(oldEarn,ad.Earn);
  121. ad.Close();oldEarn();Assert.AreEqual(0,app.Profile.Data.tickets);
  122. for(int i=1;i<=2;i++)
  123. {
  124. Button(button).onClick.Invoke();oldClose();Assert.True(ad.IsShowing);
  125. ad.Earn();ad.Earn();ad.Close();
  126. Assert.AreEqual(i,new LocalProfile(app.Profile.Path).Data.tickets);
  127. }
  128. Assert.True(app.Profile.FreeDraw);
  129. Button("Spin").onClick.Invoke();Assert.AreEqual(2,app.Profile.Data.tickets);
  130. yield return null;
  131. }
  132. [UnityTest] public IEnumerator TimeoutDuringMoveCanReviveOnCloseAndWinOnce()
  133. {
  134. var ad=app.gameObject.AddComponent<TestRewardedAds>();app.Ads=ad;
  135. app.Profile.Data.reduceMotion=false;app.Profile.Data.tutorial=1;
  136. app.StartLevel(1,1,true);var round=app.Game;
  137. round.Hand[0]=new HandGroup(0,1);round.Hand[1]=round.Hand[2]=null;
  138. Assert.True(app.PlaceAt(0,0));round.Time=0;
  139. yield return new WaitUntil(()=>round.Over);
  140. Assert.AreEqual(2,round.Score);Assert.True(round.Hand.All(h=>h!=null));
  141. Button("AdRevive").onClick.Invoke();ad.Close();Assert.True(round.Over);Assert.False(round.ReviveUsed);
  142. Button("AdRevive").onClick.Invoke();ad.Earn();Assert.True(round.Over);ad.Close();
  143. Assert.False(round.Over);Assert.AreSame(round,app.Game);Assert.AreEqual(30,round.Time);
  144. Assert.True(round.ReviveUsed);Assert.IsNull(app.Modal);Assert.False(app.Busy);
  145. app.Profile.Data.reduceMotion=true;round.Score=round.Spec.target-2;
  146. round.Hand[0]=new HandGroup(1,1);Assert.True(app.PlaceAt(0,1));
  147. Assert.AreEqual(1,app.Profile.Data.totalWins);Assert.True(round.Over);
  148. ad.Close();Assert.AreEqual(1,app.Profile.Data.totalWins);
  149. Assert.False(app.Modal.GetComponentsInChildren<Button>().Any(b=>b.name=="AdRevive"));
  150. }
  151. [UnityTest] public IEnumerator FullBoardOffersOnlyOneRevivePerRound()
  152. {
  153. var ad=app.gameObject.AddComponent<TestRewardedAds>();app.Ads=ad;
  154. app.StartLevel(0,1,true);
  155. foreach(var c in app.Game.Cells)c.tiles.Add(0);
  156. app.UseItem(1);Assert.True(app.Game.Over);
  157. Button("AdRevive").onClick.Invoke();ad.Earn();ad.Close();
  158. Assert.AreEqual(3,app.Game.Cells.Count(c=>c.tiles.Count==0));
  159. foreach(var c in app.Game.Cells.Where(c=>c.tiles.Count==0))c.tiles.Add(0);
  160. app.Profile.Data.items[1]++;app.UseItem(1);Assert.True(app.Game.Over);
  161. Assert.False(app.Modal.GetComponentsInChildren<Button>().Any(b=>b.name=="AdRevive"));
  162. Assert.NotNull(Button("Footer_RETRY"));yield return null;
  163. }
  164. Vector2 Position(RectTransform rect)=>RectTransformUtility.WorldToScreenPoint(null,rect.position);
  165. [UnityTest] public IEnumerator StartLobbyModesAndPanelsWork()
  166. {
  167. Assert.AreEqual("Start",app.ScreenName);Button("GameStart").onClick.Invoke();Assert.AreEqual("Lobby",app.ScreenName);
  168. Button("CoinBalance").onClick.Invoke();
  169. Assert.AreEqual("Modal_shop", app.Modal.name);
  170. app.ClosePanel();
  171. Button("NextMode").onClick.Invoke();Button("Play").onClick.Invoke();Assert.AreEqual(1,app.Game.Spec.mode);
  172. Button("Pause").onClick.Invoke();Assert.True(app.Paused);Button("Resume").onClick.Invoke();Assert.False(app.Paused);
  173. 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);}
  174. }
  175. [UnityTest] public IEnumerator RealDragHandlersSurviveBeginDragAndFinishTutorial()
  176. {
  177. app.StartLevel(0,1);yield return null;
  178. var source=app.Root.GetComponentsInChildren<HandDrag>().First(h=>h.slot==0);
  179. int center=app.Game.Cells.FindIndex(c=>c.q==0&&c.r==0);int target=app.Game.Neighbors(center)[0];
  180. var e=new PointerEventData(EventSystem.current){position=Position((RectTransform)source.transform)};
  181. ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.beginDragHandler);yield return null;
  182. Assert.True(source!=null,"BeginDrag must not destroy its own receiver");
  183. e.position=Position((RectTransform)Button("Cell_"+target).transform);
  184. ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.dragHandler);ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.endDragHandler);yield return null;
  185. Assert.AreEqual(62,app.Game.Score);Assert.AreEqual(1,app.Profile.Data.tutorial);Assert.False(app.Busy);
  186. 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");
  187. }
  188. [UnityTest] public IEnumerator DownwardDragReleasedOverHandSlotLeavesAllThreeSlotsUsable()
  189. {
  190. app.Profile.Data.tutorial = 1;
  191. app.StartLevel(0, 6);
  192. yield return null;
  193. var effects = app.Root.Find("Effects");
  194. var hand = app.Game.Hand.ToArray();
  195. foreach (int slot in new[] { 2, 0, 1 })
  196. {
  197. var source = app.Root.GetComponentsInChildren<HandDrag>().Single(h => h.slot == slot);
  198. var rect = (RectTransform)source.transform;
  199. var e = new PointerEventData(EventSystem.current) { position = Position(rect), eligibleForClick = true };
  200. ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.pointerDownHandler);
  201. e.position = RectTransformUtility.WorldToScreenPoint(null, rect.TransformPoint(Vector2.down * rect.rect.height * .4f));
  202. ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.beginDragHandler);
  203. e.dragging = true;
  204. ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.dragHandler);
  205. Assert.AreEqual(1, effects.childCount, "Each slot must be able to start a fresh drag");
  206. // uGUI sends a same-target click before endDrag if the gesture is still eligible.
  207. if (e.eligibleForClick)
  208. ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.pointerClickHandler);
  209. ExecuteEvents.Execute(source.gameObject, e, ExecuteEvents.endDragHandler);
  210. yield return null;
  211. Assert.AreEqual(0, effects.childCount, "Releasing below the tile must remove the preview");
  212. Assert.AreEqual(-1, app.SelectedSlot);
  213. CollectionAssert.AreEqual(hand, app.Game.Hand);
  214. source = app.Root.GetComponentsInChildren<HandDrag>().Single(h => h.slot == slot);
  215. var tap = new PointerEventData(EventSystem.current) { position = Position((RectTransform)source.transform) };
  216. ExecuteEvents.Execute(source.gameObject, tap, ExecuteEvents.pointerDownHandler);
  217. ExecuteEvents.Execute(source.gameObject, tap, ExecuteEvents.pointerClickHandler);
  218. Assert.AreEqual(slot, app.SelectedSlot);
  219. }
  220. }
  221. [UnityTest] public IEnumerator InvalidDropPreservesHandAndShopRefreshesInventory()
  222. {
  223. 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]);
  224. app.Game.Cells[0].tiles.Add(0);yield return null;
  225. var source=app.Root.GetComponentsInChildren<HandDrag>().First(h=>h.slot==0);
  226. var e=new PointerEventData(EventSystem.current){position=Position((RectTransform)source.transform)};
  227. ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.beginDragHandler);
  228. e.position=Position((RectTransform)Button("Cell_0").transform);ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.endDragHandler);
  229. yield return null;Assert.AreSame(group,app.Game.Hand[0]);
  230. ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.pointerDownHandler);ExecuteEvents.Execute(source.gameObject,e,ExecuteEvents.pointerClickHandler);
  231. Assert.AreEqual(-1,app.SelectedSlot,"The next tap after an occupied-cell drop must not be swallowed");
  232. app.OpenPanel("shop");Button("BuyItem_0").onClick.Invoke();app.ClosePanel();yield return null;
  233. Assert.AreEqual(700,app.Profile.Data.coins);Assert.AreEqual("2",Button("UseItem_0").GetComponentsInChildren<TextMeshProUGUI>().First(t=>t.name=="Count").text);
  234. }
  235. [UnityTest] public IEnumerator TimerPausesAndTimeoutDoesNotPayRewards()
  236. {
  237. 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);
  238. 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);
  239. }
  240. [UnityTest] public IEnumerator WinPaysOnceAndNextLevelAdvances()
  241. {
  242. app.StartLevel(0,1,true);app.Game.Score=app.Game.Spec.target-2;app.Game.Hand[0]=new HandGroup(0,1);
  243. 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);
  244. 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);
  245. }
  246. [UnityTest] public IEnumerator AllLevelViewsStayInsideDesignCanvasAndTextRenders()
  247. {
  248. for(int mode=0;mode<3;mode++)foreach(int level in new[]{1,7,20})
  249. {
  250. app.StartLevel(mode,level,true);yield return null;Canvas.ForceUpdateCanvases();
  251. foreach(var b in app.Root.GetComponentsInChildren<Button>().Where(b=>b.name.StartsWith("Cell_")))
  252. {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);}
  253. foreach(var text in app.Root.GetComponentsInChildren<TextMeshProUGUI>().Where(t=>!string.IsNullOrEmpty(t.text)))
  254. {text.ForceMeshUpdate();Assert.Greater(text.textInfo.characterCount,0,"Empty text mesh: "+text.name+" / "+text.text);}
  255. }
  256. }
  257. [UnityTest] public IEnumerator CarouselAnimatesExistingCardsAndCanRetargetWithoutRebuilding()
  258. {
  259. app.Profile.Data.reduceMotion=false;app.ShowLobby();yield return null;
  260. var center=(RectTransform)Button("ModeCard0").transform;var next=(RectTransform)Button("ModeCard1").transform;int instance=center.GetInstanceID();
  261. var carousel=app.Root.GetComponentInChildren<HexLobbyCarousel>();float start=next.anchoredPosition.x;
  262. Button("NextMode").onClick.Invoke();Assert.True(carousel.IsAnimating);Assert.AreEqual(1,app.SelectedMode);
  263. yield return new WaitForSecondsRealtime(.08f);
  264. Assert.Less(next.anchoredPosition.x,start);Assert.Greater(next.anchoredPosition.x,375);Assert.AreEqual(instance,Button("ModeCard0").transform.GetInstanceID());
  265. Button("NextMode").onClick.Invoke();Button("NextMode").onClick.Invoke();Assert.AreEqual(0,app.SelectedMode);
  266. yield return new WaitForSecondsRealtime(.47f);
  267. Assert.False(carousel.IsAnimating);Assert.AreEqual(375,center.anchoredPosition.x,.01f);Assert.AreEqual(1,center.localScale.x,.001f);
  268. Assert.AreEqual(HexLobbyCarousel.SideScale,next.localScale.x,.001f);Assert.AreEqual(.5f,next.GetComponent<CanvasGroup>().alpha,.001f);
  269. Button("ModeCard0").onClick.Invoke();Assert.AreEqual("Game",app.ScreenName);Assert.AreEqual(0,app.Game.Spec.mode);
  270. }
  271. [UnityTest] public IEnumerator CarouselSwipeDotsAndPlayUseTheSelectedMode()
  272. {
  273. app.Profile.Data.cleared[1]=6;app.Profile.Data.cleared[2]=20;app.ShowLobby();yield return null;
  274. var carousel=app.Root.GetComponentInChildren<HexLobbyCarousel>();
  275. var e=new PointerEventData(EventSystem.current){position=Position((RectTransform)carousel.transform)};var start=e.position;
  276. ExecuteEvents.Execute(carousel.gameObject,e,ExecuteEvents.beginDragHandler);e.position=start+new Vector2(-150,0);ExecuteEvents.Execute(carousel.gameObject,e,ExecuteEvents.endDragHandler);
  277. Assert.AreEqual(1,app.SelectedMode);Button("Play").onClick.Invoke();Assert.AreEqual(1,app.Game.Spec.mode);Assert.AreEqual(7,app.Game.Spec.level);
  278. app.LeaveGame();Button("ModeDot2").onClick.Invoke();Assert.AreEqual("REPLAY",Button("Play").GetComponentInChildren<TextMeshProUGUI>().text);
  279. Button("Play").onClick.Invoke();Assert.AreEqual(2,app.Game.Spec.mode);Assert.AreEqual(20,app.Game.Spec.level);
  280. }
  281. [UnityTest] public IEnumerator SpriteMeshesKeepTheirNativeAspectAcrossScreensAndPanels()
  282. {
  283. app.ShowLobby();yield return null;CheckImageMeshes();
  284. app.StartLevel(2,1,true);yield return null;CheckImageMeshes();
  285. foreach(var panel in new[]{"shop","draw","profile","settings"}){app.OpenPanel(panel);yield return null;CheckImageMeshes();app.ClosePanel();}
  286. }
  287. void CheckImageMeshes()
  288. {
  289. Canvas.ForceUpdateCanvases();
  290. foreach(var im in app.Root.GetComponentsInChildren<Image>().Where(i=>i.sprite!=null))
  291. {
  292. if(HexUI.Resizable(im.sprite.name)){Assert.AreEqual(Image.Type.Sliced,im.type,im.name);Assert.Greater(im.sprite.border.sqrMagnitude,0);continue;}
  293. Assert.True(im.preserveAspect,im.name+" must retain native aspect");var mesh=im.canvasRenderer.GetMesh();if(mesh==null||mesh.vertexCount==0)continue;
  294. 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);
  295. // Unity trims transparent sprite padding. Compare geometry to sampled texels,
  296. // not the full source rectangle, so the test measures actual deformation.
  297. 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;
  298. Assert.AreEqual(pixelsWide/pixelsHigh,w/h,.025f,"Stretched mesh: "+im.name);
  299. }
  300. }
  301. }
  302. }