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