cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / policy / core / browser / android / policy_converter_unittest.cc
blob748eb5fed5ce485463d63ae715c3ebab3715d9e4
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
3 // found in the LICENSE file.
5 #include "base/android/jni_android.h"
6 #include "base/android/jni_array.h"
7 #include "base/android/jni_string.h"
8 #include "base/json/json_writer.h"
9 #include "base/values.h"
10 #include "components/policy/core/browser/android/policy_converter.h"
11 #include "components/policy/core/common/schema.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::DictionaryValue;
15 using base::FundamentalValue;
16 using base::ListValue;
17 using base::StringValue;
18 using base::Value;
20 namespace policy {
21 namespace android {
23 class PolicyConverterTest : public testing::Test {
24 public:
25 void SetUp() override {
26 const char kSchemaTemplate[] =
27 "{"
28 " \"type\": \"object\","
29 " \"properties\": {"
30 " \"null\": { \"type\": \"null\" },"
31 " \"bool\": { \"type\": \"boolean\" },"
32 " \"int\": { \"type\": \"integer\" },"
33 " \"double\": { \"type\": \"number\" },"
34 " \"string\": { \"type\": \"string\" },"
35 " \"list\": {"
36 " \"type\": \"array\","
37 " \"items\": { \"type\": \"string\"}"
38 " },"
39 " \"dict\": { \"type\": \"object\" }"
40 " }"
41 "}";
43 std::string error;
44 schema_ = Schema::Parse(kSchemaTemplate, &error);
45 ASSERT_TRUE(schema_.valid()) << error;
48 protected:
49 // Converts the passed in value to the passed in schema, and serializes the
50 // result to JSON, to make it easier to compare with EXPECT_EQ.
51 std::string Convert(Value* value, const Schema& value_schema) {
52 scoped_ptr<Value> converted_value(PolicyConverter::ConvertValueToSchema(
53 scoped_ptr<Value>(value), value_schema));
55 std::string json_string;
56 EXPECT_TRUE(
57 base::JSONWriter::Write(*(converted_value.get()), &json_string));
58 return json_string;
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);
69 std::string json_string;
70 EXPECT_TRUE(base::JSONWriter::Write(*list, &json_string));
72 return json_string;
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());
86 return java_array;
89 Schema schema_;
92 TEST_F(PolicyConverterTest, ConvertToNullValue) {
93 Schema null_schema = schema_.GetKnownProperty("null");
94 ASSERT_TRUE(null_schema.valid());
96 EXPECT_EQ("null", Convert(new StringValue("foo"), null_schema));
99 TEST_F(PolicyConverterTest, ConvertToBoolValue) {
100 Schema bool_schema = schema_.GetKnownProperty("bool");
101 ASSERT_TRUE(bool_schema.valid());
103 EXPECT_EQ("true", Convert(new FundamentalValue(true), bool_schema));
104 EXPECT_EQ("false", Convert(new FundamentalValue(false), bool_schema));
105 EXPECT_EQ("true", Convert(new StringValue("true"), bool_schema));
106 EXPECT_EQ("false", Convert(new StringValue("false"), bool_schema));
107 EXPECT_EQ("\"narf\"", Convert(new StringValue("narf"), bool_schema));
108 EXPECT_EQ("false", Convert(new FundamentalValue(0), bool_schema));
109 EXPECT_EQ("true", Convert(new FundamentalValue(1), bool_schema));
110 EXPECT_EQ("true", Convert(new FundamentalValue(42), bool_schema));
111 EXPECT_EQ("true", Convert(new FundamentalValue(-1), bool_schema));
112 EXPECT_EQ("\"1\"", Convert(new StringValue("1"), bool_schema));
113 EXPECT_EQ("{}", Convert(new DictionaryValue(), bool_schema));
116 TEST_F(PolicyConverterTest, ConvertToIntValue) {
117 Schema int_schema = schema_.GetKnownProperty("int");
118 ASSERT_TRUE(int_schema.valid());
120 EXPECT_EQ("23", Convert(new FundamentalValue(23), int_schema));
121 EXPECT_EQ("42", Convert(new StringValue("42"), int_schema));
122 EXPECT_EQ("-1", Convert(new StringValue("-1"), int_schema));
123 EXPECT_EQ("\"poit\"", Convert(new StringValue("poit"), int_schema));
124 EXPECT_EQ("false", Convert(new FundamentalValue(false), int_schema));
127 TEST_F(PolicyConverterTest, ConvertToDoubleValue) {
128 Schema double_schema = schema_.GetKnownProperty("double");
129 ASSERT_TRUE(double_schema.valid());
131 EXPECT_EQ("3", Convert(new FundamentalValue(3), double_schema));
132 EXPECT_EQ("3.14", Convert(new FundamentalValue(3.14), double_schema));
133 EXPECT_EQ("2.71", Convert(new StringValue("2.71"), double_schema));
134 EXPECT_EQ("\"zort\"", Convert(new StringValue("zort"), double_schema));
135 EXPECT_EQ("true", Convert(new FundamentalValue(true), double_schema));
138 TEST_F(PolicyConverterTest, ConvertToStringValue) {
139 Schema string_schema = schema_.GetKnownProperty("string");
140 ASSERT_TRUE(string_schema.valid());
142 EXPECT_EQ("\"troz\"", Convert(new StringValue("troz"), string_schema));
143 EXPECT_EQ("4711", Convert(new FundamentalValue(4711), string_schema));
146 TEST_F(PolicyConverterTest, ConvertToListValue) {
147 Schema list_schema = schema_.GetKnownProperty("list");
148 ASSERT_TRUE(list_schema.valid());
150 ListValue* list = new ListValue;
151 list->AppendString("foo");
152 list->AppendString("bar");
153 EXPECT_EQ("[\"foo\",\"bar\"]", Convert(list, list_schema));
154 EXPECT_EQ("[\"baz\",\"blurp\"]",
155 Convert(new StringValue("[\"baz\", \"blurp\"]"), list_schema));
156 EXPECT_EQ("\"hurz\"", Convert(new StringValue("hurz"), list_schema));
157 EXPECT_EQ("19", Convert(new FundamentalValue(19), list_schema));
160 TEST_F(PolicyConverterTest, ConvertFromJavaListToListValue) {
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, {})));
169 TEST_F(PolicyConverterTest, ConvertToDictionaryValue) {
170 Schema dict_schema = schema_.GetKnownProperty("dict");
171 ASSERT_TRUE(dict_schema.valid());
173 DictionaryValue* dict = new DictionaryValue;
174 dict->SetInteger("thx", 1138);
175 EXPECT_EQ("{\"thx\":1138}", Convert(dict, dict_schema));
176 EXPECT_EQ("{\"moose\":true}",
177 Convert(new StringValue("{\"moose\": true}"), dict_schema));
178 EXPECT_EQ("\"fnord\"", Convert(new StringValue("fnord"), dict_schema));
179 EXPECT_EQ("1729", Convert(new FundamentalValue(1729), dict_schema));
182 } // namespace android
183 } // namespace policy