ConvertMaskMenuTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor;
  4. using NUnit.Framework;
  5. using System;
  6. using System.Linq;
  7. using Object = UnityEngine.Object;
  8. namespace SoftMasking.Editor {
  9. public class ConvertMaskMenuTest {
  10. [Test] public void WhenNoObjectSelected_ShouldBeNotAvailable() {
  11. SelectObjects();
  12. Assert.IsFalse(ConvertMaskMenu.CanConvert());
  13. }
  14. [Test] public void WhenEmptyObjectSelected_ShouldBeNotAvailable() {
  15. AssertObjectIsNotConvertible();
  16. }
  17. [Test] public void WhenObjectWithMaskButWithoutGraphicSelected_ShouldBeNotAvailable() {
  18. AssertObjectIsNotConvertible(typeof(Mask));
  19. }
  20. [Test] public void WhenObjectWithGraphicButWithoutMaskSelected_ShouldBeNotAvailable() {
  21. AssertObjectIsNotConvertible(typeof(Image));
  22. }
  23. [Test] public void WhenObjectWithWrongTypeOfGraphicSelected_ShouldBeNotAvailable() {
  24. AssertObjectIsNotConvertible(typeof(Text), typeof(Mask));
  25. }
  26. void AssertObjectIsNotConvertible(params Type[] componentTypes) {
  27. var go = CreateGameObject(componentTypes);
  28. SelectObjects(go);
  29. Assert.IsFalse(ConvertMaskMenu.CanConvert());
  30. }
  31. GameObject CreateGameObject(params Type[] componentTypes) {
  32. var go = new GameObject("TestObject", componentTypes);
  33. Undo.RegisterCreatedObjectUndo(go, "Undo TestObject creation");
  34. return go;
  35. }
  36. void SelectObjects(params GameObject[] objects) {
  37. Selection.objects = objects.Cast<Object>().ToArray();
  38. }
  39. [Test] public void WhenNotAllOfSelectedObjectsConvertible_ShouldBeNotAvailable() {
  40. var good = CreateGameObject(typeof(Mask), typeof(Image));
  41. var bad = CreateGameObject(typeof(Mask), typeof(Text));
  42. SelectObjects(good, bad);
  43. Assert.IsFalse(ConvertMaskMenu.CanConvert());
  44. }
  45. [Test] public void WhenConvertibleObjectsSelected_ShouldBeAvailable() {
  46. var go = CreateGameObject(typeof(Mask), typeof(Image));
  47. SelectObjects(go);
  48. Assert.IsTrue(ConvertMaskMenu.CanConvert());
  49. }
  50. [Test] public void WhenInvokedOnSeveralObjects_TheyAllShouldBeConverted() {
  51. var gos = new [] {
  52. CreateObjectWithImageMask(renderable: true),
  53. CreateObjectWithImageMask(renderable: false),
  54. CreateObjectWithRawImageMask(renderable: true),
  55. CreateObjectWithRawImageMask(renderable: false),
  56. };
  57. SelectAndConvertObjects(gos);
  58. AssertConvertedProperly(gos[0], renderable: true, raw: false);
  59. AssertConvertedProperly(gos[1], renderable: false, raw: false);
  60. AssertConvertedProperly(gos[2], renderable: true, raw: true);
  61. AssertConvertedProperly(gos[3], renderable: false, raw: true);
  62. }
  63. void SelectAndConvertObjects(params GameObject[] objects) {
  64. SelectObjects(objects);
  65. ConvertMaskMenu.Convert();
  66. }
  67. void AssertConvertedProperly(GameObject go, bool renderable, bool raw) {
  68. var softMask = go.GetComponent<SoftMask>();
  69. Assert.IsNotNull(softMask);
  70. Assert.IsNull(go.GetComponent<Mask>());
  71. if (renderable) {
  72. Assert.AreEqual(SoftMask.MaskSource.Graphic, softMask.source);
  73. if (raw)
  74. AssertHasComponent<RawImage>(go);
  75. else
  76. AssertHasComponent<Image>(go);
  77. } else {
  78. if (raw) {
  79. AssertHasNoComponent<RawImage>(go);
  80. AssertRawImageConvertedProperly(softMask);
  81. } else {
  82. AssertHasNoComponent<Image>(go);
  83. AssertImageConvertedProperly(softMask);
  84. }
  85. }
  86. }
  87. void AssertHasComponent<T>(GameObject go) where T : Component {
  88. Assert.IsNotNull(go.GetComponent<T>());
  89. }
  90. void AssertHasNoComponent<T>(GameObject go) where T : Component {
  91. Assert.IsNull(go.GetComponent<T>());
  92. }
  93. static void AssertRawImageConvertedProperly(SoftMask softMask) {
  94. Assert.AreEqual(standardUISprite.texture, softMask.texture);
  95. Assert.AreEqual(standardRect, softMask.textureUVRect);
  96. }
  97. static void AssertImageConvertedProperly(SoftMask softMask) {
  98. Assert.AreEqual(standardUISprite, softMask.sprite);
  99. Assert.AreEqual(SoftMask.BorderMode.Sliced, softMask.spriteBorderMode);
  100. #if UNITY_2019_2_OR_NEWER
  101. Assert.AreEqual(120, softMask.spritePixelsPerUnitMultiplier);
  102. #endif
  103. }
  104. GameObject CreateObjectWithImageMask(bool renderable, Sprite sprite = null) {
  105. var go = CreateGameObject();
  106. var image = go.AddComponent<Image>();
  107. image.sprite = sprite ? sprite : standardUISprite;
  108. image.type = Image.Type.Sliced;
  109. #if UNITY_2019_2_OR_NEWER
  110. image.pixelsPerUnitMultiplier = 120;
  111. #endif
  112. var mask = go.AddComponent<Mask>();
  113. mask.showMaskGraphic = renderable;
  114. return go;
  115. }
  116. static Sprite _standardUISprite;
  117. static Sprite standardUISprite {
  118. get {
  119. return _standardUISprite
  120. ? _standardUISprite
  121. : (_standardUISprite = AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"));
  122. }
  123. }
  124. GameObject CreateObjectWithRawImageMask(bool renderable) {
  125. var go = CreateGameObject();
  126. var image = go.AddComponent<RawImage>();
  127. image.texture = standardUISprite.texture;
  128. image.uvRect = standardRect;
  129. var mask = go.AddComponent<Mask>();
  130. mask.showMaskGraphic = renderable;
  131. return go;
  132. }
  133. static readonly Rect standardRect = new Rect(0.2f, 0.1f, 0.7f, 0.6f);
  134. [Test] public void WhenImageWithStandardUIMaskSpriteConverted_SoftMaskShouldHaveAdaptedSprite() {
  135. foreach (var renderable in trueAndFalse) {
  136. var go = CreateAndConvertObjectWithImageMask(renderable, sprite: ConvertMaskMenu.standardUIMaskSprite);
  137. AssertMaskHaveAdaptedSprite(go);
  138. }
  139. }
  140. static readonly bool[] trueAndFalse = new[] { true, false };
  141. GameObject CreateAndConvertObjectWithImageMask(bool renderable, Sprite sprite = null) {
  142. var go = CreateObjectWithImageMask(renderable, sprite: sprite);
  143. SelectAndConvertObjects(go);
  144. return go;
  145. }
  146. void AssertMaskHaveAdaptedSprite(GameObject go) {
  147. var softMask = go.GetComponent<SoftMask>();
  148. Assert.AreEqual(ConvertMaskMenu.adaptedUIMaskSprite, softMask.sprite);
  149. }
  150. [Test] public void WhenRenderableImageWithStandardUIMaskSpriteConverted_ImageShouldSkillHaveStandardSprite() {
  151. var go = CreateAndConvertObjectWithImageMask(renderable: true, sprite: ConvertMaskMenu.standardUIMaskSprite);
  152. var image = go.GetComponent<Image>();
  153. Assert.AreEqual(ConvertMaskMenu.standardUIMaskSprite, image.sprite);
  154. }
  155. [Test] public void AfterConversion_AllTheChangesMayBeUndoneInSingleStep() {
  156. foreach (var renderable in trueAndFalse) {
  157. Undo.IncrementCurrentGroup();
  158. var go = CreateAndConvertObjectWithImageMask(renderable);
  159. Undo.PerformUndo();
  160. AssertHasComponent<Mask>(go);
  161. AssertHasComponent<Image>(go);
  162. AssertHasNoComponent<SoftMask>(go);
  163. }
  164. }
  165. [Test] public void AfterUndo_AllTheChangesMayBeReappliedInSingleStep() {
  166. foreach (var renderable in trueAndFalse) {
  167. var go = CreateAndConvertObjectWithImageMask(renderable);
  168. Undo.PerformUndo();
  169. Undo.PerformRedo();
  170. AssertConvertedProperly(go, renderable, raw: false);
  171. }
  172. }
  173. [Test] public void WhenRawImageWithUnsupportedTextureTypeConverted_ShouldThrow() {
  174. var unsupportedTexture = new Texture3D(4, 4, 4, TextureFormat.Alpha8, false);
  175. try {
  176. foreach (var renderable in trueAndFalse) {
  177. var go = CreateObjectWithRawImageMask(renderable);
  178. go.GetComponent<RawImage>().texture = unsupportedTexture;
  179. SelectObjects(go);
  180. Assert.Throws(typeof(ConvertMaskMenu.UnsupportedRawImageTextureType), ConvertMaskMenu.Convert);
  181. }
  182. } finally {
  183. Object.DestroyImmediate(unsupportedTexture);
  184. }
  185. }
  186. [Test] public void WhenImageWithoutSpriteConverted_ShouldConvertToSoftMaskWithoutSprite() {
  187. var go = CreateObjectWithImageMask(renderable: false);
  188. go.GetComponent<Image>().sprite = null;
  189. SelectAndConvertObjects(go);
  190. AssertHasComponent<SoftMask>(go);
  191. AssertHasNoComponent<Image>(go);
  192. Assert.IsNull(go.GetComponent<SoftMask>().sprite);
  193. }
  194. [Test] public void WhenRawImageWithoutTextureConverted_ShouldConvertToSoftMaskWithoutTexture() {
  195. var go = CreateObjectWithRawImageMask(renderable: false);
  196. go.GetComponent<RawImage>().texture = null;
  197. SelectAndConvertObjects(go);
  198. AssertHasComponent<SoftMask>(go);
  199. AssertHasNoComponent<RawImage>(go);
  200. Assert.IsNull(go.GetComponent<SoftMask>().texture);
  201. }
  202. [Test] public void WhenImageOfUnsupportedTypeConverted_ShouldThrow() {
  203. foreach (var renderable in trueAndFalse) {
  204. var go = CreateObjectWithImageMask(renderable);
  205. go.GetComponent<Image>().type = Image.Type.Filled;
  206. SelectObjects(go);
  207. Assert.Throws(typeof(ConvertMaskMenu.UnsupportedImageType), ConvertMaskMenu.Convert);
  208. }
  209. }
  210. }
  211. }