use of org.robolectric.shadows.ShadowLooper 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"));
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class ResultCodeTest method testInvalidSizeForBanner.
// @Test
// TODO: Activate this test after setting up test server
public void testInvalidSizeForBanner() {
PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
PrebidMobile.setPrebidServerAccountId("b7adad2c-e042-4126-8ca1-b3caac7d3e5c");
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(0, 250));
RequestParams requestParams = new RequestParams("e2edc23f-0b3b-4203-81b5-7cc97132f418", 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_SIZE, uuid);
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class DemandFetcherTest method testAutoRefreshForMoPubAdObject.
@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();
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runToEndOfTasks();
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();
bgLooper.runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(mockListener).onComplete(ResultCode.NO_BIDS);
assertNotSame(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
adViewKeywords = adView.getKeywords();
assertEquals("", adViewKeywords);
}
use of org.robolectric.shadows.ShadowLooper 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));
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class DemandFetcherTest method testSingleRequestOneBidResponseForMoPubAdObject.
@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();
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));
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);
}
Aggregations