| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/android/jni_android.h" |
| 6 #include "base/android/jni_array.h" |
| 7 #include "base/android/jni_string.h" |
| 5 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 6 #include "base/values.h" | 9 #include "base/values.h" |
| 7 #include "chrome/browser/android/policy/policy_manager.h" | 10 #include "components/policy/core/browser/android/policy_converter.h" |
| 8 #include "components/policy/core/common/schema.h" | 11 #include "components/policy/core/common/schema.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 using base::DictionaryValue; | 14 using base::DictionaryValue; |
| 12 using base::FundamentalValue; | 15 using base::FundamentalValue; |
| 13 using base::ListValue; | 16 using base::ListValue; |
| 14 using base::StringValue; | 17 using base::StringValue; |
| 15 using base::Value; | 18 using base::Value; |
| 16 | 19 |
| 17 class PolicyManagerTest : public testing::Test { | 20 namespace policy { |
| 21 namespace android { |
| 22 |
| 23 class PolicyConverterTest : public testing::Test { |
| 18 public: | 24 public: |
| 19 void SetUp() override { | 25 void SetUp() override { |
| 20 const char kSchemaTemplate[] = | 26 const char kSchemaTemplate[] = |
| 21 "{" | 27 "{" |
| 22 " \"type\": \"object\"," | 28 " \"type\": \"object\"," |
| 23 " \"properties\": {" | 29 " \"properties\": {" |
| 24 " \"null\": { \"type\": \"null\" }," | 30 " \"null\": { \"type\": \"null\" }," |
| 25 " \"bool\": { \"type\": \"boolean\" }," | 31 " \"bool\": { \"type\": \"boolean\" }," |
| 26 " \"int\": { \"type\": \"integer\" }," | 32 " \"int\": { \"type\": \"integer\" }," |
| 27 " \"double\": { \"type\": \"number\" }," | 33 " \"double\": { \"type\": \"number\" }," |
| 28 " \"string\": { \"type\": \"string\" }," | 34 " \"string\": { \"type\": \"string\" }," |
| 29 " \"list\": {" | 35 " \"list\": {" |
| 30 " \"type\": \"array\"," | 36 " \"type\": \"array\"," |
| 31 " \"items\": { \"type\": \"string\"}" | 37 " \"items\": { \"type\": \"string\"}" |
| 32 " }," | 38 " }," |
| 33 " \"dict\": { \"type\": \"object\" }" | 39 " \"dict\": { \"type\": \"object\" }" |
| 34 " }" | 40 " }" |
| 35 "}"; | 41 "}"; |
| 36 | 42 |
| 37 std::string error; | 43 std::string error; |
| 38 schema_ = policy::Schema::Parse(kSchemaTemplate, &error); | 44 schema_ = Schema::Parse(kSchemaTemplate, &error); |
| 39 ASSERT_TRUE(schema_.valid()) << error; | 45 ASSERT_TRUE(schema_.valid()) << error; |
| 40 } | 46 } |
| 41 | 47 |
| 42 protected: | 48 protected: |
| 43 // Converts the passed in value to the passed in schema, and serializes the | 49 // Converts the passed in value to the passed in schema, and serializes the |
| 44 // result to JSON, to make it easier to compare with EXPECT_EQ. | 50 // result to JSON, to make it easier to compare with EXPECT_EQ. |
| 45 std::string Convert(Value* value, const policy::Schema& value_schema) { | 51 std::string Convert(Value* value, const Schema& value_schema) { |
| 46 scoped_ptr<Value> converted_value( | 52 scoped_ptr<Value> converted_value(PolicyConverter::ConvertValueToSchema( |
| 47 PolicyManager::ConvertValueToSchema(value, value_schema)); | 53 scoped_ptr<Value>(value), value_schema)); |
| 48 | 54 |
| 49 std::string json_string; | 55 std::string json_string; |
| 50 EXPECT_TRUE( | 56 EXPECT_TRUE( |
| 51 base::JSONWriter::Write(*(converted_value.get()), &json_string)); | 57 base::JSONWriter::Write(*(converted_value.get()), &json_string)); |
| 52 return json_string; | 58 return json_string; |
| 53 } | 59 } |
| 54 | 60 |
| 55 policy::Schema schema_; | 61 // Uses|PolicyConverter::ConvertJavaStringArrayToListValue| to convert the |
| 62 // passed in java array and serializes the result to JSON, to make it easier |
| 63 // to compare with EXPECT_EQ. |
| 64 std::string ConvertJavaStringArrayToListValue(JNIEnv* env, |
| 65 jobjectArray java_array) { |
| 66 scoped_ptr<ListValue> list = |
| 67 PolicyConverter::ConvertJavaStringArrayToListValue(env, java_array); |
| 68 |
| 69 std::string json_string; |
| 70 EXPECT_TRUE(base::JSONWriter::Write(*list, &json_string)); |
| 71 |
| 72 return json_string; |
| 73 } |
| 74 |
| 75 // Converts the passed in values to a java string array |
| 76 jobjectArray MakeJavaStringArray(JNIEnv* env, |
| 77 std::vector<std::string> values) { |
| 78 jobjectArray java_array = (jobjectArray)env->NewObjectArray( |
| 79 values.size(), env->FindClass("java/lang/String"), nullptr); |
| 80 for (size_t i = 0; i < values.size(); i++) { |
| 81 env->SetObjectArrayElement( |
| 82 java_array, i, |
| 83 base::android::ConvertUTF8ToJavaString(env, values[i]).obj()); |
| 84 } |
| 85 |
| 86 return java_array; |
| 87 } |
| 88 |
| 89 Schema schema_; |
| 56 }; | 90 }; |
| 57 | 91 |
| 58 TEST_F(PolicyManagerTest, ConvertToNullValue) { | 92 TEST_F(PolicyConverterTest, ConvertToNullValue) { |
| 59 policy::Schema null_schema = schema_.GetKnownProperty("null"); | 93 Schema null_schema = schema_.GetKnownProperty("null"); |
| 60 ASSERT_TRUE(null_schema.valid()); | 94 ASSERT_TRUE(null_schema.valid()); |
| 61 | 95 |
| 62 EXPECT_EQ("null", Convert(new StringValue("foo"), null_schema)); | 96 EXPECT_EQ("null", Convert(new StringValue("foo"), null_schema)); |
| 63 } | 97 } |
| 64 | 98 |
| 65 TEST_F(PolicyManagerTest, ConvertToBoolValue) { | 99 TEST_F(PolicyConverterTest, ConvertToBoolValue) { |
| 66 policy::Schema bool_schema = schema_.GetKnownProperty("bool"); | 100 Schema bool_schema = schema_.GetKnownProperty("bool"); |
| 67 ASSERT_TRUE(bool_schema.valid()); | 101 ASSERT_TRUE(bool_schema.valid()); |
| 68 | 102 |
| 69 EXPECT_EQ("true", Convert(new FundamentalValue(true), bool_schema)); | 103 EXPECT_EQ("true", Convert(new FundamentalValue(true), bool_schema)); |
| 70 EXPECT_EQ("false", Convert(new FundamentalValue(false), bool_schema)); | 104 EXPECT_EQ("false", Convert(new FundamentalValue(false), bool_schema)); |
| 71 EXPECT_EQ("true", Convert(new StringValue("true"), bool_schema)); | 105 EXPECT_EQ("true", Convert(new StringValue("true"), bool_schema)); |
| 72 EXPECT_EQ("false", Convert(new StringValue("false"), bool_schema)); | 106 EXPECT_EQ("false", Convert(new StringValue("false"), bool_schema)); |
| 73 EXPECT_EQ("\"narf\"", Convert(new StringValue("narf"), bool_schema)); | 107 EXPECT_EQ("\"narf\"", Convert(new StringValue("narf"), bool_schema)); |
| 74 EXPECT_EQ("false", Convert(new FundamentalValue(0), bool_schema)); | 108 EXPECT_EQ("false", Convert(new FundamentalValue(0), bool_schema)); |
| 75 EXPECT_EQ("true", Convert(new FundamentalValue(1), bool_schema)); | 109 EXPECT_EQ("true", Convert(new FundamentalValue(1), bool_schema)); |
| 76 EXPECT_EQ("true", Convert(new FundamentalValue(42), bool_schema)); | 110 EXPECT_EQ("true", Convert(new FundamentalValue(42), bool_schema)); |
| 77 EXPECT_EQ("true", Convert(new FundamentalValue(-1), bool_schema)); | 111 EXPECT_EQ("true", Convert(new FundamentalValue(-1), bool_schema)); |
| 78 EXPECT_EQ("\"1\"", Convert(new StringValue("1"), bool_schema)); | 112 EXPECT_EQ("\"1\"", Convert(new StringValue("1"), bool_schema)); |
| 79 EXPECT_EQ("{}", Convert(new DictionaryValue(), bool_schema)); | 113 EXPECT_EQ("{}", Convert(new DictionaryValue(), bool_schema)); |
| 80 } | 114 } |
| 81 | 115 |
| 82 TEST_F(PolicyManagerTest, ConvertToIntValue) { | 116 TEST_F(PolicyConverterTest, ConvertToIntValue) { |
| 83 policy::Schema int_schema = schema_.GetKnownProperty("int"); | 117 Schema int_schema = schema_.GetKnownProperty("int"); |
| 84 ASSERT_TRUE(int_schema.valid()); | 118 ASSERT_TRUE(int_schema.valid()); |
| 85 | 119 |
| 86 EXPECT_EQ("23", Convert(new FundamentalValue(23), int_schema)); | 120 EXPECT_EQ("23", Convert(new FundamentalValue(23), int_schema)); |
| 87 EXPECT_EQ("42", Convert(new StringValue("42"), int_schema)); | 121 EXPECT_EQ("42", Convert(new StringValue("42"), int_schema)); |
| 88 EXPECT_EQ("-1", Convert(new StringValue("-1"), int_schema)); | 122 EXPECT_EQ("-1", Convert(new StringValue("-1"), int_schema)); |
| 89 EXPECT_EQ("\"poit\"", Convert(new StringValue("poit"), int_schema)); | 123 EXPECT_EQ("\"poit\"", Convert(new StringValue("poit"), int_schema)); |
| 90 EXPECT_EQ("false", Convert(new FundamentalValue(false), int_schema)); | 124 EXPECT_EQ("false", Convert(new FundamentalValue(false), int_schema)); |
| 91 } | 125 } |
| 92 | 126 |
| 93 TEST_F(PolicyManagerTest, ConvertToDoubleValue) { | 127 TEST_F(PolicyConverterTest, ConvertToDoubleValue) { |
| 94 policy::Schema double_schema = schema_.GetKnownProperty("double"); | 128 Schema double_schema = schema_.GetKnownProperty("double"); |
| 95 ASSERT_TRUE(double_schema.valid()); | 129 ASSERT_TRUE(double_schema.valid()); |
| 96 | 130 |
| 97 EXPECT_EQ("3", Convert(new FundamentalValue(3), double_schema)); | 131 EXPECT_EQ("3", Convert(new FundamentalValue(3), double_schema)); |
| 98 EXPECT_EQ("3.14", Convert(new FundamentalValue(3.14), double_schema)); | 132 EXPECT_EQ("3.14", Convert(new FundamentalValue(3.14), double_schema)); |
| 99 EXPECT_EQ("2.71", Convert(new StringValue("2.71"), double_schema)); | 133 EXPECT_EQ("2.71", Convert(new StringValue("2.71"), double_schema)); |
| 100 EXPECT_EQ("\"zort\"", Convert(new StringValue("zort"), double_schema)); | 134 EXPECT_EQ("\"zort\"", Convert(new StringValue("zort"), double_schema)); |
| 101 EXPECT_EQ("true", Convert(new FundamentalValue(true), double_schema)); | 135 EXPECT_EQ("true", Convert(new FundamentalValue(true), double_schema)); |
| 102 } | 136 } |
| 103 | 137 |
| 104 TEST_F(PolicyManagerTest, ConvertToStringValue) { | 138 TEST_F(PolicyConverterTest, ConvertToStringValue) { |
| 105 policy::Schema string_schema = schema_.GetKnownProperty("string"); | 139 Schema string_schema = schema_.GetKnownProperty("string"); |
| 106 ASSERT_TRUE(string_schema.valid()); | 140 ASSERT_TRUE(string_schema.valid()); |
| 107 | 141 |
| 108 EXPECT_EQ("\"troz\"", Convert(new StringValue("troz"), string_schema)); | 142 EXPECT_EQ("\"troz\"", Convert(new StringValue("troz"), string_schema)); |
| 109 EXPECT_EQ("4711", Convert(new FundamentalValue(4711), string_schema)); | 143 EXPECT_EQ("4711", Convert(new FundamentalValue(4711), string_schema)); |
| 110 } | 144 } |
| 111 | 145 |
| 112 TEST_F(PolicyManagerTest, ConvertToListValue) { | 146 TEST_F(PolicyConverterTest, ConvertToListValue) { |
| 113 policy::Schema list_schema = schema_.GetKnownProperty("list"); | 147 Schema list_schema = schema_.GetKnownProperty("list"); |
| 114 ASSERT_TRUE(list_schema.valid()); | 148 ASSERT_TRUE(list_schema.valid()); |
| 115 | 149 |
| 116 ListValue* list = new ListValue; | 150 ListValue* list = new ListValue; |
| 117 list->AppendString("foo"); | 151 list->AppendString("foo"); |
| 118 list->AppendString("bar"); | 152 list->AppendString("bar"); |
| 119 EXPECT_EQ("[\"foo\",\"bar\"]", Convert(list, list_schema)); | 153 EXPECT_EQ("[\"foo\",\"bar\"]", Convert(list, list_schema)); |
| 120 EXPECT_EQ("[\"baz\",\"blurp\"]", | 154 EXPECT_EQ("[\"baz\",\"blurp\"]", |
| 121 Convert(new StringValue("[\"baz\", \"blurp\"]"), list_schema)); | 155 Convert(new StringValue("[\"baz\", \"blurp\"]"), list_schema)); |
| 122 EXPECT_EQ("\"hurz\"", Convert(new StringValue("hurz"), list_schema)); | 156 EXPECT_EQ("\"hurz\"", Convert(new StringValue("hurz"), list_schema)); |
| 123 EXPECT_EQ("19", Convert(new FundamentalValue(19), list_schema)); | 157 EXPECT_EQ("19", Convert(new FundamentalValue(19), list_schema)); |
| 124 } | 158 } |
| 125 | 159 |
| 126 TEST_F(PolicyManagerTest, ConvertToDictionaryValue) { | 160 TEST_F(PolicyConverterTest, ConvertFromJavaListToListValue) { |
| 127 policy::Schema dict_schema = schema_.GetKnownProperty("dict"); | 161 JNIEnv* env = base::android::AttachCurrentThread(); |
| 162 EXPECT_EQ("[\"foo\",\"bar\",\"baz\"]", |
| 163 ConvertJavaStringArrayToListValue( |
| 164 env, MakeJavaStringArray(env, {"foo", "bar", "baz"}))); |
| 165 EXPECT_EQ("[]", ConvertJavaStringArrayToListValue( |
| 166 env, MakeJavaStringArray(env, {}))); |
| 167 } |
| 168 |
| 169 TEST_F(PolicyConverterTest, ConvertToDictionaryValue) { |
| 170 Schema dict_schema = schema_.GetKnownProperty("dict"); |
| 128 ASSERT_TRUE(dict_schema.valid()); | 171 ASSERT_TRUE(dict_schema.valid()); |
| 129 | 172 |
| 130 DictionaryValue* dict = new DictionaryValue; | 173 DictionaryValue* dict = new DictionaryValue; |
| 131 dict->SetInteger("thx", 1138); | 174 dict->SetInteger("thx", 1138); |
| 132 EXPECT_EQ("{\"thx\":1138}", Convert(dict, dict_schema)); | 175 EXPECT_EQ("{\"thx\":1138}", Convert(dict, dict_schema)); |
| 133 EXPECT_EQ("{\"moose\":true}", | 176 EXPECT_EQ("{\"moose\":true}", |
| 134 Convert(new StringValue("{\"moose\": true}"), dict_schema)); | 177 Convert(new StringValue("{\"moose\": true}"), dict_schema)); |
| 135 EXPECT_EQ("\"fnord\"", Convert(new StringValue("fnord"), dict_schema)); | 178 EXPECT_EQ("\"fnord\"", Convert(new StringValue("fnord"), dict_schema)); |
| 136 EXPECT_EQ("1729", Convert(new FundamentalValue(1729), dict_schema)); | 179 EXPECT_EQ("1729", Convert(new FundamentalValue(1729), dict_schema)); |
| 137 } | 180 } |
| 181 |
| 182 } // namespace android |
| 183 } // namespace policy |
| OLD | NEW |