ContentObserver 在仪器测试期间从未收到 onChange 回调?
在为我的内容提供商编写测试时,我偶然发现了一个奇怪的问题。以下代码只是尝试在底层数据更改时验证对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在活动中使用访问器来获取处理程序的引用解决了该问题。定义处理程序后,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...