I am using Robolectric 3.4.2 and I need to test the interaction between two services.
In my test I wrote a dummy service:
ShadowApplication.getInstance().setComponentNameAndServiceForBindService(
new ComponentName(SERVICE.getPackageName(), SERVICE.getClassName()),
new Binder() {
@Override
public IInterface queryLocalInterface(final String descriptor) {
return null;
}
}
);
and it works if I invoke BindService directly from my test case, but if the call to bindService is in a different thread (like in the real application), the onServiceConnected() callback is never called.
ServiceConnection connection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
try {
doSomething();
} catch (Exception e) {
Log.e(TAG, "Cannot Do Something", e);
}
mContext.unbindService(this);
}
@Override
public void onServiceDisconnected(ComponentName name) { }
};
new Thread(new Runnable() {
@Override
public void run() {
mContext.bindService(SERVICE.createIntent()), connection, Context.BIND_AUTO_CREATE);
}
}).start();
Am I doing something wrong, or is it expected to work this way?