Search in sources :

Example 11 with BackgroundThreadExecutor

use of org.prebid.mobile.tasksmanager.BackgroundThreadExecutor 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 12 with BackgroundThreadExecutor

use of org.prebid.mobile.tasksmanager.BackgroundThreadExecutor 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 13 with BackgroundThreadExecutor

use of org.prebid.mobile.tasksmanager.BackgroundThreadExecutor 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 14 with BackgroundThreadExecutor

use of org.prebid.mobile.tasksmanager.BackgroundThreadExecutor 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)

Example 15 with BackgroundThreadExecutor

use of org.prebid.mobile.tasksmanager.BackgroundThreadExecutor in project prebid-mobile-android by prebid.

the class ResultCodeTest method testSuccessForDFP.

@Test
public void testSuccessForDFP() 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);
    AdManagerAdRequest testRequest = new AdManagerAdRequest.Builder().build();
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testRequest, 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);
    Bundle bundle = testRequest.getCustomTargeting();
    assertEquals(10, bundle.size());
    assertTrue(bundle.containsKey("hb_pb"));
    assertEquals("0.50", bundle.get("hb_pb"));
    assertTrue(bundle.containsKey("hb_bidder"));
    assertEquals("appnexus", bundle.get("hb_bidder"));
    assertTrue(bundle.containsKey("hb_bidder_appnexus"));
    assertEquals("appnexus", bundle.get("hb_bidder_appnexus"));
    assertTrue(bundle.containsKey("hb_cache_id"));
    assertEquals("df4aba04-5e69-44b8-8608-058ab21600b8", bundle.get("hb_cache_id"));
    assertTrue(bundle.containsKey("hb_cache_id_appnexus"));
    assertEquals("df4aba04-5e69-44b8-8608-058ab21600b8", bundle.get("hb_cache_id_appnexus"));
    assertTrue(bundle.containsKey("hb_env"));
    assertEquals("mobile-app", bundle.get("hb_env"));
    assertTrue(bundle.containsKey("hb_env_appnexus"));
    assertEquals("mobile-app", bundle.get("hb_env_appnexus"));
    assertTrue(bundle.containsKey("hb_pb_appnexus"));
    assertEquals("0.50", bundle.get("hb_pb_appnexus"));
    assertTrue(bundle.containsKey("hb_size"));
    assertEquals("300x250", bundle.get("hb_size"));
    assertTrue(bundle.containsKey("hb_size_appnexus"));
    assertEquals("300x250", bundle.get("hb_size_appnexus"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Bundle(android.os.Bundle) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Aggregations

BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)42 ShadowLooper (org.robolectric.shadows.ShadowLooper)42 Test (org.junit.Test)36 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 NativeCustomTemplateAd (com.google.android.gms.ads.formats.NativeCustomTemplateAd)3 Field (java.lang.reflect.Field)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 View (android.view.View)2 Dispatcher (okhttp3.mockwebserver.Dispatcher)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 Headers (okhttp3.Headers)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1