Chromium Code Reviews| 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()); |
| + } |
| + } |
| +} |