Search in sources :

Example 1 with BackgroundThreadExecutor

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

the class AdUnitContentTest method testAdUnitUsingContentUrl.

@Test
public void testAdUnitUsingContentUrl() throws Exception {
    final String expectedContentUrl = "http://www.something.com/somewhere/here";
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("123456");
    final CompletableFuture<JSONObject> future = new CompletableFuture<>();
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            String postData = request.getBody().readUtf8();
            try {
                JSONObject jsonObject = new JSONObject(postData);
                future.complete(jsonObject);
            } catch (JSONException err) {
                Log.d("Error", err.toString());
                future.cancel(true);
            }
            return new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid());
        }
    });
    BannerAdUnit adUnit = new BannerAdUnit("123456", 320, 50);
    ContentObject contentObject = new ContentObject();
    contentObject.setUrl(expectedContentUrl);
    adUnit.setAppContent(contentObject);
    adUnit.fetchDemand(new OnCompleteListener2() {

        @Override
        public void onComplete(ResultCode resultCode, Map<String, String> unmodifiableMap) {
            System.out.println("I am in onComplete " + resultCode.toString());
            future.cancel(true);
        }
    });
    DemandFetcher fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
    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();
    JSONObject requestBody = future.get(10, TimeUnit.SECONDS);
    JSONObject app = requestBody.getJSONObject("app");
    assertTrue(app.has("content"));
    JSONObject content = app.getJSONObject("content");
    assertTrue(content.has("url"));
    assertEquals(expectedContentUrl, content.getString("url"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) JSONException(org.json.JSONException) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) Dispatcher(okhttp3.mockwebserver.Dispatcher) HttpUrl(okhttp3.HttpUrl) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 2 with BackgroundThreadExecutor

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

the class PrebidServerAdapterTest method testInvalidPrebidServerConfigIdForAppNexusHostedPrebidServer.

// @Test
// TODO: Activate this test after setting up test server
public void testInvalidPrebidServerConfigIdForAppNexusHostedPrebidServer() {
    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-ffffffffffff", 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 3 with BackgroundThreadExecutor

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

the class PrebidServerAdapterTest method testRubiconDefaultError.

@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);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onDemandFailed(ResultCode.PREBID_SERVER_ERROR, 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 4 with BackgroundThreadExecutor

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

the class PrebidServerAdapterTest method testSuccessfulBidResponseWithoutCacheId.

@Test
public void testSuccessfulBidResponseWithoutCacheId() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.invalidBidResponseWithoutCacheId()));
    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();
    verify(mockListener).onDemandFailed(ResultCode.NO_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 5 with BackgroundThreadExecutor

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

the class PrebidServerAdapterTest method testListenerMapping.

@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);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    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);
}
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)

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