Android, testing service from the test case!

Hello everybody,

This sample is specially created for Shivang Seth from Robotium conference and I would like to describe how to bind service from the test case! 🙂

First of all you will need:

1) Bind/unbind methods for service for setUp() and tearDown() methods accordingly;

2) Connection class to bind the service;

3) Binder implementation inside the service, should have public method ‘getService’ otherwise you will be not able to get the service outside the package:

public class MyServiceBinder extends Binder {

public MyService getService() {

return MyService.this;

}

}

Test case snippet:

package com.testforrobotium.mobileservice;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.IBinder;

import android.test.ActivityInstrumentationTestCase2;

import com.testforrobotium.mobile.tabs.MyService;

import com.testforrobotium.mobileutil.LoggerUtil;

public class MainTabActivityTest extends ActivityInstrumentationTestCase2 {

protected static String TAG = MainTabActivityTest.class.getName();

public MainTabActivityTest() {

super(“com.testforrobotium.mobileservice”, MainTabActivity.class);

}

public void setUp() throws Exception {

bindMyService();

}

private MyService mMyServiceBinder;

private boolean mIsServiceBound = false;

private ServiceConnection mConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

LoggerUtil.d(“onServiceConnected”);

mMyServiceBinder = ((MyService.MyServiceBinder) iBinder).getService();

LoggerUtil.d(“Is binder == null? “ + (mMyServiceBinder == null));

mIsServiceBound = true;

}

@Override

public void onServiceDisconnected(ComponentName componentName) {

LoggerUtil.d(“onServiceDisconnected”);

mMyServiceBinder = null;

mIsServiceBound = false;

}

};

private void bindMyService() {

Intent bindIntent = new Intent(getActivity(), MyService.class);

getActivity().bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);

if(mMyServiceBinder != null)

mMyServiceBinder.sayHelloInLog();

else // service binder is null at this point, is not instantiated yet

LoggerUtil.d(“Is binder == null? ” + (mMyServiceBinder == null));

}

private void unbindMyService() {

if(mIsServiceBound) {

getActivity().unbindService(mConnection);

LoggerUtil.d(“Unbinding service”);

mIsServiceBound = false;

}

}

public void testMobileActivity() throws Exception {

Thread.sleep(15100);

Intent startIntent = new Intent(getActivity(), MyService.class);

startIntent.putExtra(“cmd”, “start”);

getActivity().startService(startIntent);

Thread.sleep(5100);

mMyServiceBinder.sayHelloTestInLog();

Thread.sleep(5100);

Intent stopIntent = new Intent(getActivity(), MyService.class);

stopIntent.putExtra(“cmd”, “stopall”);

getActivity().startService(stopIntent);

Thread.sleep(5100);

}

@Override

public void tearDown() throws Exception {

unbindMyService();

super.tearDown();

}

}

Happy testing! (:

Best regards,

Yahor