2011-07-07 38 views
5

我想用ServiceTestCase測試我的綁定服務。 測試包括綁定到MyBindServer,併發送消息。 看着日誌,你可以看到服務在調用onBind()時啓動, ,並且從testAHello()發送了一條消息,但是服務器的handleMessage()永遠不會被調用。Android的ServiceTestCase <MyService>可以向我的服務發送消息嗎?

從日誌:

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) 

這裏是MyBindServer.java代碼:

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(); 
    } 

} 

這裏是MyBindServerTest.java代碼:

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<MyBindServer> { 
    private static final String TAG = "MyBindServerTest"; 
    Messenger mServer = null; 

    public MyBindServerTest() { 
     super(MyBindServer.class); 
    } 

    public MyBindServerTest(Class<MyBindServer> 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(); 
     } 
    } 

} 

回答

3

我能使用下面的程序來獲得這個工作。如果這是不正確的,歡迎任何人加入,但上面的例子工作(即, .E。 MyBindServer的處理程序接收消息)

看起來好像ServiceTestCase的bindService()方法打算像本地服務一樣。在這種情況下,我們的目標是測試作爲一個單獨的進程,這意味着使用替代ServiceTestCase的bindService以下:

Intent bindIntent = new Intent(<registered intent>); //Where registered intent is declared in the manifest file 
getContext().bindService(bindIntent,mConn,Context.BIND_AUTO_CREATE); 

其中mConn是實現做任何測試需要它做ServiceConnection對象,在上面的情況,設置mServer。

以上所述,爲testAHello()測試調用了MyBindServer的handleMessage()。

更新:我已經注意到,根據測試處理的完成速度,可以在綁定準備就緒之前調用teardown()。在上面的情況下,添加控制變量來節制基於mConn的onServiceConnected被調用的程序流提供了一致的結果。

E.g.

protected boolean bound = false; 
protected boolean processed = false; 
private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, 
      IBinder service) { 
     Log.i(TAG,"Service conn"); 

     mServer = new Messenger(service); 
     if(mServer != null 
       && mServer != null){ 
      bound = true; 
     } 
     processed = true; 
    } 

@Override 
public void onServiceDisconnected(ComponentName name) { 
    // TODO Auto-generated method stub 
    Log.i(TAG,"Service Disconn"); 
} 
}; 

然後加:

while(!processed){  
try { 
    Thread.sleep(1000); 
    } catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
} 

到testAHello()

+0

你說的是 「登記的意圖」 是什麼意思? – serj

相關問題