use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.
the class PrebidServerAdapterTest method testNativeAdUnitInPostData.
@Test
public void testNativeAdUnitInPostData() throws Exception {
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
String postData = request.getBody().readUtf8();
try {
JSONObject jsonObject = new JSONObject(postData);
JSONArray jsonArrayImp = jsonObject.getJSONArray("imp");
JSONObject jsonObjectImp = (JSONObject) jsonArrayImp.get(0);
JSONObject nativeObject = (JSONObject) jsonObjectImp.get("native");
JSONObject nativeRequest = new JSONObject((String) nativeObject.get("request"));
JSONArray eventtrackersArray = nativeRequest.getJSONArray("eventtrackers");
JSONObject eventtracker = (JSONObject) eventtrackersArray.get(0);
JSONArray eventtrackerMethodsArray = eventtracker.getJSONArray("methods");
JSONObject ext = (JSONObject) nativeRequest.get("ext");
JSONArray assets = nativeRequest.getJSONArray("assets");
JSONObject asset1 = (JSONObject) assets.get(0);
JSONObject assetTitle = (JSONObject) asset1.get("title");
JSONObject assetImage = (JSONObject) asset1.get("img");
JSONObject asset2 = (JSONObject) assets.get(1);
JSONObject assetImage2 = (JSONObject) asset2.get("img");
JSONObject assetData = (JSONObject) asset2.get("data");
assertEquals(2, nativeRequest.getInt("context"));
assertEquals(20, nativeRequest.getInt("contextsubtype"));
assertEquals(4, nativeRequest.getInt("plcmttype"));
assertEquals(10, nativeRequest.getInt("plcmtcnt"));
assertEquals(12, nativeRequest.getInt("seq"));
assertEquals(1, nativeRequest.getInt("aurlsupport"));
assertEquals(1, nativeRequest.getInt("durlsupport"));
assertEquals(1, eventtracker.getInt("event"));
assertEquals(1, eventtrackerMethodsArray.get(0));
assertEquals(2, eventtrackerMethodsArray.get(1));
assertEquals(1, nativeRequest.getInt("privacy"));
assertEquals("value", ext.get("key"));
assertEquals(90, assetTitle.getInt("len"));
assertEquals(1, assetTitle.getInt("required"));
assertEquals(1, assetImage.getInt("type"));
assertEquals(20, assetImage.getInt("wmin"));
assertEquals(20, assetImage.getInt("hmin"));
assertEquals(1, asset1.getInt("required"));
assertEquals(3, assetImage2.getInt("type"));
assertEquals(200, assetImage2.getInt("wmin"));
assertEquals(200, assetImage2.getInt("hmin"));
assertEquals(1, asset2.getInt("required"));
assertEquals(1, assetData.getInt("type"));
assertEquals(90, assetData.getInt("len"));
assertEquals(1, assetData.getInt("required"));
} catch (JSONException err) {
Log.d("Error", err.toString());
}
return new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus());
}
});
PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
PrebidMobile.setApplicationContext(activity.getApplicationContext());
PrebidMobile.setPrebidServerAccountId("123456");
NativeAdUnit nativeAdUnit = new NativeAdUnit("123456");
nativeAdUnit.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC);
nativeAdUnit.setPlacementType(NativeAdUnit.PLACEMENTTYPE.RECOMMENDATION_WIDGET);
nativeAdUnit.setPlacementCount(10);
nativeAdUnit.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL);
nativeAdUnit.setPrivacy(true);
nativeAdUnit.setDUrlSupport(true);
nativeAdUnit.setAUrlSupport(true);
nativeAdUnit.setSeq(12);
JSONObject ext = new JSONObject();
try {
ext.put("key", "value");
} catch (JSONException e) {
e.printStackTrace();
}
nativeAdUnit.setExt(ext);
ArrayList<NativeEventTracker.EVENT_TRACKING_METHOD> methods = new ArrayList<>();
methods.add(NativeEventTracker.EVENT_TRACKING_METHOD.IMAGE);
methods.add(NativeEventTracker.EVENT_TRACKING_METHOD.JS);
try {
NativeEventTracker tracker = new NativeEventTracker(NativeEventTracker.EVENT_TYPE.IMPRESSION, methods);
nativeAdUnit.addEventTracker(tracker);
} catch (Exception e) {
e.printStackTrace();
}
NativeTitleAsset title = new NativeTitleAsset();
title.setLength(90);
title.setRequired(true);
nativeAdUnit.addAsset(title);
NativeImageAsset icon = new NativeImageAsset();
icon.setImageType(NativeImageAsset.IMAGE_TYPE.ICON);
icon.setWMin(20);
icon.setHMin(20);
icon.setRequired(true);
nativeAdUnit.addAsset(icon);
NativeImageAsset image = new NativeImageAsset();
image.setImageType(NativeImageAsset.IMAGE_TYPE.MAIN);
image.setHMin(200);
image.setWMin(200);
image.setRequired(true);
nativeAdUnit.addAsset(image);
NativeDataAsset data = new NativeDataAsset();
data.setLen(90);
data.setDataType(NativeDataAsset.DATA_TYPE.SPONSORED);
data.setRequired(true);
nativeAdUnit.addAsset(data);
nativeAdUnit.setAutoRefreshPeriodMillis(30000);
MoPubView testView = new MoPubView(activity);
OnCompleteListener mockListener = mock(OnCompleteListener.class);
nativeAdUnit.fetchDemand(testView, mockListener);
}
use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.
the class PrebidServerAdapterTest method testUserKeywordInPostData.
@Test
public void testUserKeywordInPostData() throws Exception {
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/withKeywords")) {
String postData = request.getBody().readUtf8();
errorCollector.checkThat("Post data does not contain key values: " + postData, postData, containsString("value2,value1"));
} else if (request.getPath().equals("/clearKeywords")) {
String postData = request.getBody().readUtf8();
errorCollector.checkThat("Post data should not contain key values: " + postData, postData, not(containsString("value2,value1")));
}
return new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBid());
}
});
HttpUrl httpUrl = server.url("/withKeywords");
Host.CUSTOM.setHostUrl(httpUrl.toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
PrebidMobile.setApplicationContext(activity.getApplicationContext());
PrebidMobile.setPrebidServerAccountId("123456");
BannerAdUnit adUnit = new BannerAdUnit("123456", 300, 250);
TargetingParams.addUserKeyword("value1");
TargetingParams.addUserKeyword("value2");
TargetingParams.addUserKeyword("value1");
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();
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
Host.CUSTOM.setHostUrl(server.url("/clearKeywords").toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
TargetingParams.clearUserKeywords();
OnCompleteListener mockListenerNoKV = mock(OnCompleteListener.class);
adUnit.fetchDemand(testView, mockListenerNoKV);
fetcher = (DemandFetcher) FieldUtils.readField(adUnit, "fetcher", true);
PrebidMobile.setTimeoutMillis(Integer.MAX_VALUE);
fetcherLooper = shadowOf(fetcher.getHandler().getLooper());
fetcherLooper.runOneTask();
demandLooper = shadowOf(fetcher.getDemandHandler().getLooper());
demandLooper.runOneTask();
bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(mockListener, times(1)).onComplete(ResultCode.NO_BIDS);
verify(mockListenerNoKV, times(1)).onComplete(ResultCode.NO_BIDS);
}
use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.
the class ResultCodeTest method testNoBidsRubicon.
@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();
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
verify(mockListener).onComplete(ResultCode.NO_BIDS);
assertNull(testView.getKeywords());
}
use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.
the class ResultCodeTest method testNoNegativeSizeForBanner.
@Test
public void testNoNegativeSizeForBanner() {
PrebidMobile.setPrebidServerAccountId("123456");
BannerAdUnit adUnit = new BannerAdUnit("123456", 320, 50);
adUnit.addAdditionalSize(-1, 250);
MoPubView testView = new MoPubView(activity);
OnCompleteListener mockListener = mock(OnCompleteListener.class);
adUnit.fetchDemand(testView, mockListener);
verify(mockListener).onComplete(ResultCode.INVALID_SIZE);
}
use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.
the class ResultCodeTest method testEmptyAccountId.
@Test
public void testEmptyAccountId() throws Exception {
PrebidMobile.setPrebidServerAccountId("");
BannerAdUnit adUnit = new BannerAdUnit("123456", 320, 50);
MoPubView testView = new MoPubView(activity);
OnCompleteListener mockListener = mock(OnCompleteListener.class);
adUnit.fetchDemand(testView, mockListener);
verify(mockListener).onComplete(ResultCode.INVALID_ACCOUNT_ID);
}
Aggregations