ContentObserver 在仪器测试期间从未收到 onChange 回调?

发布于 2024-10-17 16:01:09 字数 3132 浏览 2 评论 0原文

在为我的内容提供商编写测试时,我偶然发现了一个奇怪的问题。以下代码只是尝试在底层数据更改时验证对 my ContentObserver 的调用。但 onChange 回调 ContentObserverMock 永远不会被调用。如果我直接在 ContentResolver 或 Cursor 上尝试它也没有什么区别。这是我的测试:

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

提前致谢!

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:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2024-10-24 16:01:09

我通过在活动中使用访问器来获取处理程序的引用解决了该问题。定义处理程序后,ContentObserver 会像预期一样接收回调...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文