diff --git a/sdk/test/com/appnexus/opensdk/AdFetcherTest.java b/sdk/test/com/appnexus/opensdk/AdFetcherTest.java index f6b4f1175..f3729a7ee 100644 --- a/sdk/test/com/appnexus/opensdk/AdFetcherTest.java +++ b/sdk/test/com/appnexus/opensdk/AdFetcherTest.java @@ -87,6 +87,7 @@ public void testRequestExecutorForBackgroundTasks() { SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); adFetcher.start(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); diff --git a/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java b/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java index 6c4cae9a7..228c6d8c5 100644 --- a/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java +++ b/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java @@ -104,6 +104,7 @@ public void testRequestExecutorForBackgroundTasks() { assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); adRequest = new UTAdRequest(this); adRequest.execute(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); diff --git a/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java b/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java index 5840be6dc..1616365d1 100644 --- a/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java +++ b/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java @@ -29,12 +29,16 @@ import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLog; +import org.robolectric.shadows.ShadowLooper; +import static android.os.Looper.getMainLooper; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotSame; import static junit.framework.Assert.assertTrue; +import static org.robolectric.Shadows.shadowOf; /** * This tests AdViewRequestManager @@ -87,6 +91,18 @@ public void testNotifyURLForVideo() throws Exception { request = server.takeRequest(); // Discard the first request since its a HTTP Post for /ut/v3 ad request call request = server.takeRequest(); + + waitForTasks(); + // execute main ad request + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + ShadowLooper shadowLooper = shadowOf(getMainLooper()); + if (!shadowLooper.isIdle()) { + shadowLooper.idle(); + } + RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); + assertNotifyURL(request); } @@ -102,6 +118,12 @@ private void executeUTRequest() { // execute main ad request Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); + + ShadowLooper shadowLooper = shadowOf(getMainLooper()); + if (!shadowLooper.isIdle()) { + shadowLooper.idle(); + } + RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); } diff --git a/sdk/test/com/appnexus/opensdk/BannerAdViewLoadAdTests.java b/sdk/test/com/appnexus/opensdk/BannerAdViewLoadAdTests.java index 2f79741b7..540cc2307 100644 --- a/sdk/test/com/appnexus/opensdk/BannerAdViewLoadAdTests.java +++ b/sdk/test/com/appnexus/opensdk/BannerAdViewLoadAdTests.java @@ -61,6 +61,7 @@ public void testRequestExecutorForBackgroundTasks() { SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); bannerAdView.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); diff --git a/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java b/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java index d5853f64d..1d023a2e2 100644 --- a/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java +++ b/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java @@ -56,6 +56,7 @@ public void testRequestExecutorForBackgroundTasks() { SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); bannerAdView.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); diff --git a/sdk/test/com/appnexus/opensdk/BaseNativeTest.java b/sdk/test/com/appnexus/opensdk/BaseNativeTest.java index 98da0141b..a46bd246d 100644 --- a/sdk/test/com/appnexus/opensdk/BaseNativeTest.java +++ b/sdk/test/com/appnexus/opensdk/BaseNativeTest.java @@ -27,6 +27,7 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; @RunWith(RobolectricTestRunner.class) @Config(sdk = 21) @@ -35,6 +36,8 @@ public class BaseNativeTest extends BaseRoboTest implements NativeAdRequestListe protected NativeAdResponse response; protected boolean adLoaded, adFailed; + NativeAdResponse nativeAdResponse; + ResultCode failErrorCode; @Override @@ -43,6 +46,8 @@ public void setup() { adLoaded = false; adFailed = false; + nativeAdResponse = null; + failErrorCode = null; adRequest = new NativeAdRequest(activity, "0"); adRequest.setListener(this); @@ -64,6 +69,7 @@ public void assertCallbacks(boolean success) { @Override public void onAdLoaded(NativeAdResponse response) { adLoaded = true; + nativeAdResponse = response; this.response = response; Clog.w(TestUtil.testLogTag, "BaseNativeTest onAdLoaded"); } @@ -71,6 +77,7 @@ public void onAdLoaded(NativeAdResponse response) { @Override public void onAdFailed(ResultCode errorcode) { adFailed = true; + failErrorCode = errorcode; Clog.w(TestUtil.testLogTag, "BaseNativeTest onAdFailed"); } diff --git a/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java b/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java index fb641c4c2..5eec468ce 100644 --- a/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java +++ b/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java @@ -28,10 +28,13 @@ import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowActivity; import org.robolectric.shadows.ShadowLog; +import org.robolectric.shadows.ShadowLooper; +import static android.os.Looper.getMainLooper; import static com.appnexus.opensdk.AdActivity.ACTIVITY_TYPE_INTERSTITIAL; import static com.appnexus.opensdk.AdActivity.INTENT_KEY_ACTIVITY_TYPE; import static com.appnexus.opensdk.InterstitialAdView.INTENT_KEY_AUTODISMISS_DELAY; @@ -116,5 +119,15 @@ private void executeInterstitialRequest() { waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + +// ShadowLooper shadowLooper = shadowOf(getMainLooper()); +// if (!shadowLooper.isIdle()) { +// shadowLooper.idle(); +// } +// RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); } } diff --git a/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java b/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java index f06ba0f80..209c90a70 100644 --- a/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java +++ b/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java @@ -84,7 +84,7 @@ private void assertResponseURL(int curRequestPositionInQueue, ResultCode errorCo sanitizer.setAllowUnregisteredParamaters(true); sanitizer.parseUrl(response_url); - int reasonVal = Integer.parseInt(sanitizer.getValue("reason")); + int reasonVal = Integer.parseInt(sanitizer.getValue("reason").replace("_HTTP/1.1", "")); assertEquals(reasonVal, errorCode.ordinal()); @@ -125,11 +125,19 @@ private RecordedRequest takeNoAdURLRequestFromQueue(int position){ private void executeUTRequest() { requestManager.execute(); // execute main ad request + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); } private void executeAndAssertResponseURL(int positionInQueue, ResultCode errorCode,boolean checkLatency) { + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); @@ -137,6 +145,10 @@ private void executeAndAssertResponseURL(int positionInQueue, ResultCode errorCo } private void executeAndAssertNoAdURL(int positionInQueue) { + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); @@ -167,7 +179,9 @@ private void executeSSMRequest(){ Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); - + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); } // Verify that a successful mediation response, diff --git a/sdk/test/com/appnexus/opensdk/NativeRequestTest.java b/sdk/test/com/appnexus/opensdk/NativeRequestTest.java index edf3955d4..b85efbb21 100644 --- a/sdk/test/com/appnexus/opensdk/NativeRequestTest.java +++ b/sdk/test/com/appnexus/opensdk/NativeRequestTest.java @@ -40,7 +40,9 @@ import java.util.HashMap; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -108,8 +110,10 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio HttpUrl impbus = server.url("/"); UTConstants.REQUEST_BASE_URL_UT = impbus.toString(); NativeAdRequestListener listener = new NativeAdRequestListener() { + @Override public void onAdLoaded(NativeAdResponse response) { + nativeAdResponse = response; ((MockFBNativeBannerAdResponse) response).logImpression(); ((MockFBNativeBannerAdResponse) response).clickAd(); } @@ -118,15 +122,16 @@ public void onAdLoaded(NativeAdResponse response) { public void onAdFailed(ResultCode errorcode) { } }; - NativeAdRequestListener listenerSpy = spy(listener); - adRequest.setListener(listenerSpy); + adRequest.setListener(listener); adRequest.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); - verify(listenerSpy).onAdLoaded(any(MockFBNativeBannerAdResponse.class)); + assertNotNull(nativeAdResponse); + assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); waitForTasks(); @@ -140,57 +145,74 @@ public void onAdFailed(ResultCode errorcode) { @Test public void requestNativeGetsMediationNoFillThenCSR() { server.enqueue(new MockResponse().setBody(TestResponsesUT.mediationNoFillThenCSRSuccessfull()).setResponseCode(200)); - NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); - adRequest.setListener(adListener); +// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); + adRequest.setListener(this); adRequest.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); - verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class)); + assertNotNull(nativeAdResponse); + assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse); +// verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class)); } @Test public void requestNativeGetsCSRNofillThenMediation() { server.enqueue(new MockResponse().setBody(TestResponsesUT.csrNoFillThenMediationSuccessfull()).setResponseCode(200)); - NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); - adRequest.setListener(adListener); +// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); + adRequest.setListener(this); + assertNull(nativeAdResponse); adRequest.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); - verify(adListener).onAdLoaded(any(BaseNativeAdResponse.class)); + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + assertNotNull(nativeAdResponse); + assertTrue(nativeAdResponse instanceof BaseNativeAdResponse); +// verify(adListener).onAdLoaded(any(BaseNativeAdResponse.class)); } @Test public void requestNativeCSRNofill() { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.csrNativeNofill())); - NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); - adRequest.setListener(adListener); +// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); + adRequest.setListener(this); adRequest.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); - verify(adListener).onAdFailed(ResultCode.UNABLE_TO_FILL); + assertNull(nativeAdResponse); + assertAdFailed(true); + assertEquals(ResultCode.UNABLE_TO_FILL, failErrorCode); +// verify(adListener).onAdFailed(ResultCode.UNABLE_TO_FILL); } @Test public void requestNativeCSRSuccessful() { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.csrNativeSuccessful())); - NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); - adRequest.setListener(adListener); +// NativeAdRequestListener adListener = mock(NativeAdRequestListener.class); + adRequest.setListener(this); adRequest.loadAd(); + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); - verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class)); + assertNotNull(nativeAdResponse); + assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse); +// verify(adListener).onAdLoaded(any(MockFBNativeBannerAdResponse.class)); } @Test @@ -247,4 +269,4 @@ public void tearDown() { response.destroy(); } } -} +} \ No newline at end of file diff --git a/sdk/test/com/appnexus/opensdk/viewability/ANOMIDNativeViewabilityTests.java b/sdk/test/com/appnexus/opensdk/viewability/ANOMIDNativeViewabilityTests.java index 9a1495dab..d8e913734 100644 --- a/sdk/test/com/appnexus/opensdk/viewability/ANOMIDNativeViewabilityTests.java +++ b/sdk/test/com/appnexus/opensdk/viewability/ANOMIDNativeViewabilityTests.java @@ -105,6 +105,10 @@ public void testRequestExecutorForBackgroundTasks() { SDKSettings.setExternalExecutor(MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); assertNotSame(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); adRequest.loadAd(); + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks());