5

How to test my IBinder object that Service return on onBind ?

Ravi
  • 34,851
  • 21
  • 122
  • 183
alexkipar
  • 299
  • 4
  • 15

1 Answers1

3

It's according to the remote interface that you use between your context and the service (in remote call scenario). For example you can do like this:

IBinder service = this.bindService(new Intent(TestService.class.getName()));
assertNotNull(service);
assertTrue(service instanceof ITestServiceCall); //see if the service returns the correct interface
ITestServiceCall iTestServiceCall = ITestServiceCall.Stub.asInterface(service);
assertNotNull(iTestServiceCall);
iTestServiceCall.doSomething();

The ITestServiceCall is the interface that you define in an AIDL file (ITestServiceCall.aidl).

But before this can work you have to make sure your service returns the Stub of your interface correctly on onBind().

I hope this can help.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
Surya Wijaya Madjid
  • 1,823
  • 1
  • 19
  • 25
  • it's a bad idea to test your binder right after having called bindService. This method is asynchronous and returns nothing interesting before your ServiceConnection's onServiceConnected is called – Snicolas Feb 28 '12 at 13:33
  • 1
    There is no `ServiceConnection.onServiceConnected()` used in ServiceTestCase, that's why we get the IBinder object immediately after the `bindService()`. This is different from the actual service flow I think. – Surya Wijaya Madjid Feb 29 '12 at 15:15