public class AsyncTestCaseTest extends AsyncTestCase {
public void testSuccess() {
- delayTestFinish(1000);
- AsyncCallback.execute(500, new Runnable() {
- @Override
- public void run() {
- finishTest();
- }
- });
+ delayTestFinish(1000);
+ AsyncCallback.execute(500, new Runnable() {
+ @Override
+ public void run() {
+ finishTest();
+ }
+ });
}
public void testFailure() {
- expectTimeout = true;
- delayTestFinish(1000);
- AsyncCallback.execute(1500, new Runnable() {
- @Override
- public void run() {
- finishTest();
- }
- });
+ expectTimeout = true;
+ delayTestFinish(1000);
+ AsyncCallback.execute(1500, new Runnable() {
+ @Override
+ public void run() {
+ finishTest();
+ }
+ });
}
@Override
protected void runTest() throws Throwable {
- expectTimeout = false;
- try {
- super.runTest();
- } catch (TimeoutException e) {
- if (expectTimeout) {
- return;
- } else {
- throw e;
- }
- }
+ expectTimeout = false;
+ try {
+ super.runTest();
+ } catch (TimeoutException e) {
if (expectTimeout) {
- throw new AssertionFailedError("Test case did not time out.");
+ return;
+ } else {
+ throw e;
}
+ }
+ if (expectTimeout) {
+ throw new AssertionFailedError("Test case did not time out.");
+ }
}
private boolean expectTimeout;
* Helper class to perform an asynchronous callback after a specified delay.
*/
private static class AsyncCallback extends Thread {
- public static void execute(long waitMillis, Runnable callback) {
- (new AsyncCallback(waitMillis, callback)).start();
- }
+ private long waitMillis;
+ private Runnable callback;
- @Override
- public void run() {
- try {
- synchronized (this) {
- wait(mWaitMillis);
- }
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- mCallback.run();
+ private AsyncCallback(long waitMillis, Runnable callback) {
+ this.waitMillis = waitMillis;
+ this.callback = callback;
}
- private AsyncCallback(long waitMillis, Runnable callback) {
- this.mWaitMillis = waitMillis;
- this.mCallback = callback;
+ public static void execute(long waitMillis, Runnable callback) {
+ (new AsyncCallback(waitMillis, callback)).start();
}
- private long mWaitMillis;
- private Runnable mCallback;
+ @Override
+ public void run() {
+ try {
+ synchronized (this) {
+ wait(this.waitMillis);
+ }
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ this.callback.run();
+ }
}
}