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..9337cef961b3717098b869f92a6d509409c9ef18 |
--- /dev/null |
+++ b/components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java |
@@ -0,0 +1,113 @@ |
+// 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; |
Bernhard Bauer
2015/11/06 10:25:34
Add an accessor for this?
dgn
2015/11/06 16:07:08
Done.
|
+ |
+ 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() { |
+ return (mKey == null) ? 0 : mKey.hashCode(); |
+ } |
+ |
+ /** 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. |
Bernhard Bauer
2015/11/06 10:25:34
Nit: no space before the closing brace.
dgn
2015/11/06 16:07:08
Done.
|
+ * 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); |
Bernhard Bauer
2015/11/06 10:25:34
This isn't really correct... the string value is s
dgn
2015/11/06 16:07:08
Thanks! I didn't think about checking that but it'
|
+ 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()); |
+ } |
+ } |
+} |