Java Code Examples for org.robolectric.Robolectric#flushForegroundThreadScheduler()

The following examples show how to use org.robolectric.Robolectric#flushForegroundThreadScheduler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdListenerTest.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBannerNativeAdLoaded() {
    bannerAdView.setAutoRefreshInterval(30000);
    bannerAdView.setLoadsInBackground(false);
    bannerAdView.setOpensNativeBrowser(false);
    bannerAdView.setClickThroughAction(ANClickThroughAction.RETURN_URL);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.anNativeWithoutImages()));
    Assert.assertEquals(AdType.UNKNOWN, bannerAdView.getAdType());
    requestManager = new AdViewRequestManager(bannerAdView);
    requestManager.execute();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    Assert.assertEquals(30000, bannerAdView.getAutoRefreshInterval());
    Assert.assertEquals(AdType.NATIVE, bannerAdView.getAdType());
    assertCallbacks(true);
    assertOpensInNativeBrowser();
    assertLoadsInBackground();
    assertClickThroughAction();
    assertClickThroughAction(ANClickThroughAction.RETURN_URL);
}
 
Example 2
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateTimeoutMillis() {
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    assertEquals(2000, PrebidMobile.getTimeoutMillis());
    assertFalse(PrebidMobile.timeoutMillisUpdated);
    PrebidMobile.setPrebidServerAccountId("b7adad2c-e042-4126-8ca1-b3caac7d3e5c");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("e2edc23f-0b3b-4203-81b5-7cc97132f418", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.NO_BIDS, uuid);
    assertTrue("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), PrebidMobile.getTimeoutMillis() <= 2000 && PrebidMobile.getTimeoutMillis() > 700);
    assertTrue(PrebidMobile.timeoutMillisUpdated);
}
 
Example 3
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPrebidServerAccountIdForAppNexusHostedPrebidServer() {
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-ffffffffffff");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(320, 50));
    RequestParams requestParams = new RequestParams("6ace8c7d-88c0-4623-8117-75bc3f0a2e45", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.INVALID_ACCOUNT_ID, uuid);
}
 
Example 4
Source File: AdListenerTest.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testloadLazyAdAfterAdLoad() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner()));
    assertTrue(bannerAdView.enableLazyLoad());
    executeBannerRequest();
    assertLazyLoadCallbackInProgress();
    assertTrue(bannerAdView.loadLazyAd());
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertLazyLoadCallbackSuccess();
    adLoaded = false;
    adFailed = false;
    assertFalse(bannerAdView.loadLazyAd());
    assertFalse(adLoaded);
    assertFalse(adFailed);
}
 
Example 5
Source File: BannerAdViewVideoLoadAdTest.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
private void executeBannerRequest() {
        bannerAdView.setAutoRefreshInterval(15);
        bannerAdView.loadAdOffscreen();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

//        Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
//        Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();

//        ShadowLooper shadowLooper = shadowOf(getMainLooper());
//        if (!shadowLooper.isIdle()) {
//            shadowLooper.idle();
//        }
//        RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable();
    }
 
Example 6
Source File: ANMultiAdRequestLoadTests.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testMARSuccessAdUnitNoBidWithBannerRequest() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockServerResponses.marSuccessAdUnitNoBid()));
    assertFalse(marCompleted);
    executeMARRequest();
    assertTrue(marCompleted);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertCallbacks(false);
    assertBannerAdResponse(false);
    assertInterstitialAdResponse(false);
    //Load Banner
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner()));
    executeBannerRequest();
    assertFalse(marCompleted);
    assertFalse(marFailed);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertCallbacks(true);
    assertBannerAdResponse(true);
    assertInterstitialAdResponse(false);
}
 
Example 7
Source File: VideoAdTest.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetVideoOrientationPortrait() throws Exception {
    ShadowCustomWebView.aspectRatio = "0.5625"; // 9:16
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestUTResponses.video()));

    videoAd.loadAd();
    Lock.pause(1000);
    waitForTasks();
    Robolectric.flushForegroundThreadScheduler();
    Robolectric.flushBackgroundThreadScheduler();

    waitForTasks();
    Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
    Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
    assertAdLoaded(true);
    Clog.w(TestUtil.testLogTag, "VideoAdTest videoAd.getVideoOrientation()" +videoAd.getVideoOrientation());
    assertTrue(videoAd.getVideoOrientation().equals(VideoOrientation.PORTRAIT));
}
 
Example 8
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testRubiconDefaultError() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.htmlUnreachableFromRubicon()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(320, 50));
    RequestParams requestParams = new RequestParams("67890", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.PREBID_SERVER_ERROR, uuid);
}
 
Example 9
Source File: ANMultiAdRequestLoadTests.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
private void executeBannerRequest() {
        reset();

        bannerAdView.setAutoRefreshInterval(15000);
        bannerAdView.loadAd();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

//        Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
//        Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
    }
 
Example 10
Source File: VideoAdTest.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetVastXML() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestUTResponses.video()));

    videoAd.loadAd();
    Lock.pause(1000);
    waitForTasks();
    Robolectric.flushForegroundThreadScheduler();
    Robolectric.flushBackgroundThreadScheduler();

    waitForTasks();
    Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
    Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
    assertAdLoaded(true);
    Clog.w(TestUtil.testLogTag, "VideoAdTest videoAd.getVastXML()" +videoAd.getVastXML());
    assertTrue(videoAd.getVastXML() != "");
}
 
Example 11
Source File: BannerAdViewLoadAdTests.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeBannerRequest() {
        bannerAdView.setAutoRefreshInterval(15000);
        bannerAdView.loadAdOffscreen();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

//        Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
//        Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
    }
 
Example 12
Source File: TestANClickThroughAction.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeVideoRequest() {
    videoAd.loadAd();

    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
}
 
Example 13
Source File: ANMultiAdRequestLoadTests.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeMARRequest(ANMultiAdRequest anMultiAdRequest) {
//        AdViewRequestManager requestManager = new AdViewRequestManager(anMultiAdRequest);
//        requestManager.execute();
        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

        anMultiAdRequest.load();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();

        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();


//        ShadowLooper.runUiThreadTasks();
//        ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
//        ShadowLooper.idleMainLooper();
//        ShadowLooper.idleMainLooperConstantly(true);
//        ShadowLooper.shadowMainLooper().quitUnchecked();
//        ShadowLooper shadowLooper = shadowOf(getMainLooper());
//        if (!shadowLooper.isIdle()) {
//            shadowLooper.idle();
//        }
//        RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable();
    }
 
Example 14
Source File: TestUtil.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static public void execAllAsyncTask(){
    Scheduler bgTask = Robolectric.getBackgroundThreadScheduler();
    Scheduler fgTask = Robolectric.getForegroundThreadScheduler();

    while (fgTask.size()!=0 || bgTask.size()!=0){
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 15
Source File: AdFetcherTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 16
Source File: TestANClickThroughAction.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeInterstitialRequest() {
    interstitialAdView.loadAd();

    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
}
 
Example 17
Source File: AdListenerTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testLazyBannerAdLoaded() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner()));
    bannerAdView.enableLazyLoad();
    executeBannerRequest();
    assertLazyLoadCallbackInProgress();
    bannerAdView.loadLazyAd();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertLazyLoadCallbackSuccess();
}
 
Example 18
Source File: WebviewUtilTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void test1CookiesSync() {
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Set-Cookie", TestResponsesUT.UUID_COOKIE_1).setBody(TestResponsesUT.banner()));
    requestManager.execute();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    String wvcookie = WebviewUtil.getCookie();
    //Asserts the Cookie stored in the device is the same as that of the one we sent back in the response.
    assertEquals(getUUId2(wvcookie), getUUId2(TestResponsesUT.UUID_COOKIE_1));
}
 
Example 19
Source File: MediatedBannerAdViewControllerTest.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
private void executeUTRequest() {
    requestManager.execute();
    // execute main ad request
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
}
 
Example 20
Source File: MediatedInterstitialAdViewControllerTest.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
private void executeAndAssertResponseURL(int positionInQueue, ResultCode errorCode, boolean checkLatency) {
//        waitForTasks();
        Robolectric.flushBackgroundThreadScheduler();
        Robolectric.flushForegroundThreadScheduler();
        assertResponseURL(positionInQueue, errorCode, checkLatency);
    }