mobile/android/base/tests/testBrowserProvider.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 package org.mozilla.gecko.tests;
michael@0 2
michael@0 3 import java.util.ArrayList;
michael@0 4 import java.util.Random;
michael@0 5
michael@0 6 import org.mozilla.gecko.db.BrowserContract;
michael@0 7
michael@0 8 import android.content.ContentProviderOperation;
michael@0 9 import android.content.ContentProviderResult;
michael@0 10 import android.content.ContentUris;
michael@0 11 import android.content.ContentValues;
michael@0 12 import android.content.OperationApplicationException;
michael@0 13 import android.database.Cursor;
michael@0 14 import android.net.Uri;
michael@0 15 import android.os.Build;
michael@0 16 import android.util.Log;
michael@0 17
michael@0 18 /*
michael@0 19 * This test is meant to exercise all operations exposed by Fennec's
michael@0 20 * history and bookmarks content provider. It does so in an isolated
michael@0 21 * environment (see ContentProviderTest) without affecting any UI-related
michael@0 22 * code.
michael@0 23 */
michael@0 24 public class testBrowserProvider extends ContentProviderTest {
michael@0 25 private long mMobileFolderId;
michael@0 26
michael@0 27 private void loadMobileFolderId() throws Exception {
michael@0 28 Cursor c = null;
michael@0 29 try {
michael@0 30 c = getBookmarkByGuid(BrowserContract.Bookmarks.MOBILE_FOLDER_GUID);
michael@0 31 mAsserter.is(c.moveToFirst(), true, "Mobile bookmarks folder is present");
michael@0 32
michael@0 33 mMobileFolderId = c.getLong(c.getColumnIndex(BrowserContract.Bookmarks._ID));
michael@0 34 } finally {
michael@0 35 if (c != null) {
michael@0 36 c.close();
michael@0 37 }
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 private void ensureEmptyDatabase() throws Exception {
michael@0 42 Cursor c = null;
michael@0 43
michael@0 44 String guid = BrowserContract.Bookmarks.GUID;
michael@0 45
michael@0 46 mProvider.delete(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"),
michael@0 47 guid + " != ? AND " +
michael@0 48 guid + " != ? AND " +
michael@0 49 guid + " != ? AND " +
michael@0 50 guid + " != ? AND " +
michael@0 51 guid + " != ? AND " +
michael@0 52 guid + " != ? AND " +
michael@0 53 guid + " != ?",
michael@0 54 new String[] { BrowserContract.Bookmarks.PLACES_FOLDER_GUID,
michael@0 55 BrowserContract.Bookmarks.MOBILE_FOLDER_GUID,
michael@0 56 BrowserContract.Bookmarks.MENU_FOLDER_GUID,
michael@0 57 BrowserContract.Bookmarks.TAGS_FOLDER_GUID,
michael@0 58 BrowserContract.Bookmarks.TOOLBAR_FOLDER_GUID,
michael@0 59 BrowserContract.Bookmarks.UNFILED_FOLDER_GUID,
michael@0 60 BrowserContract.Bookmarks.READING_LIST_FOLDER_GUID });
michael@0 61
michael@0 62 c = mProvider.query(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), null, null, null, null);
michael@0 63 assertCountIsAndClose(c, 7, "All non-special bookmarks and folders were deleted");
michael@0 64
michael@0 65 mProvider.delete(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"), null, null);
michael@0 66 c = mProvider.query(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), null, null, null, null);
michael@0 67 assertCountIsAndClose(c, 0, "All history entries were deleted");
michael@0 68
michael@0 69 /**
michael@0 70 * There's no reason why the following two parts should fail.
michael@0 71 * But sometimes they do, and I'm not going to spend the time
michael@0 72 * to figure out why in an unrelated bug.
michael@0 73 */
michael@0 74
michael@0 75 mProvider.delete(appendUriParam(BrowserContract.Favicons.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"), null, null);
michael@0 76 c = mProvider.query(appendUriParam(BrowserContract.Favicons.CONTENT_URI,
michael@0 77 BrowserContract.PARAM_SHOW_DELETED, "1"),
michael@0 78 null, null, null, null);
michael@0 79
michael@0 80 if (c.getCount() > 0) {
michael@0 81 mAsserter.dumpLog("Unexpected favicons in ensureEmptyDatabase.");
michael@0 82 }
michael@0 83 c.close();
michael@0 84
michael@0 85 mAsserter.dumpLog("ensureEmptyDatabase: Favicon deletion completed."); // Bug 968951 debug.
michael@0 86 // assertCountIsAndClose(c, 0, "All favicons were deleted");
michael@0 87
michael@0 88 mProvider.delete(appendUriParam(BrowserContract.Thumbnails.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"), null, null);
michael@0 89 c = mProvider.query(appendUriParam(BrowserContract.Thumbnails.CONTENT_URI,
michael@0 90 BrowserContract.PARAM_SHOW_DELETED, "1"),
michael@0 91 null, null, null, null);
michael@0 92
michael@0 93 if (c.getCount() > 0) {
michael@0 94 mAsserter.dumpLog("Unexpected thumbnails in ensureEmptyDatabase.");
michael@0 95 }
michael@0 96 c.close();
michael@0 97
michael@0 98 mAsserter.dumpLog("ensureEmptyDatabase: Thumbnail deletion completed."); // Bug 968951 debug.
michael@0 99 // assertCountIsAndClose(c, 0, "All thumbnails were deleted");
michael@0 100 }
michael@0 101
michael@0 102 private ContentValues createBookmark(String title, String url, long parentId,
michael@0 103 int type, int position, String tags, String description, String keyword) throws Exception {
michael@0 104 ContentValues bookmark = new ContentValues();
michael@0 105
michael@0 106 bookmark.put(BrowserContract.Bookmarks.TITLE, title);
michael@0 107 bookmark.put(BrowserContract.Bookmarks.URL, url);
michael@0 108 bookmark.put(BrowserContract.Bookmarks.PARENT, parentId);
michael@0 109 bookmark.put(BrowserContract.Bookmarks.TYPE, type);
michael@0 110 bookmark.put(BrowserContract.Bookmarks.POSITION, position);
michael@0 111 bookmark.put(BrowserContract.Bookmarks.TAGS, tags);
michael@0 112 bookmark.put(BrowserContract.Bookmarks.DESCRIPTION, description);
michael@0 113 bookmark.put(BrowserContract.Bookmarks.KEYWORD, keyword);
michael@0 114
michael@0 115 return bookmark;
michael@0 116 }
michael@0 117
michael@0 118 private ContentValues createOneBookmark() throws Exception {
michael@0 119 return createBookmark("Example", "http://example.com", mMobileFolderId,
michael@0 120 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 121 }
michael@0 122
michael@0 123 private Cursor getBookmarksByParent(long parent) throws Exception {
michael@0 124 // Order by position.
michael@0 125 return mProvider.query(BrowserContract.Bookmarks.CONTENT_URI, null,
michael@0 126 BrowserContract.Bookmarks.PARENT + " = ?",
michael@0 127 new String[] { String.valueOf(parent) },
michael@0 128 BrowserContract.Bookmarks.POSITION);
michael@0 129 }
michael@0 130
michael@0 131 private Cursor getBookmarkByGuid(String guid) throws Exception {
michael@0 132 return mProvider.query(BrowserContract.Bookmarks.CONTENT_URI, null,
michael@0 133 BrowserContract.Bookmarks.GUID + " = ?",
michael@0 134 new String[] { guid },
michael@0 135 null);
michael@0 136 }
michael@0 137
michael@0 138 private Cursor getBookmarkById(long id) throws Exception {
michael@0 139 return getBookmarkById(BrowserContract.Bookmarks.CONTENT_URI, id, null);
michael@0 140 }
michael@0 141
michael@0 142 private Cursor getBookmarkById(long id, String[] projection) throws Exception {
michael@0 143 return getBookmarkById(BrowserContract.Bookmarks.CONTENT_URI, id, projection);
michael@0 144 }
michael@0 145
michael@0 146 private Cursor getBookmarkById(Uri bookmarksUri, long id) throws Exception {
michael@0 147 return getBookmarkById(bookmarksUri, id, null);
michael@0 148 }
michael@0 149
michael@0 150 private Cursor getBookmarkById(Uri bookmarksUri, long id, String[] projection) throws Exception {
michael@0 151 return mProvider.query(bookmarksUri, projection,
michael@0 152 BrowserContract.Bookmarks._ID + " = ?",
michael@0 153 new String[] { String.valueOf(id) },
michael@0 154 null);
michael@0 155 }
michael@0 156
michael@0 157 private ContentValues createHistoryEntry(String title, String url, int visits, long lastVisited) throws Exception {
michael@0 158 ContentValues historyEntry = new ContentValues();
michael@0 159
michael@0 160 historyEntry.put(BrowserContract.History.TITLE, title);
michael@0 161 historyEntry.put(BrowserContract.History.URL, url);
michael@0 162 historyEntry.put(BrowserContract.History.VISITS, visits);
michael@0 163 historyEntry.put(BrowserContract.History.DATE_LAST_VISITED, lastVisited);
michael@0 164
michael@0 165 return historyEntry;
michael@0 166 }
michael@0 167
michael@0 168 private ContentValues createFaviconEntry(String pageUrl, String data) throws Exception {
michael@0 169 ContentValues faviconEntry = new ContentValues();
michael@0 170
michael@0 171 faviconEntry.put(BrowserContract.Favicons.PAGE_URL, pageUrl);
michael@0 172 faviconEntry.put(BrowserContract.Favicons.URL, pageUrl + "/favicon.ico");
michael@0 173 faviconEntry.put(BrowserContract.Favicons.DATA, data.getBytes("UTF8"));
michael@0 174
michael@0 175 return faviconEntry;
michael@0 176 }
michael@0 177
michael@0 178 private ContentValues createThumbnailEntry(String pageUrl, String data) throws Exception {
michael@0 179 ContentValues thumbnailEntry = new ContentValues();
michael@0 180
michael@0 181 thumbnailEntry.put(BrowserContract.Thumbnails.URL, pageUrl);
michael@0 182 thumbnailEntry.put(BrowserContract.Thumbnails.DATA, data.getBytes("UTF8"));
michael@0 183
michael@0 184 return thumbnailEntry;
michael@0 185 }
michael@0 186
michael@0 187 private ContentValues createOneHistoryEntry() throws Exception {
michael@0 188 return createHistoryEntry("Example", "http://example.com", 10, System.currentTimeMillis());
michael@0 189 }
michael@0 190
michael@0 191 private Cursor getHistoryEntryById(long id) throws Exception {
michael@0 192 return getHistoryEntryById(BrowserContract.History.CONTENT_URI, id, null);
michael@0 193 }
michael@0 194
michael@0 195 private Cursor getHistoryEntryById(long id, String[] projection) throws Exception {
michael@0 196 return getHistoryEntryById(BrowserContract.History.CONTENT_URI, id, projection);
michael@0 197 }
michael@0 198
michael@0 199 private Cursor getHistoryEntryById(Uri historyUri, long id) throws Exception {
michael@0 200 return getHistoryEntryById(historyUri, id, null);
michael@0 201 }
michael@0 202
michael@0 203 private Cursor getHistoryEntryById(Uri historyUri, long id, String[] projection) throws Exception {
michael@0 204 return mProvider.query(historyUri, projection,
michael@0 205 BrowserContract.History._ID + " = ?",
michael@0 206 new String[] { String.valueOf(id) },
michael@0 207 null);
michael@0 208 }
michael@0 209
michael@0 210 private Cursor getFaviconsByUrl(String url) throws Exception {
michael@0 211 return mProvider.query(BrowserContract.Combined.CONTENT_URI, null,
michael@0 212 BrowserContract.Combined.URL + " = ?",
michael@0 213 new String[] { url },
michael@0 214 null);
michael@0 215 }
michael@0 216
michael@0 217 private Cursor getThumbnailByUrl(String url) throws Exception {
michael@0 218 return mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null,
michael@0 219 BrowserContract.Thumbnails.URL + " = ?",
michael@0 220 new String[] { url },
michael@0 221 null);
michael@0 222 }
michael@0 223
michael@0 224 @Override
michael@0 225 public void setUp() throws Exception {
michael@0 226 super.setUp(sBrowserProviderCallable, BrowserContract.AUTHORITY, "browser.db");
michael@0 227
michael@0 228 mTests.add(new TestSpecialFolders());
michael@0 229
michael@0 230 mTests.add(new TestInsertBookmarks());
michael@0 231 mTests.add(new TestInsertBookmarksFavicons());
michael@0 232 mTests.add(new TestDeleteBookmarks());
michael@0 233 mTests.add(new TestDeleteBookmarksFavicons());
michael@0 234 mTests.add(new TestUpdateBookmarks());
michael@0 235 mTests.add(new TestUpdateBookmarksFavicons());
michael@0 236 mTests.add(new TestPositionBookmarks());
michael@0 237
michael@0 238 mTests.add(new TestInsertHistory());
michael@0 239 mTests.add(new TestInsertHistoryFavicons());
michael@0 240 mTests.add(new TestDeleteHistory());
michael@0 241 mTests.add(new TestDeleteHistoryFavicons());
michael@0 242 mTests.add(new TestUpdateHistory());
michael@0 243 mTests.add(new TestUpdateHistoryFavicons());
michael@0 244 mTests.add(new TestUpdateOrInsertHistory());
michael@0 245 mTests.add(new TestInsertHistoryThumbnails());
michael@0 246 mTests.add(new TestUpdateHistoryThumbnails());
michael@0 247 mTests.add(new TestDeleteHistoryThumbnails());
michael@0 248
michael@0 249 mTests.add(new TestBatchOperations());
michael@0 250
michael@0 251 mTests.add(new TestCombinedView());
michael@0 252 mTests.add(new TestCombinedViewDisplay());
michael@0 253 mTests.add(new TestCombinedViewWithDeletedBookmark());
michael@0 254 mTests.add(new TestCombinedViewWithDeletedReadingListItem());
michael@0 255 mTests.add(new TestExpireHistory());
michael@0 256
michael@0 257 mTests.add(new TestBrowserProviderNotifications());
michael@0 258 }
michael@0 259
michael@0 260 public void testBrowserProvider() throws Exception {
michael@0 261 loadMobileFolderId();
michael@0 262
michael@0 263 for (int i = 0; i < mTests.size(); i++) {
michael@0 264 Runnable test = mTests.get(i);
michael@0 265
michael@0 266 final String testName = test.getClass().getSimpleName();
michael@0 267 setTestName(testName);
michael@0 268 ensureEmptyDatabase();
michael@0 269 mAsserter.dumpLog("testBrowserProvider: Database empty - Starting " + testName + ".");
michael@0 270 test.run();
michael@0 271 }
michael@0 272 }
michael@0 273
michael@0 274 private class TestBatchOperations extends TestCase {
michael@0 275 static final int TESTCOUNT = 100;
michael@0 276
michael@0 277 public void testApplyBatch() throws Exception {
michael@0 278 ArrayList<ContentProviderOperation> mOperations
michael@0 279 = new ArrayList<ContentProviderOperation>();
michael@0 280
michael@0 281 // Test a bunch of inserts with applyBatch
michael@0 282 ContentValues values = new ContentValues();
michael@0 283 ContentProviderOperation.Builder builder = null;
michael@0 284
michael@0 285 for (int i = 0; i < TESTCOUNT; i++) {
michael@0 286 values.clear();
michael@0 287 values.put(BrowserContract.History.VISITS, i);
michael@0 288 values.put(BrowserContract.History.TITLE, "Test" + i);
michael@0 289 values.put(BrowserContract.History.URL, "http://www.test.org/" + i);
michael@0 290
michael@0 291 // Insert
michael@0 292 builder = ContentProviderOperation.newInsert(BrowserContract.History.CONTENT_URI);
michael@0 293 builder.withValues(values);
michael@0 294 // Queue the operation
michael@0 295 mOperations.add(builder.build());
michael@0 296 }
michael@0 297
michael@0 298 ContentProviderResult[] applyResult =
michael@0 299 mProvider.applyBatch(mOperations);
michael@0 300
michael@0 301 boolean allFound = true;
michael@0 302 for (int i = 0; i < TESTCOUNT; i++) {
michael@0 303 Cursor cursor = mProvider.query(BrowserContract.History.CONTENT_URI,
michael@0 304 null,
michael@0 305 BrowserContract.History.URL + " = ?",
michael@0 306 new String[] { "http://www.test.org/" + i },
michael@0 307 null);
michael@0 308
michael@0 309 if (!cursor.moveToFirst())
michael@0 310 allFound = false;
michael@0 311 cursor.close();
michael@0 312 }
michael@0 313 mAsserter.is(allFound, true, "Found all batchApply entries");
michael@0 314 mOperations.clear();
michael@0 315
michael@0 316 // Update all visits to 1
michael@0 317 values.clear();
michael@0 318 values.put(BrowserContract.History.VISITS, 1);
michael@0 319 for (int i = 0; i < TESTCOUNT; i++) {
michael@0 320 builder = ContentProviderOperation.newUpdate(BrowserContract.History.CONTENT_URI);
michael@0 321 builder.withSelection(BrowserContract.History.URL + " = ?",
michael@0 322 new String[] {"http://www.test.org/" + i});
michael@0 323 builder.withValues(values);
michael@0 324 builder.withExpectedCount(1);
michael@0 325 // Queue the operation
michael@0 326 mOperations.add(builder.build());
michael@0 327 }
michael@0 328
michael@0 329 boolean seenException = false;
michael@0 330 try {
michael@0 331 applyResult = mProvider.applyBatch(mOperations);
michael@0 332 } catch (OperationApplicationException ex) {
michael@0 333 seenException = true;
michael@0 334 }
michael@0 335 mAsserter.is(seenException, false, "Batch updating succeded");
michael@0 336 mOperations.clear();
michael@0 337
michael@0 338 // Delete all visits
michael@0 339 for (int i = 0; i < TESTCOUNT; i++) {
michael@0 340 builder = ContentProviderOperation.newDelete(BrowserContract.History.CONTENT_URI);
michael@0 341 builder.withSelection(BrowserContract.History.URL + " = ?",
michael@0 342 new String[] {"http://www.test.org/" + i});
michael@0 343 builder.withExpectedCount(1);
michael@0 344 // Queue the operation
michael@0 345 mOperations.add(builder.build());
michael@0 346 }
michael@0 347 try {
michael@0 348 applyResult = mProvider.applyBatch(mOperations);
michael@0 349 } catch (OperationApplicationException ex) {
michael@0 350 seenException = true;
michael@0 351 }
michael@0 352 mAsserter.is(seenException, false, "Batch deletion succeeded");
michael@0 353 }
michael@0 354
michael@0 355 // Force a Constraint error, see if later operations still apply correctly
michael@0 356 public void testApplyBatchErrors() throws Exception {
michael@0 357 ArrayList<ContentProviderOperation> mOperations
michael@0 358 = new ArrayList<ContentProviderOperation>();
michael@0 359
michael@0 360 // Test a bunch of inserts with applyBatch
michael@0 361 ContentProviderOperation.Builder builder = null;
michael@0 362 ContentValues values = createFaviconEntry("http://www.test.org", "FAVICON");
michael@0 363 builder = ContentProviderOperation.newInsert(BrowserContract.Favicons.CONTENT_URI);
michael@0 364 builder.withValues(values);
michael@0 365 mOperations.add(builder.build());
michael@0 366
michael@0 367 // Make a duplicate, this will fail because of a UNIQUE constraint
michael@0 368 builder = ContentProviderOperation.newInsert(BrowserContract.Favicons.CONTENT_URI);
michael@0 369 builder.withValues(values);
michael@0 370 mOperations.add(builder.build());
michael@0 371
michael@0 372 // This is valid and should be in the table afterwards
michael@0 373 values.put(BrowserContract.Favicons.URL, "http://www.test.org/valid.ico");
michael@0 374 builder = ContentProviderOperation.newInsert(BrowserContract.Favicons.CONTENT_URI);
michael@0 375 builder.withValues(values);
michael@0 376 mOperations.add(builder.build());
michael@0 377
michael@0 378 boolean seenException = false;
michael@0 379
michael@0 380 try {
michael@0 381 ContentProviderResult[] applyResult =
michael@0 382 mProvider.applyBatch(mOperations);
michael@0 383 } catch (OperationApplicationException ex) {
michael@0 384 seenException = true;
michael@0 385 }
michael@0 386
michael@0 387 // This test may need to go away if Bug 717428 is fixed.
michael@0 388 mAsserter.is(seenException, true, "Expected failure in favicons table");
michael@0 389
michael@0 390 boolean allFound = true;
michael@0 391 Cursor cursor = mProvider.query(BrowserContract.Favicons.CONTENT_URI,
michael@0 392 null,
michael@0 393 BrowserContract.Favicons.URL + " = ?",
michael@0 394 new String[] { "http://www.test.org/valid.ico" },
michael@0 395 null);
michael@0 396
michael@0 397 if (!cursor.moveToFirst())
michael@0 398 allFound = false;
michael@0 399 cursor.close();
michael@0 400
michael@0 401 mAsserter.is(allFound, true, "Found all applyBatch (with error) entries");
michael@0 402 }
michael@0 403
michael@0 404 public void testBulkInsert() throws Exception {
michael@0 405 // Test a bunch of inserts with bulkInsert
michael@0 406 ContentValues allVals[] = new ContentValues[TESTCOUNT];
michael@0 407 for (int i = 0; i < TESTCOUNT; i++) {
michael@0 408 allVals[i] = new ContentValues();
michael@0 409 allVals[i].put(BrowserContract.History.URL, i);
michael@0 410 allVals[i].put(BrowserContract.History.TITLE, "Test" + i);
michael@0 411 allVals[i].put(BrowserContract.History.URL, "http://www.test.org/" + i);
michael@0 412 }
michael@0 413
michael@0 414 int inserts = mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, allVals);
michael@0 415 mAsserter.is(inserts, TESTCOUNT, "Excepted number of inserts matches");
michael@0 416
michael@0 417 boolean allFound = true;
michael@0 418 for (int i = 0; i < TESTCOUNT; i++) {
michael@0 419 Cursor cursor = mProvider.query(BrowserContract.History.CONTENT_URI,
michael@0 420 null,
michael@0 421 BrowserContract.History.URL + " = ?",
michael@0 422 new String[] { "http://www.test.org/" + i },
michael@0 423 null);
michael@0 424
michael@0 425 if (!cursor.moveToFirst())
michael@0 426 allFound = false;
michael@0 427 cursor.close();
michael@0 428 }
michael@0 429 mAsserter.is(allFound, true, "Found all bulkInsert entries");
michael@0 430 }
michael@0 431
michael@0 432 @Override
michael@0 433 public void test() throws Exception {
michael@0 434 testApplyBatch();
michael@0 435 // Clean up
michael@0 436 ensureEmptyDatabase();
michael@0 437
michael@0 438 testBulkInsert();
michael@0 439 ensureEmptyDatabase();
michael@0 440
michael@0 441 testApplyBatchErrors();
michael@0 442 }
michael@0 443 }
michael@0 444
michael@0 445 private class TestSpecialFolders extends TestCase {
michael@0 446 @Override
michael@0 447 public void test() throws Exception {
michael@0 448 Cursor c = mProvider.query(BrowserContract.Bookmarks.CONTENT_URI,
michael@0 449 new String[] { BrowserContract.Bookmarks._ID,
michael@0 450 BrowserContract.Bookmarks.GUID,
michael@0 451 BrowserContract.Bookmarks.PARENT },
michael@0 452 BrowserContract.Bookmarks.GUID + " = ? OR " +
michael@0 453 BrowserContract.Bookmarks.GUID + " = ? OR " +
michael@0 454 BrowserContract.Bookmarks.GUID + " = ? OR " +
michael@0 455 BrowserContract.Bookmarks.GUID + " = ? OR " +
michael@0 456 BrowserContract.Bookmarks.GUID + " = ? OR " +
michael@0 457 BrowserContract.Bookmarks.GUID + " = ? OR " +
michael@0 458 BrowserContract.Bookmarks.GUID + " = ?",
michael@0 459 new String[] { BrowserContract.Bookmarks.PLACES_FOLDER_GUID,
michael@0 460 BrowserContract.Bookmarks.MOBILE_FOLDER_GUID,
michael@0 461 BrowserContract.Bookmarks.MENU_FOLDER_GUID,
michael@0 462 BrowserContract.Bookmarks.TAGS_FOLDER_GUID,
michael@0 463 BrowserContract.Bookmarks.TOOLBAR_FOLDER_GUID,
michael@0 464 BrowserContract.Bookmarks.UNFILED_FOLDER_GUID,
michael@0 465 BrowserContract.Bookmarks.READING_LIST_FOLDER_GUID },
michael@0 466 null);
michael@0 467
michael@0 468 mAsserter.is(c.getCount(), 7, "Right number of special folders");
michael@0 469
michael@0 470 int rootId = BrowserContract.Bookmarks.FIXED_ROOT_ID;
michael@0 471 int readingListId = BrowserContract.Bookmarks.FIXED_READING_LIST_ID;
michael@0 472
michael@0 473 while (c.moveToNext()) {
michael@0 474 int id = c.getInt(c.getColumnIndex(BrowserContract.Bookmarks._ID));
michael@0 475 String guid = c.getString(c.getColumnIndex(BrowserContract.Bookmarks.GUID));
michael@0 476 int parentId = c.getInt(c.getColumnIndex(BrowserContract.Bookmarks.PARENT));
michael@0 477
michael@0 478 if (guid.equals(BrowserContract.Bookmarks.PLACES_FOLDER_GUID)) {
michael@0 479 mAsserter.is(new Integer(id), new Integer(rootId), "The id of places folder is correct");
michael@0 480 } else if (guid.equals(BrowserContract.Bookmarks.READING_LIST_FOLDER_GUID)) {
michael@0 481 mAsserter.is(new Integer(id), new Integer(readingListId), "The id of reading list folder is correct");
michael@0 482 }
michael@0 483
michael@0 484 mAsserter.is(new Integer(parentId), new Integer(rootId),
michael@0 485 "The PARENT of the " + guid + " special folder is correct");
michael@0 486 }
michael@0 487
michael@0 488 c.close();
michael@0 489 }
michael@0 490 }
michael@0 491
michael@0 492 private class TestInsertBookmarks extends TestCase {
michael@0 493 private long insertWithNullCol(String colName) throws Exception {
michael@0 494 ContentValues b = createOneBookmark();
michael@0 495 b.putNull(colName);
michael@0 496 long id = -1;
michael@0 497
michael@0 498 try {
michael@0 499 id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 500 } catch (Exception e) {}
michael@0 501
michael@0 502 return id;
michael@0 503 }
michael@0 504
michael@0 505 @Override
michael@0 506 public void test() throws Exception {
michael@0 507 ContentValues b = createOneBookmark();
michael@0 508 long id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 509 Cursor c = getBookmarkById(id);
michael@0 510
michael@0 511 mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
michael@0 512
michael@0 513 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TITLE)), b.getAsString(BrowserContract.Bookmarks.TITLE),
michael@0 514 "Inserted bookmark has correct title");
michael@0 515 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.URL)), b.getAsString(BrowserContract.Bookmarks.URL),
michael@0 516 "Inserted bookmark has correct URL");
michael@0 517 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TAGS)), b.getAsString(BrowserContract.Bookmarks.TAGS),
michael@0 518 "Inserted bookmark has correct tags");
michael@0 519 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.KEYWORD)), b.getAsString(BrowserContract.Bookmarks.KEYWORD),
michael@0 520 "Inserted bookmark has correct keyword");
michael@0 521 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.DESCRIPTION)), b.getAsString(BrowserContract.Bookmarks.DESCRIPTION),
michael@0 522 "Inserted bookmark has correct description");
michael@0 523 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.POSITION)), b.getAsString(BrowserContract.Bookmarks.POSITION),
michael@0 524 "Inserted bookmark has correct position");
michael@0 525 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TYPE)), b.getAsString(BrowserContract.Bookmarks.TYPE),
michael@0 526 "Inserted bookmark has correct type");
michael@0 527 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.PARENT)), b.getAsString(BrowserContract.Bookmarks.PARENT),
michael@0 528 "Inserted bookmark has correct parent ID");
michael@0 529 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.IS_DELETED)), String.valueOf(0),
michael@0 530 "Inserted bookmark has correct is-deleted state");
michael@0 531
michael@0 532 id = insertWithNullCol(BrowserContract.Bookmarks.POSITION);
michael@0 533 mAsserter.is(new Long(id), new Long(-1),
michael@0 534 "Should not be able to insert bookmark with null position");
michael@0 535
michael@0 536 id = insertWithNullCol(BrowserContract.Bookmarks.TYPE);
michael@0 537 mAsserter.is(new Long(id), new Long(-1),
michael@0 538 "Should not be able to insert bookmark with null type");
michael@0 539
michael@0 540 if (Build.VERSION.SDK_INT >= 8 &&
michael@0 541 Build.VERSION.SDK_INT < 16) {
michael@0 542 b = createOneBookmark();
michael@0 543 b.put(BrowserContract.Bookmarks.PARENT, -1);
michael@0 544 id = -1;
michael@0 545
michael@0 546 try {
michael@0 547 id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 548 } catch (Exception e) {}
michael@0 549
michael@0 550 mAsserter.is(new Long(id), new Long(-1),
michael@0 551 "Should not be able to insert bookmark with invalid parent");
michael@0 552 }
michael@0 553
michael@0 554 b = createOneBookmark();
michael@0 555 b.remove(BrowserContract.Bookmarks.TYPE);
michael@0 556 id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 557 c = getBookmarkById(id);
michael@0 558
michael@0 559 mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
michael@0 560
michael@0 561 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TYPE)), String.valueOf(BrowserContract.Bookmarks.TYPE_BOOKMARK),
michael@0 562 "Inserted bookmark has correct default type");
michael@0 563 c.close();
michael@0 564 }
michael@0 565 }
michael@0 566
michael@0 567 private class TestInsertBookmarksFavicons extends TestCase {
michael@0 568 @Override
michael@0 569 public void test() throws Exception {
michael@0 570 ContentValues b = createOneBookmark();
michael@0 571
michael@0 572 final String favicon = "FAVICON";
michael@0 573 final String pageUrl = b.getAsString(BrowserContract.Bookmarks.URL);
michael@0 574
michael@0 575 long id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 576
michael@0 577 // Insert the favicon into the favicons table
michael@0 578 mProvider.insert(BrowserContract.Favicons.CONTENT_URI, createFaviconEntry(pageUrl, favicon));
michael@0 579
michael@0 580 Cursor c = getBookmarkById(id, new String[] { BrowserContract.Bookmarks.FAVICON });
michael@0 581
michael@0 582 mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
michael@0 583
michael@0 584 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Bookmarks.FAVICON)), "UTF8"),
michael@0 585 favicon, "Inserted bookmark has corresponding favicon image");
michael@0 586 c.close();
michael@0 587
michael@0 588 c = getFaviconsByUrl(pageUrl);
michael@0 589 mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
michael@0 590
michael@0 591 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
michael@0 592 favicon, "Inserted favicon has corresponding favicon image");
michael@0 593 c.close();
michael@0 594 }
michael@0 595 }
michael@0 596
michael@0 597 private class TestDeleteBookmarks extends TestCase {
michael@0 598 private long insertOneBookmark() throws Exception {
michael@0 599 ContentValues b = createOneBookmark();
michael@0 600 long id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 601
michael@0 602 Cursor c = getBookmarkById(id);
michael@0 603 mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
michael@0 604 c.close();
michael@0 605
michael@0 606 return id;
michael@0 607 }
michael@0 608
michael@0 609 @Override
michael@0 610 public void test() throws Exception {
michael@0 611 long id = insertOneBookmark();
michael@0 612
michael@0 613 int deleted = mProvider.delete(BrowserContract.Bookmarks.CONTENT_URI,
michael@0 614 BrowserContract.Bookmarks._ID + " = ?",
michael@0 615 new String[] { String.valueOf(id) });
michael@0 616
michael@0 617 mAsserter.is((deleted == 1), true, "Inserted bookmark was deleted");
michael@0 618
michael@0 619 Cursor c = getBookmarkById(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
michael@0 620 mAsserter.is(c.moveToFirst(), true, "Deleted bookmark was only marked as deleted");
michael@0 621 c.close();
michael@0 622
michael@0 623 deleted = mProvider.delete(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"),
michael@0 624 BrowserContract.Bookmarks._ID + " = ?",
michael@0 625 new String[] { String.valueOf(id) });
michael@0 626
michael@0 627 mAsserter.is((deleted == 1), true, "Inserted bookmark was deleted");
michael@0 628
michael@0 629 c = getBookmarkById(appendUriParam(BrowserContract.Bookmarks.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
michael@0 630 mAsserter.is(c.moveToFirst(), false, "Inserted bookmark is now actually deleted");
michael@0 631 c.close();
michael@0 632
michael@0 633 id = insertOneBookmark();
michael@0 634
michael@0 635 deleted = mProvider.delete(ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id), null, null);
michael@0 636 mAsserter.is((deleted == 1), true,
michael@0 637 "Inserted bookmark was deleted using URI with id");
michael@0 638
michael@0 639 c = getBookmarkById(id);
michael@0 640 mAsserter.is(c.moveToFirst(), false,
michael@0 641 "Inserted bookmark can't be found after deletion using URI with ID");
michael@0 642 c.close();
michael@0 643
michael@0 644 if (Build.VERSION.SDK_INT >= 8 &&
michael@0 645 Build.VERSION.SDK_INT < 16) {
michael@0 646 ContentValues b = createBookmark("Folder", null, mMobileFolderId,
michael@0 647 BrowserContract.Bookmarks.TYPE_FOLDER, 0, "folderTags", "folderDescription", "folderKeyword");
michael@0 648
michael@0 649 long parentId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 650 c = getBookmarkById(parentId);
michael@0 651 mAsserter.is(c.moveToFirst(), true, "Inserted bookmarks folder found");
michael@0 652 c.close();
michael@0 653
michael@0 654 b = createBookmark("Example", "http://example.com", parentId,
michael@0 655 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 656
michael@0 657 id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 658 c = getBookmarkById(id);
michael@0 659 mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
michael@0 660 c.close();
michael@0 661
michael@0 662 deleted = 0;
michael@0 663 try {
michael@0 664 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, parentId);
michael@0 665 deleted = mProvider.delete(appendUriParam(uri, BrowserContract.PARAM_IS_SYNC, "1"), null, null);
michael@0 666 } catch(Exception e) {}
michael@0 667
michael@0 668 mAsserter.is((deleted == 0), true,
michael@0 669 "Should not be able to delete folder that causes orphan bookmarks");
michael@0 670 }
michael@0 671 }
michael@0 672 }
michael@0 673
michael@0 674 private class TestDeleteBookmarksFavicons extends TestCase {
michael@0 675 @Override
michael@0 676 public void test() throws Exception {
michael@0 677 ContentValues b = createOneBookmark();
michael@0 678
michael@0 679 final String pageUrl = b.getAsString(BrowserContract.Bookmarks.URL);
michael@0 680 long id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 681
michael@0 682 // Insert the favicon into the favicons table
michael@0 683 mProvider.insert(BrowserContract.Favicons.CONTENT_URI, createFaviconEntry(pageUrl, "FAVICON"));
michael@0 684
michael@0 685 Cursor c = getFaviconsByUrl(pageUrl);
michael@0 686 mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
michael@0 687 c.close();
michael@0 688
michael@0 689 mProvider.delete(ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id), null, null);
michael@0 690
michael@0 691 c = getFaviconsByUrl(pageUrl);
michael@0 692 mAsserter.is(c.moveToFirst(), false, "Favicon is deleted with last reference to it");
michael@0 693 c.close();
michael@0 694 }
michael@0 695 }
michael@0 696
michael@0 697 private class TestUpdateBookmarks extends TestCase {
michael@0 698 private int updateWithNullCol(long id, String colName) throws Exception {
michael@0 699 ContentValues u = new ContentValues();
michael@0 700 u.putNull(colName);
michael@0 701
michael@0 702 int updated = 0;
michael@0 703
michael@0 704 try {
michael@0 705 updated = mProvider.update(BrowserContract.Bookmarks.CONTENT_URI, u,
michael@0 706 BrowserContract.Bookmarks._ID + " = ?",
michael@0 707 new String[] { String.valueOf(id) });
michael@0 708 } catch (Exception e) {}
michael@0 709
michael@0 710 return updated;
michael@0 711 }
michael@0 712
michael@0 713 @Override
michael@0 714 public void test() throws Exception {
michael@0 715 ContentValues b = createOneBookmark();
michael@0 716 long id = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b));
michael@0 717
michael@0 718 Cursor c = getBookmarkById(id);
michael@0 719 mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
michael@0 720
michael@0 721 long dateCreated = c.getLong(c.getColumnIndex(BrowserContract.Bookmarks.DATE_CREATED));
michael@0 722 long dateModified = c.getLong(c.getColumnIndex(BrowserContract.Bookmarks.DATE_MODIFIED));
michael@0 723
michael@0 724 ContentValues u = new ContentValues();
michael@0 725 u.put(BrowserContract.Bookmarks.TITLE, b.getAsString(BrowserContract.Bookmarks.TITLE) + "CHANGED");
michael@0 726 u.put(BrowserContract.Bookmarks.URL, b.getAsString(BrowserContract.Bookmarks.URL) + "/more/stuff");
michael@0 727 u.put(BrowserContract.Bookmarks.TAGS, b.getAsString(BrowserContract.Bookmarks.TAGS) + "CHANGED");
michael@0 728 u.put(BrowserContract.Bookmarks.DESCRIPTION, b.getAsString(BrowserContract.Bookmarks.DESCRIPTION) + "CHANGED");
michael@0 729 u.put(BrowserContract.Bookmarks.KEYWORD, b.getAsString(BrowserContract.Bookmarks.KEYWORD) + "CHANGED");
michael@0 730 u.put(BrowserContract.Bookmarks.TYPE, BrowserContract.Bookmarks.TYPE_FOLDER);
michael@0 731 u.put(BrowserContract.Bookmarks.POSITION, 10);
michael@0 732
michael@0 733 int updated = mProvider.update(BrowserContract.Bookmarks.CONTENT_URI, u,
michael@0 734 BrowserContract.Bookmarks._ID + " = ?",
michael@0 735 new String[] { String.valueOf(id) });
michael@0 736
michael@0 737 mAsserter.is((updated == 1), true, "Inserted bookmark was updated");
michael@0 738 c.close();
michael@0 739
michael@0 740 c = getBookmarkById(id);
michael@0 741 mAsserter.is(c.moveToFirst(), true, "Updated bookmark found");
michael@0 742
michael@0 743 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TITLE)), u.getAsString(BrowserContract.Bookmarks.TITLE),
michael@0 744 "Inserted bookmark has correct title");
michael@0 745 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.URL)), u.getAsString(BrowserContract.Bookmarks.URL),
michael@0 746 "Inserted bookmark has correct URL");
michael@0 747 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TAGS)), u.getAsString(BrowserContract.Bookmarks.TAGS),
michael@0 748 "Inserted bookmark has correct tags");
michael@0 749 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.KEYWORD)), u.getAsString(BrowserContract.Bookmarks.KEYWORD),
michael@0 750 "Inserted bookmark has correct keyword");
michael@0 751 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.DESCRIPTION)), u.getAsString(BrowserContract.Bookmarks.DESCRIPTION),
michael@0 752 "Inserted bookmark has correct description");
michael@0 753 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.POSITION)), u.getAsString(BrowserContract.Bookmarks.POSITION),
michael@0 754 "Inserted bookmark has correct position");
michael@0 755 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.TYPE)), u.getAsString(BrowserContract.Bookmarks.TYPE),
michael@0 756 "Inserted bookmark has correct type");
michael@0 757
michael@0 758 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Bookmarks.DATE_CREATED))),
michael@0 759 new Long(dateCreated),
michael@0 760 "Updated bookmark has same creation date");
michael@0 761
michael@0 762 mAsserter.isnot(new Long(c.getLong(c.getColumnIndex(BrowserContract.Bookmarks.DATE_MODIFIED))),
michael@0 763 new Long(dateModified),
michael@0 764 "Updated bookmark has new modification date");
michael@0 765
michael@0 766 updated = updateWithNullCol(id, BrowserContract.Bookmarks.POSITION);
michael@0 767 mAsserter.is((updated > 0), false,
michael@0 768 "Should not be able to update bookmark with null position");
michael@0 769
michael@0 770 updated = updateWithNullCol(id, BrowserContract.Bookmarks.TYPE);
michael@0 771 mAsserter.is((updated > 0), false,
michael@0 772 "Should not be able to update bookmark with null type");
michael@0 773
michael@0 774 u = new ContentValues();
michael@0 775 u.put(BrowserContract.Bookmarks.URL, "http://examples2.com");
michael@0 776
michael@0 777 updated = mProvider.update(ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id), u, null, null);
michael@0 778 c.close();
michael@0 779
michael@0 780 c = getBookmarkById(id);
michael@0 781 mAsserter.is(c.moveToFirst(), true, "Updated bookmark found");
michael@0 782
michael@0 783 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Bookmarks.URL)), u.getAsString(BrowserContract.Bookmarks.URL),
michael@0 784 "Updated bookmark has correct URL using URI with id");
michael@0 785 c.close();
michael@0 786 }
michael@0 787 }
michael@0 788
michael@0 789 private class TestUpdateBookmarksFavicons extends TestCase {
michael@0 790 @Override
michael@0 791 public void test() throws Exception {
michael@0 792 ContentValues b = createOneBookmark();
michael@0 793
michael@0 794 final String favicon = "FAVICON";
michael@0 795 final String newFavicon = "NEW_FAVICON";
michael@0 796 final String pageUrl = b.getAsString(BrowserContract.Bookmarks.URL);
michael@0 797
michael@0 798 mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, b);
michael@0 799
michael@0 800 // Insert the favicon into the favicons table
michael@0 801 ContentValues f = createFaviconEntry(pageUrl, favicon);
michael@0 802 long faviconId = ContentUris.parseId(mProvider.insert(BrowserContract.Favicons.CONTENT_URI, f));
michael@0 803
michael@0 804 Cursor c = getFaviconsByUrl(pageUrl);
michael@0 805 mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
michael@0 806
michael@0 807 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
michael@0 808 favicon, "Inserted favicon has corresponding favicon image");
michael@0 809
michael@0 810 ContentValues u = createFaviconEntry(pageUrl, newFavicon);
michael@0 811 mProvider.update(BrowserContract.Favicons.CONTENT_URI, u, null, null);
michael@0 812 c.close();
michael@0 813
michael@0 814 c = getFaviconsByUrl(pageUrl);
michael@0 815 mAsserter.is(c.moveToFirst(), true, "Updated favicon found");
michael@0 816
michael@0 817 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
michael@0 818 newFavicon, "Updated favicon has corresponding favicon image");
michael@0 819 c.close();
michael@0 820 }
michael@0 821 }
michael@0 822
michael@0 823 /**
michael@0 824 * Create a folder of one thousand and one bookmarks, then impose an order
michael@0 825 * on them.
michael@0 826 *
michael@0 827 * Verify that the reordering worked by querying.
michael@0 828 */
michael@0 829 private class TestPositionBookmarks extends TestCase {
michael@0 830
michael@0 831 public String makeGUID(final long in) {
michael@0 832 String part = String.valueOf(in);
michael@0 833 return "aaaaaaaaaaaa".substring(0, (12 - part.length())) + part;
michael@0 834 }
michael@0 835
michael@0 836 public void compareCursorToItems(final Cursor c, final String[] items, final int count) {
michael@0 837 mAsserter.is(c.moveToFirst(), true, "Folder has children.");
michael@0 838
michael@0 839 int posColumn = c.getColumnIndex(BrowserContract.Bookmarks.POSITION);
michael@0 840 int guidColumn = c.getColumnIndex(BrowserContract.Bookmarks.GUID);
michael@0 841 int i = 0;
michael@0 842
michael@0 843 while (!c.isAfterLast()) {
michael@0 844 String guid = c.getString(guidColumn);
michael@0 845 long pos = c.getLong(posColumn);
michael@0 846 if ((pos != i) || (guid == null) || (!guid.equals(items[i]))) {
michael@0 847 mAsserter.is(pos, (long) i, "Position matches sequence.");
michael@0 848 mAsserter.is(guid, items[i], "GUID matches sequence.");
michael@0 849 }
michael@0 850 ++i;
michael@0 851 c.moveToNext();
michael@0 852 }
michael@0 853
michael@0 854 mAsserter.is(i, count, "Folder has the right number of children.");
michael@0 855 c.close();
michael@0 856 }
michael@0 857
michael@0 858 public static final int NUMBER_OF_CHILDREN = 1001;
michael@0 859 @Override
michael@0 860 public void test() throws Exception {
michael@0 861 // Create the containing folder.
michael@0 862 ContentValues folder = createBookmark("FolderFolder", "", mMobileFolderId,
michael@0 863 BrowserContract.Bookmarks.TYPE_FOLDER, 0, "",
michael@0 864 "description", "keyword");
michael@0 865 folder.put(BrowserContract.Bookmarks.GUID, "folderfolder");
michael@0 866 long folderId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, folder));
michael@0 867
michael@0 868 mAsserter.dumpLog("TestPositionBookmarks: Folder inserted"); // Bug 968951 debug.
michael@0 869
michael@0 870 // Create the children.
michael@0 871 String[] items = new String[NUMBER_OF_CHILDREN];
michael@0 872
michael@0 873 // Reuse the same ContentValues.
michael@0 874 ContentValues item = createBookmark("Test Bookmark", "http://example.com", folderId,
michael@0 875 BrowserContract.Bookmarks.TYPE_FOLDER, 0, "",
michael@0 876 "description", "keyword");
michael@0 877
michael@0 878 for (int i = 0; i < NUMBER_OF_CHILDREN; ++i) {
michael@0 879 String guid = makeGUID(i);
michael@0 880 items[i] = guid;
michael@0 881 item.put(BrowserContract.Bookmarks.GUID, guid);
michael@0 882 item.put(BrowserContract.Bookmarks.POSITION, i);
michael@0 883 item.put(BrowserContract.Bookmarks.URL, "http://example.com/" + guid);
michael@0 884 item.put(BrowserContract.Bookmarks.TITLE, "Test Bookmark " + guid);
michael@0 885 mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, item);
michael@0 886 }
michael@0 887
michael@0 888 mAsserter.dumpLog("TestPositionBookmarks: Bookmarks inserted"); // Bug 968951 debug.
michael@0 889
michael@0 890 Cursor c;
michael@0 891
michael@0 892 // Verify insertion.
michael@0 893 c = getBookmarksByParent(folderId);
michael@0 894 mAsserter.dumpLog("TestPositionBookmarks: Got bookmarks by parent"); // Bug 968951 debug.
michael@0 895 compareCursorToItems(c, items, NUMBER_OF_CHILDREN);
michael@0 896 c.close();
michael@0 897
michael@0 898 // Now permute the items array.
michael@0 899 Random rand = new Random();
michael@0 900 for (int i = 0; i < NUMBER_OF_CHILDREN; ++i) {
michael@0 901 final int newPosition = rand.nextInt(NUMBER_OF_CHILDREN);
michael@0 902 final String switched = items[newPosition];
michael@0 903 items[newPosition] = items[i];
michael@0 904 items[i] = switched;
michael@0 905 }
michael@0 906
michael@0 907 // Impose the positions.
michael@0 908 long updated = mProvider.update(BrowserContract.Bookmarks.POSITIONS_CONTENT_URI, null, null, items);
michael@0 909 mAsserter.is(updated, (long) NUMBER_OF_CHILDREN, "Updated " + NUMBER_OF_CHILDREN + " positions.");
michael@0 910
michael@0 911 // Verify that the database was updated.
michael@0 912 c = getBookmarksByParent(folderId);
michael@0 913 compareCursorToItems(c, items, NUMBER_OF_CHILDREN);
michael@0 914 c.close();
michael@0 915 }
michael@0 916 }
michael@0 917
michael@0 918 private class TestInsertHistory extends TestCase {
michael@0 919 private long insertWithNullCol(String colName) throws Exception {
michael@0 920 ContentValues h = createOneHistoryEntry();
michael@0 921 h.putNull(colName);
michael@0 922 long id = -1;
michael@0 923
michael@0 924 try {
michael@0 925 id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 926 } catch (Exception e) {}
michael@0 927
michael@0 928 return id;
michael@0 929 }
michael@0 930
michael@0 931 @Override
michael@0 932 public void test() throws Exception {
michael@0 933 ContentValues h = createOneHistoryEntry();
michael@0 934 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 935 Cursor c = getHistoryEntryById(id);
michael@0 936
michael@0 937 mAsserter.is(c.moveToFirst(), true, "Inserted history entry found");
michael@0 938
michael@0 939 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.TITLE)), h.getAsString(BrowserContract.History.TITLE),
michael@0 940 "Inserted history entry has correct title");
michael@0 941 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.URL)), h.getAsString(BrowserContract.History.URL),
michael@0 942 "Inserted history entry has correct URL");
michael@0 943 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.VISITS)), h.getAsString(BrowserContract.History.VISITS),
michael@0 944 "Inserted history entry has correct number of visits");
michael@0 945 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.DATE_LAST_VISITED)), h.getAsString(BrowserContract.History.DATE_LAST_VISITED),
michael@0 946 "Inserted history entry has correct last visited date");
michael@0 947 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.IS_DELETED)), String.valueOf(0),
michael@0 948 "Inserted history entry has correct is-deleted state");
michael@0 949
michael@0 950 id = insertWithNullCol(BrowserContract.History.URL);
michael@0 951 mAsserter.is(new Long(id), new Long(-1),
michael@0 952 "Should not be able to insert history with null URL");
michael@0 953
michael@0 954 id = insertWithNullCol(BrowserContract.History.VISITS);
michael@0 955 mAsserter.is(new Long(id), new Long(-1),
michael@0 956 "Should not be able to insert history with null number of visits");
michael@0 957 c.close();
michael@0 958 }
michael@0 959 }
michael@0 960
michael@0 961 private class TestInsertHistoryFavicons extends TestCase {
michael@0 962 @Override
michael@0 963 public void test() throws Exception {
michael@0 964 ContentValues h = createOneHistoryEntry();
michael@0 965
michael@0 966 final String favicon = "FAVICON";
michael@0 967 final String pageUrl = h.getAsString(BrowserContract.History.URL);
michael@0 968
michael@0 969 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 970
michael@0 971 // Insert the favicon into the favicons table
michael@0 972 mProvider.insert(BrowserContract.Favicons.CONTENT_URI, createFaviconEntry(pageUrl, favicon));
michael@0 973
michael@0 974 Cursor c = getHistoryEntryById(id, new String[] { BrowserContract.History.FAVICON });
michael@0 975
michael@0 976 mAsserter.is(c.moveToFirst(), true, "Inserted history entry found");
michael@0 977
michael@0 978 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.History.FAVICON)), "UTF8"),
michael@0 979 favicon, "Inserted history entry has corresponding favicon image");
michael@0 980 c.close();
michael@0 981
michael@0 982 c = getFaviconsByUrl(pageUrl);
michael@0 983 mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
michael@0 984
michael@0 985 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
michael@0 986 favicon, "Inserted favicon has corresponding favicon image");
michael@0 987 c.close();
michael@0 988 }
michael@0 989 }
michael@0 990
michael@0 991 private class TestDeleteHistory extends TestCase {
michael@0 992 private long insertOneHistoryEntry() throws Exception {
michael@0 993 ContentValues h = createOneHistoryEntry();
michael@0 994 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 995
michael@0 996 Cursor c = getHistoryEntryById(id);
michael@0 997 mAsserter.is(c.moveToFirst(), true, "Inserted history entry found");
michael@0 998 c.close();
michael@0 999
michael@0 1000 return id;
michael@0 1001 }
michael@0 1002
michael@0 1003 @Override
michael@0 1004 public void test() throws Exception {
michael@0 1005 long id = insertOneHistoryEntry();
michael@0 1006
michael@0 1007 int deleted = mProvider.delete(BrowserContract.History.CONTENT_URI,
michael@0 1008 BrowserContract.History._ID + " = ?",
michael@0 1009 new String[] { String.valueOf(id) });
michael@0 1010
michael@0 1011 mAsserter.is((deleted == 1), true, "Inserted history entry was deleted");
michael@0 1012
michael@0 1013 Cursor c = getHistoryEntryById(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
michael@0 1014 mAsserter.is(c.moveToFirst(), true, "Deleted history entry was only marked as deleted");
michael@0 1015
michael@0 1016 deleted = mProvider.delete(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_IS_SYNC, "1"),
michael@0 1017 BrowserContract.History._ID + " = ?",
michael@0 1018 new String[] { String.valueOf(id) });
michael@0 1019
michael@0 1020 mAsserter.is((deleted == 1), true, "Inserted history entry was deleted");
michael@0 1021 c.close();
michael@0 1022
michael@0 1023 c = getHistoryEntryById(appendUriParam(BrowserContract.History.CONTENT_URI, BrowserContract.PARAM_SHOW_DELETED, "1"), id);
michael@0 1024 mAsserter.is(c.moveToFirst(), false, "Inserted history is now actually deleted");
michael@0 1025
michael@0 1026 id = insertOneHistoryEntry();
michael@0 1027
michael@0 1028 deleted = mProvider.delete(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), null, null);
michael@0 1029 mAsserter.is((deleted == 1), true,
michael@0 1030 "Inserted history entry was deleted using URI with id");
michael@0 1031 c.close();
michael@0 1032
michael@0 1033 c = getHistoryEntryById(id);
michael@0 1034 mAsserter.is(c.moveToFirst(), false,
michael@0 1035 "Inserted history entry can't be found after deletion using URI with ID");
michael@0 1036 c.close();
michael@0 1037 }
michael@0 1038 }
michael@0 1039
michael@0 1040 private class TestDeleteHistoryFavicons extends TestCase {
michael@0 1041 @Override
michael@0 1042 public void test() throws Exception {
michael@0 1043 ContentValues h = createOneHistoryEntry();
michael@0 1044
michael@0 1045 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 1046 final String pageUrl = h.getAsString(BrowserContract.History.URL);
michael@0 1047
michael@0 1048 // Insert the favicon into the favicons table
michael@0 1049 mProvider.insert(BrowserContract.Favicons.CONTENT_URI, createFaviconEntry(pageUrl, "FAVICON"));
michael@0 1050
michael@0 1051 Cursor c = getFaviconsByUrl(pageUrl);
michael@0 1052 mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
michael@0 1053
michael@0 1054 mProvider.delete(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), null, null);
michael@0 1055 c.close();
michael@0 1056
michael@0 1057 c = getFaviconsByUrl(pageUrl);
michael@0 1058 mAsserter.is(c.moveToFirst(), false, "Favicon is deleted with last reference to it");
michael@0 1059 c.close();
michael@0 1060 }
michael@0 1061 }
michael@0 1062
michael@0 1063 private class TestUpdateHistory extends TestCase {
michael@0 1064 private int updateWithNullCol(long id, String colName) throws Exception {
michael@0 1065 ContentValues u = new ContentValues();
michael@0 1066 u.putNull(colName);
michael@0 1067
michael@0 1068 int updated = 0;
michael@0 1069
michael@0 1070 try {
michael@0 1071 updated = mProvider.update(BrowserContract.History.CONTENT_URI, u,
michael@0 1072 BrowserContract.History._ID + " = ?",
michael@0 1073 new String[] { String.valueOf(id) });
michael@0 1074 } catch (Exception e) {}
michael@0 1075
michael@0 1076 return updated;
michael@0 1077 }
michael@0 1078
michael@0 1079 @Override
michael@0 1080 public void test() throws Exception {
michael@0 1081 ContentValues h = createOneHistoryEntry();
michael@0 1082 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 1083
michael@0 1084 Cursor c = getHistoryEntryById(id);
michael@0 1085 mAsserter.is(c.moveToFirst(), true, "Inserted history entry found");
michael@0 1086
michael@0 1087 long dateCreated = c.getLong(c.getColumnIndex(BrowserContract.History.DATE_CREATED));
michael@0 1088 long dateModified = c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED));
michael@0 1089
michael@0 1090 ContentValues u = new ContentValues();
michael@0 1091 u.put(BrowserContract.History.VISITS, h.getAsInteger(BrowserContract.History.VISITS) + 1);
michael@0 1092 u.put(BrowserContract.History.DATE_LAST_VISITED, System.currentTimeMillis());
michael@0 1093 u.put(BrowserContract.History.TITLE, h.getAsString(BrowserContract.History.TITLE) + "CHANGED");
michael@0 1094 u.put(BrowserContract.History.URL, h.getAsString(BrowserContract.History.URL) + "/more/stuff");
michael@0 1095
michael@0 1096 int updated = mProvider.update(BrowserContract.History.CONTENT_URI, u,
michael@0 1097 BrowserContract.History._ID + " = ?",
michael@0 1098 new String[] { String.valueOf(id) });
michael@0 1099
michael@0 1100 mAsserter.is((updated == 1), true, "Inserted history entry was updated");
michael@0 1101 c.close();
michael@0 1102
michael@0 1103 c = getHistoryEntryById(id);
michael@0 1104 mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
michael@0 1105
michael@0 1106 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.TITLE)), u.getAsString(BrowserContract.History.TITLE),
michael@0 1107 "Updated history entry has correct title");
michael@0 1108 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.URL)), u.getAsString(BrowserContract.History.URL),
michael@0 1109 "Updated history entry has correct URL");
michael@0 1110 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.VISITS)), u.getAsString(BrowserContract.History.VISITS),
michael@0 1111 "Updated history entry has correct number of visits");
michael@0 1112 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.DATE_LAST_VISITED)), u.getAsString(BrowserContract.History.DATE_LAST_VISITED),
michael@0 1113 "Updated history entry has correct last visited date");
michael@0 1114
michael@0 1115 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_CREATED))),
michael@0 1116 new Long(dateCreated),
michael@0 1117 "Updated history entry has same creation date");
michael@0 1118
michael@0 1119 mAsserter.isnot(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED))),
michael@0 1120 new Long(dateModified),
michael@0 1121 "Updated history entry has new modification date");
michael@0 1122
michael@0 1123 updated = updateWithNullCol(id, BrowserContract.History.URL);
michael@0 1124 mAsserter.is((updated > 0), false,
michael@0 1125 "Should not be able to update history with null URL");
michael@0 1126
michael@0 1127 updated = updateWithNullCol(id, BrowserContract.History.VISITS);
michael@0 1128 mAsserter.is((updated > 0), false,
michael@0 1129 "Should not be able to update history with null number of visits");
michael@0 1130
michael@0 1131 u = new ContentValues();
michael@0 1132 u.put(BrowserContract.History.URL, "http://examples2.com");
michael@0 1133
michael@0 1134 updated = mProvider.update(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), u, null, null);
michael@0 1135 c.close();
michael@0 1136
michael@0 1137 c = getHistoryEntryById(id);
michael@0 1138 mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
michael@0 1139
michael@0 1140 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.URL)), u.getAsString(BrowserContract.History.URL),
michael@0 1141 "Updated history entry has correct URL using URI with id");
michael@0 1142 c.close();
michael@0 1143 }
michael@0 1144 }
michael@0 1145
michael@0 1146 private class TestUpdateHistoryFavicons extends TestCase {
michael@0 1147 @Override
michael@0 1148 public void test() throws Exception {
michael@0 1149 ContentValues h = createOneHistoryEntry();
michael@0 1150
michael@0 1151 final String favicon = "FAVICON";
michael@0 1152 final String newFavicon = "NEW_FAVICON";
michael@0 1153 final String pageUrl = h.getAsString(BrowserContract.History.URL);
michael@0 1154
michael@0 1155 mProvider.insert(BrowserContract.History.CONTENT_URI, h);
michael@0 1156
michael@0 1157 // Insert the favicon into the favicons table
michael@0 1158 mProvider.insert(BrowserContract.Favicons.CONTENT_URI, createFaviconEntry(pageUrl, favicon));
michael@0 1159
michael@0 1160 Cursor c = getFaviconsByUrl(pageUrl);
michael@0 1161 mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
michael@0 1162
michael@0 1163 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
michael@0 1164 favicon, "Inserted favicon has corresponding favicon image");
michael@0 1165
michael@0 1166 ContentValues u = createFaviconEntry(pageUrl, newFavicon);
michael@0 1167
michael@0 1168 mProvider.update(BrowserContract.Favicons.CONTENT_URI, u, null, null);
michael@0 1169 c.close();
michael@0 1170
michael@0 1171 c = getFaviconsByUrl(pageUrl);
michael@0 1172 mAsserter.is(c.moveToFirst(), true, "Updated favicon found");
michael@0 1173
michael@0 1174 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Combined.FAVICON)), "UTF8"),
michael@0 1175 newFavicon, "Updated favicon has corresponding favicon image");
michael@0 1176 c.close();
michael@0 1177 }
michael@0 1178 }
michael@0 1179
michael@0 1180 private class TestUpdateOrInsertHistory extends TestCase {
michael@0 1181 private final String TEST_URL_1 = "http://example.com";
michael@0 1182 private final String TEST_URL_2 = "http://example.org";
michael@0 1183 private final String TEST_TITLE = "Example";
michael@0 1184
michael@0 1185 private long getHistoryEntryIdByUrl(String url) {
michael@0 1186 Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI,
michael@0 1187 new String[] { BrowserContract.History._ID },
michael@0 1188 BrowserContract.History.URL + " = ?",
michael@0 1189 new String[] { url },
michael@0 1190 null);
michael@0 1191 c.moveToFirst();
michael@0 1192 long id = c.getLong(0);
michael@0 1193 c.close();
michael@0 1194
michael@0 1195 return id;
michael@0 1196 }
michael@0 1197
michael@0 1198 @Override
michael@0 1199 public void test() throws Exception {
michael@0 1200 Uri updateHistoryUri = BrowserContract.History.CONTENT_URI.buildUpon().
michael@0 1201 appendQueryParameter("increment_visits", "true").build();
michael@0 1202 Uri updateOrInsertHistoryUri = BrowserContract.History.CONTENT_URI.buildUpon().
michael@0 1203 appendQueryParameter("insert_if_needed", "true").
michael@0 1204 appendQueryParameter("increment_visits", "true").build();
michael@0 1205
michael@0 1206 // Update a non-existent history entry, without specifying visits or title
michael@0 1207 ContentValues values = new ContentValues();
michael@0 1208 values.put(BrowserContract.History.URL, TEST_URL_1);
michael@0 1209
michael@0 1210 int updated = mProvider.update(updateHistoryUri, values,
michael@0 1211 BrowserContract.History.URL + " = ?",
michael@0 1212 new String[] { TEST_URL_1 });
michael@0 1213 mAsserter.is((updated == 0), true, "History entry was not updated");
michael@0 1214 Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, null, null, null);
michael@0 1215 mAsserter.is(c.moveToFirst(), false, "History entry was not inserted");
michael@0 1216 c.close();
michael@0 1217
michael@0 1218 // Now let's try with update-or-insert.
michael@0 1219 updated = mProvider.update(updateOrInsertHistoryUri, values,
michael@0 1220 BrowserContract.History.URL + " = ?",
michael@0 1221 new String[] { TEST_URL_1 });
michael@0 1222 mAsserter.is((updated == 1), true, "History entry was inserted");
michael@0 1223
michael@0 1224 long id = getHistoryEntryIdByUrl(TEST_URL_1);
michael@0 1225 c = getHistoryEntryById(id);
michael@0 1226 mAsserter.is(c.moveToFirst(), true, "History entry was inserted");
michael@0 1227
michael@0 1228 long dateCreated = c.getLong(c.getColumnIndex(BrowserContract.History.DATE_CREATED));
michael@0 1229 long dateModified = c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED));
michael@0 1230
michael@0 1231 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.VISITS))), new Long(1),
michael@0 1232 "Inserted history entry has correct default number of visits");
michael@0 1233 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.TITLE)), TEST_URL_1,
michael@0 1234 "Inserted history entry has correct default title");
michael@0 1235
michael@0 1236 // Update the history entry, without specifying an additional visit count
michael@0 1237 values = new ContentValues();
michael@0 1238 values.put(BrowserContract.History.DATE_LAST_VISITED, System.currentTimeMillis());
michael@0 1239 values.put(BrowserContract.History.TITLE, TEST_TITLE);
michael@0 1240
michael@0 1241 updated = mProvider.update(updateOrInsertHistoryUri, values,
michael@0 1242 BrowserContract.History._ID + " = ?",
michael@0 1243 new String[] { String.valueOf(id) });
michael@0 1244 mAsserter.is((updated == 1), true, "Inserted history entry was updated");
michael@0 1245 c.close();
michael@0 1246
michael@0 1247 c = getHistoryEntryById(id);
michael@0 1248 mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
michael@0 1249
michael@0 1250 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.TITLE)), TEST_TITLE,
michael@0 1251 "Updated history entry has correct title");
michael@0 1252 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.VISITS))), new Long(2),
michael@0 1253 "Updated history entry has correct number of visits");
michael@0 1254 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_CREATED))), new Long(dateCreated),
michael@0 1255 "Updated history entry has same creation date");
michael@0 1256 mAsserter.isnot(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED))), new Long(dateModified),
michael@0 1257 "Updated history entry has new modification date");
michael@0 1258
michael@0 1259 // Create a new history entry, specifying visits and history
michael@0 1260 values = new ContentValues();
michael@0 1261 values.put(BrowserContract.History.URL, TEST_URL_2);
michael@0 1262 values.put(BrowserContract.History.TITLE, TEST_TITLE);
michael@0 1263 values.put(BrowserContract.History.VISITS, 10);
michael@0 1264
michael@0 1265 updated = mProvider.update(updateOrInsertHistoryUri, values,
michael@0 1266 BrowserContract.History.URL + " = ?",
michael@0 1267 new String[] { values.getAsString(BrowserContract.History.URL) });
michael@0 1268 mAsserter.is((updated == 1), true, "History entry was inserted");
michael@0 1269
michael@0 1270 id = getHistoryEntryIdByUrl(TEST_URL_2);
michael@0 1271 c.close();
michael@0 1272
michael@0 1273 c = getHistoryEntryById(id);
michael@0 1274 mAsserter.is(c.moveToFirst(), true, "History entry was inserted");
michael@0 1275
michael@0 1276 dateCreated = c.getLong(c.getColumnIndex(BrowserContract.History.DATE_CREATED));
michael@0 1277 dateModified = c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED));
michael@0 1278
michael@0 1279 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.VISITS))), new Long(10),
michael@0 1280 "Inserted history entry has correct specified number of visits");
michael@0 1281 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.TITLE)), TEST_TITLE,
michael@0 1282 "Inserted history entry has correct specified title");
michael@0 1283
michael@0 1284 // Update the history entry, specifying additional visit count
michael@0 1285 values = new ContentValues();
michael@0 1286 values.put(BrowserContract.History.VISITS, 10);
michael@0 1287
michael@0 1288 updated = mProvider.update(updateOrInsertHistoryUri, values,
michael@0 1289 BrowserContract.History._ID + " = ?",
michael@0 1290 new String[] { String.valueOf(id) });
michael@0 1291 mAsserter.is((updated == 1), true, "Inserted history entry was updated");
michael@0 1292 c.close();
michael@0 1293
michael@0 1294 c = getHistoryEntryById(id);
michael@0 1295 mAsserter.is(c.moveToFirst(), true, "Updated history entry found");
michael@0 1296
michael@0 1297 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.TITLE)), TEST_TITLE,
michael@0 1298 "Updated history entry has correct unchanged title");
michael@0 1299 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.History.URL)), TEST_URL_2,
michael@0 1300 "Updated history entry has correct unchanged URL");
michael@0 1301 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.VISITS))), new Long(20),
michael@0 1302 "Updated history entry has correct number of visits");
michael@0 1303 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_CREATED))), new Long(dateCreated),
michael@0 1304 "Updated history entry has same creation date");
michael@0 1305 mAsserter.isnot(new Long(c.getLong(c.getColumnIndex(BrowserContract.History.DATE_MODIFIED))), new Long(dateModified),
michael@0 1306 "Updated history entry has new modification date");
michael@0 1307 c.close();
michael@0 1308
michael@0 1309 }
michael@0 1310 }
michael@0 1311
michael@0 1312 private class TestInsertHistoryThumbnails extends TestCase {
michael@0 1313 @Override
michael@0 1314 public void test() throws Exception {
michael@0 1315 ContentValues h = createOneHistoryEntry();
michael@0 1316
michael@0 1317 final String thumbnail = "THUMBNAIL";
michael@0 1318 final String pageUrl = h.getAsString(BrowserContract.History.URL);
michael@0 1319
michael@0 1320 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 1321
michael@0 1322 // Insert the thumbnail into the thumbnails table
michael@0 1323 mProvider.insert(BrowserContract.Thumbnails.CONTENT_URI, createThumbnailEntry(pageUrl, thumbnail));
michael@0 1324
michael@0 1325 Cursor c = getThumbnailByUrl(pageUrl);
michael@0 1326 mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
michael@0 1327
michael@0 1328 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Thumbnails.DATA)), "UTF8"),
michael@0 1329 thumbnail, "Inserted thumbnail has corresponding thumbnail image");
michael@0 1330 c.close();
michael@0 1331 }
michael@0 1332 }
michael@0 1333
michael@0 1334 private class TestUpdateHistoryThumbnails extends TestCase {
michael@0 1335 @Override
michael@0 1336 public void test() throws Exception {
michael@0 1337 ContentValues h = createOneHistoryEntry();
michael@0 1338
michael@0 1339 final String thumbnail = "THUMBNAIL";
michael@0 1340 final String newThumbnail = "NEW_THUMBNAIL";
michael@0 1341 final String pageUrl = h.getAsString(BrowserContract.History.URL);
michael@0 1342
michael@0 1343 mProvider.insert(BrowserContract.History.CONTENT_URI, h);
michael@0 1344
michael@0 1345 // Insert the thumbnail into the thumbnails table
michael@0 1346 mProvider.insert(BrowserContract.Thumbnails.CONTENT_URI, createThumbnailEntry(pageUrl, thumbnail));
michael@0 1347
michael@0 1348 Cursor c = getThumbnailByUrl(pageUrl);
michael@0 1349 mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
michael@0 1350
michael@0 1351 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Thumbnails.DATA)), "UTF8"),
michael@0 1352 thumbnail, "Inserted thumbnail has corresponding thumbnail image");
michael@0 1353
michael@0 1354 ContentValues u = createThumbnailEntry(pageUrl, newThumbnail);
michael@0 1355
michael@0 1356 mProvider.update(BrowserContract.Thumbnails.CONTENT_URI, u, null, null);
michael@0 1357 c.close();
michael@0 1358
michael@0 1359 c = getThumbnailByUrl(pageUrl);
michael@0 1360 mAsserter.is(c.moveToFirst(), true, "Updated thumbnail found");
michael@0 1361
michael@0 1362 mAsserter.is(new String(c.getBlob(c.getColumnIndex(BrowserContract.Thumbnails.DATA)), "UTF8"),
michael@0 1363 newThumbnail, "Updated thumbnail has corresponding thumbnail image");
michael@0 1364 c.close();
michael@0 1365 }
michael@0 1366 }
michael@0 1367
michael@0 1368 private class TestDeleteHistoryThumbnails extends TestCase {
michael@0 1369 @Override
michael@0 1370 public void test() throws Exception {
michael@0 1371 ContentValues h = createOneHistoryEntry();
michael@0 1372
michael@0 1373 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 1374 final String pageUrl = h.getAsString(BrowserContract.History.URL);
michael@0 1375
michael@0 1376 // Insert the thumbnail into the thumbnails table
michael@0 1377 mProvider.insert(BrowserContract.Thumbnails.CONTENT_URI, createThumbnailEntry(pageUrl, "THUMBNAIL"));
michael@0 1378
michael@0 1379 Cursor c = getThumbnailByUrl(pageUrl);
michael@0 1380 mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
michael@0 1381
michael@0 1382 mProvider.delete(ContentUris.withAppendedId(BrowserContract.History.CONTENT_URI, id), null, null);
michael@0 1383 c.close();
michael@0 1384
michael@0 1385 c = getThumbnailByUrl(pageUrl);
michael@0 1386 mAsserter.is(c.moveToFirst(), false, "Thumbnail is deleted with last reference to it");
michael@0 1387 c.close();
michael@0 1388 }
michael@0 1389 }
michael@0 1390
michael@0 1391 private class TestCombinedView extends TestCase {
michael@0 1392 @Override
michael@0 1393 public void test() throws Exception {
michael@0 1394 final String TITLE_1 = "Test Page 1";
michael@0 1395 final String TITLE_2 = "Test Page 2";
michael@0 1396 final String TITLE_3_HISTORY = "Test Page 3 (History Entry)";
michael@0 1397 final String TITLE_3_BOOKMARK = "Test Page 3 (Bookmark Entry)";
michael@0 1398 final String TITLE_3_BOOKMARK2 = "Test Page 3 (Bookmark Entry 2)";
michael@0 1399
michael@0 1400 final String URL_1 = "http://example1.com";
michael@0 1401 final String URL_2 = "http://example2.com";
michael@0 1402 final String URL_3 = "http://example3.com";
michael@0 1403
michael@0 1404 final int VISITS = 10;
michael@0 1405 final long LAST_VISITED = System.currentTimeMillis();
michael@0 1406
michael@0 1407 // Create a basic history entry
michael@0 1408 ContentValues basicHistory = createHistoryEntry(TITLE_1, URL_1, VISITS, LAST_VISITED);
michael@0 1409 long basicHistoryId = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, basicHistory));
michael@0 1410
michael@0 1411 // Create a basic bookmark entry
michael@0 1412 ContentValues basicBookmark = createBookmark(TITLE_2, URL_2, mMobileFolderId,
michael@0 1413 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1414 long basicBookmarkId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, basicBookmark));
michael@0 1415
michael@0 1416 // Create a history entry and bookmark entry with the same URL to
michael@0 1417 // represent a visited bookmark
michael@0 1418 ContentValues combinedHistory = createHistoryEntry(TITLE_3_HISTORY, URL_3, VISITS, LAST_VISITED);
michael@0 1419 long combinedHistoryId = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, combinedHistory));
michael@0 1420
michael@0 1421
michael@0 1422 ContentValues combinedBookmark = createBookmark(TITLE_3_BOOKMARK, URL_3, mMobileFolderId,
michael@0 1423 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1424 long combinedBookmarkId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, combinedBookmark));
michael@0 1425
michael@0 1426 ContentValues combinedBookmark2 = createBookmark(TITLE_3_BOOKMARK2, URL_3, mMobileFolderId,
michael@0 1427 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1428 long combinedBookmarkId2 = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, combinedBookmark2));
michael@0 1429
michael@0 1430 // Create a bookmark folder to make sure it _doesn't_ show up in the results
michael@0 1431 ContentValues folderBookmark = createBookmark("", "", mMobileFolderId,
michael@0 1432 BrowserContract.Bookmarks.TYPE_FOLDER, 0, "tags", "description", "keyword");
michael@0 1433 mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, folderBookmark);
michael@0 1434
michael@0 1435 // Sort entries by url so we can check them individually
michael@0 1436 Cursor c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, BrowserContract.Combined.URL);
michael@0 1437
michael@0 1438 mAsserter.is(c.getCount(), 3, "3 combined entries found");
michael@0 1439
michael@0 1440 // First combined entry is basic history entry
michael@0 1441 mAsserter.is(c.moveToFirst(), true, "Found basic history entry");
michael@0 1442 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined._ID))), new Long(0),
michael@0 1443 "Combined _id column should always be 0");
michael@0 1444 // TODO: Should we change BrowserProvider to make this return -1, not 0?
michael@0 1445 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(0),
michael@0 1446 "Bookmark id should be 0 for basic history entry");
michael@0 1447 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.HISTORY_ID))), new Long(basicHistoryId),
michael@0 1448 "Basic history entry has correct history id");
michael@0 1449 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Combined.TITLE)), TITLE_1,
michael@0 1450 "Basic history entry has correct title");
michael@0 1451 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Combined.URL)), URL_1,
michael@0 1452 "Basic history entry has correct url");
michael@0 1453 mAsserter.is(c.getInt(c.getColumnIndex(BrowserContract.Combined.VISITS)), VISITS,
michael@0 1454 "Basic history entry has correct number of visits");
michael@0 1455 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DATE_LAST_VISITED))), new Long(LAST_VISITED),
michael@0 1456 "Basic history entry has correct last visit time");
michael@0 1457
michael@0 1458 // Second combined entry is basic bookmark entry
michael@0 1459 mAsserter.is(c.moveToNext(), true, "Found basic bookmark entry");
michael@0 1460 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined._ID))), new Long(0),
michael@0 1461 "Combined _id column should always be 0");
michael@0 1462 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(basicBookmarkId),
michael@0 1463 "Basic bookmark entry has correct bookmark id");
michael@0 1464 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.HISTORY_ID))), new Long(-1),
michael@0 1465 "History id should be -1 for basic bookmark entry");
michael@0 1466 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Combined.TITLE)), TITLE_2,
michael@0 1467 "Basic bookmark entry has correct title");
michael@0 1468 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Combined.URL)), URL_2,
michael@0 1469 "Basic bookmark entry has correct url");
michael@0 1470 mAsserter.is(c.getInt(c.getColumnIndex(BrowserContract.Combined.VISITS)), -1,
michael@0 1471 "Visits should be -1 for basic bookmark entry");
michael@0 1472 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DATE_LAST_VISITED))), new Long(-1),
michael@0 1473 "Basic entry has correct last visit time");
michael@0 1474
michael@0 1475 // Third combined entry is a combined history/bookmark entry
michael@0 1476 mAsserter.is(c.moveToNext(), true, "Found third combined entry");
michael@0 1477 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined._ID))), new Long(0),
michael@0 1478 "Combined _id column should always be 0");
michael@0 1479 // The bookmark data (bookmark_id and title) associated with the combined entry is non-deterministic,
michael@0 1480 // it might end up with data coming from any of the matching bookmark entries.
michael@0 1481 mAsserter.is(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID)) == combinedBookmarkId ||
michael@0 1482 c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID)) == combinedBookmarkId2, true,
michael@0 1483 "Combined entry has correct bookmark id");
michael@0 1484 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Combined.TITLE)).equals(TITLE_3_BOOKMARK) ||
michael@0 1485 c.getString(c.getColumnIndex(BrowserContract.Combined.TITLE)).equals(TITLE_3_BOOKMARK2), true,
michael@0 1486 "Combined entry has title corresponding to bookmark entry");
michael@0 1487 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.HISTORY_ID))), new Long(combinedHistoryId),
michael@0 1488 "Combined entry has correct history id");
michael@0 1489 mAsserter.is(c.getString(c.getColumnIndex(BrowserContract.Combined.URL)), URL_3,
michael@0 1490 "Combined entry has correct url");
michael@0 1491 mAsserter.is(c.getInt(c.getColumnIndex(BrowserContract.Combined.VISITS)), VISITS,
michael@0 1492 "Combined entry has correct number of visits");
michael@0 1493 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DATE_LAST_VISITED))), new Long(LAST_VISITED),
michael@0 1494 "Combined entry has correct last visit time");
michael@0 1495 c.close();
michael@0 1496 }
michael@0 1497 }
michael@0 1498
michael@0 1499 private class TestCombinedViewDisplay extends TestCase {
michael@0 1500 @Override
michael@0 1501 public void test() throws Exception {
michael@0 1502 final String TITLE_1 = "Test Page 1";
michael@0 1503 final String TITLE_2 = "Test Page 2";
michael@0 1504 final String TITLE_3_HISTORY = "Test Page 3 (History Entry)";
michael@0 1505 final String TITLE_3_BOOKMARK = "Test Page 3 (Bookmark Entry)";
michael@0 1506 final String TITLE_4 = "Test Page 4";
michael@0 1507
michael@0 1508 final String URL_1 = "http://example.com";
michael@0 1509 final String URL_2 = "http://example.org";
michael@0 1510 final String URL_3 = "http://examples2.com";
michael@0 1511 final String URL_4 = "http://readinglist.com";
michael@0 1512
michael@0 1513 final int VISITS = 10;
michael@0 1514 final long LAST_VISITED = System.currentTimeMillis();
michael@0 1515
michael@0 1516 // Create a basic history entry
michael@0 1517 ContentValues basicHistory = createHistoryEntry(TITLE_1, URL_1, VISITS, LAST_VISITED);
michael@0 1518 ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, basicHistory));
michael@0 1519
michael@0 1520 // Create a basic bookmark entry
michael@0 1521 ContentValues basicBookmark = createBookmark(TITLE_2, URL_2, mMobileFolderId,
michael@0 1522 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1523 mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, basicBookmark);
michael@0 1524
michael@0 1525 // Create a history entry and bookmark entry with the same URL to
michael@0 1526 // represent a visited bookmark
michael@0 1527 ContentValues combinedHistory = createHistoryEntry(TITLE_3_HISTORY, URL_3, VISITS, LAST_VISITED);
michael@0 1528 mProvider.insert(BrowserContract.History.CONTENT_URI, combinedHistory);
michael@0 1529
michael@0 1530 ContentValues combinedBookmark = createBookmark(TITLE_3_BOOKMARK, URL_3, mMobileFolderId,
michael@0 1531 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1532 mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, combinedBookmark);
michael@0 1533
michael@0 1534 // Create a reading list entries
michael@0 1535 int readingListId = BrowserContract.Bookmarks.FIXED_READING_LIST_ID;
michael@0 1536 ContentValues readingListItem = createBookmark(TITLE_3_BOOKMARK, URL_3, readingListId,
michael@0 1537 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1538 long readingListItemId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, readingListItem));
michael@0 1539
michael@0 1540 ContentValues readingListItem2 = createBookmark(TITLE_4, URL_4, readingListId,
michael@0 1541 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1542 long readingListItemId2 = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, readingListItem2));
michael@0 1543
michael@0 1544 Cursor c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
michael@0 1545 mAsserter.is(c.getCount(), 4, "4 combined entries found");
michael@0 1546
michael@0 1547 while (c.moveToNext()) {
michael@0 1548 long id = c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID));
michael@0 1549
michael@0 1550 int display = c.getInt(c.getColumnIndex(BrowserContract.Combined.DISPLAY));
michael@0 1551 int expectedDisplay = (id == readingListItemId || id == readingListItemId2 ? BrowserContract.Combined.DISPLAY_READER : BrowserContract.Combined.DISPLAY_NORMAL);
michael@0 1552
michael@0 1553 mAsserter.is(new Integer(display), new Integer(expectedDisplay),
michael@0 1554 "Combined display column should always be DISPLAY_READER for the reading list item");
michael@0 1555 }
michael@0 1556 c.close();
michael@0 1557 }
michael@0 1558 }
michael@0 1559
michael@0 1560 private class TestCombinedViewWithDeletedBookmark extends TestCase {
michael@0 1561 @Override
michael@0 1562 public void test() throws Exception {
michael@0 1563 final String TITLE = "Test Page 1";
michael@0 1564 final String URL = "http://example.com";
michael@0 1565 final int VISITS = 10;
michael@0 1566 final long LAST_VISITED = System.currentTimeMillis();
michael@0 1567
michael@0 1568 // Create a combined history entry
michael@0 1569 ContentValues combinedHistory = createHistoryEntry(TITLE, URL, VISITS, LAST_VISITED);
michael@0 1570 mProvider.insert(BrowserContract.History.CONTENT_URI, combinedHistory);
michael@0 1571
michael@0 1572 // Create a combined bookmark entry
michael@0 1573 ContentValues combinedBookmark = createBookmark(TITLE, URL, mMobileFolderId,
michael@0 1574 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1575 long combinedBookmarkId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, combinedBookmark));
michael@0 1576
michael@0 1577 Cursor c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
michael@0 1578 mAsserter.is(c.getCount(), 1, "1 combined entry found");
michael@0 1579
michael@0 1580 mAsserter.is(c.moveToFirst(), true, "Found combined entry with bookmark id");
michael@0 1581 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(combinedBookmarkId),
michael@0 1582 "Bookmark id should be set correctly on combined entry");
michael@0 1583
michael@0 1584 int deleted = mProvider.delete(BrowserContract.Bookmarks.CONTENT_URI,
michael@0 1585 BrowserContract.Bookmarks._ID + " = ?",
michael@0 1586 new String[] { String.valueOf(combinedBookmarkId) });
michael@0 1587
michael@0 1588 mAsserter.is((deleted == 1), true, "Inserted combined bookmark was deleted");
michael@0 1589 c.close();
michael@0 1590
michael@0 1591 c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
michael@0 1592 mAsserter.is(c.getCount(), 1, "1 combined entry found");
michael@0 1593
michael@0 1594 mAsserter.is(c.moveToFirst(), true, "Found combined entry without bookmark id");
michael@0 1595 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(0),
michael@0 1596 "Bookmark id should not be set to removed bookmark id");
michael@0 1597 c.close();
michael@0 1598 }
michael@0 1599 }
michael@0 1600
michael@0 1601 private class TestCombinedViewWithDeletedReadingListItem extends TestCase {
michael@0 1602 @Override
michael@0 1603 public void test() throws Exception {
michael@0 1604 final String TITLE = "Test Page 1";
michael@0 1605 final String URL = "http://example.com";
michael@0 1606 final int VISITS = 10;
michael@0 1607 final long LAST_VISITED = System.currentTimeMillis();
michael@0 1608
michael@0 1609 // Create a combined history entry
michael@0 1610 ContentValues combinedHistory = createHistoryEntry(TITLE, URL, VISITS, LAST_VISITED);
michael@0 1611 mProvider.insert(BrowserContract.History.CONTENT_URI, combinedHistory);
michael@0 1612
michael@0 1613 // Create a combined bookmark entry
michael@0 1614 int readingListId = BrowserContract.Bookmarks.FIXED_READING_LIST_ID;
michael@0 1615 ContentValues combinedReadingListItem = createBookmark(TITLE, URL, readingListId,
michael@0 1616 BrowserContract.Bookmarks.TYPE_BOOKMARK, 0, "tags", "description", "keyword");
michael@0 1617 long combinedReadingListItemId = ContentUris.parseId(mProvider.insert(BrowserContract.Bookmarks.CONTENT_URI, combinedReadingListItem));
michael@0 1618
michael@0 1619 Cursor c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
michael@0 1620 mAsserter.is(c.getCount(), 1, "1 combined entry found");
michael@0 1621
michael@0 1622 mAsserter.is(c.moveToFirst(), true, "Found combined entry with bookmark id");
michael@0 1623 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(combinedReadingListItemId),
michael@0 1624 "Bookmark id should be set correctly on combined entry");
michael@0 1625 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DISPLAY))), new Long(BrowserContract.Combined.DISPLAY_READER),
michael@0 1626 "Combined entry should have reader display type");
michael@0 1627
michael@0 1628 int deleted = mProvider.delete(BrowserContract.Bookmarks.CONTENT_URI,
michael@0 1629 BrowserContract.Bookmarks._ID + " = ?",
michael@0 1630 new String[] { String.valueOf(combinedReadingListItemId) });
michael@0 1631
michael@0 1632 mAsserter.is((deleted == 1), true, "Inserted combined reading list item was deleted");
michael@0 1633 c.close();
michael@0 1634
michael@0 1635 c = mProvider.query(BrowserContract.Combined.CONTENT_URI, null, "", null, null);
michael@0 1636 mAsserter.is(c.getCount(), 1, "1 combined entry found");
michael@0 1637
michael@0 1638 mAsserter.is(c.moveToFirst(), true, "Found combined entry without bookmark id");
michael@0 1639 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID))), new Long(0),
michael@0 1640 "Bookmark id should not be set to removed bookmark id");
michael@0 1641 mAsserter.is(new Long(c.getLong(c.getColumnIndex(BrowserContract.Combined.DISPLAY))), new Long(BrowserContract.Combined.DISPLAY_NORMAL),
michael@0 1642 "Combined entry should have reader display type");
michael@0 1643 c.close();
michael@0 1644 }
michael@0 1645 }
michael@0 1646
michael@0 1647 private class TestExpireHistory extends TestCase {
michael@0 1648 private void createFakeHistory(long timeShift, int count) {
michael@0 1649 // Insert a bunch of very new entries
michael@0 1650 ContentValues[] allVals = new ContentValues[count];
michael@0 1651 long time = System.currentTimeMillis() - timeShift;
michael@0 1652 for (int i = 0; i < count; i++) {
michael@0 1653 allVals[i] = new ContentValues();
michael@0 1654 allVals[i].put(BrowserContract.History.TITLE, "Test " + i);
michael@0 1655 allVals[i].put(BrowserContract.History.URL, "http://www.test.org/" + i);
michael@0 1656 allVals[i].put(BrowserContract.History.VISITS, i);
michael@0 1657 allVals[i].put(BrowserContract.History.DATE_LAST_VISITED, time);
michael@0 1658 }
michael@0 1659
michael@0 1660 int inserts = mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, allVals);
michael@0 1661 mAsserter.is(inserts, count, "Expected number of inserts matches");
michael@0 1662
michael@0 1663 // inserting a new entry sets the date created and modified automatically
michael@0 1664 // reset all of them
michael@0 1665 for (int i = 0; i < count; i++) {
michael@0 1666 ContentValues cv = new ContentValues();
michael@0 1667 cv.put(BrowserContract.History.DATE_CREATED, time);
michael@0 1668 cv.put(BrowserContract.History.DATE_MODIFIED, time);
michael@0 1669 mProvider.update(BrowserContract.History.CONTENT_URI, cv, BrowserContract.History.URL + " = ?",
michael@0 1670 new String[] { "http://www.test.org/" + i });
michael@0 1671 }
michael@0 1672
michael@0 1673 Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
michael@0 1674
michael@0 1675 assertCountIsAndClose(c, count, count + " history entries found");
michael@0 1676
michael@0 1677 // add thumbnails for each entry
michael@0 1678 allVals = new ContentValues[count];
michael@0 1679 for (int i = 0; i < count; i++) {
michael@0 1680 allVals[i] = new ContentValues();
michael@0 1681 allVals[i].put(BrowserContract.Thumbnails.DATA, i);
michael@0 1682 allVals[i].put(BrowserContract.Thumbnails.URL, "http://www.test.org/" + i);
michael@0 1683 }
michael@0 1684
michael@0 1685 inserts = mProvider.bulkInsert(BrowserContract.Thumbnails.CONTENT_URI, allVals);
michael@0 1686 mAsserter.is(inserts, count, "Expected number of inserts matches");
michael@0 1687
michael@0 1688 c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
michael@0 1689 assertCountIsAndClose(c, count, count + " thumbnails entries found");
michael@0 1690 }
michael@0 1691
michael@0 1692 @Override
michael@0 1693 public void test() throws Exception {
michael@0 1694 final int count = 3000;
michael@0 1695 final int thumbCount = 15;
michael@0 1696
michael@0 1697 // insert a bunch of new entries
michael@0 1698 createFakeHistory(0, count);
michael@0 1699
michael@0 1700 // expiring with a normal priority should not delete new entries
michael@0 1701 Uri url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "NORMAL");
michael@0 1702 mProvider.delete(url, null, null);
michael@0 1703 Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
michael@0 1704 assertCountIsAndClose(c, count, count + " history entries found");
michael@0 1705
michael@0 1706 // expiring with a normal priority should delete all but 10 thumbnails
michael@0 1707 c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
michael@0 1708 assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
michael@0 1709
michael@0 1710 ensureEmptyDatabase();
michael@0 1711
michael@0 1712 // Insert a bunch of new entries.
michael@0 1713 createFakeHistory(0, count);
michael@0 1714
michael@0 1715 // Expiring with a aggressive priority should leave 500 entries.
michael@0 1716 url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "AGGRESSIVE");
michael@0 1717 mProvider.delete(url, null, null);
michael@0 1718
michael@0 1719 c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
michael@0 1720 assertCountIsAndClose(c, 500, "500 history entries found");
michael@0 1721
michael@0 1722 // Expiring with a aggressive priority should delete all but 10 thumbnails.
michael@0 1723 c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
michael@0 1724 assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
michael@0 1725
michael@0 1726 ensureEmptyDatabase();
michael@0 1727
michael@0 1728 // Insert a bunch of entries with an old time created/modified.
michael@0 1729 long time = 1000L * 60L * 60L * 24L * 30L * 3L;
michael@0 1730 createFakeHistory(time, count);
michael@0 1731
michael@0 1732 // Expiring with an normal priority should remove at most 1000 entries,
michael@0 1733 // entries leaving at least 2000.
michael@0 1734 url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "NORMAL");
michael@0 1735 mProvider.delete(url, null, null);
michael@0 1736
michael@0 1737 c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
michael@0 1738 assertCountIsAndClose(c, 2000, "2000 history entries found");
michael@0 1739
michael@0 1740 // Expiring with a normal priority should delete all but 10 thumbnails.
michael@0 1741 c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
michael@0 1742 assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
michael@0 1743
michael@0 1744 ensureEmptyDatabase();
michael@0 1745 // insert a bunch of entries with an old time created/modified
michael@0 1746 time = 1000L * 60L * 60L * 24L * 30L * 3L;
michael@0 1747 createFakeHistory(time, count);
michael@0 1748
michael@0 1749 // Expiring with an aggressive priority should remove old
michael@0 1750 // entries, leaving at least 500.
michael@0 1751 url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "AGGRESSIVE");
michael@0 1752 mProvider.delete(url, null, null);
michael@0 1753 c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
michael@0 1754 assertCountIsAndClose(c, 500, "500 history entries found");
michael@0 1755
michael@0 1756 // expiring with an aggressive priority should delete all but 10 thumbnails
michael@0 1757 c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
michael@0 1758 assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
michael@0 1759 }
michael@0 1760 }
michael@0 1761
michael@0 1762 /*
michael@0 1763 * Verify that insert, update, delete, and bulkInsert operations
michael@0 1764 * notify the ambient content resolver. Each operation calls the
michael@0 1765 * content resolver notifyChange method synchronously, so it is
michael@0 1766 * okay to test sequentially.
michael@0 1767 */
michael@0 1768 private class TestBrowserProviderNotifications extends TestCase {
michael@0 1769 public static final String LOGTAG = "TestBPNotifications";
michael@0 1770
michael@0 1771 protected void ensureOnlyChangeNotifiedStartsWith(Uri expectedUri, String operation) {
michael@0 1772 if (expectedUri == null) {
michael@0 1773 throw new IllegalArgumentException("expectedUri must not be null");
michael@0 1774 }
michael@0 1775
michael@0 1776 if (mResolver.notifyChangeList.size() != 1) {
michael@0 1777 // Log to help post-mortem debugging
michael@0 1778 Log.w(LOGTAG, "after operation, notifyChangeList = " + mResolver.notifyChangeList);
michael@0 1779 }
michael@0 1780
michael@0 1781 mAsserter.is(Long.valueOf(mResolver.notifyChangeList.size()),
michael@0 1782 Long.valueOf(1),
michael@0 1783 "Content observer was notified exactly once by " + operation);
michael@0 1784
michael@0 1785 Uri uri = mResolver.notifyChangeList.poll();
michael@0 1786
michael@0 1787 mAsserter.isnot(uri,
michael@0 1788 null,
michael@0 1789 "Notification from " + operation + " was valid");
michael@0 1790
michael@0 1791 mAsserter.ok(uri.toString().startsWith(expectedUri.toString()),
michael@0 1792 "Content observer was notified exactly once by " + operation,
michael@0 1793 uri.toString() + " starts with expected prefix " + expectedUri);
michael@0 1794 }
michael@0 1795
michael@0 1796 @Override
michael@0 1797 public void test() throws Exception {
michael@0 1798 // Insert
michael@0 1799 final ContentValues h = createOneHistoryEntry();
michael@0 1800
michael@0 1801 mResolver.notifyChangeList.clear();
michael@0 1802 long id = ContentUris.parseId(mProvider.insert(BrowserContract.History.CONTENT_URI, h));
michael@0 1803
michael@0 1804 mAsserter.isnot(Long.valueOf(id),
michael@0 1805 Long.valueOf(-1),
michael@0 1806 "Inserted item has valid id");
michael@0 1807
michael@0 1808 ensureOnlyChangeNotifiedStartsWith(BrowserContract.History.CONTENT_URI, "insert");
michael@0 1809
michael@0 1810 // Update
michael@0 1811 mResolver.notifyChangeList.clear();
michael@0 1812 h.put(BrowserContract.History.TITLE, "http://newexample.com");
michael@0 1813
michael@0 1814 long numUpdated = mProvider.update(BrowserContract.History.CONTENT_URI, h,
michael@0 1815 BrowserContract.History._ID + " = ?",
michael@0 1816 new String[] { String.valueOf(id) });
michael@0 1817
michael@0 1818 mAsserter.is(Long.valueOf(numUpdated),
michael@0 1819 Long.valueOf(1),
michael@0 1820 "Correct number of items are updated");
michael@0 1821
michael@0 1822 ensureOnlyChangeNotifiedStartsWith(BrowserContract.History.CONTENT_URI, "update");
michael@0 1823
michael@0 1824 // Delete
michael@0 1825 mResolver.notifyChangeList.clear();
michael@0 1826 long numDeleted = mProvider.delete(BrowserContract.History.CONTENT_URI, null, null);
michael@0 1827
michael@0 1828 mAsserter.is(Long.valueOf(numDeleted),
michael@0 1829 Long.valueOf(1),
michael@0 1830 "Correct number of items are deleted");
michael@0 1831
michael@0 1832 ensureOnlyChangeNotifiedStartsWith(BrowserContract.History.CONTENT_URI, "delete");
michael@0 1833
michael@0 1834 // Bulk insert
michael@0 1835 final ContentValues[] hs = new ContentValues[] { createOneHistoryEntry() };
michael@0 1836
michael@0 1837 mResolver.notifyChangeList.clear();
michael@0 1838 long numBulkInserted = mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, hs);
michael@0 1839
michael@0 1840 mAsserter.is(Long.valueOf(numBulkInserted),
michael@0 1841 Long.valueOf(1),
michael@0 1842 "Correct number of items are bulkInserted");
michael@0 1843
michael@0 1844 ensureOnlyChangeNotifiedStartsWith(BrowserContract.History.CONTENT_URI, "bulkInsert");
michael@0 1845 }
michael@0 1846 }
michael@0 1847
michael@0 1848 /**
michael@0 1849 * Assert that the provided cursor has the expected number of rows,
michael@0 1850 * closing the cursor afterwards.
michael@0 1851 */
michael@0 1852 private void assertCountIsAndClose(Cursor c, int expectedCount, String message) {
michael@0 1853 try {
michael@0 1854 mAsserter.is(c.getCount(), expectedCount, message);
michael@0 1855 } finally {
michael@0 1856 c.close();
michael@0 1857 }
michael@0 1858 }
michael@0 1859 }

mercurial