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