package android.database.cts;
import dalvik.annotation.TestLevel;
import dalvik.annotation.TestTargetClass;
import dalvik.annotation.TestTargetNew;
import dalvik.annotation.TestTargets;
import android.content.ContentResolver;
import android.content.Context;
import android.content.IContentProvider;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.database.DataSetObserver;
import android.database.StaleDataException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.test.AndroidTestCase;
import java.io.File;
import java.util.Arrays;
@TestTargetClass(android.database.CursorWrapper.class)
public class CursorWrapperTest extends AndroidTestCase {
private static final String FIRST_NUMBER = "123";
private static final String SECOND_NUMBER = "5555";
private static final int TESTVALUE1 = 199;
private static final int TESTVALUE2 = 200;
private static final String[] NUMBER_PROJECTION = new String[] {
"_id", // 0
"number" // 1
};
private static final int DEFAULT_RECORD_COUNT = 2;
private static final int DEFAULT_COLUMN_COUNT = 2;
private SQLiteDatabase mDatabase;
private File mDatabaseFile;
private Cursor mCursor;
private static final int CURRENT_DATABASE_VERSION = 42;
@Override
protected void setUp() throws Exception {
super.setUp();
setupDatabase();
}
@Override
protected void tearDown() throws Exception {
closeDatabase();
super.tearDown();
}
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test close() and isClosed() and the constructor method.",
method = "close",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test close() and isClosed() and the constructor method.",
method = "isClosed",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test close() and isClosed() and the constructor method.",
method = "requery",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test close() and isClosed() and the constructor method.",
method = "CursorWrapper",
args = {android.database.Cursor.class}
)
})
public void testConstrucotorAndClose() {
CursorWrapper cursorWrapper = new CursorWrapper(getCursor());
assertTrue(cursorWrapper.requery());
cursorWrapper.deactivate();
cursorWrapper.move(1);
assertEquals(DEFAULT_RECORD_COUNT, cursorWrapper.getCount());
assertFalse(cursorWrapper.isClosed());
assertTrue(cursorWrapper.requery());
cursorWrapper.close();
assertTrue(cursorWrapper.isClosed());
assertFalse(cursorWrapper.requery());
}
private Cursor getCursor() {
Cursor cursor = mDatabase.query("test1", NUMBER_PROJECTION, null, null, null, null, null);
return cursor;
}
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "getCount",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "requery",
args = {}
)
})
public void testGetCount() {
CursorWrapper cursorWrapper = new CursorWrapper(getCursor());
int defaultCount = cursorWrapper.getCount();
// Add two records into the table.
addWithValue(mDatabase, TESTVALUE1);
int expected = defaultCount + 1;
assertTrue(cursorWrapper.requery());
assertEquals(expected, cursorWrapper.getCount());
addWithValue(mDatabase, TESTVALUE2);
expected += 1;
assertTrue(cursorWrapper.requery());
assertEquals(expected, cursorWrapper.getCount());
// Delete previous two records which have been added just now.
deleteWithValue(mDatabase, TESTVALUE1);
assertTrue(cursorWrapper.requery());
assertEquals(defaultCount + 1, cursorWrapper.getCount());
deleteWithValue(mDatabase, TESTVALUE2);
assertTrue(cursorWrapper.requery());
assertEquals(defaultCount, cursorWrapper.getCount());
// Continue to delete all the records
deleteAllRecords(mDatabase);
assertTrue(cursorWrapper.requery());
assertEquals(0, cursorWrapper.getCount());
cursorWrapper.close();
assertFalse(cursorWrapper.requery());
// Restore original database status
rebuildDatabase();
}
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "deactivate",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "registerDataSetObserver",
args = {android.database.DataSetObserver.class}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "unregisterDataSetObserver",
args = {android.database.DataSetObserver.class}
)
})
public void testDeactivate() throws IllegalStateException {
CursorWrapper cursorWrapper = new CursorWrapper(getCursor());
MockObserver observer = new MockObserver();
// one DataSetObserver can't unregistered before it had been registered.
try{
cursorWrapper.unregisterDataSetObserver(observer);
fail("testUnregisterDataSetObserver failed");
}catch(IllegalStateException e){
}
// Before registering, observer can't be notified.
assertFalse(observer.hasInvalidated());
cursorWrapper.moveToLast();
cursorWrapper.deactivate();
assertFalse(observer.hasInvalidated());
// Test with registering DataSetObserver
assertTrue(cursorWrapper.requery());
cursorWrapper.registerDataSetObserver(observer);
assertFalse(observer.hasInvalidated());
cursorWrapper.moveToLast();
assertEquals(Integer.parseInt(SECOND_NUMBER), cursorWrapper.getInt(1));
cursorWrapper.deactivate();
// deactivate method can invoke invalidate() method, can be observed by DataSetObserver.
assertTrue(observer.hasInvalidated());
// After deactivating, the cursor can not provide values from database record.
try {
cursorWrapper.getInt(1);
fail("After deactivating, cursor cannot execute getting value operations.");
} catch (StaleDataException e) {
}
// Can't register a same observer twice before unregister it.
try{
cursorWrapper.registerDataSetObserver(observer);
fail("testRegisterDataSetObserver failed");
}catch(IllegalStateException e){
}
// After runegistering, observer can't be notified.
cursorWrapper.unregisterDataSetObserver(observer);
observer.resetStatus();
assertFalse(observer.hasInvalidated());
cursorWrapper.moveToLast();
cursorWrapper.deactivate();
assertFalse(observer.hasInvalidated());
// one DataSetObserver can't be unregistered twice continuously.
try{
cursorWrapper.unregisterDataSetObserver(observer);
fail("testUnregisterDataSetObserver failed");
}catch(IllegalStateException e){
}
}
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test getColumnCount()",
method = "getColumnCount",
args = {}
),