Revert of Use ExtensionRegistryObserver instead of deprecated extension notification...
[chromium-blink-merge.git] / tools / json_schema_compiler / test / simple_api_unittest.cc
blobf34b9fc258201e02554b59bebe55119e846f29ea
1 // Copyright (c) 2012 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 "tools/json_schema_compiler/test/simple_api.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 using namespace test::api::simple_api;
11 namespace {
13 static scoped_ptr<base::DictionaryValue> CreateTestTypeDictionary() {
14 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
15 value->SetWithoutPathExpansion("number",
16 base::Value::CreateDoubleValue(1.1));
17 value->SetWithoutPathExpansion("integer",
18 base::Value::CreateIntegerValue(4));
19 value->SetWithoutPathExpansion("string",
20 base::Value::CreateStringValue("bling"));
21 value->SetWithoutPathExpansion("boolean",
22 base::Value::CreateBooleanValue(true));
23 return value.Pass();
26 } // namespace
28 TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerResultCreate) {
29 scoped_ptr<base::ListValue> results = IncrementInteger::Results::Create(5);
30 base::ListValue expected;
31 expected.Append(base::Value::CreateIntegerValue(5));
32 EXPECT_TRUE(results->Equals(&expected));
35 TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerParamsCreate) {
36 scoped_ptr<base::ListValue> params_value(new base::ListValue());
37 params_value->Append(base::Value::CreateIntegerValue(6));
38 scoped_ptr<IncrementInteger::Params> params(
39 IncrementInteger::Params::Create(*params_value));
40 EXPECT_TRUE(params.get());
41 EXPECT_EQ(6, params->num);
44 TEST(JsonSchemaCompilerSimpleTest, NumberOfParams) {
46 scoped_ptr<base::ListValue> params_value(new base::ListValue());
47 params_value->Append(base::Value::CreateStringValue("text"));
48 params_value->Append(base::Value::CreateStringValue("text"));
49 scoped_ptr<OptionalString::Params> params(
50 OptionalString::Params::Create(*params_value));
51 EXPECT_FALSE(params.get());
54 scoped_ptr<base::ListValue> params_value(new base::ListValue());
55 scoped_ptr<IncrementInteger::Params> params(
56 IncrementInteger::Params::Create(*params_value));
57 EXPECT_FALSE(params.get());
61 TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsCreate) {
63 scoped_ptr<base::ListValue> params_value(new base::ListValue());
64 scoped_ptr<OptionalString::Params> params(
65 OptionalString::Params::Create(*params_value));
66 EXPECT_TRUE(params.get());
67 EXPECT_FALSE(params->str.get());
70 scoped_ptr<base::ListValue> params_value(new base::ListValue());
71 params_value->Append(base::Value::CreateStringValue("asdf"));
72 scoped_ptr<OptionalString::Params> params(
73 OptionalString::Params::Create(*params_value));
74 EXPECT_TRUE(params.get());
75 EXPECT_TRUE(params->str.get());
76 EXPECT_EQ("asdf", *params->str);
80 TEST(JsonSchemaCompilerSimpleTest, OptionalParamsTakingNull) {
82 scoped_ptr<base::ListValue> params_value(new base::ListValue());
83 params_value->Append(base::Value::CreateNullValue());
84 scoped_ptr<OptionalString::Params> params(
85 OptionalString::Params::Create(*params_value));
86 EXPECT_TRUE(params.get());
87 EXPECT_FALSE(params->str.get());
91 TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsWrongType) {
93 scoped_ptr<base::ListValue> params_value(new base::ListValue());
94 params_value->Append(base::Value::CreateIntegerValue(5));
95 scoped_ptr<OptionalString::Params> params(
96 OptionalString::Params::Create(*params_value));
97 EXPECT_FALSE(params.get());
101 TEST(JsonSchemaCompilerSimpleTest, OptionalBeforeRequired) {
103 scoped_ptr<base::ListValue> params_value(new base::ListValue());
104 params_value->Append(base::Value::CreateNullValue());
105 params_value->Append(base::Value::CreateStringValue("asdf"));
106 scoped_ptr<OptionalBeforeRequired::Params> params(
107 OptionalBeforeRequired::Params::Create(*params_value));
108 EXPECT_TRUE(params.get());
109 EXPECT_FALSE(params->first.get());
110 EXPECT_EQ("asdf", params->second);
114 TEST(JsonSchemaCompilerSimpleTest, NoParamsResultCreate) {
115 scoped_ptr<base::ListValue> results = OptionalString::Results::Create();
116 base::ListValue expected;
117 EXPECT_TRUE(results->Equals(&expected));
120 TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) {
122 scoped_ptr<TestType> test_type(new TestType());
123 scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
124 EXPECT_TRUE(TestType::Populate(*value, test_type.get()));
125 EXPECT_EQ("bling", test_type->string);
126 EXPECT_EQ(1.1, test_type->number);
127 EXPECT_EQ(4, test_type->integer);
128 EXPECT_EQ(true, test_type->boolean);
129 EXPECT_TRUE(value->Equals(test_type->ToValue().get()));
132 scoped_ptr<TestType> test_type(new TestType());
133 scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
134 value->Remove("number", NULL);
135 EXPECT_FALSE(TestType::Populate(*value, test_type.get()));
139 TEST(JsonSchemaCompilerSimpleTest, GetTestType) {
141 scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
142 scoped_ptr<TestType> test_type(new TestType());
143 EXPECT_TRUE(TestType::Populate(*value, test_type.get()));
144 scoped_ptr<base::ListValue> results =
145 GetTestType::Results::Create(*test_type);
147 base::DictionaryValue* result = NULL;
148 results->GetDictionary(0, &result);
149 EXPECT_TRUE(result->Equals(value.get()));
153 TEST(JsonSchemaCompilerSimpleTest, OnIntegerFiredCreate) {
155 scoped_ptr<base::ListValue> results(OnIntegerFired::Create(5));
156 base::ListValue expected;
157 expected.Append(base::Value::CreateIntegerValue(5));
158 EXPECT_TRUE(results->Equals(&expected));
162 TEST(JsonSchemaCompilerSimpleTest, OnStringFiredCreate) {
164 scoped_ptr<base::ListValue> results(OnStringFired::Create("yo dawg"));
165 base::ListValue expected;
166 expected.Append(base::Value::CreateStringValue("yo dawg"));
167 EXPECT_TRUE(results->Equals(&expected));
171 TEST(JsonSchemaCompilerSimpleTest, OnTestTypeFiredCreate) {
173 TestType some_test_type;
174 scoped_ptr<base::DictionaryValue> expected = CreateTestTypeDictionary();
175 ASSERT_TRUE(expected->GetDouble("number", &some_test_type.number));
176 ASSERT_TRUE(expected->GetString("string", &some_test_type.string));
177 ASSERT_TRUE(expected->GetInteger("integer", &some_test_type.integer));
178 ASSERT_TRUE(expected->GetBoolean("boolean", &some_test_type.boolean));
180 scoped_ptr<base::ListValue> results(
181 OnTestTypeFired::Create(some_test_type));
182 base::DictionaryValue* result = NULL;
183 results->GetDictionary(0, &result);
184 EXPECT_TRUE(result->Equals(expected.get()));