@Override public void toggleAutoRefresh() { if (getMediaType().equals(MediaType.BANNER) && mAdFetcher.getState() == AdFetcher.STATE.STOPPED) { mAdFetcher.start(); } }
@Test public void testStartWithRefreshOff() { // Default state is auto refresh off, just start the ad fetcher assertEquals("Default state should be stopped", AdFetcher.STATE.STOPPED, adFetcher.getState()); adFetcher.start(); Lock.pause(1000); // added this so jenkins can have enough time to process assertEquals("Single request should be used.", AdFetcher.STATE.SINGLE_REQUEST, adFetcher.getState()); assertExpectedBGTasksAfterOneAdRequest(2); assertEquals("State should not be changed after request.", AdFetcher.STATE.SINGLE_REQUEST, adFetcher.getState()); }
@Test public void testDefaultState() { assertEquals(-1, adFetcher.getPeriod()); assertEquals(AdFetcher.STATE.STOPPED, adFetcher.getState()); }
@Test public void testStartWithRefreshOn() { // default state was stopped adFetcher.setPeriod(30000); adFetcher.start(); Lock.pause(1000); // added this so jenkins can have enough time to process // assert 3 here because a AAID async task assertExpectedBGTasksAfterOneAdRequest(2); assertEquals(AdFetcher.STATE.AUTO_REFRESH, adFetcher.getState()); // reset background scheduler, clear tasks for the refresh Lock.pause(30000 + 1000); // We wait for till autorefresh is triggered // in the following method, wait until next ad request is enqueued assertExpectedBGTasksAfterOneAdRequest(3); assertEquals(AdFetcher.STATE.AUTO_REFRESH, adFetcher.getState()); }
@Test public void testToggleAutoRefresh() throws Exception { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); ShadowCustomVideoWebView.simulateDelayedVideoError = true; executeBannerRequest(); assertCallbacks(true); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.STOPPED); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.AUTO_REFRESH); }
@Test public void testVideoImplementationAdLoadSuccess() throws Exception { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); executeBannerRequest(); assertCallbacks(true); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.STOPPED); }
@Test public void testVideoImplementationAdLoadFailure() throws Exception { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); ShadowCustomVideoWebView.simulateVideoError = true; executeBannerRequest(); assertCallbacks(false); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.AUTO_REFRESH); }
if (mAdFetcher.getState() == AdFetcher.STATE.AUTO_REFRESH) { mAdFetcher.stop();
@Test public void testStop() { if (adFetcher != null) { // not needed, but in case AdRequest is run server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); clearAAIDAsyncTasks(); // start an AdFetcher normally, until an AdRequest is queued adFetcher.start(); Lock.pause(1000); // added this so jenkins can have enough time to process assertExpectedBGTasksAfterOneAdRequest(1); assertNotSame(AdFetcher.STATE.STOPPED, adFetcher.getState()); adFetcher.stop(); // pause until a scheduler has a task in queue waitForTasks(); // Run the cancel command on AdRequest Robolectric.flushForegroundThreadScheduler(); // Run the pending AdRequest from start() -- should have been canceled while (Robolectric.getBackgroundThreadScheduler().areAnyRunnable()) { Robolectric.getBackgroundThreadScheduler().runOneTask(); } // A normally executed AdRequest will queue onPostExecute call to the UI thread, // but it should be canceled, and queue nothing int uiTaskCount = Robolectric.getForegroundThreadScheduler().size(); assertEquals(0, uiTaskCount); assertEquals(AdFetcher.STATE.STOPPED, adFetcher.getState()); } }
@Test public void testNotifyURLForVideo() throws Exception { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); executeUTRequest(); assertCallbacks(true); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.STOPPED); request = server.takeRequest(); // Discard the first request since its a HTTP Post for /ut/v3 ad request call request = server.takeRequest(); assertNotifyURL(request); }