Search in sources :

Example 66 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DownloadManagerFunctionalTest method testRemoveDownload.

/**
     * Tests that we can remove a download from the download manager.
     */
@LargeTest
public void testRemoveDownload() throws Exception {
    int fileSize = 1024;
    byte[] blobData = generateData(fileSize, DataType.BINARY);
    long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
    Cursor cursor = mDownloadManager.query(new Query().setFilterById(dlRequest));
    try {
        assertEquals("The count of downloads with this ID is not 1!", 1, cursor.getCount());
        mDownloadManager.remove(dlRequest);
        cursor.requery();
        assertEquals("The count of downloads with this ID is not 0!", 0, cursor.getCount());
    } finally {
        cursor.close();
    }
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 67 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DownloadManagerStressTest method testMultipleDownloads.

/**
     * Attempts to download several files simultaneously
     */
@LargeTest
public void testMultipleDownloads() throws Exception {
    // need to be sure all current downloads have stopped first
    removeAllCurrentDownloads();
    int NUM_FILES = 10;
    // 10 kb
    int MAX_FILE_SIZE = 10 * 1024;
    Random r = new LoggingRng();
    for (int i = 0; i < NUM_FILES; ++i) {
        int size = r.nextInt(MAX_FILE_SIZE);
        byte[] blobData = generateData(size, DataType.TEXT);
        Uri uri = getServerUri(DEFAULT_FILENAME + i);
        Request request = new Request(uri);
        request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));
        // Prepare the mock server with a standard response
        enqueueResponse(buildResponse(HTTP_OK, blobData));
        long requestID = mDownloadManager.enqueue(request);
    }
    waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
    Cursor cursor = mDownloadManager.query(new Query());
    try {
        assertEquals(NUM_FILES, cursor.getCount());
        if (cursor.moveToFirst()) {
            do {
                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
                String errorString = String.format("File %s failed to download successfully. Status code: %d", filename, status);
                assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
            } while (cursor.moveToNext());
        }
        assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
    } finally {
        cursor.close();
    }
}
Also used : Random(java.util.Random) Query(android.app.DownloadManager.Query) Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 68 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testRequeryWithSelectionArgs.

@MediumTest
public void testRequeryWithSelectionArgs() throws Exception {
    populateDefaultTable();
    Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = ?", new String[] { sString1 });
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    c.deactivate();
    c.requery();
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    assertEquals(sString1, c.getString(0));
    c.close();
}
Also used : SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 69 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testCursor1.

@MediumTest
public void testCursor1() throws Exception {
    populateDefaultTable();
    Cursor c = mDatabase.query("test", null, null, null, null, null, null);
    int dataColumn = c.getColumnIndexOrThrow("data");
    // The cursor should ignore text before the last period when looking for a column. (This
    // is a temporary hack in all implementations of getColumnIndex.)
    int dataColumn2 = c.getColumnIndexOrThrow("junk.data");
    assertEquals(dataColumn, dataColumn2);
    assertSame(3, c.getCount());
    assertTrue(c.isBeforeFirst());
    try {
        c.getInt(0);
        fail("CursorIndexOutOfBoundsException expected");
    } catch (CursorIndexOutOfBoundsException ex) {
    // expected
    }
    c.moveToNext();
    assertEquals(1, c.getInt(0));
    String s = c.getString(dataColumn);
    assertEquals(sString1, s);
    c.moveToNext();
    s = c.getString(dataColumn);
    assertEquals(sString2, s);
    c.moveToNext();
    s = c.getString(dataColumn);
    assertEquals(sString3, s);
    c.moveToPosition(-1);
    c.moveToNext();
    s = c.getString(dataColumn);
    assertEquals(sString1, s);
    c.moveToPosition(2);
    s = c.getString(dataColumn);
    assertEquals(sString3, s);
    int i;
    for (c.moveToFirst(), i = 0; !c.isAfterLast(); c.moveToNext(), i++) {
        c.getInt(0);
    }
    assertEquals(3, i);
    try {
        c.getInt(0);
        fail("CursorIndexOutOfBoundsException expected");
    } catch (CursorIndexOutOfBoundsException ex) {
    // expected
    }
    c.close();
}
Also used : CursorIndexOutOfBoundsException(android.database.CursorIndexOutOfBoundsException) SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 70 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DatabaseCursorTest method testBlob.

@MediumTest
public void testBlob() throws Exception {
    // create table
    mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, s TEXT, d REAL, l INTEGER, b BLOB);");
    // insert blob
    Object[] args = new Object[4];
    byte[] blob = new byte[1000];
    byte value = 99;
    Arrays.fill(blob, value);
    args[3] = blob;
    String s = new String("text");
    args[0] = s;
    Double d = 99.9;
    args[1] = d;
    Long l = (long) 1000;
    args[2] = l;
    String sql = "INSERT INTO test (s, d, l, b) VALUES (?,?,?,?)";
    mDatabase.execSQL(sql, args);
    // use cursor to access blob
    Cursor c = mDatabase.query("test", null, null, null, null, null, null);
    c.moveToNext();
    ContentValues cv = new ContentValues();
    DatabaseUtils.cursorRowToContentValues(c, cv);
    int bCol = c.getColumnIndexOrThrow("b");
    int sCol = c.getColumnIndexOrThrow("s");
    int dCol = c.getColumnIndexOrThrow("d");
    int lCol = c.getColumnIndexOrThrow("l");
    byte[] cBlob = c.getBlob(bCol);
    assertTrue(Arrays.equals(blob, cBlob));
    assertEquals(s, c.getString(sCol));
    assertEquals((double) d, c.getDouble(dCol));
    assertEquals((long) l, c.getLong(lCol));
}
Also used : ContentValues(android.content.ContentValues) SQLiteCursor(android.database.sqlite.SQLiteCursor) Cursor(android.database.Cursor) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Aggregations

Cursor (android.database.Cursor)4002 ArrayList (java.util.ArrayList)547 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)527 Uri (android.net.Uri)467 ContentValues (android.content.ContentValues)334 ContentResolver (android.content.ContentResolver)193 Test (org.junit.Test)183 RemoteException (android.os.RemoteException)182 File (java.io.File)170 IOException (java.io.IOException)159 MatrixCursor (android.database.MatrixCursor)154 Intent (android.content.Intent)140 SQLException (android.database.SQLException)126 MediumTest (android.test.suitebuilder.annotation.MediumTest)116 HashMap (java.util.HashMap)108 SQLiteException (android.database.sqlite.SQLiteException)94 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)93 SQLiteCursor (android.database.sqlite.SQLiteCursor)88 Query (android.app.DownloadManager.Query)76 MergeCursor (android.database.MergeCursor)75