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 android.test.suitebuilder.annotation.Suppress;
     24 import junit.framework.TestCase;
     25 
     26 @Suppress  // Failing.
     27 public class MessageQueueTest extends TestCase {
     28 
     29     private static class BaseTestHandler extends TestHandlerThread {
     30         Handler mHandler;
     31         int mLastMessage;
     32         int mCount;
     33 
     34         public BaseTestHandler() {
     35         }
     36 
     37         public void go() {
     38             mHandler = new Handler() {
     39                 public void handleMessage(Message msg) {
     40                     BaseTestHandler.this.handleMessage(msg);
     41                 }
     42             };
     43         }
     44 
     45         public void handleMessage(Message msg) {
     46             if (!msg.isInUse()) {
     47                 failure(new RuntimeException(
     48                         "msg.isInuse is false, should always be true, #" + msg.what));
     49             }
     50             if (mCount <= mLastMessage) {
     51                 if (msg.what != mCount) {
     52                     failure(new RuntimeException(
     53                             "Expected message #" + mCount
     54                                     + ", received #" + msg.what));
     55                 } else if (mCount == mLastMessage) {
     56                     success();
     57                 }
     58                 mCount++;
     59             } else {
     60                 failure(new RuntimeException(
     61                         "Message received after done, #" + msg.what));
     62             }
     63         }
     64     }
     65 
     66     @MediumTest
     67     public void testMessageOrder() throws Exception {
     68         TestHandlerThread tester = new BaseTestHandler() {
     69             public void go() {
     70                 super.go();
     71                 long now = SystemClock.uptimeMillis() + 200;
     72                 mLastMessage = 4;
     73                 mCount = 0;
     74                 mHandler.sendMessageAtTime(mHandler.obtainMessage(2), now + 1);
     75                 mHandler.sendMessageAtTime(mHandler.obtainMessage(3), now + 2);
     76                 mHandler.sendMessageAtTime(mHandler.obtainMessage(4), now + 2);
     77                 mHandler.sendMessageAtTime(mHandler.obtainMessage(0), now + 0);
     78                 mHandler.sendMessageAtTime(mHandler.obtainMessage(1), now + 0);
     79             }
     80         };
     81 
     82         tester.doTest(1000);
     83     }
     84 
     85     @MediumTest
     86     public void testAtFrontOfQueue() throws Exception {
     87         TestHandlerThread tester = new BaseTestHandler() {
     88             public void go() {
     89                 super.go();
     90                 long now = SystemClock.uptimeMillis() + 200;
     91                 mLastMessage = 3;
     92                 mCount = 0;
     93                 mHandler.sendMessageAtTime(mHandler.obtainMessage(3), now);
     94                 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(2));
     95                 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(0));
     96             }
     97 
     98             public void handleMessage(Message msg) {
     99                 super.handleMessage(msg);
    100                 if (msg.what == 0) {
    101                     mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(1));
    102                 }
    103             }
    104         };
    105 
    106         tester.doTest(1000);
    107     }
    108 
    109     private static class TestFieldIntegrityHandler extends TestHandlerThread {
    110         Handler mHandler;
    111         int mLastMessage;
    112         int mCount;
    113 
    114         public TestFieldIntegrityHandler() {
    115         }
    116 
    117         public void go() {
    118             mHandler = new Handler() {
    119                 public void handleMessage(Message msg) {
    120                     TestFieldIntegrityHandler.this.handleMessage(msg);
    121                 }
    122             };
    123         }
    124 
    125         public void handleMessage(Message msg) {
    126             if (!msg.isInUse()) {
    127                 failure(new RuntimeException(
    128                         "msg.isInuse is false, should always be true, #" + msg.what));
    129             }
    130             if (mCount <= mLastMessage) {
    131                 if (msg.what != mCount) {
    132                     failure(new RuntimeException(
    133                             "Expected message #" + mCount
    134                                     + ", received #" + msg.what));
    135                 } else if (mCount == mLastMessage) {
    136                     success();
    137                 }
    138                 mCount++;
    139             } else {
    140                 failure(new RuntimeException(
    141                         "Message received after done, #" + msg.what));
    142             }
    143         }
    144     }
    145 
    146     @MediumTest
    147     public void testFieldIntegrity() throws Exception {
    148 
    149         TestHandlerThread tester = new TestFieldIntegrityHandler() {
    150             Bundle mBundle;
    151 
    152             public void go() {
    153                 super.go();
    154                 mLastMessage = 1;
    155                 mCount = 0;
    156                 mHandler.sendMessage(mHandler.obtainMessage(0));
    157             }
    158 
    159             public void handleMessage(Message msg) {
    160                 super.handleMessage(msg);
    161                 if (msg.what == 0) {
    162                     msg.flags = -1;
    163                     msg.what = 1;
    164                     msg.arg1 = 456;
    165                     msg.arg2 = 789;
    166                     msg.obj = this;
    167                     msg.replyTo = null;
    168                     mBundle = new Bundle();
    169                     msg.data = mBundle;
    170                     msg.data.putString("key", "value");
    171 
    172                     Message newMsg = mHandler.obtainMessage();
    173                     newMsg.copyFrom(msg);
    174                     if (newMsg.isInUse() != false) {
    175                         failure(new RuntimeException(
    176                                 "newMsg.isInUse is true should be false after copyFrom"));
    177                     }
    178                     if (newMsg.flags != 0) {
    179                         failure(new RuntimeException(String.format(
    180                         "newMsg.flags is %d should be 0 after copyFrom", newMsg.flags)));
    181                     }
    182                     if (newMsg.what != 1) {
    183                         failure(new RuntimeException(String.format(
    184                                 "newMsg.what is %d should be %d after copyFrom", newMsg.what, 1)));
    185                     }
    186                     if (newMsg.arg1 != 456) {
    187                         failure(new RuntimeException(String.format(
    188                                 "newMsg.arg1 is %d should be %d after copyFrom", msg.arg1, 456)));
    189                     }
    190                     if (newMsg.arg2 != 789) {
    191                         failure(new RuntimeException(String.format(
    192                                 "newMsg.arg2 is %d should be %d after copyFrom", msg.arg2, 789)));
    193                     }
    194                     if (newMsg.obj != this) {
    195                         failure(new RuntimeException(
    196                                 "newMsg.obj should be 'this' after copyFrom"));
    197                     }
    198                     if (newMsg.replyTo != null) {
    199                         failure(new RuntimeException(
    200                                 "newMsg.replyTo should be null after copyFrom"));
    201                     }
    202                     if (newMsg.data == mBundle) {
    203                         failure(new RuntimeException(
    204                                 "newMsg.data should NOT be mBundle after copyFrom"));
    205                     }
    206                     if (!newMsg.data.getString("key").equals(mBundle.getString("key"))) {
    207                         failure(new RuntimeException(String.format(
    208                                 "newMsg.data.getString(\"key\") is %s and does not equal" +
    209                                 " mBundle.getString(\"key\") which is %s after copyFrom",
    210                                 newMsg.data.getString("key"),  mBundle.getString("key"))));
    211                     }
    212                     if (newMsg.when != 0) {
    213                         failure(new RuntimeException(String.format(
    214                                 "newMsg.when is %d should be 0 after copyFrom", newMsg.when)));
    215                     }
    216                     if (newMsg.target != mHandler) {
    217                         failure(new RuntimeException(
    218                                 "newMsg.target is NOT mHandler after copyFrom"));
    219                     }
    220                     if (newMsg.callback != null) {
    221                         failure(new RuntimeException(
    222                                 "newMsg.callback is NOT null after copyFrom"));
    223                     }
    224 
    225                     mHandler.sendMessage(newMsg);
    226                 } else if (msg.what == 1) {
    227                     if (msg.isInUse() != true) {
    228                         failure(new RuntimeException(String.format(
    229                                 "msg.isInUse is false should be true after when processing %d",
    230                                 msg.what)));
    231                     }
    232                     if (msg.arg1 != 456) {
    233                         failure(new RuntimeException(String.format(
    234                                 "msg.arg1 is %d should be %d when processing # %d",
    235                                 msg.arg1, 456, msg.what)));
    236                     }
    237                     if (msg.arg2 != 789) {
    238                         failure(new RuntimeException(String.format(
    239                                 "msg.arg2 is %d should be %d when processing # %d",
    240                                 msg.arg2, 789, msg.what)));
    241                     }
    242                     if (msg.obj != this) {
    243                         failure(new RuntimeException(String.format(
    244                                 "msg.obj should be 'this' when processing # %d", msg.what)));
    245                     }
    246                     if (msg.replyTo != null) {
    247                         failure(new RuntimeException(String.format(
    248                                 "msg.replyTo should be null when processing # %d", msg.what)));
    249                     }
    250                     if (!msg.data.getString("key").equals(mBundle.getString("key"))) {
    251                         failure(new RuntimeException(String.format(
    252                                 "msg.data.getString(\"key\") is %s and does not equal" +
    253                                 " mBundle.getString(\"key\") which is %s when processing # %d",
    254                                 msg.data.getString("key"),  mBundle.getString("key"), msg.what)));
    255                     }
    256                     if (msg.when != 0) {
    257                         failure(new RuntimeException(String.format(
    258                                 "msg.when is %d should be 0 when processing # %d",
    259                                 msg.when, msg.what)));
    260                     }
    261                     if (msg.target != null) {
    262                         failure(new RuntimeException(String.format(
    263                                 "msg.target is NOT null when processing # %d", msg.what)));
    264                     }
    265                     if (msg.callback != null) {
    266                         failure(new RuntimeException(String.format(
    267                                 "msg.callback is NOT null when processing # %d", msg.what)));
    268                     }
    269                 } else {
    270                     failure(new RuntimeException(String.format(
    271                             "Unexpected msg.what is %d" + msg.what)));
    272                 }
    273             }
    274         };
    275 
    276         tester.doTest(1000);
    277     }
    278 }
    279