| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "tools/json_schema_compiler/test/simple_api.h" | 5 #include "tools/json_schema_compiler/test/simple_api.h" |
| 6 | 6 |
| 7 #include "chrome/common/extensions/value_builder.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 9 |
| 9 using namespace test::api::simple_api; | 10 using namespace test::api::simple_api; |
| 11 using namespace extensions; |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 static scoped_ptr<DictionaryValue> CreateTestTypeDictionary() { | 15 static scoped_ptr<DictionaryValue> CreateTestTypeDictionary() { |
| 14 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 16 scoped_ptr<DictionaryValue> value(DictionaryBuilder() |
| 15 value->SetWithoutPathExpansion("number", Value::CreateDoubleValue(1.1)); | 17 .Set("number", 1.1).Set("integer", 4).Set("string", "bling") |
| 16 value->SetWithoutPathExpansion("integer", Value::CreateIntegerValue(4)); | 18 .SetBoolean("boolean", true).Build()); |
| 17 value->SetWithoutPathExpansion("string", Value::CreateStringValue("bling")); | |
| 18 value->SetWithoutPathExpansion("boolean", Value::CreateBooleanValue(true)); | |
| 19 return value.Pass(); | 19 return value.Pass(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerResultCreate) { | 24 TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerResultCreate) { |
| 25 scoped_ptr<Value> result(IncrementInteger::Result::Create(5)); | 25 scoped_ptr<ListValue> results(IncrementInteger::Result::Create(5)); |
| 26 int temp = 0; | 26 scoped_ptr<ListValue> expected(ListBuilder().Append(5).Build()); |
| 27 EXPECT_TRUE(result->GetAsInteger(&temp)); | 27 EXPECT_TRUE(results->Equals(expected.get())); |
| 28 EXPECT_EQ(5, temp); | |
| 29 } | 28 } |
| 30 | 29 |
| 31 TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerParamsCreate) { | 30 TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerParamsCreate) { |
| 32 scoped_ptr<ListValue> params_value(new ListValue()); | 31 scoped_ptr<ListValue> params_value(ListBuilder().Append(6).Build()); |
| 33 params_value->Append(Value::CreateIntegerValue(6)); | |
| 34 scoped_ptr<IncrementInteger::Params> params( | 32 scoped_ptr<IncrementInteger::Params> params( |
| 35 IncrementInteger::Params::Create(*params_value)); | 33 IncrementInteger::Params::Create(*params_value)); |
| 36 EXPECT_TRUE(params.get()); | 34 EXPECT_TRUE(params.get()); |
| 37 EXPECT_EQ(6, params->num); | 35 EXPECT_EQ(6, params->num); |
| 38 } | 36 } |
| 39 | 37 |
| 40 TEST(JsonSchemaCompilerSimpleTest, NumberOfParams) { | 38 TEST(JsonSchemaCompilerSimpleTest, NumberOfParams) { |
| 41 { | 39 { |
| 42 scoped_ptr<ListValue> params_value(new ListValue()); | 40 scoped_ptr<ListValue> params_value( |
| 43 params_value->Append(Value::CreateStringValue("text")); | 41 ListBuilder().Append("text").Append("text").Build()); |
| 44 params_value->Append(Value::CreateStringValue("text")); | |
| 45 scoped_ptr<OptionalString::Params> params( | 42 scoped_ptr<OptionalString::Params> params( |
| 46 OptionalString::Params::Create(*params_value)); | 43 OptionalString::Params::Create(*params_value)); |
| 47 EXPECT_FALSE(params.get()); | 44 EXPECT_FALSE(params.get()); |
| 48 } | 45 } |
| 49 { | 46 { |
| 50 scoped_ptr<ListValue> params_value(new ListValue()); | 47 scoped_ptr<ListValue> params_value(new ListValue()); |
| 51 scoped_ptr<IncrementInteger::Params> params( | 48 scoped_ptr<IncrementInteger::Params> params( |
| 52 IncrementInteger::Params::Create(*params_value)); | 49 IncrementInteger::Params::Create(*params_value)); |
| 53 EXPECT_FALSE(params.get()); | 50 EXPECT_FALSE(params.get()); |
| 54 } | 51 } |
| 55 } | 52 } |
| 56 | 53 |
| 57 TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsCreate) { | 54 TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsCreate) { |
| 58 { | 55 { |
| 59 scoped_ptr<ListValue> params_value(new ListValue()); | 56 scoped_ptr<ListValue> params_value(new ListValue()); |
| 60 scoped_ptr<OptionalString::Params> params( | 57 scoped_ptr<OptionalString::Params> params( |
| 61 OptionalString::Params::Create(*params_value)); | 58 OptionalString::Params::Create(*params_value)); |
| 62 EXPECT_TRUE(params.get()); | 59 EXPECT_TRUE(params.get()); |
| 63 EXPECT_FALSE(params->str.get()); | 60 EXPECT_FALSE(params->str.get()); |
| 64 } | 61 } |
| 65 { | 62 { |
| 66 scoped_ptr<ListValue> params_value(new ListValue()); | 63 scoped_ptr<ListValue> params_value(ListBuilder().Append("asdf").Build()); |
| 67 params_value->Append(Value::CreateStringValue("asdf")); | |
| 68 scoped_ptr<OptionalString::Params> params( | 64 scoped_ptr<OptionalString::Params> params( |
| 69 OptionalString::Params::Create(*params_value)); | 65 OptionalString::Params::Create(*params_value)); |
| 70 EXPECT_TRUE(params.get()); | 66 EXPECT_TRUE(params.get()); |
| 71 EXPECT_TRUE(params->str.get()); | 67 EXPECT_TRUE(params->str.get()); |
| 72 EXPECT_EQ("asdf", *params->str); | 68 EXPECT_EQ("asdf", *params->str); |
| 73 } | 69 } |
| 74 } | 70 } |
| 75 | 71 |
| 76 TEST(JsonSchemaCompilerSimpleTest, OptionalParamsTakingNull) { | 72 TEST(JsonSchemaCompilerSimpleTest, OptionalParamsTakingNull) { |
| 77 { | 73 { |
| 78 scoped_ptr<ListValue> params_value(new ListValue()); | 74 scoped_ptr<ListValue> params_value(new ListValue()); |
| 79 params_value->Append(Value::CreateNullValue()); | 75 params_value->Append(Value::CreateNullValue()); |
| 80 scoped_ptr<OptionalString::Params> params( | 76 scoped_ptr<OptionalString::Params> params( |
| 81 OptionalString::Params::Create(*params_value)); | 77 OptionalString::Params::Create(*params_value)); |
| 82 EXPECT_TRUE(params.get()); | 78 EXPECT_TRUE(params.get()); |
| 83 EXPECT_FALSE(params->str.get()); | 79 EXPECT_FALSE(params->str.get()); |
| 84 } | 80 } |
| 85 } | 81 } |
| 86 | 82 |
| 87 TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsWrongType) { | 83 TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsWrongType) { |
| 88 { | 84 { |
| 89 scoped_ptr<ListValue> params_value(new ListValue()); | 85 scoped_ptr<ListValue> params_value(ListBuilder().Append(5).Build()); |
| 90 params_value->Append(Value::CreateIntegerValue(5)); | |
| 91 scoped_ptr<OptionalString::Params> params( | 86 scoped_ptr<OptionalString::Params> params( |
| 92 OptionalString::Params::Create(*params_value)); | 87 OptionalString::Params::Create(*params_value)); |
| 93 EXPECT_FALSE(params.get()); | 88 EXPECT_FALSE(params.get()); |
| 94 } | 89 } |
| 95 } | 90 } |
| 96 | 91 |
| 97 TEST(JsonSchemaCompilerSimpleTest, OptionalBeforeRequired) { | 92 TEST(JsonSchemaCompilerSimpleTest, OptionalBeforeRequired) { |
| 98 { | 93 { |
| 99 scoped_ptr<ListValue> params_value(new ListValue()); | 94 scoped_ptr<ListValue> params_value(new ListValue()); |
| 100 params_value->Append(Value::CreateNullValue()); | 95 params_value->Append(Value::CreateNullValue()); |
| 101 params_value->Append(Value::CreateStringValue("asdf")); | 96 params_value->Append(Value::CreateStringValue("asdf")); |
| 102 scoped_ptr<OptionalBeforeRequired::Params> params( | 97 scoped_ptr<OptionalBeforeRequired::Params> params( |
| 103 OptionalBeforeRequired::Params::Create(*params_value)); | 98 OptionalBeforeRequired::Params::Create(*params_value)); |
| 104 EXPECT_TRUE(params.get()); | 99 EXPECT_TRUE(params.get()); |
| 105 EXPECT_FALSE(params->first.get()); | 100 EXPECT_FALSE(params->first.get()); |
| 106 EXPECT_EQ("asdf", params->second); | 101 EXPECT_EQ("asdf", params->second); |
| 107 } | 102 } |
| 108 } | 103 } |
| 109 | 104 |
| 110 TEST(JsonSchemaCompilerSimpleTest, NoParamsResultCreate) { | 105 TEST(JsonSchemaCompilerSimpleTest, NoParamsResultCreate) { |
| 111 scoped_ptr<Value> result(OptionalString::Result::Create()); | 106 scoped_ptr<ListValue> results(OptionalString::Result::Create()); |
| 112 scoped_ptr<Value> expected(Value::CreateNullValue()); | 107 ListValue expected; |
| 113 EXPECT_TRUE(Value::Equals(result.get(), expected.get())); | 108 EXPECT_TRUE(results->Equals(&expected)); |
| 114 } | 109 } |
| 115 | 110 |
| 116 TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) { | 111 TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) { |
| 117 { | 112 { |
| 118 scoped_ptr<TestType> test_type(new TestType()); | 113 scoped_ptr<TestType> test_type(new TestType()); |
| 119 scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); | 114 scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); |
| 120 EXPECT_TRUE(TestType::Populate(*value, test_type.get())); | 115 EXPECT_TRUE(TestType::Populate(*value, test_type.get())); |
| 121 EXPECT_EQ("bling", test_type->string); | 116 EXPECT_EQ("bling", test_type->string); |
| 122 EXPECT_EQ(1.1, test_type->number); | 117 EXPECT_EQ(1.1, test_type->number); |
| 123 EXPECT_EQ(4, test_type->integer); | 118 EXPECT_EQ(4, test_type->integer); |
| 124 EXPECT_EQ(true, test_type->boolean); | 119 EXPECT_EQ(true, test_type->boolean); |
| 125 EXPECT_TRUE(value->Equals(test_type->ToValue().get())); | 120 EXPECT_TRUE(value->Equals(test_type->ToValue().get())); |
| 126 } | 121 } |
| 127 { | 122 { |
| 128 scoped_ptr<TestType> test_type(new TestType()); | 123 scoped_ptr<TestType> test_type(new TestType()); |
| 129 scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); | 124 scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); |
| 130 value->Remove("number", NULL); | 125 value->Remove("number", NULL); |
| 131 EXPECT_FALSE(TestType::Populate(*value, test_type.get())); | 126 EXPECT_FALSE(TestType::Populate(*value, test_type.get())); |
| 132 } | 127 } |
| 133 } | 128 } |
| 134 | 129 |
| 135 TEST(JsonSchemaCompilerSimpleTest, GetTestType) { | 130 TEST(JsonSchemaCompilerSimpleTest, GetTestType) { |
| 136 scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); | 131 { |
| 137 scoped_ptr<TestType> test_type(new TestType()); | 132 scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); |
| 138 EXPECT_TRUE(TestType::Populate(*value, test_type.get())); | 133 scoped_ptr<TestType> test_type(new TestType()); |
| 139 scoped_ptr<Value> result(GetTestType::Result::Create(*test_type)); | 134 EXPECT_TRUE(TestType::Populate(*value, test_type.get())); |
| 140 EXPECT_TRUE(value->Equals(result.get())); | 135 scoped_ptr<ListValue> results(GetTestType::Result::Create(*test_type)); |
| 136 |
| 137 DictionaryValue* result = NULL; |
| 138 ASSERT_TRUE(results->GetDictionary(0, &result)); |
| 139 EXPECT_TRUE(result->Equals(value.get())); |
| 140 } |
| 141 } | 141 } |
| 142 | 142 |
| 143 TEST(JsonSchemaCompilerSimpleTest, OnIntegerFiredCreate) { |
| 144 { |
| 145 scoped_ptr<ListValue> results(OnIntegerFired::Create(5)); |
| 146 scoped_ptr<ListValue> expected(ListBuilder().Append(5).Build()); |
| 147 EXPECT_TRUE(results->Equals(expected.get())); |
| 148 } |
| 149 } |
| 150 |
| 151 TEST(JsonSchemaCompilerSimpleTest, OnStringFiredCreate) { |
| 152 { |
| 153 scoped_ptr<ListValue> results(OnStringFired::Create("yo dawg")); |
| 154 scoped_ptr<ListValue> expected(ListBuilder().Append("yo dawg").Build()); |
| 155 EXPECT_TRUE(results->Equals(expected.get())); |
| 156 } |
| 157 } |
| 158 |
| 159 TEST(JsonSchemaCompilerSimpleTest, OnTestTypeFiredCreate) { |
| 160 { |
| 161 TestType some_test_type; |
| 162 scoped_ptr<DictionaryValue> expected = CreateTestTypeDictionary(); |
| 163 ASSERT_TRUE(expected->GetDouble("number", &some_test_type.number)); |
| 164 ASSERT_TRUE(expected->GetString("string", &some_test_type.string)); |
| 165 ASSERT_TRUE(expected->GetInteger("integer", &some_test_type.integer)); |
| 166 ASSERT_TRUE(expected->GetBoolean("boolean", &some_test_type.boolean)); |
| 167 |
| 168 scoped_ptr<ListValue> results(OnTestTypeFired::Create(some_test_type)); |
| 169 DictionaryValue* result = NULL; |
| 170 ASSERT_TRUE(results->GetDictionary(0, &result)); |
| 171 EXPECT_TRUE(result->Equals(expected.get())); |
| 172 } |
| 173 } |
| OLD | NEW |