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.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.ServiceConnection; 23 import android.os.RemoteException; 24 import android.os.Handler; 25 import android.os.IBinder; 26 import android.os.Message; 27 import android.os.Messenger; 28 import android.test.AndroidTestCase; 29 import android.test.suitebuilder.annotation.MediumTest; 30 31 public class MessengerTest extends AndroidTestCase { 32 private Messenger mServiceMessenger; 33 34 private ServiceConnection mConnection = new ServiceConnection() { 35 public void onServiceConnected(ComponentName name, IBinder service) { 36 synchronized (MessengerTest.this) { 37 mServiceMessenger = new Messenger(service); 38 MessengerTest.this.notifyAll(); 39 } 40 } 41 public void onServiceDisconnected(ComponentName name) { 42 mServiceMessenger = null; 43 } 44 }; 45 46 private class TestThread extends TestHandlerThread { 47 private Handler mTestHandler; 48 private Messenger mTestMessenger; 49 50 public void go() { 51 synchronized (MessengerTest.this) { 52 mTestHandler = new Handler() { 53 public void handleMessage(Message msg) { 54 TestThread.this.handleMessage(msg); 55 } 56 }; 57 mTestMessenger = new Messenger(mTestHandler); 58 TestThread.this.executeTest(); 59 } 60 } 61 62 public void executeTest() { 63 Message msg = Message.obtain(); 64 msg.arg1 = 100; 65 msg.arg2 = 1000; 66 msg.replyTo = mTestMessenger; 67 try { 68 mServiceMessenger.send(msg); 69 } catch (RemoteException e) { 70 } 71 } 72 73 public void handleMessage(Message msg) { 74 if (msg.arg1 != 100) { 75 failure(new RuntimeException( 76 "Message.arg1 is not 100: " + msg.arg1)); 77 return; 78 } 79 if (msg.arg2 != 1000) { 80 failure(new RuntimeException( 81 "Message.arg2 is not 1000: " + msg.arg2)); 82 return; 83 } 84 if (!mTestMessenger.equals(msg.replyTo)) { 85 failure(new RuntimeException( 86 "Message.replyTo is not me: " + msg.replyTo)); 87 return; 88 } 89 success(); 90 } 91 }; 92 93 @Override 94 protected void setUp() throws Exception { 95 super.setUp(); 96 getContext().bindService(new Intent(mContext, MessengerService.class), 97 mConnection, Context.BIND_AUTO_CREATE); 98 synchronized (this) { 99 while (mServiceMessenger == null) { 100 try { 101 wait(); 102 } catch (InterruptedException e) { 103 } 104 } 105 } 106 } 107 108 @Override 109 protected void tearDown() throws Exception { 110 super.tearDown(); 111 getContext().unbindService(mConnection); 112 } 113 114 @MediumTest 115 public void testSend() { 116 (new TestThread()).doTest(1000); 117 118 } 119 } 120