Java Code Examples for org.robolectric.shadows.ShadowLooper#runOneTask()

The following examples show how to use org.robolectric.shadows.ShadowLooper#runOneTask() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@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();
    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());

}
 
Example 2
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@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.runOneTask();
    ShadowLooper demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(null, testView.getKeywords());
}
 
Example 3
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@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();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);

    assertNull(testView.getKeywords());
}
 
Example 4
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBaseConditions() throws Exception {
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest request = builder.build();
    DemandFetcher demandFetcher = new DemandFetcher(request);
    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);
    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();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    demandFetcher.destroy();
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
}
 
Example 5
Source File: BackgroundLayoutLooperRule.java    From litho with Apache License 2.0 5 votes vote down vote up
public LayoutLooperThread(
    final ShadowLooper layoutLooper, final BlockingQueue<Message> messages) {
  super(
      new Runnable() {
        @Override
        public void run() {
          boolean keepGoing = true;
          while (keepGoing) {
            final Message message;
            try {
              message = messages.take();
            } catch (InterruptedException e) {
              throw new RuntimeException(e);
            }
            switch (message.mMessageType) {
              case DRAIN_ONE:
                layoutLooper.runOneTask();
                message.mSemaphore.release();
                break;
              case DRAIN_ALL:
                layoutLooper.runToEndOfTasks();
                message.mSemaphore.release();
                break;
              case QUIT:
                keepGoing = false;
                break;
            }
          }
        }
      });
}
 
Example 6
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@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()));
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest 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();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
}
 
Example 7
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@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()));
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest 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();
    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);
}
 
Example 8
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleRequestOneBidResponseForMoPubAdObject() 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()));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    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();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    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,", adViewKeywords);
}
 
Example 9
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleRequestOneBidRubiconResponseForMoPubAdObject() 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()));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    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();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    assertEquals("hb_env:mobile-app,hb_cache_hostpath:https://prebid-cache-europe.rubiconproject.com/cache,hb_size_rubicon:300x250,hb_cache_id:a2f41588-4727-425c-9ef0-3b382debef1e,hb_cache_path_rubicon:/cache,hb_cache_host_rubicon:prebid-cache-europe.rubiconproject.com,hb_pb:1.20,hb_pb_rubicon:1.20,hb_cache_id_rubicon:a2f41588-4727-425c-9ef0-3b382debef1e,hb_cache_path:/cache,hb_size:300x250,hb_cache_hostpath_rubicon:https://prebid-cache-europe.rubiconproject.com/cache,hb_env_rubicon:mobile-app,hb_bidder:rubicon,hb_bidder_rubicon:rubicon,hb_cache_host:prebid-cache-europe.rubiconproject.com,", adViewKeywords);
}
 
Example 10
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@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);
    PublisherAdRequest testRequest = new PublisherAdRequest.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();
    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"));
}
 
Example 11
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@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);
    PublisherAdRequest testRequest = new PublisherAdRequest.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();
    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));
}
 
Example 12
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@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()));
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest 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();
    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"));
}
 
Example 13
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@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()));
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest 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();
    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));
}
 
Example 14
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testAutoRefreshForMoPubAdObject() 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("{}"));
    MoPubView adView = new MoPubView(activity);
    adView.setAdUnitId("123456789");
    DemandFetcher demandFetcher = new DemandFetcher(adView);
    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();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.SUCCESS);
    assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    String adViewKeywords = adView.getKeywords();
    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,", adViewKeywords);
    fetcherLooper.runOneTask();
    demandLooper.runOneTask();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener).onComplete(ResultCode.NO_BIDS);
    assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
    adViewKeywords = adView.getKeywords();
    assertEquals("", adViewKeywords);
}
 
Example 15
Source File: DemandFetcherTest.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
@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("{}"));
    PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
    PublisherAdRequest 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();
    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();
    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());
}