From 318bbb567beb8c01ebd3c94b3141471174173d21 Mon Sep 17 00:00:00 2001 From: maxbogue Date: Thu, 4 Jun 2015 15:54:17 -0700 Subject: [PATCH] [Sync] Test local bookmark sync on Android, take 2 This is a reland of http://crrev.com/1151833003, which was missing a DEPS fix. Adds testUploadModifyAndDeleteBookmark. Also merges testDownloadBookmark and testDownloadDeletedBookmark into testDownloadAndDeleteBookmark. This change also pulls the JSON parts out into getClientBookmarks in order to simplify the test case code. BUG=480604 Review URL: https://codereview.chromium.org/1158893003 Cr-Commit-Position: refs/heads/master@{#332952} --- chrome/android/sync_shell/javatests/DEPS | 1 + .../chrome/browser/sync/BookmarksTest.java | 185 +++++++++++++++++---- 2 files changed, 150 insertions(+), 36 deletions(-) diff --git a/chrome/android/sync_shell/javatests/DEPS b/chrome/android/sync_shell/javatests/DEPS index 036906fe988f..915be8789bce 100644 --- a/chrome/android/sync_shell/javatests/DEPS +++ b/chrome/android/sync_shell/javatests/DEPS @@ -1,4 +1,5 @@ include_rules = [ + "+components/bookmarks", "+content/public/android/java", "+sync/android/java/src/org/chromium/sync", "+sync/test/android/javatests/src/org/chromium/sync/test/util", diff --git a/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/BookmarksTest.java b/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/BookmarksTest.java index c3cea45d6170..092574e258ad 100644 --- a/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/BookmarksTest.java +++ b/chrome/android/sync_shell/javatests/src/org/chromium/chrome/browser/sync/BookmarksTest.java @@ -7,13 +7,21 @@ package org.chromium.chrome.browser.sync; import android.test.suitebuilder.annotation.LargeTest; import android.util.Pair; +import org.chromium.base.ThreadUtils; import org.chromium.base.test.util.Feature; +import org.chromium.chrome.browser.BookmarksBridge; +import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.test.util.browser.sync.SyncTestUtil; +import org.chromium.components.bookmarks.BookmarkId; import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.CriteriaHelper; +import org.chromium.sync.internal_api.pub.base.ModelType; +import org.json.JSONException; import org.json.JSONObject; +import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; /** * Test suite for the bookmarks sync data type. @@ -23,63 +31,168 @@ public class BookmarksTest extends SyncTestBase { private static final String BOOKMARKS_TYPE_STRING = "Bookmarks"; - /** - * This method performs all steps to test that the client successfully downloads a bookmark from - * the server. If successful, a single bookmark will exist on the fake Sync server and on the - * client. This state can then be used to test deletion or modification of the bookmark. - * - * @return the downloaded bookmark's server ID - */ - private String runDownloadBookmarkTestScenario() throws Exception { - setupTestAccountAndSignInToSync(CLIENT_ID); - assertEquals("No bookmarks should exist on the client by default.", - 0, SyncTestUtil.getLocalData(mContext, BOOKMARKS_TYPE_STRING).size()); + private static final String URL = "http://chromium.org/"; + private static final String TITLE = "Chromium"; + private static final String MODIFIED_TITLE = "Chromium2"; - String title = "Title"; - String url = "http://chromium.org/"; - mFakeServerHelper.injectBookmarkEntity( - title, url, mFakeServerHelper.getBookmarkBarFolderId()); + private BookmarksBridge mBookmarksBridge; - SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); + private static class Bookmark { + public final String id; + public final String title; + public final String url; - List> bookmarks = SyncTestUtil.getLocalData( - mContext, BOOKMARKS_TYPE_STRING); - assertEquals("Only the injected bookmark should exist on the client.", - 1, bookmarks.size()); - Pair bookmark = bookmarks.get(0); - JSONObject bookmarkJson = bookmark.second; - assertEquals("The wrong title was found for the bookmark.", title, - bookmarkJson.getString("title")); - assertEquals("The wrong URL was found for the bookmark.", - url, bookmarkJson.getString("url")); - - return bookmark.first; + private Bookmark(String id, String title, String url) { + this.id = id; + this.title = title; + this.url = url; + } } + @Override + public void setUp() throws Exception { + super.setUp(); + ThreadUtils.runOnUiThreadBlocking(new Runnable() { + @Override + public void run() { + mBookmarksBridge = new BookmarksBridge(Profile.getLastUsedProfile()); + mBookmarksBridge.loadEmptyPartnerBookmarkShimForTesting(); + } + }); + } + + // Test syncing bookmark data from server to client: a new bookmark and a tombstone. @LargeTest @Feature({"Sync"}) - public void testDownloadBookmark() throws Exception { - runDownloadBookmarkTestScenario(); + public void testDownloadAndDeleteBookmark() throws Exception { + setupTestAccountAndSignInToSync(CLIENT_ID); + assertClientBookmarkCount(0); + + addServerBookmarkAndSync(TITLE, URL); + List bookmarks = getClientBookmarks(); + assertEquals("Only the injected bookmark should exist on the client.", + 1, bookmarks.size()); + Bookmark bookmark = bookmarks.get(0); + assertEquals("The wrong title was found for the bookmark.", TITLE, bookmark.title); + assertEquals("The wrong URL was found for the bookmark.", URL, bookmark.url); + + mFakeServerHelper.deleteEntity(bookmark.id); + waitForServerBookmarkCountWithName(0, TITLE); + SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); + waitForClientBookmarkCount(0); } + // Test syncing bookmark data from client to server: a new bookmark, a modification, + // and a tombstone. @LargeTest @Feature({"Sync"}) - public void testDownloadDeletedBookmark() throws Exception { - String id = runDownloadBookmarkTestScenario(); - mFakeServerHelper.deleteEntity(id); + public void testUploadModifyAndDeleteBookmark() throws Exception { + setupTestAccountAndSignInToSync(CLIENT_ID); + assertClientBookmarkCount(0); + assertServerBookmarkCountWithName(0, TITLE); + assertServerBookmarkCountWithName(0, MODIFIED_TITLE); + + BookmarkId bookmarkId = addClientBookmark(TITLE, URL); + waitForClientBookmarkCount(1); + waitForServerBookmarkCountWithName(1, TITLE); + + setClientBookmarkTitle(bookmarkId, MODIFIED_TITLE); + waitForServerBookmarkCountWithName(1, MODIFIED_TITLE); + assertServerBookmarkCountWithName(0, TITLE); + + deleteClientBookmark(bookmarkId); + waitForClientBookmarkCount(0); + waitForServerBookmarkCountWithName(0, TITLE); + assertServerBookmarkCountWithName(0, MODIFIED_TITLE); + } + + private BookmarkId addClientBookmark(final String title, final String url) { + final AtomicReference id = new AtomicReference(); + ThreadUtils.runOnUiThreadBlocking(new Runnable() { + @Override + public void run() { + BookmarkId parentId = mBookmarksBridge.getMobileFolderId(); + id.set(mBookmarksBridge.addBookmark(parentId, 0, title, url)); + } + }); + assertNotNull("Failed to create bookmark.", id.get()); + return id.get(); + } + + private void addServerBookmarkAndSync(String title, String url) throws InterruptedException { + mFakeServerHelper.injectBookmarkEntity( + title, url, mFakeServerHelper.getBookmarkBarFolderId()); SyncTestUtil.triggerSyncAndWaitForCompletion(mContext); + } + + private void deleteClientBookmark(final BookmarkId id) { + ThreadUtils.runOnUiThreadBlocking(new Runnable() { + @Override + public void run() { + mBookmarksBridge.deleteBookmark(id); + } + }); + } - boolean bookmarkDeleted = CriteriaHelper.pollForCriteria(new Criteria() { + private void setClientBookmarkTitle(final BookmarkId id, final String title) { + ThreadUtils.runOnUiThreadBlocking(new Runnable() { + @Override + public void run() { + mBookmarksBridge.setBookmarkTitle(id, title); + } + }); + } + + private List getClientBookmarks() throws JSONException { + List> rawBookmarks = SyncTestUtil.getLocalData( + mContext, BOOKMARKS_TYPE_STRING); + List bookmarks = new ArrayList(rawBookmarks.size()); + for (Pair rawBookmark : rawBookmarks) { + String id = rawBookmark.first; + JSONObject json = rawBookmark.second; + bookmarks.add(new Bookmark(id, json.getString("title"), json.getString("url"))); + } + return bookmarks; + } + + private void assertClientBookmarkCount(int count) throws JSONException { + assertEquals("There should be " + count + " local bookmarks.", + count, SyncTestUtil.getLocalData(mContext, BOOKMARKS_TYPE_STRING).size()); + } + + private void assertServerBookmarkCountWithName(int count, String name) { + assertTrue("There should be " + count + " remote bookmarks with name " + name + ".", + mFakeServerHelper.verifyEntityCountByTypeAndName( + count, ModelType.BOOKMARK, name)); + } + + private void waitForClientBookmarkCount(final int n) throws InterruptedException { + boolean success = CriteriaHelper.pollForCriteria(new Criteria() { @Override public boolean isSatisfied() { try { - return SyncTestUtil.getLocalData(mContext, BOOKMARKS_TYPE_STRING).isEmpty(); + return SyncTestUtil.getLocalData(mContext, BOOKMARKS_TYPE_STRING).size() == n; } catch (Exception e) { throw new RuntimeException(e); } } }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); + assertTrue("There should be " + n + " local bookmarks.", success); + } - assertTrue("The lone bookmark should have been deleted.", bookmarkDeleted); + private void waitForServerBookmarkCountWithName(final int count, final String name) + throws InterruptedException { + boolean success = CriteriaHelper.pollForCriteria(new Criteria() { + @Override + public boolean isSatisfied() { + try { + return mFakeServerHelper.verifyEntityCountByTypeAndName( + count, ModelType.BOOKMARK, name); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS); + assertTrue("Expected " + count + " remote bookmarks with name " + name + ".", success); } } -- 2.11.4.GIT