summaryrefslogtreecommitdiffstats
path: root/tests/CoreTests/android/core/TestHandler.java
blob: 4ff2e6e4d8b6ee4746f3b9e246c47ad3db65851f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* //device/java/android/com/android/tests/TestHandler.java
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

package android.core;

import com.android.internal.os.HandlerHelper;
import android.os.HandlerInterface;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

/**
 * Naive class that implements a getNextMessage()
 * by running a Handler in a new thread. <p>
 * <p/>
 * This class blocks the Handler thread when the getNextMessage() thread
 * is not in getNextMessage(). This allows the getNextMessage() thread to
 * inspect state that is otherwise unguarded and would otherwise be prone to
 * race conditions.<p>
 * <p/>
 * Please note that both threads are allowed to run unsynchronized until
 * the first message is posted to this handler.
 * <p/>
 * Please call hh.looper.quit() when done to clean this up
 */
public class TestHandler implements Runnable, HandlerInterface {
    //***** Instance Variables

    public HandlerHelper hh;
    public Looper looper;

    Runnable setupRoutine;
    Message nextMessage;
    long failTimeoutMillis;
    boolean waitBeforeReturning = true;

    //***** Class Methods

    public static TestHandler create() {
        return create("TestHandler", null);
    }

    public static TestHandler create(String name) {
        return create(name, null);
    }

    public static TestHandler create(String name, Runnable doSetup) {
        TestHandler ret;

        ret = new TestHandler();

        ret.setupRoutine = doSetup;

        synchronized (ret) {
            new Thread(ret, name).start();
            while (ret.looper == null) {
                try {
                    ret.wait();
                } catch (InterruptedException ex) {
                }
            }
        }

        return ret;
    }

    //***** Public Methods

    /**
     * Maximum time to wait for a message before failing
     * by throwing exception
     */
    public void setFailTimeoutMillis(long msec) {
        failTimeoutMillis = msec;
    }

    /**
     * Waits for the next message to be sent to this handler and returns it.
     * Blocks the Handler's looper thread until another call to getNextMessage()
     * is made
     */

    public Message getNextMessage() {
        Message ret;

        synchronized (this) {
            long time = SystemClock.uptimeMillis();

            waitBeforeReturning = false;
            this.notifyAll();

            try {
                while (nextMessage == null) {
                    if (failTimeoutMillis > 0
                            && ((SystemClock.uptimeMillis() - time)
                            > failTimeoutMillis)) {
                        throw new RuntimeException("Timeout exceeded exceeded");
                    }

                    try {
                        this.wait(failTimeoutMillis);
                    } catch (InterruptedException ex) {
                    }
                }
                ret = nextMessage;
                nextMessage = null;
            } finally {
                waitBeforeReturning = true;
            }
        }

        return ret;
    }

    //***** Overridden from Runnable

    public void run() {
        Looper.prepare();
        hh = new HandlerHelper(this);

        if (setupRoutine != null) {
            setupRoutine.run();
        }

        synchronized (this) {
            looper = Looper.myLooper();
            this.notify();
        }

        Looper.loop();
    }

    //***** HandlerHelper implementation

    public void handleMessage(Message msg) {
        synchronized (this) {
            while (nextMessage != null) {
                try {
                    this.wait();
                } catch (InterruptedException ex) {
                }
            }

            // msg will be recycled when this method returns.
            // so we need to make a copy of it.
            nextMessage = Message.obtain();
            nextMessage.copyFrom(msg);
            this.notifyAll();

            while (waitBeforeReturning) {
                try {
                    this.wait();
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}