Home | History | Annotate | Download | only in addressinput
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      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
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package com.android.i18n.addressinput;
     18 
     19 import com.android.i18n.addressinput.testing.AsyncTestCase;
     20 
     21 import junit.framework.AssertionFailedError;
     22 
     23 import java.util.concurrent.TimeoutException;
     24 
     25 public class AsyncTestCaseTest extends AsyncTestCase {
     26 
     27   public void testSuccess() {
     28       delayTestFinish(1000);
     29       AsyncCallback.execute(500, new Runnable() {
     30           @Override
     31           public void run() {
     32               finishTest();
     33           }
     34       });
     35   }
     36 
     37   public void testFailure() {
     38       expectTimeout = true;
     39       delayTestFinish(1000);
     40       AsyncCallback.execute(1500, new Runnable() {
     41           @Override
     42           public void run() {
     43               finishTest();
     44           }
     45       });
     46   }
     47 
     48   @Override
     49   protected void runTest() throws Throwable {
     50       expectTimeout = false;
     51       try {
     52           super.runTest();
     53       } catch (TimeoutException e) {
     54           if (expectTimeout) {
     55               return;
     56           } else {
     57               throw e;
     58           }
     59       }
     60       if (expectTimeout) {
     61           throw new AssertionFailedError("Test case did not time out.");
     62       }
     63   }
     64 
     65   private boolean expectTimeout;
     66 
     67   /**
     68    * Helper class to perform an asynchronous callback after a specified delay.
     69    */
     70   private static class AsyncCallback extends Thread {
     71       public static void execute(long waitMillis, Runnable callback) {
     72           (new AsyncCallback(waitMillis, callback)).start();
     73       }
     74 
     75       @Override
     76       public void run() {
     77           try {
     78               synchronized (this) {
     79               wait(mWaitMillis);
     80           }
     81       } catch (InterruptedException e) {
     82           throw new RuntimeException(e);
     83       }
     84       mCallback.run();
     85     }
     86 
     87     private AsyncCallback(long waitMillis, Runnable callback) {
     88         this.mWaitMillis = waitMillis;
     89         this.mCallback = callback;
     90     }
     91 
     92     private long mWaitMillis;
     93     private Runnable mCallback;
     94   }
     95 }
     96