Commit 40a831ce authored by maxbogue's avatar maxbogue Committed by Commit bot

[Sync] Improve Android typed URL tests.

- Split the tests into their own file.
- Add the deleted on server case.
- Factor out many common subroutines.

BUG=480604

Review URL: https://codereview.chromium.org/1168533002

Cr-Commit-Position: refs/heads/master@{#333079}
parent effbb9ae
...@@ -8,11 +8,9 @@ import android.accounts.Account; ...@@ -8,11 +8,9 @@ import android.accounts.Account;
import android.app.Activity; import android.app.Activity;
import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import org.chromium.base.ActivityState; import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationStatus; import org.chromium.base.ApplicationStatus;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.chrome.shell.ChromeShellActivity; import org.chromium.chrome.shell.ChromeShellActivity;
import org.chromium.chrome.test.util.browser.sync.SyncTestUtil; import org.chromium.chrome.test.util.browser.sync.SyncTestUtil;
...@@ -20,18 +18,10 @@ import org.chromium.content.browser.ContentViewCore; ...@@ -20,18 +18,10 @@ import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.browser.test.util.JavaScriptUtils; import org.chromium.content.browser.test.util.JavaScriptUtils;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.sync.AndroidSyncSettings; import org.chromium.sync.AndroidSyncSettings;
import org.chromium.sync.internal_api.pub.base.ModelType;
import org.chromium.sync.protocol.EntitySpecifics;
import org.chromium.sync.protocol.SyncEnums;
import org.chromium.sync.protocol.TypedUrlSpecifics;
import org.chromium.sync.signin.AccountManagerHelper; import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController; import org.chromium.sync.signin.ChromeSigninController;
import org.chromium.ui.base.PageTransition;
import org.json.JSONObject;
import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
/** /**
...@@ -180,65 +170,6 @@ public class SyncTest extends SyncTestBase { ...@@ -180,65 +170,6 @@ public class SyncTest extends SyncTestBase {
SyncTestUtil.verifySignedInWithAccount(mContext, account); SyncTestUtil.verifySignedInWithAccount(mContext, account);
} }
@LargeTest
@Feature({"Sync"})
public void testUploadTypedUrl() throws Exception {
setupTestAccountAndSignInToSync(CLIENT_ID);
// TestHttpServerClient is preferred here but it can't be used. The test server
// serves pages on localhost and Chrome doesn't sync localhost URLs as typed URLs.
// This type of URL requires no external data connection or resources.
final String urlToLoad = "data:text,testTypedUrl";
assertTrue("A typed URL entity for " + urlToLoad + " already exists on the fake server.",
mFakeServerHelper.verifyEntityCountByTypeAndName(0, ModelType.TYPED_URL,
urlToLoad));
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
LoadUrlParams params = new LoadUrlParams(urlToLoad, PageTransition.TYPED);
getActivity().getActiveTab().loadUrl(params);
}
});
boolean synced = CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return mFakeServerHelper.verifyEntityCountByTypeAndName(1, ModelType.TYPED_URL,
urlToLoad);
}
}, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS);
assertTrue("The typed URL entity for " + urlToLoad + " was not found on the fake server.",
synced);
}
@LargeTest
@Feature({"Sync"})
public void testDownloadTypedUrl() throws Exception {
setupTestAccountAndSignInToSync(CLIENT_ID);
assertEquals("No typed URLs should exist on the client by default.",
0, SyncTestUtil.getLocalData(mContext, "Typed URLs").size());
String url = "data:text,testDownloadTypedUrl";
EntitySpecifics specifics = new EntitySpecifics();
specifics.typedUrl = new TypedUrlSpecifics();
specifics.typedUrl.url = url;
specifics.typedUrl.title = url;
specifics.typedUrl.visits = new long[]{1L};
specifics.typedUrl.visitTransitions = new int[]{SyncEnums.TYPED};
mFakeServerHelper.injectUniqueClientEntity(url /* name */, specifics);
SyncTestUtil.triggerSyncAndWaitForCompletion(mContext);
List<Pair<String, JSONObject>> typedUrls = SyncTestUtil.getLocalData(
mContext, "Typed URLs");
assertEquals("Only the injected typed URL should exist on the client.",
1, typedUrls.size());
JSONObject typedUrl = typedUrls.get(0).second;
assertEquals("The wrong URL was found for the typed URL.", url, typedUrl.getString("url"));
}
private static ContentViewCore getContentViewCore(ChromeShellActivity activity) { private static ContentViewCore getContentViewCore(ChromeShellActivity activity) {
return activity.getActiveContentViewCore(); return activity.getActiveContentViewCore();
} }
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
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.test.util.browser.sync.SyncTestUtil;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.sync.internal_api.pub.base.ModelType;
import org.chromium.sync.protocol.EntitySpecifics;
import org.chromium.sync.protocol.SyncEnums;
import org.chromium.sync.protocol.TypedUrlSpecifics;
import org.chromium.ui.base.PageTransition;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Test suite for the typed URLs sync data type.
*/
public class TypedUrlsTest extends SyncTestBase {
private static final String TAG = "TypedUrlsTest";
private static final String TYPED_URLS_TYPE = "Typed URLs";
// TestHttpServerClient is preferred here but it can't be used. The test server
// serves pages on localhost and Chrome doesn't sync localhost URLs as typed URLs.
// This type of URL requires no external data connection or resources.
private static final String URL = "data:text,testTypedUrl";
// A container to store typed URL information for data verification.
private static class TypedUrl {
public final String id;
public final String url;
public TypedUrl(String id, String url) {
this.id = id;
this.url = url;
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
setupTestAccountAndSignInToSync(CLIENT_ID);
// Make sure the initial state is clean.
assertClientTypedUrlCount(0);
assertServerTypedUrlCountWithName(0, URL);
}
// Test syncing a typed URL from client to server.
@LargeTest
@Feature({"Sync"})
public void testUploadTypedUrl() throws Exception {
loadUrlByTyping(URL);
waitForClientTypedUrlCount(1);
waitForServerTypedUrlCountWithName(1, URL);
}
// Test syncing a typed URL from server to client.
@LargeTest
@Feature({"Sync"})
public void testDownloadTypedUrl() throws Exception {
addServerTypedUrl(URL);
SyncTestUtil.triggerSyncAndWaitForCompletion(mContext);
// Verify data synced to client.
List<TypedUrl> typedUrls = getClientTypedUrls();
assertEquals("Only the injected typed URL should exist on the client.",
1, typedUrls.size());
TypedUrl typedUrl = typedUrls.get(0);
assertEquals("The wrong URL was found for the typed URL.", URL, typedUrl.url);
}
// Test syncing a typed URL deletion from server to client.
@LargeTest
@Feature({"Sync"})
public void testDownloadDeletedTypedUrl() throws Exception {
// Add the entity to test deleting.
addServerTypedUrl(URL);
SyncTestUtil.triggerSyncAndWaitForCompletion(mContext);
assertServerTypedUrlCountWithName(1, URL);
assertClientTypedUrlCount(1);
// Delete on server, sync, and verify deleted locally.
TypedUrl typedUrl = getClientTypedUrls().get(0);
mFakeServerHelper.deleteEntity(typedUrl.id);
waitForServerTypedUrlCountWithName(0, URL);
SyncTestUtil.triggerSyncAndWaitForCompletion(mContext);
waitForClientTypedUrlCount(0);
}
private void loadUrlByTyping(final String url) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
LoadUrlParams params = new LoadUrlParams(url, PageTransition.TYPED);
getActivity().getActiveTab().loadUrl(params);
}
});
}
private void addServerTypedUrl(String url) throws InterruptedException {
EntitySpecifics specifics = new EntitySpecifics();
specifics.typedUrl = new TypedUrlSpecifics();
specifics.typedUrl.url = url;
specifics.typedUrl.title = url;
specifics.typedUrl.visits = new long[]{1L};
specifics.typedUrl.visitTransitions = new int[]{SyncEnums.TYPED};
mFakeServerHelper.injectUniqueClientEntity(url /* name */, specifics);
}
private List<TypedUrl> getClientTypedUrls() throws JSONException {
List<Pair<String, JSONObject>> rawTypedUrls = SyncTestUtil.getLocalData(
mContext, TYPED_URLS_TYPE);
List<TypedUrl> typedUrls = new ArrayList<TypedUrl>(rawTypedUrls.size());
for (Pair<String, JSONObject> rawTypedUrl : rawTypedUrls) {
String id = rawTypedUrl.first;
typedUrls.add(new TypedUrl(id, rawTypedUrl.second.getString("url")));
}
return typedUrls;
}
private void assertClientTypedUrlCount(int count) throws JSONException {
assertEquals("There should be " + count + " local typed URL entities.",
count, SyncTestUtil.getLocalData(mContext, TYPED_URLS_TYPE).size());
}
private void assertServerTypedUrlCountWithName(int count, String name) {
assertTrue("Expected " + count + " server typed URLs with name " + name + ".",
mFakeServerHelper.verifyEntityCountByTypeAndName(
count, ModelType.TYPED_URL, name));
}
private void waitForClientTypedUrlCount(final int count) throws InterruptedException {
boolean success = CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
try {
return SyncTestUtil.getLocalData(mContext, TYPED_URLS_TYPE).size() == count;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS);
assertTrue("Expected " + count + " local typed URL entities.", success);
}
private void waitForServerTypedUrlCountWithName(final int count, final String name)
throws InterruptedException {
boolean success = CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
try {
return mFakeServerHelper.verifyEntityCountByTypeAndName(
count, ModelType.TYPED_URL, name);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, SyncTestUtil.UI_TIMEOUT_MS, SyncTestUtil.CHECK_INTERVAL_MS);
assertTrue("Expected " + count + " server typed URLs with name " + name + ".", success);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment