objects_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/objects.h"
6
7#include "base/json/json_writer.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10using namespace test::api::objects;
11
12TEST(JsonSchemaCompilerObjectsTest, ObjectParamParamsCreate) {
13  {
14    scoped_ptr<ListValue> strings(new ListValue());
15    strings->Append(Value::CreateStringValue("one"));
16    strings->Append(Value::CreateStringValue("two"));
17    scoped_ptr<DictionaryValue> info_value(new DictionaryValue());
18    info_value->Set("strings", strings.release());
19    info_value->Set("integer", Value::CreateIntegerValue(5));
20    info_value->Set("boolean", Value::CreateBooleanValue(true));
21
22    scoped_ptr<ListValue> params_value(new ListValue());
23    params_value->Append(info_value.release());
24    scoped_ptr<ObjectParam::Params> params(
25        ObjectParam::Params::Create(*params_value));
26    EXPECT_TRUE(params.get());
27    EXPECT_EQ((size_t) 2, params->info.strings.size());
28    EXPECT_EQ("one", params->info.strings[0]);
29    EXPECT_EQ("two", params->info.strings[1]);
30    EXPECT_EQ(5, params->info.integer);
31    EXPECT_TRUE(params->info.boolean);
32  }
33  {
34    scoped_ptr<ListValue> strings(new ListValue());
35    strings->Append(Value::CreateStringValue("one"));
36    strings->Append(Value::CreateStringValue("two"));
37    scoped_ptr<DictionaryValue> info_value(new DictionaryValue());
38    info_value->Set("strings", strings.release());
39    info_value->Set("integer", Value::CreateIntegerValue(5));
40
41    scoped_ptr<ListValue> params_value(new ListValue());
42    params_value->Append(info_value.release());
43    scoped_ptr<ObjectParam::Params> params(
44        ObjectParam::Params::Create(*params_value));
45    EXPECT_FALSE(params.get());
46  }
47}
48
49TEST(JsonSchemaCompilerObjectsTest, ReturnsObjectResultCreate) {
50  ReturnsObject::Results::Info info;
51  info.state = ReturnsObject::Results::Info::STATE_FOO;
52  scoped_ptr<ListValue> results = ReturnsObject::Results::Create(info);
53
54  DictionaryValue expected;
55  expected.SetString("state", "foo");
56  DictionaryValue* result = NULL;
57  ASSERT_TRUE(results->GetDictionary(0, &result));
58  ASSERT_TRUE(result->Equals(&expected));
59}
60
61TEST(JsonSchemaCompilerObjectsTest, OnObjectFiredCreate) {
62  OnObjectFired::SomeObject object;
63  object.state = OnObjectFired::SomeObject::STATE_BAR;
64  scoped_ptr<ListValue> results(OnObjectFired::Create(object));
65
66  DictionaryValue expected;
67  expected.SetString("state", "bar");
68  DictionaryValue* result = NULL;
69  ASSERT_TRUE(results->GetDictionary(0, &result));
70  ASSERT_TRUE(result->Equals(&expected));
71
72  std::string json1 = OnObjectFired::ToJson(object);
73  std::string json2;
74  base::JSONWriter::Write(results.get(), &json2);
75  ASSERT_EQ(json1, json2);
76}
77