Bug 975792 - ContentProvider tests don't clean up their cursors. r=nalexander

This commit is contained in:
Richard Newman 2014-02-23 10:53:10 -08:00
Родитель 1885d7760f
Коммит 05a1dafd92
1 изменённых файлов: 99 добавлений и 27 удалений

Просмотреть файл

@ -65,17 +65,23 @@ public class testBrowserProvider extends ContentProviderTest {
BrowserContract.Bookmarks.READING_LIST_FOLDER_GUID });
c = mProvider.query(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), null, null, null, null);
mAsserter.is(c.getCount(), 7, "All non-special bookmarks and folders were deleted");
assertCountIsAndClose(c, 7, "All non-special bookmarks and folders were deleted");
mProvider.delete(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"), null, null);
c = mProvider.query(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), null, null, null, null);
mAsserter.is(c.getCount(), 0, "All history entries were deleted");
assertCountIsAndClose(c, 0, "All history entries were deleted");
mProvider.delete(BrowserContract.Favicons.CONTENT_URI, null, null);
mAsserter.is(c.getCount(), 0, "All favicons were deleted");
c = mProvider.query(appendUriParam(BrowserContract.Favicons.CONTENT_URI,
BrowserContract.PARAM_SHOW_DELETED, "1"),
null, null, null, null);
assertCountIsAndClose(c, 0, "All favicons were deleted");
mProvider.delete(BrowserContract.Thumbnails.CONTENT_URI, null, null);
mAsserter.is(c.getCount(), 0, "All thumbnails were deleted");
c = mProvider.query(appendUriParam(BrowserContract.Thumbnails.CONTENT_URI,
BrowserContract.PARAM_SHOW_DELETED, "1"),
null, null, null, null);
assertCountIsAndClose(c, 0, "All thumbnails were deleted");
}
private ContentValues createBookmark(String title, String url, long parentId,
@ -326,7 +332,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(seenException, false, "Batch updating succeded");
mOperations.clear();
// Delte all visits
// Delete all visits
for (int i = 0; i < TESTCOUNT; i++) {
builder = ContentProviderOperation.newDelete(BrowserContract.History.CONTENT_URI);
builder.withSelection(BrowserContract.History.URL + " = ?",
@ -340,7 +346,7 @@ public class testBrowserProvider extends ContentProviderTest {
} catch (OperationApplicationException ex) {
seenException = true;
}
mAsserter.is(seenException, false, "Batch deletion succeded");
mAsserter.is(seenException, false, "Batch deletion succeeded");
}
// Force a Constraint error, see if later operations still apply correctly
@ -475,6 +481,8 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(new Integer(parentId), new Integer(rootId),
"The PARENT of the " + guid + " special folder is correct");
}
c.close();
}
}
@ -549,6 +557,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TYPE)), String.valueOf(BrowserContract.Bookmarks.TYPE_BOOKMARK),
"Inserted bookmark has correct default type");
c.close();
}
}
@ -571,12 +580,14 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Bookmarks.FAVICON)), "UTF8"),
favicon, "Inserted bookmark has corresponding favicon image");
c.close();
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
favicon, "Inserted favicon has corresponding favicon image");
c.close();
}
}
@ -587,6 +598,7 @@ public class testBrowserProvider extends ContentProviderTest {
Cursor c = getBookmarkById(id);
mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
c.close();
return id;
}
@ -603,6 +615,7 @@ public class testBrowserProvider extends ContentProviderTest {
Cursor c = getBookmarkById(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
mAsserter.is(c.moveToFirst(), true, "Deleted bookmark was only marked as deleted");
c.close();
deleted = mProvider.delete(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"),
BrowserContract.Bookmarks._ID + " = ?",
@ -612,6 +625,7 @@ public class testBrowserProvider extends ContentProviderTest {
c = getBookmarkById(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
mAsserter.is(c.moveToFirst(), false, "Inserted bookmark is now actually deleted");
c.close();
id = insertOneBookmark();
@ -622,6 +636,7 @@ public class testBrowserProvider extends ContentProviderTest {
c = getBookmarkById(id);
mAsserter.is(c.moveToFirst(), false,
"Inserted bookmark can't be found after deletion using URI with ID");
c.close();
if (Build.VERSION.SDK_INT >= 8 &&
Build.VERSION.SDK_INT < 16) {
@ -631,6 +646,7 @@ public class testBrowserProvider extends ContentProviderTest {
long parentId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
c = getBookmarkById(parentId);
mAsserter.is(c.moveToFirst(), true, "Inserted bookmarks folder found");
c.close();
b = createBookmark("Example", "http://example.com", parentId,
BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
@ -638,6 +654,7 @@ public class testBrowserProvider extends ContentProviderTest {
id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
c = getBookmarkById(id);
mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
c.close();
deleted = 0;
try {
@ -664,11 +681,13 @@ public class testBrowserProvider extends ContentProviderTest {
Cursor c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
c.close();
mProvider.delete(ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id), null, null);
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), false, "Favicon is deleted with last reference to it");
c.close();
}
}
@ -713,6 +732,7 @@ public class testBrowserProvider extends ContentProviderTest {
new String[] { String.valueOf(id) });
mAsserter.is((updated == 1), true, "Inserted bookmark was updated");
c.close();
c = getBookmarkById(id);
mAsserter.is(c.moveToFirst(), true, "Updated bookmark found");
@ -752,12 +772,14 @@ public class testBrowserProvider extends ContentProviderTest {
u.put(BrowserContract.Bookmarks.URL, "http://examples2.com");
updated = mProvider.update(ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id), u, null, null);
c.close();
c = getBookmarkById(id);
mAsserter.is(c.moveToFirst(), true, "Updated bookmark found");
mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.URL)), u.getAsString(BrowserContract.Bookmarks.URL),
"Updated bookmark has correct URL using URI with id");
c.close();
}
}
@ -784,12 +806,14 @@ public class testBrowserProvider extends ContentProviderTest {
ContentValues u = createFaviconEntry(pageUrl, newFavicon);
mProvider.update(BrowserContract.Favicons.CONTENT_URI, u, null, null);
c.close();
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Updated favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
newFavicon, "Updated favicon has corresponding favicon image");
c.close();
}
}
@ -825,6 +849,7 @@ public class testBrowserProvider extends ContentProviderTest {
}
mAsserter.is(i, count, "Folder has the right number of children.");
c.close();
}
public static final int NUMBER_OF_CHILDREN = 1001;
@ -921,6 +946,7 @@ public class testBrowserProvider extends ContentProviderTest {
id = insertWithNullCol(BrowserContract.History.VISITS);
mAsserter.is(new Long(id), new Long(-1),
"Should not be able to insert history with null number of visits");
c.close();
}
}
@ -943,12 +969,14 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.History.FAVICON)), "UTF8"),
favicon, "Inserted history entry has corresponding favicon image");
c.close();
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
favicon, "Inserted favicon has corresponding favicon image");
c.close();
}
}
@ -959,6 +987,7 @@ public class testBrowserProvider extends ContentProviderTest {
Cursor c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "Inserted history entry found");
c.close();
return id;
}
@ -981,6 +1010,7 @@ public class testBrowserProvider extends ContentProviderTest {
new String[] { String.valueOf(id) });
mAsserter.is((deleted == 1), true, "Inserted history entry was deleted");
c.close();
c = getHistoryEntryById(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
mAsserter.is(c.moveToFirst(), false, "Inserted history is now actually deleted");
@ -990,10 +1020,12 @@ public class testBrowserProvider extends ContentProviderTest {
deleted = mProvider.delete(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), null, null);
mAsserter.is((deleted == 1), true,
"Inserted history entry was deleted using URI with id");
c.close();
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), false,
"Inserted history entry can't be found after deletion using URI with ID");
c.close();
}
}
@ -1012,9 +1044,11 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mProvider.delete(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), null, null);
c.close();
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), false, "Favicon is deleted with last reference to it");
c.close();
}
}
@ -1056,6 +1090,7 @@ public class testBrowserProvider extends ContentProviderTest {
new String[] { String.valueOf(id) });
mAsserter.is((updated == 1), true, "Inserted history entry was updated");
c.close();
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
@ -1089,12 +1124,14 @@ public class testBrowserProvider extends ContentProviderTest {
u.put(BrowserContract.History.URL, "http://examples2.com");
updated = mProvider.update(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), u, null, null);
c.close();
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.URL)), u.getAsString(BrowserContract.History.URL),
"Updated history entry has correct URL using URI with id");
c.close();
}
}
@ -1121,12 +1158,14 @@ public class testBrowserProvider extends ContentProviderTest {
ContentValues u = createFaviconEntry(pageUrl, newFavicon);
mProvider.update(BrowserContract.Favicons.CONTENT_URI, u, null, null);
c.close();
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Updated favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
newFavicon, "Updated favicon has corresponding favicon image");
c.close();
}
}
@ -1195,6 +1234,7 @@ public class testBrowserProvider extends ContentProviderTest {
BrowserContract.History._ID + " = ?",
new String[] { String.valueOf(id) });
mAsserter.is((updated == 1), true, "Inserted history entry was updated");
c.close();
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
@ -1220,6 +1260,8 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is((updated == 1), true, "History entry was inserted");
id = getHistoryEntryIdByUrl(TEST_URL_2);
c.close();
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "History entry was inserted");
@ -1239,6 +1281,7 @@ public class testBrowserProvider extends ContentProviderTest {
BrowserContract.History._ID + " = ?",
new String[] { String.valueOf(id) });
mAsserter.is((updated == 1), true, "Inserted history entry was updated");
c.close();
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
@ -1253,6 +1296,7 @@ public class testBrowserProvider extends ContentProviderTest {
"Updated history entry has same creation date");
mAsserter.isnot(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED))), new Long(dateModified),
"Updated history entry has new modification date");
c.close();
}
}
@ -1275,6 +1319,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Thumbnails.DATA)), "UTF8"),
thumbnail, "Inserted thumbnail has corresponding thumbnail image");
c.close();
}
}
@ -1301,12 +1346,14 @@ public class testBrowserProvider extends ContentProviderTest {
ContentValues u = createThumbnailEntry(pageUrl, newThumbnail);
mProvider.update(BrowserContract.Thumbnails.CONTENT_URI, u, null, null);
c.close();
c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Updated thumbnail found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Thumbnails.DATA)), "UTF8"),
newThumbnail, "Updated thumbnail has corresponding thumbnail image");
c.close();
}
}
@ -1325,9 +1372,11 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
mProvider.delete(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), null, null);
c.close();
c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), false, "Thumbnail is deleted with last reference to it");
c.close();
}
}
@ -1435,6 +1484,7 @@ public class testBrowserProvider extends ContentProviderTest {
"Combined entry has correct number of visits");
mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DATE_LAST_VISITED))), new Long(LAST_VISITED),
"Combined entry has correct last visit time");
c.close();
}
}
@ -1495,6 +1545,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(new Integer(display), new Integer(expectedDisplay),
"Combined display column should always be DISPLAY_READER for the reading list item");
}
c.close();
}
}
@ -1527,6 +1578,7 @@ public class testBrowserProvider extends ContentProviderTest {
new String[] { String.valueOf(combinedBookmarkId) });
mAsserter.is((deleted == 1), true, "Inserted combined bookmark was deleted");
c.close();
c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), 1, "1 combined entry found");
@ -1534,6 +1586,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.moveToFirst(), true, "Found combined entry without bookmark id");
mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(0),
"Bookmark id should not be set to removed bookmark id");
c.close();
}
}
@ -1569,6 +1622,7 @@ public class testBrowserProvider extends ContentProviderTest {
new String[] { String.valueOf(combinedReadingListItemId) });
mAsserter.is((deleted == 1), true, "Inserted combined reading list item was deleted");
c.close();
c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), 1, "1 combined entry found");
@ -1578,6 +1632,7 @@ public class testBrowserProvider extends ContentProviderTest {
"Bookmark id should not be set to removed bookmark id");
mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DISPLAY))), new Long(BrowserContract.Combined.DISPLAY_NORMAL),
"Combined entry should have reader display type");
c.close();
}
}
@ -1608,7 +1663,8 @@ public class testBrowserProvider extends ContentProviderTest {
}
Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), count, count + " history entries found");
assertCountIsAndClose(c, count, count + " history entries found");
// add thumbnails for each entry
allVals = new ContentValues[count];
@ -1622,7 +1678,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(inserts, count, "Expected number of inserts matches");
c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
mAsserter.is(c.getCount(), count, count + " thumbnails entries found");
assertCountIsAndClose(c, count, count + " thumbnails entries found");
}
@Override
@ -1637,57 +1693,61 @@ public class testBrowserProvider extends ContentProviderTest {
Uri url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "NORMAL");
mProvider.delete(url, null, null);
Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), count, count + " history entries found");
assertCountIsAndClose(c, count, count + " history entries found");
// expiring with a normal priority should delete all but 10 thumbnails
c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
// insert a bunch of new entries
// Insert a bunch of new entries.
createFakeHistory(0, count);
// expiring with a aggressive priority should leave 500 entries
// Expiring with a aggressive priority should leave 500 entries.
url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "AGGRESSIVE");
mProvider.delete(url, null, null);
c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), 500, "500 history entries found");
// expiring with a aggressive priority should delete all but 10 thumbnails
c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
assertCountIsAndClose(c, 500, "500 history entries found");
// Expiring with a aggressive priority should delete all but 10 thumbnails.
c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
// insert a bunch of entries with an old time created/modified
// Insert a bunch of entries with an old time created/modified.
long time = 1000L * 60L * 60L * 24L * 30L * 3L;
createFakeHistory(time, count);
// expiring with an normal priority should remove at most 1000 entries
// entries leaving at least 2000
// Expiring with an normal priority should remove at most 1000 entries,
// entries leaving at least 2000.
url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "NORMAL");
mProvider.delete(url, null, null);
c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), 2000, "2000 history entries found");
// expiring with a normal priority should delete all but 10 thumbnails
c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
assertCountIsAndClose(c, 2000, "2000 history entries found");
// Expiring with a normal priority should delete all but 10 thumbnails.
c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
// insert a bunch of entries with an old time created/modified
time = 1000L * 60L * 60L * 24L * 30L * 3L;
createFakeHistory(time, count);
// expiring with an agressive priority should remove old
// entries leaving at least 500
// Expiring with an aggressive priority should remove old
// entries, leaving at least 500.
url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "AGGRESSIVE");
mProvider.delete(url, null, null);
c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
mAsserter.is(c.getCount(), 500, "500 history entries found");
assertCountIsAndClose(c, 500, "500 history entries found");
// expiring with an aggressive priority should delete all but 10 thumbnails
c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
}
}
@ -1776,4 +1836,16 @@ public class testBrowserProvider extends ContentProviderTest {
ensureOnlyChangeNotifiedStartsWith(BrowserContract.History.CONTENT_URI, "bulkInsert");
}
}
/**
* Assert that the provided cursor has the expected number of rows,
* closing the cursor afterwards.
*/
private void assertCountIsAndClose(Cursor c, int expectedCount, String message) {
try {
mAsserter.is(c.getCount(), expectedCount, message);
} finally {
c.close();
}
}
}