2 * Copyright (C) 2010 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.i18n.addressinput.testing;
19 import junit.framework.TestCase;
21 import java.util.concurrent.TimeoutException;
24 * An extension of TestCase that provides delayTestFinish() and finishTest() methods that behave
25 * like the corresponding methods in GWTTestCase for testing asynchronous code.
27 public abstract class AsyncTestCase extends TestCase {
29 * Tracks whether this test is completely done.
31 private boolean testIsFinished;
34 * The system time in milliseconds when the test should time out.
36 private long testTimeoutMillis;
39 * Puts the current test in asynchronous mode.
41 * @param timeoutMillis time to wait before failing the test for timing out
43 protected void delayTestFinish(int timeoutMillis) {
44 testTimeoutMillis = System.currentTimeMillis() + timeoutMillis;
48 * Causes this test to succeed during asynchronous mode.
50 protected void finishTest() {
51 testIsFinished = true;
58 protected void runTest() throws Throwable {
59 testIsFinished = false;
60 testTimeoutMillis = 0;
63 if (testTimeoutMillis > 0) {
64 long timeoutMillis = testTimeoutMillis - System.currentTimeMillis();
65 if (timeoutMillis > 0) {
70 if (!testIsFinished) {
71 throw new TimeoutException("Waited " + timeoutMillis + " ms!");