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 
17 package android.os.cts;
18 
19 import junit.framework.TestCase;
20 
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.os.Message;
24 import android.os.SystemClock;
25 import android.os.Handler.Callback;
26 import android.test.UiThreadTest;
27 import android.util.Printer;
28 
29 import com.android.compatibility.common.util.TestThread;
30 
31 public class HandlerTest extends TestCase {
32 
33     public static final int MESSAGE_WHAT = 3;
34 
35     // time when message should be handled.
36     static final int RUNTIME = 300;
37 
38     // time when check whether the message is handled.
39     static final long DELAYED = RUNTIME + 50;
40 
41     // Handler
42     private final Handler mHandler = new Handler(Looper.getMainLooper());
43     private final MockHandler mHandler1 = new MockHandler(Looper.getMainLooper());
44     private final Object mLock = new Object();
45 
46     @Override
tearDown()47     protected void tearDown() throws Exception {
48         mHandler1.reset();
49         super.tearDown();
50     }
51 
testConstructor()52     public void testConstructor() throws Throwable {
53         final Callback cb = new Callback() {
54             public boolean handleMessage(Message msg) {
55                 return false;
56             }
57         };
58 
59         new TestThread(new Runnable() {
60             public void run() {
61                 Looper.prepare();
62                 new Handler();
63                 new Handler(cb);
64             }
65         }).runTest(RUNTIME);
66 
67         // new the Handler instance
68         new Handler(Looper.getMainLooper());
69         new Handler(Looper.getMainLooper(), cb);
70     }
71 
testPostAtTime1()72     public void testPostAtTime1() {
73         MockRunnable r = new MockRunnable();
74         assertTrue(mHandler.postAtTime(r, SystemClock.uptimeMillis() + RUNTIME));
75         assertFalse(r.isRun());
76         sleep(DELAYED);
77         assertTrue(r.isRun());
78         mHandler.removeCallbacks(r);
79     }
80 
testPostAtTime2()81     public void testPostAtTime2() {
82         MockRunnable r = new MockRunnable();
83         Object token = new Object();
84         assertTrue(mHandler.postAtTime(r, token, SystemClock.uptimeMillis() + RUNTIME));
85         assertFalse(r.isRun());
86         sleep(DELAYED);
87         assertTrue(r.isRun());
88         mHandler.removeCallbacks(r);
89     }
90 
testSendMessageAtTime()91     public void testSendMessageAtTime() {
92         Message msg = mHandler1.obtainMessage();
93         assertTrue(mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME));
94         assertNull(mHandler1.message);
95         sleep(DELAYED);
96         assertSame(msg, mHandler1.message);
97         mHandler1.removeMessages(msg.what);
98     }
99 
testDump()100     public void testDump() {
101         final String prefix = "AndroidTest";
102         MockPrinter pw = new MockPrinter();
103         mHandler.dump(pw, prefix);
104     }
105 
testHasMessagesWithInt()106     public void testHasMessagesWithInt() {
107         Message msg = mHandler.obtainMessage();
108         assertFalse(mHandler.hasMessages(msg.what));
109         mHandler.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
110         assertTrue(mHandler.hasMessages(msg.what));
111         mHandler.removeMessages(msg.what);
112         assertFalse(mHandler.hasMessages(msg.what));
113     }
114 
testHasMessagesWithObject()115     public void testHasMessagesWithObject() {
116         Message msg = mHandler.obtainMessage();
117         msg.obj = new Object();
118         assertFalse(mHandler.hasMessages(msg.what, msg.obj));
119         mHandler.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
120         assertTrue(mHandler.hasMessages(msg.what, msg.obj));
121         mHandler.removeMessages(msg.what);
122         assertFalse(mHandler.hasMessages(msg.what, msg.obj));
123     }
124 
testRemoveCallbacksAndMessages()125     public void testRemoveCallbacksAndMessages() {
126         Message msg = mHandler1.obtainMessage();
127         mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
128         sleep(RUNTIME / 2);
129 
130         // Test the obj == null
131         mHandler1.removeCallbacksAndMessages(null);
132         sleep(RUNTIME / 2);
133         assertNull(mHandler1.message);
134         mHandler1.reset();
135 
136         msg = mHandler1.obtainMessage();
137         msg.obj = new Object();
138         mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
139         sleep(RUNTIME / 2);
140 
141         // Test the obj == p.obj for message
142         mHandler1.removeCallbacksAndMessages(msg.obj);
143         sleep(RUNTIME / 2);
144         assertNull(mHandler1.message);
145         mHandler1.reset();
146 
147         // Test remove a callback
148         final Object obj = new Object();
149         MockRunnable mr1 = new MockRunnable();
150         mHandler1.postAtTime(mr1, obj, SystemClock.uptimeMillis() + RUNTIME);
151         sleep(RUNTIME / 2);
152         mHandler1.removeCallbacksAndMessages(obj);
153         sleep(RUNTIME / 2);
154         assertFalse(mr1.isRun());
155 
156         // test remove a wrong callback
157         mr1 = new MockRunnable();
158         mHandler1.postAtTime(mr1, obj, SystemClock.uptimeMillis() + RUNTIME);
159         sleep(DELAYED / 2);
160         mHandler1.removeCallbacksAndMessages(new Object());
161         sleep(DELAYED / 2);
162         assertTrue(mr1.isRun());
163     }
164 
testSendEmptyMessageAtTime()165     public void testSendEmptyMessageAtTime() {
166         long uptime = SystemClock.uptimeMillis() + RUNTIME;
167         assertTrue(mHandler1.sendEmptyMessageAtTime(MESSAGE_WHAT, uptime));
168         assertEquals(0, mHandler1.what);
169         sleep(DELAYED);
170         assertEquals(MESSAGE_WHAT, mHandler1.what);
171         mHandler1.removeMessages(MESSAGE_WHAT);
172     }
173 
testGetLooper()174     public void testGetLooper() {
175         // new the Handler instance
176         Looper looper = Looper.getMainLooper();
177         Handler handler = new Handler(looper);
178         assertSame(looper, handler.getLooper());
179     }
180 
testRemoveCallbacks()181     public void testRemoveCallbacks() {
182         // test remove right object.
183         MockRunnable r = new MockRunnable();
184         mHandler.postAtTime(r, SystemClock.uptimeMillis() + RUNTIME);
185         sleep(DELAYED / 2);
186         mHandler.removeCallbacks(r);
187         sleep(DELAYED / 2);
188         assertFalse(r.isRun());
189 
190         // test remove wrong object.
191         r = new MockRunnable();
192         MockRunnable mr = new MockRunnable();
193         mHandler.postAtTime(r, SystemClock.uptimeMillis() + RUNTIME);
194         sleep(DELAYED / 2);
195         mHandler.removeCallbacks(mr);
196         sleep(DELAYED / 2);
197         assertTrue(r.isRun());
198     }
199 
testRemoveCallbacksWithObject()200     public void testRemoveCallbacksWithObject() {
201         // test remove right object.
202         MockRunnable r1 = new MockRunnable();
203         Object token = new Object();
204         mHandler.postAtTime(r1, token, SystemClock.uptimeMillis() + RUNTIME);
205         sleep(DELAYED / 2);
206         mHandler.removeCallbacks(r1, token);
207         sleep(DELAYED / 2);
208         assertFalse(r1.isRun());
209 
210         // test remove wrong object.
211         r1 = new MockRunnable();
212         MockRunnable r2 = new MockRunnable();
213 
214         mHandler.postAtTime(r1, token, SystemClock.uptimeMillis() + RUNTIME);
215         sleep(DELAYED / 2);
216         mHandler.removeCallbacks(r2, token);
217         sleep(DELAYED / 2);
218         assertTrue(r1.isRun());
219 
220         // test remove with right callback and wrong token
221         mHandler.postAtTime(r1, token, SystemClock.uptimeMillis() + RUNTIME);
222         Object wrongToken = new Object();
223         sleep(DELAYED / 2);
224         mHandler.removeCallbacks(r1, wrongToken);
225         sleep(DELAYED / 2);
226         assertTrue(r1.isRun());
227     }
228 
testRemoveMessages()229     public void testRemoveMessages() {
230         // test remove right message
231         Message msg = mHandler1.obtainMessage();
232         msg.what = 100;
233         mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
234         sleep(DELAYED / 2);
235         mHandler1.removeMessages(msg.what);
236         sleep(DELAYED / 2);
237         assertNull(mHandler1.message);
238         assertEquals(0, mHandler1.what);
239         mHandler1.reset();
240 
241         // test remove wrong message
242         msg = mHandler1.obtainMessage();
243         msg.what = 100;
244         mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
245         sleep(DELAYED / 2);
246         mHandler1.removeMessages(101);
247         sleep(DELAYED / 2);
248         assertEquals(100, mHandler1.what);
249     }
250 
testRemoveMessagesWithObject()251     public void testRemoveMessagesWithObject() {
252         // test remove right message
253         Message msg = mHandler1.obtainMessage();
254         msg.obj = new Object();
255         msg.what = 100;
256         mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
257         sleep(DELAYED / 2);
258         mHandler1.removeMessages(msg.what, msg.obj);
259         sleep(DELAYED / 2);
260         assertNull(mHandler1.message);
261         assertEquals(0, mHandler1.what);
262         mHandler1.reset();
263 
264         // test remove wrong message
265         msg = mHandler1.obtainMessage();
266         msg.obj = new Object();
267         msg.what = 100;
268         Message wrongMessage = mHandler1.obtainMessage();
269         wrongMessage.obj = new Object();
270         wrongMessage.what = 111;
271         mHandler1.sendMessageAtTime(msg, SystemClock.uptimeMillis() + RUNTIME);
272         sleep(DELAYED / 2);
273         mHandler1.removeMessages(msg.what, wrongMessage.obj);
274         sleep(DELAYED / 2);
275         assertEquals(100, mHandler1.what);
276     }
277 
testSendMessage()278     public void testSendMessage() {
279         Message msg = mHandler1.obtainMessage();
280         assertTrue(mHandler1.sendMessage(msg));
281         sleep(DELAYED);
282         assertSame(msg, mHandler1.message);
283         mHandler1.removeMessages(msg.what);
284     }
285 
testObtainMessage()286     public void testObtainMessage() {
287          Message msg = mHandler.obtainMessage();
288          assertNotNull(msg);
289          assertEquals(mHandler, msg.getTarget());
290     }
291 
testObtainMessageWithInt()292     public void testObtainMessageWithInt() {
293          // new the Handler instance
294          Handler handler = new Handler(Looper.getMainLooper());
295          Message msg = handler.obtainMessage();
296          msg.what = 100;
297          Message msg1 = mHandler.obtainMessage(msg.what);
298          assertNotNull(msg1);
299          assertEquals(mHandler, msg1.getTarget());
300          assertEquals(msg.what, msg1.what);
301     }
302 
testObtainMessageWithIntObject()303     public void testObtainMessageWithIntObject() {
304         // new the Handler instance
305         Handler handler = new Handler(Looper.getMainLooper());
306         Message msg = handler.obtainMessage();
307         msg.what = 100;
308         msg.obj = new Object();
309         Message msg1 = mHandler.obtainMessage(msg.what, msg.obj);
310         assertNotNull(msg1);
311         assertEquals(mHandler, msg1.getTarget());
312         assertEquals(msg.what, msg1.what);
313         assertSame(msg.obj, msg1.obj);
314     }
315 
testObtainMessageWithMutiInt()316     public void testObtainMessageWithMutiInt() {
317         // new the Handler instance
318         Handler handler = new Handler(Looper.getMainLooper());
319         Message msg = handler.obtainMessage();
320         msg.what = 100;
321         msg.arg1 = 101;
322         msg.arg2 = 102;
323         Message msg1 = mHandler.obtainMessage(msg.what, msg.arg1, msg.arg2);
324         assertNotNull(msg1);
325         assertEquals(mHandler, msg1.getTarget());
326         assertEquals(msg.what, msg1.what);
327         assertEquals(msg.arg1, msg1.arg1);
328         assertEquals(msg.arg2, msg1.arg2);
329     }
330 
testObtainMessageWithMutiIntObject()331     public void testObtainMessageWithMutiIntObject() {
332         // new the Handler instance
333         Handler handler = new Handler(Looper.getMainLooper());
334         Message msg = handler.obtainMessage();
335         msg.what = 100;
336         msg.arg1 = 1000;
337         msg.arg2 = 2000;
338         msg.obj = new Object();
339         Message msg1 = mHandler.obtainMessage(msg.what, msg.arg1, msg.arg2, msg.obj);
340         assertNotNull(msg1);
341         assertEquals(mHandler, msg1.getTarget());
342         assertEquals(msg.arg1, msg1.arg1);
343         assertEquals(msg.arg2, msg1.arg2);
344         assertSame(msg.obj, msg1.obj);
345     }
346 
testSendMessageAtFrontOfQueue()347     public void testSendMessageAtFrontOfQueue() {
348         Message lateMsg = mHandler1.obtainMessage();
349         mHandler1.sendEmptyMessageAtTime(lateMsg.what, SystemClock.uptimeMillis() + RUNTIME * 5);
350         Message msg = mHandler1.obtainMessage();
351         msg.what = 100;
352         assertTrue(mHandler1.sendMessageAtFrontOfQueue(msg));
353         sleep(DELAYED);
354         assertSame(msg, mHandler1.message);
355         mHandler1.removeMessages(msg.what);
356     }
357 
testPostDelayed()358     public void testPostDelayed() {
359         MockRunnable r = new MockRunnable();
360         assertTrue(mHandler.postDelayed(r, DELAYED));
361         assertFalse(r.isRun());
362         sleep(DELAYED + 500);
363         assertTrue(r.isRun());
364         mHandler.removeCallbacks(r);
365     }
366 
testPostAtFrontOfQueue()367     public void testPostAtFrontOfQueue() {
368         MockRunnable r = new MockRunnable();
369         MockRunnable mr = new MockRunnable();
370         assertFalse(r.isRun());
371         assertTrue(mHandler.postDelayed(mr, DELAYED));
372         assertTrue(mHandler.postAtFrontOfQueue(r));
373         sleep(DELAYED / 2);
374         assertTrue(r.isRun());
375         mHandler.removeCallbacks(r);
376     }
377 
testSendMessageDelayed()378     public void testSendMessageDelayed() {
379         Message msg = mHandler1.obtainMessage();
380         assertTrue(mHandler1.sendMessageDelayed(msg, DELAYED));
381         assertNull(mHandler1.message);
382         sleep(DELAYED + 500);
383         assertSame(msg, mHandler1.message);
384         mHandler1.removeMessages(msg.what);
385     }
386 
testPost()387     public void testPost() {
388         MockRunnable r = new MockRunnable();
389         assertFalse(r.isRun());
390         assertTrue(mHandler.post(r));
391         sleep(DELAYED);
392         assertTrue(r.isRun());
393         mHandler.removeCallbacks(r);
394     }
395 
testSendEmptyMessageDelayed()396     public void testSendEmptyMessageDelayed() {
397         Message msg = mHandler1.obtainMessage();
398         msg.what = 100;
399         assertTrue(mHandler1.sendEmptyMessageDelayed(msg.what, DELAYED));
400         sleep(DELAYED + 500);
401         assertEquals(msg.what, mHandler1.what);
402         mHandler1.removeMessages(msg.what);
403     }
404 
testDispatchMessage1()405     public void testDispatchMessage1() {
406         // new the Handler instance
407         MockHandler handler = new MockHandler();
408         MockRunnable callback = new MockRunnable();
409         Message msg = Message.obtain(handler, callback);
410         handler.dispatchMessage(msg);
411         assertNotNull(msg.getCallback());
412 
413         // Test the if branch
414         assertTrue(callback.isRun());
415     }
416 
testDispatchMessage2()417     public void testDispatchMessage2() {
418         // new the Handler instance
419         MockHandler handler = new MockHandler();
420         Message msg = handler.obtainMessage();
421         handler.dispatchMessage(msg);
422 
423         // Test the else branch
424         assertSame(msg, handler.message);
425     }
426 
testSendEmptyMessage()427     public void testSendEmptyMessage() {
428         Message msg = mHandler1.obtainMessage();
429         msg.what = 100;
430         assertTrue(mHandler1.sendEmptyMessage(msg.what));
431         sleep(DELAYED);
432         assertEquals(msg.what, mHandler1.what);
433         mHandler1.removeMessages(msg.what);
434     }
435 
testToString()436     public void testToString() {
437         assertNotNull(mHandler1.toString());
438     }
439 
440     /**
441      * MockRunnable
442      */
443     private class MockRunnable implements Runnable {
444         // MockRunnable run
445         private boolean mIsRun;
446 
run()447         public void run() {
448             mIsRun = true;
449         }
450 
isRun()451         public boolean isRun() {
452             return mIsRun;
453         }
454     }
455 
456     /**
457      * MockPrinter
458      */
459     private class MockPrinter implements Printer {
460         String mOutput;
461 
println(String x)462         public void println(String x) {
463             mOutput = x;
464         }
465 
getPrint()466         public String getPrint() {
467             return mOutput;
468         }
469     }
470 
471     /**
472      * MockHandler
473      */
474     private class MockHandler extends Handler {
475         public Message message;
476         public int what;
477 
MockHandler()478         MockHandler() {
479             super(Looper.getMainLooper());
480         }
481 
MockHandler(Looper looper)482         MockHandler(Looper looper) {
483             super(looper);
484         }
485 
486         @Override
handleMessage(Message msg)487         public void handleMessage(Message msg) {
488             message = msg;
489             what = message.what;
490         }
491 
reset()492         public void reset() {
493             message = null;
494             what = 0;
495         }
496     }
497 
sleep(long time)498     public void sleep(long time) {
499         try {
500             Thread.sleep(time);
501         } catch (InterruptedException e) {
502             fail(e.getMessage());
503         }
504     }
505 }
506