Friday, May 21, 2010

[android-developers] ServiceTestCase does not notify Observers and does not call onStartCommand

Hi,

I am currently trying to test a Service class using ServiceTestCase
and I am facing some problems with it. My Service class is responsible
to do some queries on another application ContentProvider, updating my
own application DB when the other one changes. It is like a sync
service that the application depends on.

The first thing I noticed while using the ServiceTestCase is that it
does not call the onStartCommand method, that causes my service to not
initialize itself correctly. I did that manually and everything seems
to work fine, except one important part. My service ContentObservers,
that are registered on the cursor returned by the queries I do when
the onStartCommand runs, never gets the onChange() method called, even
when I manually update the other applications database using its
content provider.

To check that this is not a bug of the other ContentProvider, I
registered a similar ContentObserver inside my test and called
notifyChange on the same URI I registered using the content resolver
registerContentObserver method.

The setup() and tearDown() methods just call super() and I use the
getSystemContext() to retrieve the context. I am also using the
default MockApplication class.

Is there any known issue with ServiceTestCase that causes that?

Below is a snippet of the misbehaving code. onChange log is never
printed using logcat

...
public void testOne() {
...
Intent startServiceIntent = new Intent(ACTION);
startService(startServiceIntent);

// Call manually onStartCommand since it is not being called by
the test case automatically
getService().onStartCommand(startServiceIntent, 0, 0);

Log.d("UT","testOne");

mContext.getContentResolver.registerContentObserver(uri, true, new
ContentObserver(new Handler()){
@Override
public void onChange(boolean selfChange) {
Log.d("UT","Uri observer called");
}
});

mContext.getContentResolver().notifyChange(uri, null, false);

...
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment