Note that there are some explanatory texts on larger screens.

plurals
  1. POContentObserver never receives a onChange callback during instrumentation testing?
    text
    copied!<p>while writing tests for my content provider i stumbled upon a weird problem. The following code simply tries to verify a call on the my ContentObserver when the underlying data changes. But the onChange callback ContentObserverMock never gets invoked. It also makes no difference if i try it diretcly on the ContentResolver or the Cursor. Here is my test:</p> <pre><code>public class TestCursor extends AndroidTestCase { private class ContentObserverMock extends ContentObserver { public boolean cursorObserverIsTriggered = false; /** * @param handler */ public ContentObserverMock(Handler handler) { super(handler); } @Override public boolean deliverSelfNotifications() { return true; } /** * {@inheritDoc} */ @Override public void onChange(boolean selfChange) { Log.d(TestCursor.TAG, "ONCHANGE is called"); cursorObserverIsTriggered = true; super.onChange(selfChange); } } private final static String TAG = TestCursor.class.getSimpleName(); /** * {@inheritDoc} */ @Override protected void setUp() throws Exception { super.setUp(); Globals.setApplicationContext(getContext()); DummyDataDB.insertDummyDataIntoDB(); } /** * {@inheritDoc} */ @Override protected void tearDown() throws Exception { super.tearDown(); DummyDataDB.clearDB(); } @SmallTest public void testContentResolver() { ContentResolver resolver = getContext().getContentResolver(); Uri uri = MyContentProvider.CONTENT_URI; Handler handler = new Handler(); ContentObserverMock contentObserver = new ContentObserverMock(handler); resolver.registerContentObserver(uri, true, contentObserver); Cursor cursor = resolver.query(uri, null, null, null, null); Assert.assertNotNull(cursor); int count = cursor.getCount(); DomainObject contentStub = StubFactory.createContentStub(); ContentValues cv = HelperDomainObjectToContentValues.contentValuesFor(contentStub ); resolver.insert(uri, cv); cursor = resolver.query(uri, null, null, null, null); Assert.assertEquals(count, (cursor.getCount() - 1)); Assert.assertEquals(true, contentObserver.cursorObserverIsTriggered); } @SmallTest public void testCursor() { Log.d(TestCursor.TAG, "testCursor()"); DbHelper dbHelper = new DbHelper(getContext()); Cursor cursor = dbHelper.selectAllDomainObjects(); Handler handler = new Handler(); ContentObserverMock contentObserver = new ContentObserverMock(handler); Log.d(TestCursor.TAG, "registerContentObserver()"); cursor.registerContentObserver(contentObserver); DomainObject contentStub = StubFactory.createContentStub(); ContentValues cv = HelperDomainObjectToContentValues.contentValuesFor(contentStub ); dbHelper.writeDOmainObject(contentValues); Log.d(TestCursor.TAG, "cursor Requery()"); cursor.requery(); Assert.assertEquals(true, contentObserver.cursorObserverIsTriggered); } </code></pre> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload