Search in sources :

Example 6 with AdManagerAdRequest

use of com.google.android.gms.ads.admanager.AdManagerAdRequest in project prebid-mobile-android by prebid.

the class DemandFetcherTest method testSingleRequestNoBidsResponse.

@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()));
    AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    AdManagerAdRequest 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();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with AdManagerAdRequest

use of com.google.android.gms.ads.admanager.AdManagerAdRequest in project prebid-mobile-android by prebid.

the class DemandFetcherTest method testSingleRequestOneBidRubiconResponseForDFPAdObject.

@Test
public void testSingleRequestOneBidRubiconResponseForDFPAdObject() 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.oneBidFromRubicon()));
    AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    AdManagerAdRequest 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();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    Bundle bundle = request.getCustomTargeting();
    Assert.assertEquals(16, bundle.size());
    String hb_pb = "hb_pb";
    Assert.assertTrue(bundle.containsKey(hb_pb));
    Assert.assertEquals("1.20", bundle.get(hb_pb));
    String hb_pb_rubicon = "hb_pb_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_pb_rubicon));
    Assert.assertEquals("1.20", bundle.get(hb_pb_rubicon));
    String hb_bidder = "hb_bidder";
    Assert.assertTrue(bundle.containsKey(hb_bidder));
    Assert.assertEquals("rubicon", bundle.get(hb_bidder));
    String hb_bidder_rubicon = "hb_bidder_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_bidder_rubicon));
    Assert.assertEquals("rubicon", bundle.get(hb_bidder_rubicon));
    String hb_cache_id = "hb_cache_id";
    Assert.assertTrue(bundle.containsKey(hb_cache_id));
    Assert.assertEquals("a2f41588-4727-425c-9ef0-3b382debef1e", bundle.get(hb_cache_id));
    String hb_cache_id_rubicon = "hb_cache_id_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_cache_id_rubicon));
    Assert.assertEquals("a2f41588-4727-425c-9ef0-3b382debef1e", bundle.get(hb_cache_id_rubicon));
    String hb_env = "hb_env";
    Assert.assertTrue(bundle.containsKey(hb_env));
    Assert.assertEquals("mobile-app", bundle.get(hb_env));
    String hb_env_rubicon = "hb_env_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_env_rubicon));
    Assert.assertEquals("mobile-app", bundle.get(hb_env_rubicon));
    String hb_size = "hb_size";
    Assert.assertTrue(bundle.containsKey(hb_size));
    Assert.assertEquals("300x250", bundle.get(hb_size));
    String hb_size_rubicon = "hb_size_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_size_rubicon));
    Assert.assertEquals("300x250", bundle.get(hb_size_rubicon));
    String hb_cache_hostpath = "hb_cache_hostpath";
    Assert.assertTrue(bundle.containsKey(hb_cache_hostpath));
    Assert.assertEquals("https://prebid-cache-europe.rubiconproject.com/cache", bundle.get(hb_cache_hostpath));
    String hb_cache_hostpath_rubicon = "hb_cache_hostpath_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_cache_hostpath_rubicon));
    Assert.assertEquals("https://prebid-cache-europe.rubiconproject.com/cache", bundle.get(hb_cache_hostpath_rubicon));
    String hb_cache_path = "hb_cache_path";
    Assert.assertTrue(bundle.containsKey(hb_cache_path));
    Assert.assertEquals("/cache", bundle.get(hb_cache_path));
    String hb_cache_path_rubicon = "hb_cache_path_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_cache_path_rubicon));
    Assert.assertEquals("/cache", bundle.get(hb_cache_path_rubicon));
    String hb_cache_host = "hb_cache_host";
    Assert.assertTrue(bundle.containsKey(hb_cache_host));
    Assert.assertEquals("prebid-cache-europe.rubiconproject.com", bundle.get(hb_cache_host));
    String hb_cache_host_rubicon = "hb_cache_host_rubicon";
    Assert.assertTrue(bundle.containsKey(hb_cache_host_rubicon));
    Assert.assertEquals("prebid-cache-europe.rubiconproject.com", bundle.get(hb_cache_host_rubicon));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Bundle(android.os.Bundle) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with AdManagerAdRequest

use of com.google.android.gms.ads.admanager.AdManagerAdRequest in project prebid-mobile-android by prebid.

the class DemandFetcherTest method testDestroyAutoRefresh.

@Test
public void testDestroyAutoRefresh() 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()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid()));
    AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    AdManagerAdRequest request = builder.build();
    DemandFetcher demandFetcher = new DemandFetcher(request);
    PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
    demandFetcher.setPeriodMillis(30);
    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();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.destroy();
    assertTrue(!Robolectric.getForegroundThreadScheduler().areAnyRunnable());
    assertTrue(!Robolectric.getBackgroundThreadScheduler().areAnyRunnable());
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    verify(mockListener, Mockito.times(1)).onComplete(ResultCode.NO_BIDS);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with AdManagerAdRequest

use of com.google.android.gms.ads.admanager.AdManagerAdRequest in project prebid-mobile-android by prebid.

the class DemandFetcherTest method testSingleRequestOneBidResponseForDFPAdObject.

@Test
public void testSingleRequestOneBidResponseForDFPAdObject() 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()));
    AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    AdManagerAdRequest 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();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    Bundle bundle = request.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) HttpUrl(okhttp3.HttpUrl) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with AdManagerAdRequest

use of com.google.android.gms.ads.admanager.AdManagerAdRequest in project prebid-mobile-android by prebid.

the class DemandFetcherTest method testAutoRefreshForDFPAdObject.

@Test
public void testAutoRefreshForDFPAdObject() 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("{}"));
    AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    AdManagerAdRequest request = builder.build();
    DemandFetcher demandFetcher = new DemandFetcher(request);
    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();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    Bundle bundle = request.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"));
    fetcherLooper.runOneTask();
    demandLooper.runOneTask();
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    bundle = request.getCustomTargeting();
    assertEquals(0, bundle.size());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Bundle(android.os.Bundle) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) HttpUrl(okhttp3.HttpUrl) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AdManagerAdRequest (com.google.android.gms.ads.admanager.AdManagerAdRequest)29 Test (org.junit.Test)17 OnCompleteListener (org.prebid.mobile.OnCompleteListener)13 ResultCode (org.prebid.mobile.ResultCode)13 Bundle (android.os.Bundle)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 HttpUrl (okhttp3.HttpUrl)8 BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)8 ShadowLooper (org.robolectric.shadows.ShadowLooper)8 NonNull (androidx.annotation.NonNull)7 FrameLayout (android.widget.FrameLayout)6 AdManagerAdView (com.google.android.gms.ads.admanager.AdManagerAdView)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 BannerAdUnit (org.prebid.mobile.BannerAdUnit)6 AdListener (com.google.android.gms.ads.AdListener)5 LoadAdError (com.google.android.gms.ads.LoadAdError)5 AdSize (com.google.android.gms.ads.AdSize)4 PbFindSizeError (org.prebid.mobile.addendum.PbFindSizeError)4 AlertDialog (androidx.appcompat.app.AlertDialog)3