| 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..60136e7e72ebea1d3826dd14b0b2f5d958f54955
|
| --- /dev/null
|
| +++ b/components/policy/android/javatests/src/org/chromium/policy/test/PolicyData.java
|
| @@ -0,0 +1,93 @@
|
| +// 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;
|
| +
|
| +/**
|
| + * 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 {
|
| + 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) {
|
| + data.putInBundle(bundle);
|
| + }
|
| + return bundle;
|
| + }
|
| +
|
| + @Override
|
| + public int hashCode() {
|
| + final int prime = 31;
|
| + int result = 1;
|
| + 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);
|
| + }
|
| + }
|
| +
|
| + /** {@link PolicyData} with no type, 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) {}
|
| + }
|
| +
|
| + /**
|
| + * {@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;
|
| + }
|
| +
|
| + @Override
|
| + public void putInBundle(Bundle bundle) {
|
| + String joinedStrings = TextUtils.join("\",\"", mValue);
|
| + bundle.putString(mKey, "[\"" + joinedStrings + "\"]");
|
| + }
|
| + }
|
| +}
|
|
|