Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10095)

Unified Diff: chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/sync/SyncTestUtil.java

Issue 1127233008: Sync: local data verification for Android tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix threading Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/sync/profile_sync_service_android.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/sync/SyncTestUtil.java
diff --git a/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/sync/SyncTestUtil.java b/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/sync/SyncTestUtil.java
index 879f2bf9b4afcee506da8dce0c36447d022b13b6..4ca412642334ff96881820d13b3a6e7879bd3af8 100644
--- a/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/sync/SyncTestUtil.java
+++ b/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/sync/SyncTestUtil.java
@@ -30,7 +30,10 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
@@ -78,28 +81,6 @@ public final class SyncTestUtil {
}
/**
- * Creates a {@link Map} containing the counts of each entity by model type.
- */
- private static Map<String, Integer> createModelTypeCount(String rawJson) throws JSONException {
- Map<String, Integer> modelTypeCount = new HashMap<String, Integer>();
- JSONObject aboutInfo = new JSONObject(rawJson);
-
- JSONArray typeStatusArray = aboutInfo.getJSONArray("type_status");
- for (int i = 0; i < typeStatusArray.length(); i++) {
- JSONObject typeInfo = typeStatusArray.getJSONObject(i);
- String name = typeInfo.getString("name");
- try {
- int total = typeInfo.getInt("num_entries");
- modelTypeCount.put(name, total);
- } catch (JSONException e) {
- // This is the header entry which does not have a valid count. Don't include it in
- // the map.
- }
- }
- return modelTypeCount;
- }
-
- /**
* Parses raw JSON into a map with keys Pair<String, String>. The first string in each Pair
* corresponds to the title under which a given stat_name/stat_value is situated, and the second
* contains the name of the actual stat. For example, a stat named "Syncing" which falls under
@@ -369,6 +350,102 @@ public final class SyncTestUtil {
}
/**
+ * Retrieves the local Sync data as a JSONArray via ProfileSyncService.
+ *
+ * This method blocks until the data is available or until it times out.
+ */
+ private static JSONArray getAllNodesAsJsonArray(final Context context) throws JSONException {
+ final Semaphore semaphore = new Semaphore(0);
+ final ProfileSyncService.GetAllNodesCallback callback =
+ new ProfileSyncService.GetAllNodesCallback() {
+ @Override
+ public void onResult(String nodesString) {
+ super.onResult(nodesString);
+ semaphore.release();
+ }
+ };
+
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() {
+ @Override
+ public void run() {
+ ProfileSyncService.get(context).getAllNodes(callback);
+ }
+ });
+
+ try {
+ Assert.assertTrue("Semaphore should have been released.",
+ semaphore.tryAcquire(UI_TIMEOUT_MS, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+
+ return callback.getNodesAsJsonArray();
+ }
+
+
+ /**
+ * Extracts datatype-specific information from the given JSONObject. The returned JSONObject
+ * contains the same data as a specifics protocol buffer (e.g., TypedUrlSpecifics).
+ */
+ private static JSONObject extractSpecifics(JSONObject node) throws JSONException {
+ JSONObject specifics = node.getJSONObject("SPECIFICS");
+ // The key name here is type-specific (e.g., "typed_url" for Typed URLs), so we
+ // can't hard code a value.
+ Iterator<String> keysIterator = specifics.keys();
+ String key = null;
+ if (!keysIterator.hasNext()) {
+ throw new JSONException("Specifics object has 0 keys.");
+ }
+ key = keysIterator.next();
+
+ if (keysIterator.hasNext()) {
+ throw new JSONException("Specifics object has more than 1 key.");
+ }
+ return specifics.getJSONObject(key);
+ }
+
+ /**
+ * Returns the local Sync data present for a single datatype.
+ *
+ * For each data entity, a Pair is returned. The first piece of data is the entity's server ID.
+ * This is useful for activities like deleting an entity on the server. The second piece of data
+ * is a JSONObject representing the datatype-specific information for the entity. This data is
+ * the same as the data stored in a specifics protocol buffer (e.g., TypedUrlSpecifics).
+ *
+ * @param context the Context used to retreive the correct ProfileSyncService
+ * @param typeString a String representing a specific datatype.
+ *
+ * TODO(pvalenzuela): Replace typeString with the native ModelType enum or something else
+ * that will avoid callers needing to specify the native string version.
+ *
+ * @return a List of Pair<String, JSONObject> representing the local Sync data
+ */
+ public static List<Pair<String, JSONObject>> getLocalData(
+ Context context, String typeString) throws JSONException {
+ JSONArray localData = getAllNodesAsJsonArray(context);
+ JSONArray datatypeNodes = new JSONArray();
+ for (int i = 0; i < localData.length(); i++) {
+ JSONObject datatypeObject = localData.getJSONObject(i);
+ if (datatypeObject.getString("type").equals(typeString)) {
+ datatypeNodes = datatypeObject.getJSONArray("nodes");
+ break;
+ }
+ }
+
+ List<Pair<String, JSONObject>> localDataForDatatype =
+ new ArrayList<Pair<String, JSONObject>>(datatypeNodes.length());
+ for (int i = 0; i < datatypeNodes.length(); i++) {
+ JSONObject entity = datatypeNodes.getJSONObject(i);
+ if (!entity.getString("UNIQUE_SERVER_TAG").isEmpty()) {
+ // Ignore permanent items (e.g., root datatype folders).
+ continue;
+ }
+ localDataForDatatype.add(Pair.create(entity.getString("ID"), extractSpecifics(entity)));
+ }
+ return localDataForDatatype;
+ }
+
+ /**
* Retrieves the sync internals information which is the basis for chrome://sync-internals and
* makes the result available in {@link AboutSyncInfoGetter#getAboutInfo()}.
*
@@ -378,12 +455,10 @@ public final class SyncTestUtil {
private static final String TAG = "AboutSyncInfoGetter";
final Context mContext;
Map<Pair<String, String>, String> mAboutInfo;
- Map<String, Integer> mModelTypeCount;
public AboutSyncInfoGetter(Context context) {
mContext = context.getApplicationContext();
mAboutInfo = new HashMap<Pair<String, String>, String>();
- mModelTypeCount = new HashMap<String, Integer>();
}
@Override
@@ -391,7 +466,6 @@ public final class SyncTestUtil {
String info = ProfileSyncService.get(mContext).getSyncInternalsInfoForTest();
try {
mAboutInfo = getAboutInfoStats(info);
- mModelTypeCount = createModelTypeCount(info);
} catch (JSONException e) {
Log.w(TAG, "Unable to parse JSON message: " + info, e);
}
@@ -400,10 +474,6 @@ public final class SyncTestUtil {
public Map<Pair<String, String>, String> getAboutInfo() {
return mAboutInfo;
}
-
- public Map<String, Integer> getModelTypeCount() {
- return mModelTypeCount;
- }
}
/**
« no previous file with comments | « chrome/browser/sync/profile_sync_service_android.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698