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