开发者

ContentObserver never receives a onChange callback during instrumentation testing?

开发者 https://www.devze.com 2023-02-11 03:50 出处:网络
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 onChan

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 t开发者_如何学Cest:

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);
    }

Thanks in advance!


I resolved the issue with using an accessor on my activity to get a reference on the handler. With the handler defined the ContentObserver receives the callback like expected...

0

精彩评论

暂无评论...
验证码 换一张
取 消