Search in sources :

Example 26 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class PrebidServerAdapterTest method testInvalidPrebidServerIdSyntaxForAppNexusHostedPrebidServer2.

// @Test
// TODO: Activate this test after setting up test server
public void testInvalidPrebidServerIdSyntaxForAppNexusHostedPrebidServer2() {
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0");
    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-75bc3f0a2e", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.INVALID_CONFIG_ID, uuid);
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 27 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class PrebidServerAdapterTest method testSuccessfulBidResponse.

@Test
public void testSuccessfulBidResponse() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
    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);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    HashMap<String, String> bids = new HashMap<String, String>();
    bids.put("hb_bidder", "appnexus");
    bids.put("hb_bidder_appnexus", "appnexus");
    bids.put("hb_cache_id", "df4aba04-5e69-44b8-8608-058ab21600b8");
    bids.put("hb_cache_id_appnexus", "df4aba04-5e69-44b8-8608-058ab21600b8");
    bids.put("hb_env", "mobile-app");
    bids.put("hb_env_appnexus", "mobile-app");
    bids.put("hb_pb", "0.50");
    bids.put("hb_pb_appnexus", "0.50");
    bids.put("hb_size", "300x250");
    bids.put("hb_size_appnexus", "300x250");
    verify(mockListener).onDemandReady(bids, uuid);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 28 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class PrebidServerAdapterTest method testUserKeywordInPostData.

@Test
public void testUserKeywordInPostData() throws Exception {
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            if (request.getPath().equals("/withKeywords")) {
                String postData = request.getBody().readUtf8();
                errorCollector.checkThat("Post data does not contain key values: " + postData, postData, containsString("value2,value1"));
            } else if (request.getPath().equals("/clearKeywords")) {
                String postData = request.getBody().readUtf8();
                errorCollector.checkThat("Post data should not contain key values: " + postData, postData, not(containsString("value2,value1")));
            }
            return new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid());
        }
    });
    HttpUrl httpUrl = server.url("/withKeywords");
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("123456");
    BannerAdUnit adUnit = new BannerAdUnit("123456", 300, 250);
    TargetingParams.addUserKeyword("value1");
    TargetingParams.addUserKeyword("value2");
    TargetingParams.addUserKeyword("value1");
    MoPubView testView = new MoPubView(activity);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListener);
    DemandFetcher fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    ShadowLooper fetcherLooper = shadowOf(fetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    Host.CUSTOM.setHostUrl(server.url("/clearKeywords").toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    TargetingParams.clearUserKeywords();
    OnCompleteListener mockListenerNoKV = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListenerNoKV);
    fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    fetcherLooper = shadowOf(fetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener, times(1)).onComplete(ResultCode.NO_BIDS);
    verify(mockListenerNoKV, times(1)).onComplete(ResultCode.NO_BIDS);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Dispatcher(okhttp3.mockwebserver.Dispatcher) HttpUrl(okhttp3.HttpUrl) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 29 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class ResultCodeTest method testNoBidsRubicon.

@Test
public void testNoBidsRubicon() throws Exception {
    HttpUrl httpUrl = server.url("/");
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("123456");
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidFromRubicon()));
    BannerAdUnit adUnit = new BannerAdUnit("123456", 300, 250);
    MoPubView testView = new MoPubView(activity);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListener);
    DemandFetcher fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    ShadowLooper fetcherLooper = shadowOf(fetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertNull(testView.getKeywords());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 30 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class ResultCodeTest method testSuccessForMoPub.

@Test
public void testSuccessForMoPub() throws Exception {
    HttpUrl httpUrl = server.url("/");
    Host.CUSTOM.setHostUrl(httpUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("123456");
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
    BannerAdUnit adUnit = new BannerAdUnit("123456", 300, 250);
    MoPubView testView = new MoPubView(activity);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListener);
    DemandFetcher fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    ShadowLooper fetcherLooper = shadowOf(fetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    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,", testView.getKeywords());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Aggregations

ShadowLooper (org.robolectric.shadows.ShadowLooper)66 Test (org.junit.Test)56 BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)42 HttpUrl (okhttp3.HttpUrl)35 MockResponse (okhttp3.mockwebserver.MockResponse)35 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)23 HashSet (java.util.HashSet)10 AdManagerAdRequest (com.google.android.gms.ads.admanager.AdManagerAdRequest)8 MoPubView (com.mopub.mobileads.MoPubView)7 Bundle (android.os.Bundle)5 Handler (android.os.Handler)4 MediaCodec (android.media.MediaCodec)3 Looper (android.os.Looper)3 Message (android.os.Message)3 NativeCustomTemplateAd (com.google.android.gms.ads.formats.NativeCustomTemplateAd)3 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)3 Scheduler (io.reactivex.rxjava3.core.Scheduler)3 Field (java.lang.reflect.Field)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3