| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "components/policy/core/browser/android/policy_converter.h" | 12 #include "components/policy/core/browser/android/policy_converter.h" |
| 13 #include "components/policy/core/common/schema.h" | 13 #include "components/policy/core/common/schema.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using base::DictionaryValue; | 16 using base::DictionaryValue; |
| 17 using base::ListValue; | 17 using base::ListValue; |
| 18 using base::StringValue; | |
| 19 using base::Value; | 18 using base::Value; |
| 20 using base::android::JavaRef; | 19 using base::android::JavaRef; |
| 21 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
| 22 | 21 |
| 23 namespace policy { | 22 namespace policy { |
| 24 namespace android { | 23 namespace android { |
| 25 | 24 |
| 26 class PolicyConverterTest : public testing::Test { | 25 class PolicyConverterTest : public testing::Test { |
| 27 public: | 26 public: |
| 28 void SetUp() override { | 27 void SetUp() override { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 return ScopedJavaLocalRef<jobjectArray>(env, java_array); | 91 return ScopedJavaLocalRef<jobjectArray>(env, java_array); |
| 93 } | 92 } |
| 94 | 93 |
| 95 Schema schema_; | 94 Schema schema_; |
| 96 }; | 95 }; |
| 97 | 96 |
| 98 TEST_F(PolicyConverterTest, ConvertToNullValue) { | 97 TEST_F(PolicyConverterTest, ConvertToNullValue) { |
| 99 Schema null_schema = schema_.GetKnownProperty("null"); | 98 Schema null_schema = schema_.GetKnownProperty("null"); |
| 100 ASSERT_TRUE(null_schema.valid()); | 99 ASSERT_TRUE(null_schema.valid()); |
| 101 | 100 |
| 102 EXPECT_EQ("null", Convert(new StringValue("foo"), null_schema)); | 101 EXPECT_EQ("null", Convert(new Value("foo"), null_schema)); |
| 103 } | 102 } |
| 104 | 103 |
| 105 TEST_F(PolicyConverterTest, ConvertToBoolValue) { | 104 TEST_F(PolicyConverterTest, ConvertToBoolValue) { |
| 106 Schema bool_schema = schema_.GetKnownProperty("bool"); | 105 Schema bool_schema = schema_.GetKnownProperty("bool"); |
| 107 ASSERT_TRUE(bool_schema.valid()); | 106 ASSERT_TRUE(bool_schema.valid()); |
| 108 | 107 |
| 109 EXPECT_EQ("true", Convert(new Value(true), bool_schema)); | 108 EXPECT_EQ("true", Convert(new Value(true), bool_schema)); |
| 110 EXPECT_EQ("false", Convert(new Value(false), bool_schema)); | 109 EXPECT_EQ("false", Convert(new Value(false), bool_schema)); |
| 111 EXPECT_EQ("true", Convert(new StringValue("true"), bool_schema)); | 110 EXPECT_EQ("true", Convert(new Value("true"), bool_schema)); |
| 112 EXPECT_EQ("false", Convert(new StringValue("false"), bool_schema)); | 111 EXPECT_EQ("false", Convert(new Value("false"), bool_schema)); |
| 113 EXPECT_EQ("\"narf\"", Convert(new StringValue("narf"), bool_schema)); | 112 EXPECT_EQ("\"narf\"", Convert(new Value("narf"), bool_schema)); |
| 114 EXPECT_EQ("false", Convert(new Value(0), bool_schema)); | 113 EXPECT_EQ("false", Convert(new Value(0), bool_schema)); |
| 115 EXPECT_EQ("true", Convert(new Value(1), bool_schema)); | 114 EXPECT_EQ("true", Convert(new Value(1), bool_schema)); |
| 116 EXPECT_EQ("true", Convert(new Value(42), bool_schema)); | 115 EXPECT_EQ("true", Convert(new Value(42), bool_schema)); |
| 117 EXPECT_EQ("true", Convert(new Value(-1), bool_schema)); | 116 EXPECT_EQ("true", Convert(new Value(-1), bool_schema)); |
| 118 EXPECT_EQ("\"1\"", Convert(new StringValue("1"), bool_schema)); | 117 EXPECT_EQ("\"1\"", Convert(new Value("1"), bool_schema)); |
| 119 EXPECT_EQ("{}", Convert(new DictionaryValue(), bool_schema)); | 118 EXPECT_EQ("{}", Convert(new DictionaryValue(), bool_schema)); |
| 120 } | 119 } |
| 121 | 120 |
| 122 TEST_F(PolicyConverterTest, ConvertToIntValue) { | 121 TEST_F(PolicyConverterTest, ConvertToIntValue) { |
| 123 Schema int_schema = schema_.GetKnownProperty("int"); | 122 Schema int_schema = schema_.GetKnownProperty("int"); |
| 124 ASSERT_TRUE(int_schema.valid()); | 123 ASSERT_TRUE(int_schema.valid()); |
| 125 | 124 |
| 126 EXPECT_EQ("23", Convert(new Value(23), int_schema)); | 125 EXPECT_EQ("23", Convert(new Value(23), int_schema)); |
| 127 EXPECT_EQ("42", Convert(new StringValue("42"), int_schema)); | 126 EXPECT_EQ("42", Convert(new Value("42"), int_schema)); |
| 128 EXPECT_EQ("-1", Convert(new StringValue("-1"), int_schema)); | 127 EXPECT_EQ("-1", Convert(new Value("-1"), int_schema)); |
| 129 EXPECT_EQ("\"poit\"", Convert(new StringValue("poit"), int_schema)); | 128 EXPECT_EQ("\"poit\"", Convert(new Value("poit"), int_schema)); |
| 130 EXPECT_EQ("false", Convert(new Value(false), int_schema)); | 129 EXPECT_EQ("false", Convert(new Value(false), int_schema)); |
| 131 } | 130 } |
| 132 | 131 |
| 133 TEST_F(PolicyConverterTest, ConvertToDoubleValue) { | 132 TEST_F(PolicyConverterTest, ConvertToDoubleValue) { |
| 134 Schema double_schema = schema_.GetKnownProperty("double"); | 133 Schema double_schema = schema_.GetKnownProperty("double"); |
| 135 ASSERT_TRUE(double_schema.valid()); | 134 ASSERT_TRUE(double_schema.valid()); |
| 136 | 135 |
| 137 EXPECT_EQ("3", Convert(new Value(3), double_schema)); | 136 EXPECT_EQ("3", Convert(new Value(3), double_schema)); |
| 138 EXPECT_EQ("3.14", Convert(new Value(3.14), double_schema)); | 137 EXPECT_EQ("3.14", Convert(new Value(3.14), double_schema)); |
| 139 EXPECT_EQ("2.71", Convert(new StringValue("2.71"), double_schema)); | 138 EXPECT_EQ("2.71", Convert(new Value("2.71"), double_schema)); |
| 140 EXPECT_EQ("\"zort\"", Convert(new StringValue("zort"), double_schema)); | 139 EXPECT_EQ("\"zort\"", Convert(new Value("zort"), double_schema)); |
| 141 EXPECT_EQ("true", Convert(new Value(true), double_schema)); | 140 EXPECT_EQ("true", Convert(new Value(true), double_schema)); |
| 142 } | 141 } |
| 143 | 142 |
| 144 TEST_F(PolicyConverterTest, ConvertToStringValue) { | 143 TEST_F(PolicyConverterTest, ConvertToStringValue) { |
| 145 Schema string_schema = schema_.GetKnownProperty("string"); | 144 Schema string_schema = schema_.GetKnownProperty("string"); |
| 146 ASSERT_TRUE(string_schema.valid()); | 145 ASSERT_TRUE(string_schema.valid()); |
| 147 | 146 |
| 148 EXPECT_EQ("\"troz\"", Convert(new StringValue("troz"), string_schema)); | 147 EXPECT_EQ("\"troz\"", Convert(new Value("troz"), string_schema)); |
| 149 EXPECT_EQ("4711", Convert(new Value(4711), string_schema)); | 148 EXPECT_EQ("4711", Convert(new Value(4711), string_schema)); |
| 150 } | 149 } |
| 151 | 150 |
| 152 TEST_F(PolicyConverterTest, ConvertToListValue) { | 151 TEST_F(PolicyConverterTest, ConvertToListValue) { |
| 153 Schema list_schema = schema_.GetKnownProperty("list"); | 152 Schema list_schema = schema_.GetKnownProperty("list"); |
| 154 ASSERT_TRUE(list_schema.valid()); | 153 ASSERT_TRUE(list_schema.valid()); |
| 155 | 154 |
| 156 ListValue* list = new ListValue; | 155 ListValue* list = new ListValue; |
| 157 list->AppendString("foo"); | 156 list->AppendString("foo"); |
| 158 list->AppendString("bar"); | 157 list->AppendString("bar"); |
| 159 EXPECT_EQ("[\"foo\",\"bar\"]", Convert(list, list_schema)); | 158 EXPECT_EQ("[\"foo\",\"bar\"]", Convert(list, list_schema)); |
| 160 EXPECT_EQ("[\"baz\",\"blurp\"]", | 159 EXPECT_EQ("[\"baz\",\"blurp\"]", |
| 161 Convert(new StringValue("[\"baz\", \"blurp\"]"), list_schema)); | 160 Convert(new Value("[\"baz\", \"blurp\"]"), list_schema)); |
| 162 EXPECT_EQ("\"hurz\"", Convert(new StringValue("hurz"), list_schema)); | 161 EXPECT_EQ("\"hurz\"", Convert(new Value("hurz"), list_schema)); |
| 163 EXPECT_EQ("19", Convert(new Value(19), list_schema)); | 162 EXPECT_EQ("19", Convert(new Value(19), list_schema)); |
| 164 } | 163 } |
| 165 | 164 |
| 166 TEST_F(PolicyConverterTest, ConvertFromJavaListToListValue) { | 165 TEST_F(PolicyConverterTest, ConvertFromJavaListToListValue) { |
| 167 JNIEnv* env = base::android::AttachCurrentThread(); | 166 JNIEnv* env = base::android::AttachCurrentThread(); |
| 168 EXPECT_EQ("[\"foo\",\"bar\",\"baz\"]", | 167 EXPECT_EQ("[\"foo\",\"bar\",\"baz\"]", |
| 169 ConvertJavaStringArrayToListValue( | 168 ConvertJavaStringArrayToListValue( |
| 170 env, MakeJavaStringArray(env, {"foo", "bar", "baz"}))); | 169 env, MakeJavaStringArray(env, {"foo", "bar", "baz"}))); |
| 171 EXPECT_EQ("[]", ConvertJavaStringArrayToListValue( | 170 EXPECT_EQ("[]", ConvertJavaStringArrayToListValue( |
| 172 env, MakeJavaStringArray(env, {}))); | 171 env, MakeJavaStringArray(env, {}))); |
| 173 } | 172 } |
| 174 | 173 |
| 175 TEST_F(PolicyConverterTest, ConvertToDictionaryValue) { | 174 TEST_F(PolicyConverterTest, ConvertToDictionaryValue) { |
| 176 Schema dict_schema = schema_.GetKnownProperty("dict"); | 175 Schema dict_schema = schema_.GetKnownProperty("dict"); |
| 177 ASSERT_TRUE(dict_schema.valid()); | 176 ASSERT_TRUE(dict_schema.valid()); |
| 178 | 177 |
| 179 DictionaryValue* dict = new DictionaryValue; | 178 DictionaryValue* dict = new DictionaryValue; |
| 180 dict->SetInteger("thx", 1138); | 179 dict->SetInteger("thx", 1138); |
| 181 EXPECT_EQ("{\"thx\":1138}", Convert(dict, dict_schema)); | 180 EXPECT_EQ("{\"thx\":1138}", Convert(dict, dict_schema)); |
| 182 EXPECT_EQ("{\"moose\":true}", | 181 EXPECT_EQ("{\"moose\":true}", |
| 183 Convert(new StringValue("{\"moose\": true}"), dict_schema)); | 182 Convert(new Value("{\"moose\": true}"), dict_schema)); |
| 184 EXPECT_EQ("\"fnord\"", Convert(new StringValue("fnord"), dict_schema)); | 183 EXPECT_EQ("\"fnord\"", Convert(new Value("fnord"), dict_schema)); |
| 185 EXPECT_EQ("1729", Convert(new Value(1729), dict_schema)); | 184 EXPECT_EQ("1729", Convert(new Value(1729), dict_schema)); |
| 186 } | 185 } |
| 187 | 186 |
| 188 } // namespace android | 187 } // namespace android |
| 189 } // namespace policy | 188 } // namespace policy |
| OLD | NEW |