Android的ServiceTestCase&LT即可;为MyService>发送消息给我的服务? [英] Can Android's ServiceTestCase<MyService> send Messages to my service?

查看:121
本文介绍了Android的ServiceTestCase&LT即可;为MyService>发送消息给我的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的绑定服务与ServiceTestCase。 该测试包括结合MyBindServer,并发送一个消息。 看着日志,就可以看到该服务启动时onBind()被调用, 并且消息从testAHello(),但是,服务器的handleMessage()函数发送永远不会被调用。

从日志中:

  I / TestRunner的(2099年):启动:testAHello(com.inthinc.mybindserver.test.MyBindServerTest)
I / MyBindServerTest(2099):设置()
I / MyBindServer(2099):onBind,行动= com.inthinc.mybindserver.START
I / MyBindServerTest(2099):testAHello
I / MyBindServerTest(2099):发送SAY_HELLO
[这里就是我希望看到的handleMessage()的输出]
I / MyBindServerTest(2099):tearDown的()
I / TestRunner的(2099年):完成:testAHello(com.inthinc.mybindserver.test.MyBindServerTest)
I / TestRunner的(2099):通过:testAHello(com.inthinc.mybindserver.test.MyBindServerTest)
 

下面是$ C $下MyBindServer.java:

 包com.inthinc.mybindserver;

进口android.app.Service;
进口android.content.Intent;
进口android.os.Handler;
进口android.os.IBinder;
进口android.os.Message;
进口android.os.Messenger;
进口android.util.Log;

公共类MyBindServer延伸服务{
    static final的字符串变量=MyBindServer;
    公共静态最终诠释MSG_SAY_HELLO = 1;
    最后的使者mMessenger =新使者(新IncomingHandler());

    类IncomingHandler扩展处理程序{
        @覆盖
        公共无效的handleMessage(信息MSG){
            Log.i(TAG,的String.Format(的handleMessage,什么=%D,msg.what));
            开关(msg.what){
                案例MSG_SAY_HELLO:
                    Log.i(TAG,你好);
                    打破;
                默认:
                    super.handleMessage(MSG);
            }
        }
    }

    @覆盖
    公众的IBinder onBind(意向意图){
        Log.i(TAG,的String.Format(onBind,行动=%s时,intent.getAction()));
        返回mMessenger.getBinder();
    }

}
 

下面是$ C $下MyBindServerTest.java:

 包com.inthinc.mybindserver.test;

进口com.inthinc.mybindserver.MyBindServer;

进口android.content.Intent;
进口android.os.IBinder;
进口android.os.Message;
进口android.os.Messenger;
进口android.os.RemoteException;
进口android.test.ServiceTestCase;
进口android.test.suitebuilder.annotation.SmallTest;
进口android.util.Log;

公共类MyBindServerTest扩展ServiceTestCase< MyBindServer> {
    私有静态最后字符串变量=MyBindServerTest;
    信使mServer上= NULL;

    公共MyBindServerTest(){
        超(MyBindServer.class);
    }

    公共MyBindServerTest(类< MyBindServer> serviceClass){
        超(serviceClass);
    }

    @覆盖
    公共无效设置(){
        尝试 {
            super.setUp();
            Log.i(TAG,设置());
            意图bindIntent =新的意向书(com.inthinc.mybindserver.START);
            的IBinder粘合剂= bindService(bindIntent);
            assertNotNull(粘合剂);
            mServer上=新的信使(粘合剂);
        }赶上(例外五){
            e.printStackTrace();
        }
    }

    @覆盖
    公共无效的teardown(){
        尝试 {
            super.tearDown();
            Log.i(TAG,tearDown的());
        }赶上(例外五){
            e.printStackTrace();
        }
    }

    @SmallTest
    公共无效testAHello(){
        Log.i(TAG,testAHello);
        assertNotNull(mServer上);
        消息味精= Message.obtain(空,MyBindServer.MSG_SAY_HELLO);
        Log.i(TAG,送SAY_HELLO);
        尝试 {
            mServer.send(MSG);
        }赶上(RemoteException的E){
            e.printStackTrace();
        }
    }

}
 

解决方案

我能得到使用过程below..anyone欢迎附和,如果这是不正确这个工作,但上述工程的例子(即MyBindServer的处理程序接收消息)

这好像ServiceTestCase的bindService()方法打算像本地服务。在这种情况下,目标是测试作为一个单独的过程,这意味着使用以下代替ServiceTestCase的bindService的:

 意图bindIntent =新的意图(小于注册意图>); //凡注册意图在清单文件中声明
。的getContext()bindService(bindIntent,mConn,Context.BIND_AUTO_CREATE);
 

在这里mConn是实现做ServiceConnection对象,无论您的测试需要它做,在上述情况下,设置mServer上。

通过上述,MyBindServer的handleMessage()函数被调用的testAHello()测试。

更新:我注意到,这取决于如何快速检测处理完成,拆除()可以调用之前绑定就可以使用了。在上面加控制变量油门基于mConn的onServiceConnected程序流程的情况下被调用提供了一致的结果。

例如。

 保护布尔约束= FALSE;
处理= FALSE保护的布尔值;
私人ServiceConnection mConnection =新ServiceConnection(){
    公共无效onServiceConnected(组件名类名,
            的IBinder服务){
        Log.i(TAG,服务conn将);

        mServer上=新的信使(服务);
        如果(mServer上!= NULL
                &功放;&安培; mServer上!= NULL){
            势必= TRUE;
        }
        处理=真;
    }

@覆盖
公共无效onServiceDisconnected(组件名名){
    // TODO自动生成方法存根
    Log.i(TAG,服务DISCONN);
}
};
 

然后添加:

 而(!处理){
尝试 {
   视频下载(1000);
   }赶上(InterruptedException异常E){
  // TODO自动生成的catch块
  e.printStackTrace();
   }
}
 

要testAHello()

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.

From the logs:

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)

Here is the code for 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();
    }

}

Here is the code for 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();
        }
    }

}

解决方案

I was able to get this working using the procedure below..anyone is welcome to chime in if this is incorrect, but the example above works (i.e. MyBindServer's handler receives messages)

It seems as though ServiceTestCase's bindService() method intends to act like a local service. In this case, the goal is to test as a separate process, which means using the following instead of ServiceTestCase's bindService:

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

where mConn is a ServiceConnection object implemented to do whatever your test needs it to do, in the case above, set mServer.

With the above, MyBindServer's handleMessage() is called for the testAHello() test.

UPDATE: I have noticed that depending on how quickly the test processing is done, teardown() can be called before the binding is ready to use. In the case above adding control variables to throttle the program flow based on mConn's onServiceConnected being called provided consistent results.

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

Then add:

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

to testAHello()

这篇关于Android的ServiceTestCase&LT即可;为MyService&GT;发送消息给我的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆