1 /* 2 * Copyright (C) 2007 The Android Open Source Project 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 android.os; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.os.Message; 22 23 public abstract class HandlerTester extends Thread { 24 public abstract void go(); 25 public abstract void handleMessage(Message msg); 26 27 public HandlerTester() { 28 } 29 30 public void doTest(long timeout) { 31 start(); 32 33 synchronized (this) { 34 try { 35 wait(timeout); 36 quit(); 37 } 38 catch (InterruptedException e) { 39 } 40 } 41 42 if (!mDone) { 43 throw new RuntimeException("test timed out"); 44 } 45 if (!mSuccess) { 46 throw new RuntimeException("test failed"); 47 } 48 } 49 50 public void success() { 51 mDone = true; 52 mSuccess = true; 53 } 54 55 public void failure() { 56 mDone = true; 57 mSuccess = false; 58 } 59 60 public void run() { 61 Looper.prepare(); 62 mLooper = Looper.myLooper(); 63 go(); 64 Looper.loop(); 65 } 66 67 protected class H extends Handler { 68 public void handleMessage(Message msg) { 69 synchronized (HandlerTester.this) { 70 // Call into them with our monitor locked, so they don't have 71 // to deal with other races. 72 HandlerTester.this.handleMessage(msg); 73 if (mDone) { 74 HandlerTester.this.notify(); 75 quit(); 76 } 77 } 78 } 79 } 80 81 private void quit() { 82 mLooper.quit(); 83 } 84 85 private boolean mDone = false; 86 private boolean mSuccess = false; 87 private Looper mLooper; 88 } 89 90