Bug 926430 - Part 4: fix pinning and other DB use to correctly handle update-or-insert. r=bnicholson

* * *
Update robocop test.
This commit is contained in:
Richard Newman 2013-10-31 10:35:17 -07:00
parent f71e30dccb
commit 398bae3054
3 changed files with 46 additions and 36 deletions

View File

@ -2851,9 +2851,12 @@ public class BrowserProvider extends ContentProvider {
if (updated > 0)
return updated;
insertBookmark(uri, values);
if (0 <= insertBookmark(uri, values)) {
// We 'updated' one row.
return 1;
}
// Return 0 if we added a new row
// If something went wrong, then we updated zero rows.
return 0;
}
@ -2930,9 +2933,10 @@ public class BrowserProvider extends ContentProvider {
if (!values.containsKey(History.TITLE))
values.put(History.TITLE, values.getAsString(History.URL));
insertHistory(uri, values);
if (0 <= insertHistory(uri, values)) {
return 1;
}
// Return 0 if we added a new row
return 0;
}

View File

@ -600,14 +600,14 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
// Restore deleted record if possible
values.put(Bookmarks.IS_DELETED, 0);
int updated = cr.update(mBookmarksUriWithProfile,
values,
Bookmarks.URL + " = ? AND " +
Bookmarks.PARENT + " = ?",
new String[] { uri, String.valueOf(folderId) });
if (updated == 0)
cr.insert(mBookmarksUriWithProfile, values);
final Uri bookmarksWithInsert = mBookmarksUriWithProfile.buildUpon()
.appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true")
.build();
cr.update(bookmarksWithInsert,
values,
Bookmarks.URL + " = ? AND " +
Bookmarks.PARENT + " = " + folderId,
new String[] { uri });
// Bump parent modified time using its ID.
debug("Bumping parent modified time for addition to: " + folderId);
@ -617,7 +617,7 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
ContentValues bumped = new ContentValues();
bumped.put(Bookmarks.DATE_MODIFIED, now);
updated = cr.update(mBookmarksUriWithProfile, bumped, where, args);
final int updated = cr.update(mBookmarksUriWithProfile, bumped, where, args);
debug("Updated " + updated + " rows to new modified time.");
}
@ -775,14 +775,10 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
Uri faviconsUri = getAllFaviconsUri().buildUpon().
appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true").build();
int updated = cr.update(faviconsUri,
values,
Favicons.URL + " = ?",
new String[] { faviconUri });
if (updated == 0) {
cr.insert(mFaviconsUriWithProfile, values);
}
cr.update(faviconsUri,
values,
Favicons.URL + " = ?",
new String[] { faviconUri });
}
@Override
@ -802,13 +798,12 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
values.put(Thumbnails.DATA, data);
values.put(Thumbnails.URL, uri);
int updated = cr.update(mThumbnailsUriWithProfile,
values,
Thumbnails.URL + " = ?",
new String[] { uri });
if (updated == 0)
cr.insert(mThumbnailsUriWithProfile, values);
Uri thumbnailsUri = mThumbnailsUriWithProfile.buildUpon().
appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true").build();
cr.update(thumbnailsUri,
values,
Thumbnails.URL + " = ?",
new String[] { uri });
}
@Override

View File

@ -1273,7 +1273,9 @@ public class testBrowserProvider extends ContentProviderTest {
@Override
public void test() throws Exception {
Uri updateHistoryUriWithProfile = mHistoryUri.buildUpon().
Uri updateHistoryUri = mHistoryUri.buildUpon().
appendQueryParameter("increment_visits", "true").build();
Uri updateOrInsertHistoryUri = mHistoryUri.buildUpon().
appendQueryParameter("insert_if_needed", "true").
appendQueryParameter("increment_visits", "true").build();
@ -1281,13 +1283,22 @@ public class testBrowserProvider extends ContentProviderTest {
ContentValues values = new ContentValues();
values.put(mHistoryUrlCol, TEST_URL_1);
int updated = mProvider.update(updateHistoryUriWithProfile, values,
int updated = mProvider.update(updateHistoryUri, values,
mHistoryUrlCol + " = ?",
new String[] { TEST_URL_1 });
mAsserter.is((updated == 0), true, "History entry was inserted, not updated");
mAsserter.is((updated == 0), true, "History entry was not updated");
Cursor c = mProvider.query(mHistoryUri, null, null, null, null);
mAsserter.is(c.moveToFirst(), false, "History entry was not inserted");
c.close();
// Now let's try with update-or-insert.
updated = mProvider.update(updateOrInsertHistoryUri, values,
mHistoryUrlCol + " = ?",
new String[] { TEST_URL_1 });
mAsserter.is((updated == 1), true, "History entry was inserted");
long id = getHistoryEntryIdByUrl(TEST_URL_1);
Cursor c = getHistoryEntryById(id);
c = getHistoryEntryById(id);
mAsserter.is(c.moveToFirst(), true, "History entry was inserted");
long dateCreated = c.getLong(c.getColumnIndex(mHistoryDateCreatedCol));
@ -1303,7 +1314,7 @@ public class testBrowserProvider extends ContentProviderTest {
values.put(mHistoryLastVisitedCol, System.currentTimeMillis());
values.put(mHistoryTitleCol, TEST_TITLE);
updated = mProvider.update(updateHistoryUriWithProfile, values,
updated = mProvider.update(updateOrInsertHistoryUri, values,
mHistoryIdCol + " = ?",
new String[] { String.valueOf(id) });
mAsserter.is((updated == 1), true, "Inserted history entry was updated");
@ -1326,10 +1337,10 @@ public class testBrowserProvider extends ContentProviderTest {
values.put(mHistoryTitleCol, TEST_TITLE);
values.put(mHistoryVisitsCol, 10);
updated = mProvider.update(updateHistoryUriWithProfile, values,
updated = mProvider.update(updateOrInsertHistoryUri, values,
mHistoryUrlCol + " = ?",
new String[] { values.getAsString(mHistoryUrlCol) });
mAsserter.is((updated == 0), true, "History entry was inserted, not updated");
mAsserter.is((updated == 1), true, "History entry was inserted");
id = getHistoryEntryIdByUrl(TEST_URL_2);
c = getHistoryEntryById(id);
@ -1347,7 +1358,7 @@ public class testBrowserProvider extends ContentProviderTest {
values = new ContentValues();
values.put(mHistoryVisitsCol, 10);
updated = mProvider.update(updateHistoryUriWithProfile, values,
updated = mProvider.update(updateOrInsertHistoryUri, values,
mHistoryIdCol + " = ?",
new String[] { String.valueOf(id) });
mAsserter.is((updated == 1), true, "Inserted history entry was updated");