Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Android's ServiceTestCase<MyService> send Messages to my service?
    primarykey
    data
    text
    <p>I want to test my bound service with ServiceTestCase. The testing consists of binding to MyBindServer, and sending a Message. Watching the logs, you can see the service is started when onBind() is called, and a message is sent from testAHello(), but, the server's handleMessage() is never called.</p> <p>From the logs:</p> <pre><code>I/TestRunner( 2099): started: testAHello(com.inthinc.mybindserver.test.MyBindServerTest) I/MyBindServerTest( 2099): setUp() I/MyBindServer( 2099): onBind, action=com.inthinc.mybindserver.START I/MyBindServerTest( 2099): testAHello I/MyBindServerTest( 2099): sending SAY_HELLO [here is where I expect to see the output from handleMessage()] I/MyBindServerTest( 2099): tearDown() I/TestRunner( 2099): finished:testAHello(com.inthinc.mybindserver.test.MyBindServerTest) I/TestRunner( 2099): passed: testAHello(com.inthinc.mybindserver.test.MyBindServerTest) </code></pre> <p>Here is the code for MyBindServer.java:</p> <pre><code>package com.inthinc.mybindserver; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.util.Log; public class MyBindServer extends Service { static final String TAG = "MyBindServer"; public static final int MSG_SAY_HELLO = 1; final Messenger mMessenger = new Messenger(new IncomingHandler()); class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { Log.i(TAG, String.format("handleMessage, what=%d", msg.what)); switch (msg.what) { case MSG_SAY_HELLO: Log.i(TAG, "hello"); break; default: super.handleMessage(msg); } } } @Override public IBinder onBind(Intent intent) { Log.i(TAG, String.format("onBind, action=%s", intent.getAction())); return mMessenger.getBinder(); } } </code></pre> <p>Here is the code for MyBindServerTest.java:</p> <pre><code>package com.inthinc.mybindserver.test; import com.inthinc.mybindserver.MyBindServer; import android.content.Intent; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.test.ServiceTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.util.Log; public class MyBindServerTest extends ServiceTestCase&lt;MyBindServer&gt; { private static final String TAG = "MyBindServerTest"; Messenger mServer = null; public MyBindServerTest() { super(MyBindServer.class); } public MyBindServerTest(Class&lt;MyBindServer&gt; serviceClass) { super(serviceClass); } @Override public void setUp() { try { super.setUp(); Log.i(TAG, "setUp()"); Intent bindIntent = new Intent("com.inthinc.mybindserver.START"); IBinder binder = bindService(bindIntent); assertNotNull(binder); mServer = new Messenger(binder); } catch (Exception e) { e.printStackTrace(); } } @Override public void tearDown() { try { super.tearDown(); Log.i(TAG, "tearDown()"); } catch (Exception e) { e.printStackTrace(); } } @SmallTest public void testAHello() { Log.i(TAG, "testAHello"); assertNotNull(mServer); Message msg = Message.obtain(null, MyBindServer.MSG_SAY_HELLO); Log.i(TAG, "sending SAY_HELLO"); try { mServer.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload