1 /*
2  * Copyright (C) 2009 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 package com.android.compatibility.common.util;
17 
18 /**
19  * Thread class for executing a Runnable containing assertions in a separate thread.
20  * Uncaught exceptions in the Runnable are rethrown in the context of the the thread
21  * calling the <code>runTest()</code> method.
22  */
23 public final class TestThread extends Thread {
24     private Throwable mThrowable;
25     private Runnable mTarget;
26 
TestThread(Runnable target)27     public TestThread(Runnable target) {
28         mTarget = target;
29     }
30 
31     @Override
run()32     public final void run() {
33         try {
34             mTarget.run();
35         } catch (Throwable t) {
36             mThrowable = t;
37         }
38     }
39 
40     /**
41      * Run the target Runnable object and wait until the test finish or throw
42      * out Exception if test fail.
43      *
44      * @param runTime
45      * @throws Throwable
46      */
runTest(long runTime)47     public void runTest(long runTime) throws Throwable {
48         start();
49         joinAndCheck(runTime);
50     }
51 
52     /**
53      * Get the Throwable object which is thrown when test running
54      * @return  The Throwable object
55      */
getThrowable()56     public Throwable getThrowable() {
57         return mThrowable;
58     }
59 
60     /**
61      * Set the Throwable object which is thrown when test running
62      * @param t The Throwable object
63      */
setThrowable(Throwable t)64     public void setThrowable(Throwable t) {
65         mThrowable = t;
66     }
67 
68     /**
69      * Wait for the test thread to complete and throw the stored exception if there is one.
70      *
71      * @param runTime The time to wait for the test thread to complete.
72      * @throws Throwable
73      */
joinAndCheck(long runTime)74     public void joinAndCheck(long runTime) throws Throwable {
75         this.join(runTime);
76         if (this.isAlive()) {
77             this.interrupt();
78             this.join(runTime);
79             throw new Exception("Thread did not finish within allotted time.");
80         }
81         checkException();
82     }
83 
84     /**
85      * Check whether there is an exception when running Runnable object.
86      * @throws Throwable
87      */
checkException()88     public void checkException() throws Throwable {
89         if (mThrowable != null) {
90             throw mThrowable;
91         }
92     }
93 }
94