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

Unified Diff: components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java

Issue 1387633002: Update instrumentation tests to use PreTestHooks for policies (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase, replace bundle manipulation with a simple variable Created 5 years, 2 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
Index: components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java
diff --git a/components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java b/components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java
new file mode 100644
index 0000000000000000000000000000000000000000..b9129653130e78f1f76e2b5181a8bca80d842568
--- /dev/null
+++ b/components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java
@@ -0,0 +1,116 @@
+// 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.policy.test;
+
+import android.os.Bundle;
+import android.text.TextUtils;
+
+import org.chromium.base.Log;
+
+/**
+ * Helper class to transform Java types to {@link Bundle}s usable by the Policy system.
+ *
+ * Use the subclasses to define the data and then transform it using {@link #asBundle(Iterable)}
+ */
+public abstract class PolicyData {
+ private static final String TAG = "policy_test";
+ public final String mKey;
+
+ public PolicyData(String key) {
+ mKey = key;
+ }
+
+ public abstract void putInBundle(Bundle bundle);
+
+ public static Bundle asBundle(Iterable<PolicyData> policies) {
+ Bundle bundle = new Bundle();
+ for (PolicyData data : policies) {
+ Log.d(TAG, "Adding to policy bundle: %s", data);
+ data.putInBundle(bundle);
+ }
+ return bundle;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
bartfab (slow) 2015/10/20 00:44:39 1) Nit: Why do you need to feed this constant into
dgn 2015/10/20 10:09:46 I just used the code generated by eclipse. But it'
+ result = prime * result + ((mKey == null) ? 0 : mKey.hashCode());
+ return result;
+ }
+
+ /** Equality for this class hierarchy is based only on the key. */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof PolicyData)) return false;
+ PolicyData other = (PolicyData) obj;
+ return TextUtils.equals(mKey, other.mKey);
+ }
+
+ /** {@link PolicyData} for the {@link String} type. */
+ public static class Str extends PolicyData {
+ public final String mValue;
+
+ public Str(String key, String value) {
+ super(key);
+ mValue = value;
+ }
+
+ @Override
+ public void putInBundle(Bundle bundle) {
+ bundle.putString(mKey, mValue);
+ }
+
+ @Override
+ public String toString() {
+ return String.format("PolicyData.Str{%s=%s}", mKey, mValue);
+ }
+ }
+
+ /** {@link PolicyData} with no value, for error states. Doesn't put anything in a bundle.*/
+ public static class Undefined extends PolicyData {
+ public Undefined(String key) {
+ super(key);
+ }
+
+ @Override
+ public void putInBundle(Bundle bundle) {}
+
+ @Override
+ public String toString() {
+ return String.format("PolicyData.Undefined{%s}", mKey);
+ }
+ }
+
+ /**
+ * {@link PolicyData} for the {@link String } array type.
+ * Outputs a string encoded as a JSON array.
+ */
+ public static class StrArray extends PolicyData {
+ public final String[] mValue;
+
+ public StrArray(String key, String[] value) {
+ super(key);
+ mValue = value.clone();
+ }
+
+ private String valueToString() {
+ String joinedStrings = TextUtils.join("\",\"", mValue);
bartfab (slow) 2015/10/20 00:44:39 Nit: Does this escape commas, square brackets, etc
dgn 2015/10/20 10:09:46 No. But that's to be used for tests, I think it's
+ return "[\"" + joinedStrings + "\"]";
+ }
+
+ @Override
+ public void putInBundle(Bundle bundle) {
+ bundle.putString(mKey, valueToString());
+ }
+
+ @Override
+ public String toString() {
+ return String.format("PolicyData.StrArray{%s=%s}", mKey, valueToString());
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698