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