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..e937467295b1f333d585f5bd4050d986c0313cb1 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,97 @@ 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(Context context) { |
+ ProfileSyncService syncService = ProfileSyncService.get(context); |
+ final ProfileSyncService.GetAllNodesCallback callback = |
+ new ProfileSyncService.GetAllNodesCallback(); |
+ syncService.getAllNodes(callback); |
+ |
+ Criteria getAllNodesCriteria = new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ return callback.hasResult(); |
+ } |
+ }; |
+ |
+ boolean finished = false; |
+ try { |
+ finished = CriteriaHelper.pollForCriteria(getAllNodesCriteria, UI_TIMEOUT_MS, |
+ CHECK_INTERVAL_MS); |
+ } 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"); |
+ } |
+ } |
+ |
+ List<Pair<String, JSONObject>> localDataForDatatype = |
+ new ArrayList<Pair<String, JSONObject>>(datatypeNodes.length()); |
+ for (int i = 0; i < datatypeNodes.length(); i++) { |
+ JSONObject typedUrl = datatypeNodes.getJSONObject(i); |
+ if (!typedUrl.getString("UNIQUE_SERVER_TAG").isEmpty()) { |
+ // Ignore permanent items (e.g., root datatype folders). |
+ continue; |
+ } |
+ localDataForDatatype.add(Pair.create(typedUrl.getString("ID"), |
+ extractSpecifics(typedUrl))); |
+ } |
+ 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 +450,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 +461,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 +469,6 @@ public final class SyncTestUtil { |
public Map<Pair<String, String>, String> getAboutInfo() { |
return mAboutInfo; |
} |
- |
- public Map<String, Integer> getModelTypeCount() { |
- return mModelTypeCount; |
- } |
} |
/** |