use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCrashAppForExceptionInOnSuccess.
@Test(expected = RuntimeException.class)
public void shouldCrashAppForExceptionInOnSuccess() {
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
final boolean[] callRecord = new boolean[] { false };
Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {
@Override
public String doInBackground() throws Exception {
return "";
}
@Override
public void onSuccess(String result) {
throw new RuntimeException();
}
@Override
public void onException(Exception e) {
}
@Override
public void onFinally() {
callRecord[0] = true;
}
});
// Run all the pending tasks on the ui thread
while (!callRecord[callRecord.length - 1]) looper.runToEndOfTasks();
}
use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCrashAppForExceptionInOnException.
@Test(expected = RuntimeException.class)
public void shouldCrashAppForExceptionInOnException() {
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
final boolean[] callRecord = new boolean[] { false };
Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {
@Override
public String doInBackground() throws Exception {
throw new Exception();
}
@Override
public void onSuccess(String result) {
}
@Override
public void onException(Exception e) {
throw new RuntimeException();
}
@Override
public void onFinally() {
callRecord[0] = true;
}
});
// Run all the pending tasks on the ui thread
while (!callRecord[callRecord.length - 1]) looper.runToEndOfTasks();
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class DemandFetcherTest method testBaseConditions.
@Test
public void testBaseConditions() throws Exception {
AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
AdManagerAdRequest 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();
ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
demandFetcher.destroy();
assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidServerAdapterTest method testConnectionHasSetCustomHeaders.
@Test
public void testConnectionHasSetCustomHeaders() throws Exception {
server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
HttpUrl hostUrl = server.url("/");
Host.CUSTOM.setHostUrl(hostUrl.toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
PrebidMobile.setPrebidServerAccountId("12345");
PrebidMobile.setShareGeoLocation(true);
PrebidMobile.setApplicationContext(activity.getApplicationContext());
HashMap<String, String> customHeaders = new HashMap<>();
customHeaders.put("test-key", "test-value");
PrebidMobile.setCustomHeaders(customHeaders);
DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
PrebidServerAdapter adapter = new PrebidServerAdapter();
HashSet<AdSize> sizes = new HashSet<>();
sizes.add(new AdSize(300, 250));
RequestParams requestParams = new RequestParams("67890", 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();
RecordedRequest recordedRequest = server.takeRequest();
Headers headers = recordedRequest.getHeaders();
assertEquals("test-value", headers.get("test-key"));
}
use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.
the class PrebidServerAdapterTest method testUpdateTimeoutMillis2.
@Test
public void testUpdateTimeoutMillis2() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidResponseNoTmax()));
server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidResponseTmaxTooLarge()));
HttpUrl hostUrl = server.url("/");
Host.CUSTOM.setHostUrl(hostUrl.toString());
PrebidMobile.setPrebidServerHost(Host.CUSTOM);
PrebidMobile.setPrebidServerAccountId("12345");
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(300, 250));
RequestParams requestParams = new RequestParams("67890", 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();
assertEquals("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), 2000, PrebidMobile.getTimeoutMillis());
assertTrue(!PrebidMobile.timeoutMillisUpdated);
adapter.requestDemand(requestParams, mockListener, uuid);
bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
bgLooper.runToEndOfTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertEquals("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), 2000, PrebidMobile.getTimeoutMillis());
assertTrue(PrebidMobile.timeoutMillisUpdated);
}
Aggregations