暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BannerTests.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #if NUGET_MOQ_AVAILABLE && UNITY_EDITOR
  2. using System;
  3. using System.Collections;
  4. using Moq;
  5. using NUnit.Framework;
  6. using UnityEngine.Advertisements.Utilities;
  7. using UnityEngine.TestTools;
  8. namespace UnityEngine.Advertisements.Tests
  9. {
  10. public class BannerTests
  11. {
  12. private Mock<IUnityLifecycleManager> m_CoroutineExecutorMock;
  13. private Mock<INativeBanner> m_NativeBannerMock;
  14. [SetUp]
  15. public void Setup()
  16. {
  17. m_CoroutineExecutorMock = new Mock<IUnityLifecycleManager>(MockBehavior.Strict);
  18. m_NativeBannerMock = new Mock<INativeBanner>(MockBehavior.Default);
  19. m_CoroutineExecutorMock.Setup(x => x.Post(It.IsAny<Action>())).Callback<Action>((action) => { action?.Invoke(); });
  20. m_CoroutineExecutorMock.Setup(x => x.StartCoroutine(It.IsAny<IEnumerator>())).Callback<IEnumerator>(x => {
  21. while (x.MoveNext()) {}
  22. }).Returns<Coroutine>(null);
  23. }
  24. [Test]
  25. [TestCase(false, false)]
  26. [TestCase(true, true)]
  27. [TestCase(null, false)]
  28. public void GetIsLoaded(bool actual, bool expected)
  29. {
  30. m_NativeBannerMock.Setup(x => x.IsLoaded).Returns(actual);
  31. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  32. Assert.That(banner.IsLoaded, Is.EqualTo(expected), "IsLoaded did not return the correct value");
  33. }
  34. [Test]
  35. [TestCase(false, false)]
  36. [TestCase(true, true)]
  37. [TestCase(null, false)]
  38. public void GetAndSetShowAfterLoad(bool actual, bool expected)
  39. {
  40. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  41. banner.ShowAfterLoad = actual;
  42. Assert.That(banner.ShowAfterLoad, Is.EqualTo(expected), "ShowAfterLoad did not return the correct value");
  43. }
  44. [Test]
  45. [TestCase("loadBanner")]
  46. [TestCase("")]
  47. [TestCase(null)]
  48. public void NativeBannerLoad(string placementId)
  49. {
  50. m_NativeBannerMock.Setup(x => x.Load(It.IsAny<string>(), It.IsAny<BannerLoadOptions>()));
  51. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  52. banner.Load(placementId, new BannerLoadOptions());
  53. m_NativeBannerMock.Verify(x => x.Load(It.IsAny<string>(), It.IsAny<BannerLoadOptions>()), Times.Once(), "Banner.Load() was not called as expected");
  54. }
  55. [Test]
  56. [TestCase("loadBanner")]
  57. [TestCase("")]
  58. [TestCase(null)]
  59. public void NativeBannerShow(string placementId)
  60. {
  61. m_NativeBannerMock.Setup(x => x.Show(It.IsAny<string>(), It.IsAny<BannerOptions>()));
  62. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  63. banner.Show(placementId, new BannerOptions());
  64. m_NativeBannerMock.Verify(x => x.Show(It.IsAny<string>(), It.IsAny<BannerOptions>()), Times.Once(), "Banner.Show() was not called as expected");
  65. }
  66. [Test]
  67. [TestCase(true)]
  68. [TestCase(false)]
  69. [TestCase(null)]
  70. public void NativeBannerHide(bool destroy)
  71. {
  72. m_NativeBannerMock.Setup(x => x.Hide(It.IsAny<bool>()));
  73. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  74. banner.Hide();
  75. m_NativeBannerMock.Verify(x => x.Hide(It.IsAny<bool>()), Times.Once(), "Banner.Hide() was not called as expected");
  76. }
  77. [Test]
  78. [TestCase(BannerPosition.CENTER)]
  79. [TestCase(BannerPosition.TOP_LEFT)]
  80. [TestCase(BannerPosition.TOP_RIGHT)]
  81. [TestCase(BannerPosition.TOP_CENTER)]
  82. [TestCase(BannerPosition.BOTTOM_LEFT)]
  83. [TestCase(BannerPosition.BOTTOM_RIGHT)]
  84. [TestCase(BannerPosition.BOTTOM_CENTER)]
  85. public void NativePlatformSetPosition(BannerPosition bannerPosition)
  86. {
  87. m_NativeBannerMock.Setup(x => x.SetPosition(It.IsAny<BannerPosition>()));
  88. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  89. banner.SetPosition(bannerPosition);
  90. m_NativeBannerMock.Verify(x => x.SetPosition(It.IsAny<BannerPosition>()), Times.Once(), "Banner.SetPosition() was not called as expected");
  91. }
  92. [UnityTest]
  93. [TestCase("randomPlacementId", ExpectedResult = null)]
  94. [Timeout(1000)]
  95. public IEnumerator UnityAdsBannerDidLoad(string expectedPlacementId)
  96. {
  97. var hasCalledListener = false;
  98. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  99. var bannerLoadOptions = new BannerLoadOptions();
  100. bannerLoadOptions.loadCallback += () => {
  101. hasCalledListener = true;
  102. };
  103. banner.UnityAdsBannerDidLoad(expectedPlacementId, bannerLoadOptions);
  104. while (!hasCalledListener) yield return null;
  105. Assert.That(hasCalledListener, Is.True, "The loadCallback should have been called");
  106. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Once(), "Calls should happen on the main thread and should all batched together as 1 call");
  107. }
  108. [UnityTest]
  109. [TestCase("randomPlacementId", ExpectedResult = null)]
  110. [Timeout(1000)]
  111. public IEnumerator UnityAdsBannerDidLoadNullOptions(string expectedPlacementId)
  112. {
  113. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  114. banner.UnityAdsBannerDidLoad(expectedPlacementId, null);
  115. yield return null;
  116. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Never(), "There should have been no calls to the coroutineExecutor");
  117. }
  118. [UnityTest]
  119. [TestCase("Some error message", ExpectedResult = null)]
  120. [Timeout(1000)]
  121. public IEnumerator UnityAdsBannerDidError(string expectedErrorMessage)
  122. {
  123. var hasCalledListener = false;
  124. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  125. var bannerLoadOptions = new BannerLoadOptions();
  126. bannerLoadOptions.errorCallback += (message) => {
  127. hasCalledListener = true;
  128. Assert.That(message, Is.EqualTo(expectedErrorMessage), "The error message should be: ");
  129. };
  130. banner.UnityAdsBannerDidError(expectedErrorMessage, bannerLoadOptions);
  131. while (!hasCalledListener) yield return null;
  132. Assert.That(hasCalledListener, Is.True, "The errorCallback should have been called");
  133. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Once(), "Calls should happen on the main thread and should all batched together as 1 call");
  134. }
  135. [UnityTest]
  136. [TestCase("Some error message", ExpectedResult = null)]
  137. [Timeout(1000)]
  138. public IEnumerator UnityAdsBannerDidErrorNullOptions(string expectedErrorMessage)
  139. {
  140. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  141. banner.UnityAdsBannerDidError(expectedErrorMessage, null);
  142. yield return null;
  143. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Never(), "There should have been no calls to the coroutineExecutor");
  144. }
  145. [UnityTest]
  146. [TestCase("randomPlacementId", ExpectedResult = null)]
  147. [Timeout(1000)]
  148. public IEnumerator UnityAdsBannerDidShow(string expectedPlacementId)
  149. {
  150. var hasCalledListener = false;
  151. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  152. var bannerLoadOptions = new BannerOptions();
  153. bannerLoadOptions.showCallback += () => {
  154. hasCalledListener = true;
  155. };
  156. banner.UnityAdsBannerDidShow(expectedPlacementId, bannerLoadOptions);
  157. while (!hasCalledListener) yield return null;
  158. Assert.That(hasCalledListener, Is.True, "The loadCallback should have been called");
  159. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Once(), "Calls should happen on the main thread and should all batched together as 1 call");
  160. }
  161. [UnityTest]
  162. [TestCase("randomPlacementId", ExpectedResult = null)]
  163. [Timeout(1000)]
  164. public IEnumerator UnityAdsBannerDidShowNullOptions(string expectedPlacementId)
  165. {
  166. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  167. banner.UnityAdsBannerDidShow(expectedPlacementId, null);
  168. yield return null;
  169. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Never(), "There should have been no calls to the coroutineExecutor");
  170. }
  171. [UnityTest]
  172. [TestCase("randomPlacementId", ExpectedResult = null)]
  173. [Timeout(1000)]
  174. public IEnumerator UnityAdsBannerClick(string expectedPlacementId)
  175. {
  176. var hasCalledListener = false;
  177. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  178. var bannerLoadOptions = new BannerOptions();
  179. bannerLoadOptions.clickCallback += () => {
  180. hasCalledListener = true;
  181. };
  182. banner.UnityAdsBannerClick(expectedPlacementId, bannerLoadOptions);
  183. while (!hasCalledListener) yield return null;
  184. Assert.That(hasCalledListener, Is.True, "The clickCallback should have been called");
  185. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Once(), "Calls should happen on the main thread and should all batched together as 1 call");
  186. }
  187. [UnityTest]
  188. [TestCase("randomPlacementId", ExpectedResult = null)]
  189. [Timeout(1000)]
  190. public IEnumerator UnityAdsBannerClickNullOptions(string expectedPlacementId)
  191. {
  192. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  193. banner.UnityAdsBannerClick(expectedPlacementId, null);
  194. yield return null;
  195. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Never(), "There should have been no calls to the coroutineExecutor");
  196. }
  197. [UnityTest]
  198. [TestCase("randomPlacementId", ExpectedResult = null)]
  199. [Timeout(1000)]
  200. public IEnumerator UnityAdsBannerDidHide(string expectedPlacementId)
  201. {
  202. var hasCalledListener = false;
  203. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  204. var bannerLoadOptions = new BannerOptions();
  205. bannerLoadOptions.hideCallback += () => {
  206. hasCalledListener = true;
  207. };
  208. banner.UnityAdsBannerDidHide(expectedPlacementId, bannerLoadOptions);
  209. while (!hasCalledListener) yield return null;
  210. Assert.That(hasCalledListener, Is.True, "The hideCallback should have been called");
  211. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Once(), "Calls should happen on the main thread and should all batched together as 1 call");
  212. }
  213. [UnityTest]
  214. [TestCase("randomPlacementId", ExpectedResult = null)]
  215. [Timeout(1000)]
  216. public IEnumerator UnityAdsBannerDidHideNullOptions(string expectedPlacementId)
  217. {
  218. var banner = new Banner(m_NativeBannerMock.Object, m_CoroutineExecutorMock.Object);
  219. banner.UnityAdsBannerDidHide(expectedPlacementId, null);
  220. yield return null;
  221. m_CoroutineExecutorMock.Verify(x => x.Post(It.IsAny<Action>()), Times.Never(), "There should have been no calls to the coroutineExecutor");
  222. }
  223. }
  224. }
  225. #endif