use of org.robolectric.shadows.ShadowLooper in project robolectric by robolectric.
the class LooperModeConfigurerClassTest method shouldUseRealisticShadows.
@Test
@LooperMode(Mode.PAUSED)
public void shouldUseRealisticShadows() {
assertThat(ConfigurationRegistry.get(LooperMode.Mode.class)).isSameInstanceAs(Mode.PAUSED);
ShadowLooper looper = Shadow.extract(Looper.getMainLooper());
assertThat(looper).isInstanceOf(ShadowPausedLooper.class);
}
use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCallOnExceptionForExceptionInDoInBackground.
@Test
public void shouldCallOnExceptionForExceptionInDoInBackground() {
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
final boolean[] callRecord = new boolean[] { false, false, false, false };
final boolean[] correctAnswer = new boolean[] { true, false, true, true };
Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {
@Override
public void onPreCall() {
callRecord[0] = true;
}
@Override
public String doInBackground() throws Exception {
throw new Exception();
}
@Override
public void onSuccess(String result) {
callRecord[1] = true;
}
@Override
public void onException(Exception e) {
callRecord[2] = true;
}
@Override
public void onFinally() {
callRecord[3] = true;
}
});
// Run all the pending tasks on the ui thread
while (!callRecord[callRecord.length - 1]) looper.runToEndOfTasks();
assertThat(callRecord, equalTo(correctAnswer));
}
use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldHaveCorrectStackTrace.
@Test
public void shouldHaveCorrectStackTrace() {
final Exception[] exception = { null };
final StackTraceElement[] here;
final StackTraceElement[][] there = new StackTraceElement[][] { null };
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
Ln.setLoggingLevel(Log.DEBUG);
try {
throw new UnsupportedOperationException();
} catch (UnsupportedOperationException e) {
here = e.getStackTrace();
}
Executors.newSingleThreadExecutor(new MyThreadFactory(new Thread[] { null })).submit(new AndroidCallable<String>() {
@Override
public String doInBackground() throws Exception {
try {
throw new NullPointerException();
} catch (NullPointerException e) {
there[0] = e.getStackTrace();
throw e;
}
}
@Override
public void onException(Exception e) {
exception[0] = e;
}
@Override
public void onSuccess(String result) {
}
}, false);
// Run all the pending tasks on the ui thread
while (exception[0] == null) looper.runToEndOfTasks();
assertThat(exception[0].getStackTrace().length, equalTo(here.length + there[0].length + 3));
int i = 0;
while (i < there[0].length) {
assertThat(exception[0].getStackTrace()[i], equalTo(there[0][i]));
++i;
}
for (// skip 3 frames due to differences in where we got our stacktrace from
int j = i + 3, k = 0; // skip 3 frames due to differences in where we got our stacktrace from
k < here.length; // skip 3 frames due to differences in where we got our stacktrace from
++k, // skip 3 frames due to differences in where we got our stacktrace from
++j) // line numbers may be off
assertThat(exception[0].getStackTrace()[j].getFileName(), equalTo(here[k].getFileName()));
}
use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCrashAppForExceptionInOnFinally.
@Test(expected = RuntimeException.class)
public void shouldCrashAppForExceptionInOnFinally() {
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
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() {
throw new RuntimeException();
}
});
// noinspection InfiniteLoopStatement
while (true) looper.runToEndOfTasks();
}
use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCallMethodsUsingProperThreadsWithException.
@Test
public void shouldCallMethodsUsingProperThreadsWithException() throws Exception {
final Thread fgThread = Thread.currentThread();
final Thread[] bgThread = { null };
final Thread[] answers = new Thread[5];
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
Executors.newSingleThreadExecutor(new MyThreadFactory(bgThread)).submit(new StringAndroidCallable(answers, true));
// Run all the pending tasks on the ui thread
while (answers[answers.length - 1] == null) looper.runToEndOfTasks();
final Thread[] correctAnswer = new Thread[] { fgThread, bgThread[0], fgThread, null, fgThread };
assertThat(answers, equalTo(correctAnswer));
}
Aggregations