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

The following examples show how to use org.robolectric.Robolectric#flushBackgroundThreadScheduler() . 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: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoBidResponse() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    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("67890", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.NO_BIDS, uuid);
}
 
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: ANOMIDNativeViewabilityTests.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testOmidNativeJSEventsRTB() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.anOMIDNativeRTB()));
    adRequest.loadAd();
    Lock.pause(1000);
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.getForegroundThreadScheduler().runOneTask();
    assertAdLoaded(true);
    assertVerificationScriptResourceRTB();
    assertANOMIDAdSessionPresent();
    attachNativeAdToViewAndRegisterTracking();
    assertOMIDSessionStartRTB();
    NativeAdSDK.unRegisterTracking(dummyNativeView);
    Lock.pause(1000);
    waitForTasks();
    Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
    assertOMIDSessionFinish();
}
 
Example 4
Source File: ANMultiAdRequestLoadTests.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testMARSuccessAdUnitNoBidWithInterstitialRequest() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockServerResponses.marSuccessAdUnitNoBid()));
    assertFalse(marCompleted);
    executeMARRequest();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertTrue(marCompleted);
    assertCallbacks(false);
    assertBannerAdResponse(false);
    assertInterstitialAdResponse(false);
    //Load Banner
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner()));
    executeInterstitialRequest();
    assertFalse(marCompleted);
    assertFalse(marFailed);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertCallbacks(true);
    assertInterstitialAdResponse(true);
    assertBannerAdResponse(false);
}
 
Example 5
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPrebidServerConfigIdForRubiconHostedPrebidServer() {
    server.enqueue(new MockResponse().setResponseCode(400).setBody(MockPrebidServerResponses.invalidConfigIdFromRubicon()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);

    PrebidMobile.setPrebidServerAccountId("1001");
    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("1001-1_INVALID_CONFIG_ID", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    verify(mockListener).onDemandFailed(ResultCode.INVALID_CONFIG_ID, uuid);
}
 
Example 6
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 7
Source File: VideoAdFriendlyObstructionTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeVideoRequest() {
    videoAd.loadAd();
    Lock.pause(1000);
    waitForTasks();
    Robolectric.flushForegroundThreadScheduler();
    Robolectric.flushBackgroundThreadScheduler();

    waitForTasks();
    Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
    Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
}
 
Example 8
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testListenerMapping() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener1 = mock(DemandAdapter.DemandAdapterListener.class);
    DemandAdapter.DemandAdapterListener mockListener2 = 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 uuid1 = UUID.randomUUID().toString();
    String uuid2 = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener1, uuid1);
    adapter.requestDemand(requestParams, mockListener2, uuid2);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener1).onDemandFailed(ResultCode.NO_BIDS, uuid1);
    verify(mockListener1, never()).onDemandFailed(ResultCode.NO_BIDS, uuid2);
    verify(mockListener2).onDemandFailed(ResultCode.NO_BIDS, uuid2);
    verify(mockListener2, never()).onDemandFailed(ResultCode.NO_BIDS, uuid1);
}
 
Example 9
Source File: MediatedSSMAdViewControllerTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeUTRequest() {
    requestManager.execute();
    // execute main ad request
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

    Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
    Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
}
 
Example 10
Source File: MediatedSSMAdViewControllerTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeSSMRequest(){
    // Execute the SSM Request
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();

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

    Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
    Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
}
 
Example 11
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleRequestNoBidsResponse() throws Exception {
    HttpUrl httpUrl = server.url("/");
    PrebidMobile.setApplicationContext(activity);
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest request = builder.build();
    DemandFetcher demandFetcher = new DemandFetcher(request);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    demandFetcher.setPeriodMillis(0);
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("12345", AdType.BANNER, sizes);
    demandFetcher.setRequestParams(requestParams);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    demandFetcher.setListener(mockListener);
    assertEquals(DemandFetcher.STATE.STOPPED, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.start();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    ShadowLooper fetcherLooper = Shadows.shadowOf(demandFetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = Shadows.shadowOf(demandFetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
}
 
Example 12
Source File: ANMultiAdRequestLoadTests.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeInterstitialRequest() {
    reset();

    interstitialAdView.loadAd();

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

    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
}
 
Example 13
Source File: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateTimeoutMillis2() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidResponseNoTmax()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidResponseTmaxTooLarge()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    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("67890", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertEquals("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), 2000, PrebidMobile.getTimeoutMillis());
    assertTrue(!PrebidMobile.timeoutMillisUpdated);
    adapter.requestDemand(requestParams, mockListener, uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertEquals("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), 2000, PrebidMobile.getTimeoutMillis());
    assertTrue(PrebidMobile.timeoutMillisUpdated);
}
 
Example 14
Source File: TestANClickThroughAction.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void executeBannerRequest() {
        bannerAdView.loadAd();

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

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

//        Robolectric.getBackgroundThreadScheduler().advanceToNextPostedRunnable();
//        Robolectric.getForegroundThreadScheduler().advanceToNextPostedRunnable();
    }
 
Example 15
Source File: AdFetcherTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
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());
}
 
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: MediatedInterstitialAdViewControllerTest.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttp404ErrorResponseFromFailure() {
    String[] classNames = {"MediatedInterstitialNoFillView"};
    String[] responseURLs = {"http://wiki221random.devnxs.net/"};
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.waterfall_CSM_Banner_Interstitial(classNames, responseURLs)));
    server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank()));

    executeUTRequest();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertCallbacks(false);
    assertFalse(interstitialAdView.isReady());
}
 
Example 18
Source File: UTAdRequestTest.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
private void clearAAIDAsyncTasks() {
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
}
 
Example 19
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);
    }
 
Example 20
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testAutoRefreshForMoPubAdObject() throws Exception {
    HttpUrl httpUrl = server.url("/");
    PrebidMobile.setApplicationContext(activity);
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    demandFetcher.setPeriodMillis(2000);
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("12345", AdType.BANNER, sizes);
    demandFetcher.setRequestParams(requestParams);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    demandFetcher.setListener(mockListener);
    assertEquals(DemandFetcher.STATE.STOPPED, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.start();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    ShadowLooper fetcherLooper = Shadows.shadowOf(demandFetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = Shadows.shadowOf(demandFetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    assertEquals("hb_pb:0.50,hb_env:mobile-app,hb_pb_appnexus:0.50,hb_size:300x250,hb_bidder_appnexus:appnexus,hb_bidder:appnexus,hb_cache_id:df4aba04-5e69-44b8-8608-058ab21600b8,hb_env_appnexus:mobile-app,hb_size_appnexus:300x250,hb_cache_id_appnexus:df4aba04-5e69-44b8-8608-058ab21600b8,", adViewKeywords);
    fetcherLooper.runOneTask();
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    adViewKeywords = adView.getKeywords();
    assertEquals("", adViewKeywords);
}