2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
27 public class MessageQueueTest extends TestCase {
29 private static class BaseTestHandler extends TestHandlerThread {
34 public BaseTestHandler() {
38 mHandler = new Handler() {
39 public void handleMessage(Message msg) {
40 BaseTestHandler.this.handleMessage(msg);
45 public void handleMessage(Message msg) {
47 failure(new RuntimeException(
48 "msg.isInuse is false, should always be true, #" + msg.what));
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) {
60 failure(new RuntimeException(
61 "Message received after done, #" + msg.what));
67 public void testMessageOrder() throws Exception {
68 TestHandlerThread tester = new BaseTestHandler() {
71 long now = SystemClock.uptimeMillis() + 200;
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);
86 public void testAtFrontOfQueue() throws Exception {
87 TestHandlerThread tester = new BaseTestHandler() {
90 long now = SystemClock.uptimeMillis() + 200;
93 mHandler.sendMessageAtTime(mHandler.obtainMessage(3), now);
94 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(2));
95 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(0));
98 public void handleMessage(Message msg) {
99 super.handleMessage(msg);
101 mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(1));
109 private static class TestFieldIntegrityHandler extends TestHandlerThread {
114 public TestFieldIntegrityHandler() {
118 mHandler = new Handler() {
119 public void handleMessage(Message msg) {
120 TestFieldIntegrityHandler.this.handleMessage(msg);
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));
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) {
140 failure(new RuntimeException(
141 "Message received after done, #" + msg.what));
147 public void testFieldIntegrity() throws Exception {
149 TestHandlerThread tester = new TestFieldIntegrityHandler() {
156 mHandler.sendMessage(mHandler.obtainMessage(0));
159 public void handleMessage(Message msg) {
160 super.handleMessage(msg);
168 mBundle = new Bundle();
170 msg.data.putString("key", "value");
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"));
178 if (newMsg.flags != 0) {
179 failure(new RuntimeException(String.format(
180 "newMsg.flags is %d should be 0 after copyFrom", newMsg.flags)));
182 if (newMsg.what != 1) {
183 failure(new RuntimeException(String.format(
184 "newMsg.what is %d should be %d after copyFrom", newMsg.what, 1)));
186 if (newMsg.arg1 != 456) {
187 failure(new RuntimeException(String.format(
188 "newMsg.arg1 is %d should be %d after copyFrom", msg.arg1, 456)));
190 if (newMsg.arg2 != 789) {
191 failure(new RuntimeException(String.format(
192 "newMsg.arg2 is %d should be %d after copyFrom", msg.arg2, 789)));
194 if (newMsg.obj != this) {
195 failure(new RuntimeException(
196 "newMsg.obj should be 'this' after copyFrom"));
198 if (newMsg.replyTo != null) {
199 failure(new RuntimeException(
200 "newMsg.replyTo should be null after copyFrom"));
202 if (newMsg.data == mBundle) {
203 failure(new RuntimeException(
204 "newMsg.data should NOT be mBundle after copyFrom"));
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"))));
212 if (newMsg.when != 0) {
213 failure(new RuntimeException(String.format(
214 "newMsg.when is %d should be 0 after copyFrom", newMsg.when)));
216 if (newMsg.target != mHandler) {
217 failure(new RuntimeException(
218 "newMsg.target is NOT mHandler after copyFrom"));
220 if (newMsg.callback != null) {
221 failure(new RuntimeException(
222 "newMsg.callback is NOT null after copyFrom"));
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",
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)));
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)));
242 if (msg.obj != this) {
243 failure(new RuntimeException(String.format(
244 "msg.obj should be 'this' when processing # %d", msg.what)));
246 if (msg.replyTo != null) {
247 failure(new RuntimeException(String.format(
248 "msg.replyTo should be null when processing # %d", msg.what)));
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)));
257 failure(new RuntimeException(String.format(
258 "msg.when is %d should be 0 when processing # %d",
259 msg.when, msg.what)));
261 if (msg.target != null) {
262 failure(new RuntimeException(String.format(
263 "msg.target is NOT null when processing # %d", msg.what)));
265 if (msg.callback != null) {
266 failure(new RuntimeException(String.format(
267 "msg.callback is NOT null when processing # %d", msg.what)));
270 failure(new RuntimeException(String.format(
271 "Unexpected msg.what is %d" + msg.what)));