use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidNativeNativeTest method testSuccessfulPrebidNativeAdExpiry.
@Test
public void testSuccessfulPrebidNativeAdExpiry() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.validResponsePrebidNativeNative()));
HttpUrl hostUrl = server.url("/");
Host.CUSTOM.setHostUrl(hostUrl.toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
PrebidMobile.setPrebidServerAccountId(PBS_ACCOUNT_ID_APPNEXUS);
PrebidMobile.setShareGeoLocation(true);
PrebidMobile.setApplicationContext(activity.getApplicationContext());
DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
PrebidServerAdapter adapter = new PrebidServerAdapter();
RequestParams requestParams = new RequestParams(PBS_CONFIG_ID_NATIVE_APPNEXUS, AdType.NATIVE, null, new HashMap<String, Set<String>>(), new HashSet<String>(), null, null, null, null, null, new ArrayList<DataObject>());
requestParams.setNativeRequestParams(new NativeRequestParams());
String uuid = UUID.randomUUID().toString();
adapter.requestDemand(requestParams, mockListener, uuid);
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runOneTask();
bgLooper.runOneTask();
runAllMainThreadExecutorTasks();
Robolectric.getBackgroundThreadScheduler().runOneTask();
Robolectric.getBackgroundThreadScheduler().runOneTask();
String cacheId = "";
try {
Class clazz = Class.forName(CacheManager.class.getName());
Field field = clazz.getDeclaredField("savedValues");
field.setAccessible(true);
HashMap<String, String> map = (HashMap<String, String>) field.get(clazz);
for (String key : map.keySet()) {
cacheId = key;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
HashMap<String, String> bids = new HashMap<String, String>();
bids.put("hb_bidder", "appnexus");
bids.put("hb_bidder_appnexus", "appnexus");
bids.put("hb_cache_id", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
bids.put("hb_cache_id_appnexus", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
bids.put("hb_env", "mobile-app");
bids.put("hb_env_appnexus", "mobile-app");
bids.put("hb_pb", "0.00");
bids.put("hb_pb_appnexus", "0.00");
bids.put("hb_cache_host_appnex", "prebid.sin3.adnxs.com");
bids.put("hb_cache_path_appnex", "/pbc/v1/cache");
bids.put("hb_cache_path", "/pbc/v1/cache");
bids.put("hb_cache_host", "prebid.sin3.adnxs.com");
bids.put("hb_cache_id_local", cacheId);
verify(mockListener).onDemandReady(bids, uuid);
// Test findNative on responded cacheId local
NativeCustomTemplateAd nativeCustomTemplateAd = Mockito.mock(NativeCustomTemplateAd.class);
Mockito.when(nativeCustomTemplateAd.getText("isPrebid")).thenReturn("1");
Mockito.when(nativeCustomTemplateAd.getText("hb_cache_id_local")).thenReturn(cacheId);
AdViewUtils.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {
@Override
public void onPrebidNativeLoaded(PrebidNativeAd ad) {
assertTrue(ad != null);
View view = new View(activity);
view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
ad.registerPrebidNativeAdEventListener(new PrebidNativeAdEventListener() {
@Override
public void onAdClicked() {
fail();
}
@Override
public void onAdImpression() {
fail();
}
@Override
public void onAdExpired() {
adExpired = true;
}
});
assertFalse(adExpired);
Robolectric.getBackgroundThreadScheduler().advanceBy(302, TimeUnit.SECONDS);
Robolectric.getForegroundThreadScheduler().advanceBy(302, TimeUnit.SECONDS);
assertTrue(adExpired);
}
@Override
public void onPrebidNativeNotFound() {
fail();
}
@Override
public void onPrebidNativeNotValid() {
fail();
}
});
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidNativeNativeTest method testSuccessfulPrebidNativeAdClick.
@Test
public void testSuccessfulPrebidNativeAdClick() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.validResponsePrebidNativeNative()));
HttpUrl hostUrl = server.url("/");
Host.CUSTOM.setHostUrl(hostUrl.toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
PrebidMobile.setPrebidServerAccountId(PBS_ACCOUNT_ID_APPNEXUS);
PrebidMobile.setShareGeoLocation(true);
PrebidMobile.setApplicationContext(activity.getApplicationContext());
DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
PrebidServerAdapter adapter = new PrebidServerAdapter();
RequestParams requestParams = new RequestParams(PBS_CONFIG_ID_NATIVE_APPNEXUS, AdType.NATIVE, null, new HashMap<String, Set<String>>(), new HashSet<String>(), null, null, null, null, null, new ArrayList<DataObject>());
requestParams.setNativeRequestParams(new NativeRequestParams());
String uuid = UUID.randomUUID().toString();
adapter.requestDemand(requestParams, mockListener, uuid);
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runOneTask();
bgLooper.runOneTask();
runAllMainThreadExecutorTasks();
Robolectric.getBackgroundThreadScheduler().runOneTask();
Robolectric.getBackgroundThreadScheduler().runOneTask();
String cacheId = "";
try {
Class clazz = Class.forName(CacheManager.class.getName());
Field field = clazz.getDeclaredField("savedValues");
field.setAccessible(true);
HashMap<String, String> map = (HashMap<String, String>) field.get(clazz);
for (String key : map.keySet()) {
cacheId = key;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
HashMap<String, String> bids = new HashMap<String, String>();
bids.put("hb_bidder", "appnexus");
bids.put("hb_bidder_appnexus", "appnexus");
bids.put("hb_cache_id", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
bids.put("hb_cache_id_appnexus", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
bids.put("hb_env", "mobile-app");
bids.put("hb_env_appnexus", "mobile-app");
bids.put("hb_pb", "0.00");
bids.put("hb_pb_appnexus", "0.00");
bids.put("hb_cache_host_appnex", "prebid.sin3.adnxs.com");
bids.put("hb_cache_path_appnex", "/pbc/v1/cache");
bids.put("hb_cache_path", "/pbc/v1/cache");
bids.put("hb_cache_host", "prebid.sin3.adnxs.com");
bids.put("hb_cache_id_local", cacheId);
verify(mockListener).onDemandReady(bids, uuid);
// Test findNative on responded cacheId local
NativeCustomTemplateAd nativeCustomTemplateAd = Mockito.mock(NativeCustomTemplateAd.class);
Mockito.when(nativeCustomTemplateAd.getText("isPrebid")).thenReturn("1");
Mockito.when(nativeCustomTemplateAd.getText("hb_cache_id_local")).thenReturn(cacheId);
AdViewUtils.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {
@Override
public void onPrebidNativeLoaded(PrebidNativeAd ad) {
assertTrue(ad != null);
View view = new View(activity);
view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
ad.registerView(view, new PrebidNativeAdEventListener() {
@Override
public void onAdClicked() {
adClicked = true;
}
@Override
public void onAdImpression() {
fail();
}
@Override
public void onAdExpired() {
fail();
}
});
activity.addContentView(view, new ViewGroup.LayoutParams(200, 200));
view.setVisibility(View.VISIBLE);
assertFalse(adClicked);
view.performClick();
assertTrue(adClicked);
}
@Override
public void onPrebidNativeNotFound() {
fail();
}
@Override
public void onPrebidNativeNotValid() {
fail();
}
});
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidNativeNativeTest method runAllMainThreadExecutorTasks.
private void runAllMainThreadExecutorTasks() {
ShadowLooper mainLooper = Shadows.shadowOf(((MainThreadExecutor) TasksManager.getInstance().mainThreadExecutor).getMainExecutor().getLooper());
mainLooper.runToEndOfTasks();
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidNativeNativeTest method testSuccessfulPrebidNativeResponse.
@Test
public void testSuccessfulPrebidNativeResponse() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.validResponsePrebidNativeNative()));
HttpUrl hostUrl = server.url("/");
Host.CUSTOM.setHostUrl(hostUrl.toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
PrebidMobile.setPrebidServerAccountId(PBS_ACCOUNT_ID_APPNEXUS);
PrebidMobile.setShareGeoLocation(true);
PrebidMobile.setApplicationContext(activity.getApplicationContext());
DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
PrebidServerAdapter adapter = new PrebidServerAdapter();
RequestParams requestParams = new RequestParams(PBS_CONFIG_ID_NATIVE_APPNEXUS, AdType.NATIVE, null, new HashMap<String, Set<String>>(), new HashSet<String>(), null, null, null, null, null, new ArrayList<DataObject>());
requestParams.setNativeRequestParams(new NativeRequestParams());
String uuid = UUID.randomUUID().toString();
adapter.requestDemand(requestParams, mockListener, uuid);
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runOneTask();
bgLooper.runOneTask();
runAllMainThreadExecutorTasks();
Robolectric.getBackgroundThreadScheduler().runOneTask();
Robolectric.getBackgroundThreadScheduler().runOneTask();
String cacheId = "";
try {
Class clazz = Class.forName(CacheManager.class.getName());
Field field = clazz.getDeclaredField("savedValues");
field.setAccessible(true);
HashMap<String, String> map = (HashMap<String, String>) field.get(clazz);
for (String key : map.keySet()) {
cacheId = key;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
HashMap<String, String> bids = new HashMap<String, String>();
bids.put("hb_bidder", "appnexus");
bids.put("hb_bidder_appnexus", "appnexus");
bids.put("hb_cache_id", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
bids.put("hb_cache_id_appnexus", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
bids.put("hb_env", "mobile-app");
bids.put("hb_env_appnexus", "mobile-app");
bids.put("hb_pb", "0.00");
bids.put("hb_pb_appnexus", "0.00");
bids.put("hb_cache_host_appnex", "prebid.sin3.adnxs.com");
bids.put("hb_cache_path_appnex", "/pbc/v1/cache");
bids.put("hb_cache_path", "/pbc/v1/cache");
bids.put("hb_cache_host", "prebid.sin3.adnxs.com");
bids.put("hb_cache_id_local", cacheId);
verify(mockListener).onDemandReady(bids, uuid);
// Test findNative on responded cacheId local
NativeCustomTemplateAd nativeCustomTemplateAd = Mockito.mock(NativeCustomTemplateAd.class);
Mockito.when(nativeCustomTemplateAd.getText("isPrebid")).thenReturn("1");
Mockito.when(nativeCustomTemplateAd.getText("hb_cache_id_local")).thenReturn(cacheId);
AdViewUtils.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {
@Override
public void onPrebidNativeLoaded(PrebidNativeAd ad) {
assertTrue(ad != null);
}
@Override
public void onPrebidNativeNotFound() {
fail();
}
@Override
public void onPrebidNativeNotValid() {
fail();
}
});
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidServerAdapterTest method testInvalidPrebidServerIdSyntaxForAppNexusHostedPrebidServer.
// @Test
// TODO: Activate this test after setting up test server
public void testInvalidPrebidServerIdSyntaxForAppNexusHostedPrebidServer() {
PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
// invalid account id
PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888d");
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("6ace8c7d-88c0-4623-8117-75bc3f0a2e45", 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_ACCOUNT_ID, uuid);
}
Aggregations