15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "tools/json_schema_compiler/test/simple_api.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace test::api::simple_api;
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static scoped_ptr<base::DictionaryValue> CreateTestTypeDictionary() {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  value->SetWithoutPathExpansion("number", new base::FundamentalValue(1.1));
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  value->SetWithoutPathExpansion("integer", new base::FundamentalValue(4));
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  value->SetWithoutPathExpansion("string", new base::StringValue("bling"));
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  value->SetWithoutPathExpansion("boolean", new base::FundamentalValue(true));
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return value.Pass();
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerResultCreate) {
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> results = IncrementInteger::Results::Create(5);
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue expected;
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  expected.Append(new base::FundamentalValue(5));
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(results->Equals(&expected));
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerParamsCreate) {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> params_value(new base::ListValue());
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  params_value->Append(new base::FundamentalValue(6));
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<IncrementInteger::Params> params(
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IncrementInteger::Params::Create(*params_value));
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(params.get());
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(6, params->num);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, NumberOfParams) {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    params_value->Append(new base::StringValue("text"));
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    params_value->Append(new base::StringValue("text"));
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<OptionalString::Params> params(
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OptionalString::Params::Create(*params_value));
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(params.get());
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<IncrementInteger::Params> params(
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IncrementInteger::Params::Create(*params_value));
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(params.get());
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsCreate) {
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<OptionalString::Params> params(
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OptionalString::Params::Create(*params_value));
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(params.get());
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(params->str.get());
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    params_value->Append(new base::StringValue("asdf"));
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<OptionalString::Params> params(
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OptionalString::Params::Create(*params_value));
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(params.get());
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(params->str.get());
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ("asdf", *params->str);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OptionalParamsTakingNull) {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params_value->Append(base::Value::CreateNullValue());
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<OptionalString::Params> params(
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OptionalString::Params::Create(*params_value));
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(params.get());
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(params->str.get());
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OptionalStringParamsWrongType) {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    params_value->Append(new base::FundamentalValue(5));
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<OptionalString::Params> params(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OptionalString::Params::Create(*params_value));
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(params.get());
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OptionalBeforeRequired) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> params_value(new base::ListValue());
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params_value->Append(base::Value::CreateNullValue());
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    params_value->Append(new base::StringValue("asdf"));
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<OptionalBeforeRequired::Params> params(
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OptionalBeforeRequired::Params::Create(*params_value));
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(params.get());
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(params->first.get());
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ("asdf", params->second);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, NoParamsResultCreate) {
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> results = OptionalString::Results::Create();
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue expected;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(results->Equals(&expected));
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<TestType> test_type(new TestType());
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(TestType::Populate(*value, test_type.get()));
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ("bling", test_type->string);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(1.1, test_type->number);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(4, test_type->integer);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(true, test_type->boolean);
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(value->Equals(test_type->ToValue().get()));
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<TestType> test_type(new TestType());
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    value->Remove("number", NULL);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(TestType::Populate(*value, test_type.get()));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, GetTestType) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<TestType> test_type(new TestType());
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(TestType::Populate(*value, test_type.get()));
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> results =
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        GetTestType::Results::Create(*test_type);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* result = NULL;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    results->GetDictionary(0, &result);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(result->Equals(value.get()));
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OnIntegerFiredCreate) {
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> results(OnIntegerFired::Create(5));
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue expected;
153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    expected.Append(new base::FundamentalValue(5));
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(results->Equals(&expected));
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OnStringFiredCreate) {
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> results(OnStringFired::Create("yo dawg"));
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue expected;
1625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    expected.Append(new base::StringValue("yo dawg"));
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(results->Equals(&expected));
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(JsonSchemaCompilerSimpleTest, OnTestTypeFiredCreate) {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestType some_test_type;
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> expected = CreateTestTypeDictionary();
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(expected->GetDouble("number", &some_test_type.number));
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(expected->GetString("string", &some_test_type.string));
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(expected->GetInteger("integer", &some_test_type.integer));
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(expected->GetBoolean("boolean", &some_test_type.boolean));
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::ListValue> results(
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        OnTestTypeFired::Create(some_test_type));
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* result = NULL;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    results->GetDictionary(0, &result);
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(result->Equals(expected.get()));
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
183