Search in sources :

Example 56 with ShadowLooper

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

the class PrebidServerAdapterTest method testNoBidRubiconResponse.

@Test
public void testNoBidRubiconResponse() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidFromRubicon()));
    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);
    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 57 with ShadowLooper

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

the class PrebidServerAdapterTest method testBlankResponseReturnsNoBid.

@Test
public void testBlankResponseReturnsNoBid() {
    server.enqueue(new MockResponse().setResponseCode(204).setBody(""));
    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);
    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 58 with ShadowLooper

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

the class PrebidServerAdapterTest method testSuccessfulBidResponseWithoutCacheId3.

@Test
public void testSuccessfulBidResponseWithoutCacheId3() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.invalidBidResponseTopBidNoCacheId()));
    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 59 with ShadowLooper

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

the class ResultCodeTest method testNoBids.

@Test
public void testNoBids() 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.noBid()));
    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.runToEndOfTasks();
    ShadowLooper demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runToEndOfTasks();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(null, 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 60 with ShadowLooper

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

the class ResultCodeTest method testSuccessRubiconForDFP.

@Test
public void testSuccessRubiconForDFP() 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.oneBidFromRubicon()));
    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(16, bundle.size());
    String hb_pb = "hb_pb";
    assertTrue(bundle.containsKey(hb_pb));
    assertEquals("1.20", bundle.get(hb_pb));
    String hb_pb_rubicon = "hb_pb_rubicon";
    assertTrue(bundle.containsKey(hb_pb_rubicon));
    assertEquals("1.20", bundle.get(hb_pb_rubicon));
    String hb_bidder = "hb_bidder";
    assertTrue(bundle.containsKey(hb_bidder));
    assertEquals("rubicon", bundle.get(hb_bidder));
    String hb_bidder_rubicon = "hb_bidder_rubicon";
    assertTrue(bundle.containsKey(hb_bidder_rubicon));
    assertEquals("rubicon", bundle.get(hb_bidder_rubicon));
    String hb_cache_id = "hb_cache_id";
    assertTrue(bundle.containsKey(hb_cache_id));
    assertEquals("a2f41588-4727-425c-9ef0-3b382debef1e", bundle.get(hb_cache_id));
    String hb_cache_id_rubicon = "hb_cache_id_rubicon";
    assertTrue(bundle.containsKey(hb_cache_id_rubicon));
    assertEquals("a2f41588-4727-425c-9ef0-3b382debef1e", bundle.get(hb_cache_id_rubicon));
    String hb_env = "hb_env";
    assertTrue(bundle.containsKey(hb_env));
    assertEquals("mobile-app", bundle.get(hb_env));
    String hb_env_rubicon = "hb_env_rubicon";
    assertTrue(bundle.containsKey(hb_env_rubicon));
    assertEquals("mobile-app", bundle.get(hb_env_rubicon));
    String hb_size = "hb_size";
    assertTrue(bundle.containsKey(hb_size));
    assertEquals("300x250", bundle.get(hb_size));
    String hb_size_rubicon = "hb_size_rubicon";
    assertTrue(bundle.containsKey(hb_size_rubicon));
    assertEquals("300x250", bundle.get(hb_size_rubicon));
    String hb_cache_hostpath = "hb_cache_hostpath";
    assertTrue(bundle.containsKey(hb_cache_hostpath));
    assertEquals("https://prebid-cache-europe.rubiconproject.com/cache", bundle.get(hb_cache_hostpath));
    String hb_cache_hostpath_rubicon = "hb_cache_hostpath_rubicon";
    assertTrue(bundle.containsKey(hb_cache_hostpath_rubicon));
    assertEquals("https://prebid-cache-europe.rubiconproject.com/cache", bundle.get(hb_cache_hostpath_rubicon));
    String hb_cache_path = "hb_cache_path";
    assertTrue(bundle.containsKey(hb_cache_path));
    assertEquals("/cache", bundle.get(hb_cache_path));
    String hb_cache_path_rubicon = "hb_cache_path_rubicon";
    assertTrue(bundle.containsKey(hb_cache_path_rubicon));
    assertEquals("/cache", bundle.get(hb_cache_path_rubicon));
    String hb_cache_host_rubicon = "hb_cache_host_rubicon";
    assertTrue(bundle.containsKey(hb_cache_host_rubicon));
    assertEquals("prebid-cache-europe.rubiconproject.com", bundle.get(hb_cache_host_rubicon));
    String hb_cache_host = "hb_cache_host";
    assertTrue(bundle.containsKey(hb_cache_host));
    assertEquals("prebid-cache-europe.rubiconproject.com", bundle.get(hb_cache_host));
}
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

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