Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2007 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;
     18 
     19 import android.os.Handler;
     20 import android.os.Message;
     21 import android.os.SystemClock;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 import junit.framework.TestCase;
     24 
     25 public class MessageQueueTest extends TestCase {
     26 
     27     private static class BaseTestHandler extends TestHandlerThread {
     28         Handler mHandler;
     29         int mLastMessage;
     30         int mCount;
     31 
     32         public BaseTestHandler() {
     33         }
     34 
     35         public void go() {
     36             mHandler = new Handler() {
     37                 public void handleMessage(Message msg) {
     38                     BaseTestHandler.this.handleMessage(msg);
     39                 }
     40             };
     41         }
     42 
     43         public void handleMessage(Message msg) {
     44             if (mCount <= mLastMessage) {
     45                 if (msg.what != mCount) {
     46                     failure(new RuntimeException(
     47                             "Expected message #" + mCount
     48                                     + ", received #" + msg.what));
     49                 } else if (mCount == mLastMessage) {
     50                     success();
     51                 }
     52                 mCount++;
     53             } else {
     54                 failure(new RuntimeException(
     55                         "Message received after done, #" + msg.what));
     56             }
     57         }
     58     }
     59 
     60     @MediumTest
     61     public void testMessageOrder() throws Exception {
     62         TestHandlerThread tester = new BaseTestHandler() {
     63             public void go() {
     64                 super.go();
     65                 long now = SystemClock.uptimeMillis() + 200;
     66                 mLastMessage = 4;
     67                 mCount = 0;
     68                 mHandler.sendMessageAtTime(mHandler.obtainMessage(2), now + 1);
     69                 mHandler.sendMessageAtTime(mHandler.obtainMessage(3), now + 2);
     70                 mHandler.sendMessageAtTime(mHandler.obtainMessage(4), now + 2);
     71                 mHandler.sendMessageAtTime(mHandler.obtainMessage(0), now + 0);
     72                 mHandler.sendMessageAtTime(mHandler.obtainMessage(1), now + 0);
     73             }
     74         };
     75 
     76         tester.doTest(1000);
     77     }
     78 
     79     @MediumTest
     80     public void testAtFrontOfQueue() throws Exception {
     81         TestHandlerThread tester = new BaseTestHandler() {
     82             public void go() {
     83                 super.go();
     84                 long now = SystemClock.uptimeMillis() + 200;
     85                 mLastMessage = 3;
     86                 mCount = 0;
     87                 mHandler.sendMessageAtTime(mHandler.obtainMessage(3), now);
     88                 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(2));
     89                 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(0));
     90             }
     91 
     92             public void handleMessage(Message msg) {
     93                 super.handleMessage(msg);
     94                 if (msg.what == 0) {
     95                     mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(1));
     96                 }
     97             }
     98         };
     99 
    100         tester.doTest(1000);
    101     }
    102 }
    103 
    104