simple_api_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 9using namespace test::api::simple_api; 10 11namespace { 12 13static 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(); 24} 25 26} // namespace 27 28TEST(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)); 33} 34 35TEST(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); 42} 43 44TEST(JsonSchemaCompilerSimpleTest, NumberOfParams) { 45 { 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()); 52 } 53 { 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()); 58 } 59} 60 61TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsCreate) { 62 { 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()); 68 } 69 { 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); 77 } 78} 79 80TEST(JsonSchemaCompilerSimpleTest, OptionalParamsTakingNull) { 81 { 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()); 88 } 89} 90 91TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsWrongType) { 92 { 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()); 98 } 99} 100 101TEST(JsonSchemaCompilerSimpleTest, OptionalBeforeRequired) { 102 { 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); 111 } 112} 113 114TEST(JsonSchemaCompilerSimpleTest, NoParamsResultCreate) { 115 scoped_ptr<base::ListValue> results = OptionalString::Results::Create(); 116 base::ListValue expected; 117 EXPECT_TRUE(results->Equals(&expected)); 118} 119 120TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) { 121 { 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())); 130 } 131 { 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())); 136 } 137} 138 139TEST(JsonSchemaCompilerSimpleTest, GetTestType) { 140 { 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); 146 147 base::DictionaryValue* result = NULL; 148 results->GetDictionary(0, &result); 149 EXPECT_TRUE(result->Equals(value.get())); 150 } 151} 152 153TEST(JsonSchemaCompilerSimpleTest, OnIntegerFiredCreate) { 154 { 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)); 159 } 160} 161 162TEST(JsonSchemaCompilerSimpleTest, OnStringFiredCreate) { 163 { 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)); 168 } 169} 170 171TEST(JsonSchemaCompilerSimpleTest, OnTestTypeFiredCreate) { 172 { 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)); 179 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())); 185 } 186} 187