// Make sure we can still delete after adding/updating stuff // // Student: Uncomment this test after you have completed writing the insert functionality // in your provider. It relies on insertions with testInsertReadProvider, so insert and // query functionality must also be complete before this test can be used. public void testInsertReadProvider() { ContentValues testValues = TestUtilities.createMovie(); // Register a content observer for our insert. This time, directly with the content resolver TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver(); mContext.getContentResolver().registerContentObserver(MovieEntry.CONTENT_URI, true, tco); Uri movieUri = mContext.getContentResolver().insert(MovieEntry.CONTENT_URI, testValues); // Did our content observer get called? Students: If this fails, your insert movie // isn't calling getContext().getContentResolver().notifyChange(uri, null); tco.waitForNotificationOrFail(); mContext.getContentResolver().unregisterContentObserver(tco); long movieRowId = ContentUris.parseId(movieUri); // Verify we got a row back. assertTrue(movieRowId != -1); // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made // the round trip. // A cursor is your primary interface to the query results. Cursor cursor = mContext .getContentResolver() .query( MovieEntry.CONTENT_URI, null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null // sort order ); TestUtilities.validateCursor( "testInsertReadProvider. Error validating MovieEntry.", cursor, testValues); }
/* This test uses the provider to insert and then update the data. Uncomment this test to see if your update movie is functioning correctly. */ public void testUpdateMovie() { // Create a new map of values, where column names are the keys ContentValues values = TestUtilities.createMovie(); Uri movieUri = mContext.getContentResolver().insert(MovieEntry.CONTENT_URI, values); long movieRowId = ContentUris.parseId(movieUri); // Verify we got a row back. assertTrue(movieRowId != -1); Log.d(LOG_TAG, "New row id: " + movieRowId); ContentValues updatedValues = new ContentValues(values); updatedValues.put(MovieEntry._ID, movieRowId); updatedValues.put(MovieEntry.COLUMN_MOVIE_TITLE, "Dhoom 3"); // Create a cursor with observer to make sure that the content provider is notifying // the observers as expected Cursor movieCursor = mContext.getContentResolver().query(MovieEntry.CONTENT_URI, null, null, null, null); TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver(); movieCursor.registerContentObserver(tco); int count = mContext .getContentResolver() .update( MovieEntry.CONTENT_URI, updatedValues, MovieEntry._ID + "= ?", new String[] {Long.toString(movieRowId)}); assertEquals(count, 1); // Test to make sure our observer is called. If not, we throw an assertion. // // Students: If your code is failing here, it means that your content provider // isn't calling getContext().getContentResolver().notifyChange(uri, null); tco.waitForNotificationOrFail(); movieCursor.unregisterContentObserver(tco); movieCursor.close(); // A cursor is your primary interface to the query results. Cursor cursor = mContext .getContentResolver() .query( MovieEntry.CONTENT_URI, null, // projection MovieEntry._ID + " = " + movieRowId, null, // Values for the "where" clause null // sort order ); TestUtilities.validateCursor( "testUpdatemovie. Error validating movie entry update.", cursor, updatedValues); cursor.close(); }
// Make sure we can still delete after adding/updating stuff // // Student: Uncomment this test after you have completed writing the delete functionality // in your provider. It relies on insertions with testInsertReadProvider, so insert and // query functionality must also be complete before this test can be used. public void testDeleteRecords() { testInsertReadProvider(); // Register a content observer for our Movie delete. TestUtilities.TestContentObserver movieObserver = TestUtilities.getTestContentObserver(); mContext .getContentResolver() .registerContentObserver(MovieEntry.CONTENT_URI, true, movieObserver); deleteAllRecordsFromProvider(); // Students: If either of these fail, you most-likely are not calling the // getContext().getContentResolver().notifyChange(uri, null); in the ContentProvider // delete. (only if the insertReadProvider is succeeding) movieObserver.waitForNotificationOrFail(); mContext.getContentResolver().unregisterContentObserver(movieObserver); }
// Student: Uncomment this test after you have completed writing the BulkInsert functionality // in your provider. Note that this test will work with the built-in (default) provider // implementation, which just inserts records one-at-a-time, so really do implement the // BulkInsert ContentProvider function. public void testBulkInsert() { // first, let's create a movie value ContentValues testValues = TestUtilities.createMovie(); Uri movieUri = mContext.getContentResolver().insert(MovieEntry.CONTENT_URI, testValues); long movieRowId = ContentUris.parseId(movieUri); // Verify we got a row back. assertTrue(movieRowId != -1); // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made // the round trip. // A cursor is your primary interface to the query results. Cursor cursor = mContext .getContentResolver() .query( MovieEntry.CONTENT_URI, null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null // sort order ); TestUtilities.validateCursor( "testBulkInsert. Error validating MovieEntry.", cursor, testValues); // Now we can bulkInsert some movie. In fact, we only implement BulkInsert for movie // entries. With ContentProviders, you really only have to implement the features you // use, after all. ContentValues[] bulkInsertContentValues = createBulkInsertMovieValues(); // Register a content observer for our bulk insert. TestUtilities.TestContentObserver movieObserver = TestUtilities.getTestContentObserver(); mContext .getContentResolver() .registerContentObserver(MovieEntry.CONTENT_URI, true, movieObserver); int insertCount = mContext.getContentResolver().bulkInsert(MovieEntry.CONTENT_URI, bulkInsertContentValues); // Students: If this fails, it means that you most-likely are not calling the // getContext().getContentResolver().notifyChange(uri, null); in your BulkInsert // ContentProvider method. movieObserver.waitForNotificationOrFail(); mContext.getContentResolver().unregisterContentObserver(movieObserver); assertEquals(insertCount, BULK_INSERT_RECORDS_TO_INSERT); // A cursor is your primary interface to the query results. cursor = mContext .getContentResolver() .query( MovieEntry.CONTENT_URI, null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause MovieEntry.COLUMN_MOVIE_RELEASE_DATE + " ASC" // sort order == by DATE ASCENDING ); // we should have as many records in the database as we've inserted assertEquals(cursor.getCount(), BULK_INSERT_RECORDS_TO_INSERT + 1); // and let's make sure they match the ones we created cursor.moveToFirst(); // cursor.moveToNext(); for (int i = 1; i < BULK_INSERT_RECORDS_TO_INSERT; i++, cursor.moveToNext()) { TestUtilities.validateCurrentRecord( "testBulkInsert. Error validating MovieEntry " + (i - 1), cursor, bulkInsertContentValues[i]); } cursor.close(); }