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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/*
* Copyright (C) 2017 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.testing;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.os.TestLooperManager;
import android.support.test.InstrumentationRegistry;
import android.util.ArrayMap;
import org.junit.runners.model.FrameworkMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
/**
* This is a wrapper around {@link TestLooperManager} to make it easier to manage
* and provide an easy annotation for use with tests.
*
* @see TestableLooperTest TestableLooperTest for examples.
*/
public class TestableLooper {
private Looper mLooper;
private MessageQueue mQueue;
private MessageHandler mMessageHandler;
private Handler mHandler;
private Runnable mEmptyMessage;
private TestLooperManager mQueueWrapper;
public TestableLooper(Looper l) throws Exception {
this(acquireLooperManager(l), l);
}
private TestableLooper(TestLooperManager wrapper, Looper l) throws Exception {
mQueueWrapper = wrapper;
setupQueue(l);
}
private TestableLooper(Looper looper, boolean b) throws Exception {
setupQueue(looper);
}
public Looper getLooper() {
return mLooper;
}
private void setupQueue(Looper l) throws Exception {
mLooper = l;
mQueue = mLooper.getQueue();
mHandler = new Handler(mLooper);
}
/**
* Must be called to release the looper when the test is complete, otherwise
* the looper will not be available for any subsequent tests. This is
* automatically handled for tests using {@link RunWithLooper}.
*/
public void destroy() throws NoSuchFieldException, IllegalAccessException {
mQueueWrapper.release();
if (mLooper == Looper.getMainLooper()) {
TestableInstrumentation.releaseMain();
}
}
/**
* Sets a callback for all messages processed on this TestableLooper.
*
* @see {@link MessageHandler}
*/
public void setMessageHandler(MessageHandler handler) {
mMessageHandler = handler;
}
/**
* Parse num messages from the message queue.
*
* @param num Number of messages to parse
*/
public int processMessages(int num) {
for (int i = 0; i < num; i++) {
if (!parseMessageInt()) {
return i + 1;
}
}
return num;
}
/**
* Process messages in the queue until no more are found.
*/
public void processAllMessages() {
while (processQueuedMessages() != 0) ;
}
private int processQueuedMessages() {
int count = 0;
mEmptyMessage = () -> { };
mHandler.post(mEmptyMessage);
waitForMessage(mQueueWrapper, mHandler, mEmptyMessage);
while (parseMessageInt()) count++;
return count;
}
private boolean parseMessageInt() {
try {
Message result = mQueueWrapper.next();
if (result != null) {
// This is a break message.
if (result.getCallback() == mEmptyMessage) {
mQueueWrapper.recycle(result);
return false;
}
if (mMessageHandler != null) {
if (mMessageHandler.onMessageHandled(result)) {
result.getTarget().dispatchMessage(result);
mQueueWrapper.recycle(result);
} else {
mQueueWrapper.recycle(result);
// Message handler indicated it doesn't want us to continue.
return false;
}
} else {
result.getTarget().dispatchMessage(result);
mQueueWrapper.recycle(result);
}
} else {
// No messages, don't continue parsing
return false;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return true;
}
/**
* Runs an executable with myLooper set and processes all messages added.
*/
public void runWithLooper(RunnableWithException runnable) throws Exception {
new Handler(getLooper()).post(() -> {
try {
runnable.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
processAllMessages();
}
public interface RunnableWithException {
void run() throws Exception;
}
/**
* Annotation that tells the {@link AndroidTestingRunner} to create a TestableLooper and
* run this test/class on that thread. The {@link TestableLooper} can be acquired using
* {@link #get(Object)}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RunWithLooper {
boolean setAsMainLooper() default false;
}
private static void waitForMessage(TestLooperManager queueWrapper, Handler handler,
Runnable execute) {
for (int i = 0; i < 10; i++) {
if (!queueWrapper.hasMessages(handler, null, execute)) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
}
if (!queueWrapper.hasMessages(handler, null, execute)) {
throw new RuntimeException("Message didn't queue...");
}
}
private static TestLooperManager acquireLooperManager(Looper l) {
if (l == Looper.getMainLooper()) {
TestableInstrumentation.acquireMain();
}
return InstrumentationRegistry.getInstrumentation().acquireLooperManager(l);
}
private static final Map<Object, TestableLooper> sLoopers = new ArrayMap<>();
/**
* For use with {@link RunWithLooper}, used to get the TestableLooper that was
* automatically created for this test.
*/
public static TestableLooper get(Object test) {
return sLoopers.get(test);
}
static class LooperFrameworkMethod extends FrameworkMethod {
private HandlerThread mHandlerThread;
private final TestableLooper mTestableLooper;
private final Looper mLooper;
private final Handler mHandler;
public LooperFrameworkMethod(FrameworkMethod base, boolean setAsMain, Object test) {
super(base.getMethod());
try {
mLooper = setAsMain ? Looper.getMainLooper() : createLooper();
mTestableLooper = new TestableLooper(mLooper, false);
} catch (Exception e) {
throw new RuntimeException(e);
}
sLoopers.put(test, mTestableLooper);
mHandler = new Handler(mLooper);
}
public LooperFrameworkMethod(TestableLooper other, FrameworkMethod base) {
super(base.getMethod());
mLooper = other.mLooper;
mTestableLooper = other;
mHandler = new Handler(mLooper);
}
public static FrameworkMethod get(FrameworkMethod base, boolean setAsMain, Object test) {
if (sLoopers.containsKey(test)) {
return new LooperFrameworkMethod(sLoopers.get(test), base);
}
return new LooperFrameworkMethod(base, setAsMain, test);
}
@Override
public Object invokeExplosively(Object target, Object... params) throws Throwable {
if (Looper.myLooper() == mLooper) {
// Already on the right thread from another statement, just execute then.
return super.invokeExplosively(target, params);
}
boolean set = mTestableLooper.mQueueWrapper == null;
if (set) {
mTestableLooper.mQueueWrapper = acquireLooperManager(mLooper);
}
try {
Object[] ret = new Object[1];
// Run the execution on the looper thread.
Runnable execute = () -> {
try {
ret[0] = super.invokeExplosively(target, params);
} catch (Throwable throwable) {
throw new LooperException(throwable);
}
};
Message m = Message.obtain(mHandler, execute);
// Dispatch our message.
try {
mTestableLooper.mQueueWrapper.execute(m);
} catch (LooperException e) {
throw e.getSource();
} catch (RuntimeException re) {
// If the TestLooperManager has to post, it will wrap what it throws in a
// RuntimeException, make sure we grab the actual source.
if (re.getCause() instanceof LooperException) {
throw ((LooperException) re.getCause()).getSource();
} else {
throw re.getCause();
}
} finally {
m.recycle();
}
return ret[0];
} finally {
if (set) {
mTestableLooper.mQueueWrapper.release();
mTestableLooper.mQueueWrapper = null;
if (mLooper == Looper.getMainLooper()) {
TestableInstrumentation.releaseMain();
}
}
}
}
private Looper createLooper() {
// TODO: Find way to share these.
mHandlerThread = new HandlerThread(TestableLooper.class.getSimpleName());
mHandlerThread.start();
return mHandlerThread.getLooper();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
if (mHandlerThread != null) {
mHandlerThread.quit();
}
}
private static class LooperException extends RuntimeException {
private final Throwable mSource;
public LooperException(Throwable t) {
mSource = t;
}
public Throwable getSource() {
return mSource;
}
}
}
/**
* Callback to control the execution of messages on the looper, when set with
* {@link #setMessageHandler(MessageHandler)} then {@link #onMessageHandled(Message)}
* will get called back for every message processed on the {@link TestableLooper}.
*/
public interface MessageHandler {
/**
* Return true to have the message executed and delivered to target.
* Return false to not execute the message and stop executing messages.
*/
boolean onMessageHandled(Message m);
}
}
|