OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/TracedValue.h" | 5 #include "platform/TracedValue.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include <memory> | |
11 | 10 |
12 namespace blink { | 11 namespace blink { |
13 | 12 |
14 std::unique_ptr<base::Value> parseTracedValue(std::unique_ptr<TracedValue> value
) | 13 std::unique_ptr<base::Value> parseTracedValue(PassOwnPtr<TracedValue> value) |
15 { | 14 { |
16 base::JSONReader reader; | 15 base::JSONReader reader; |
17 CString utf8 = value->toString().utf8(); | 16 CString utf8 = value->toString().utf8(); |
18 return reader.Read(utf8.data()); | 17 return reader.Read(utf8.data()); |
19 } | 18 } |
20 | 19 |
21 TEST(TracedValueTest, FlatDictionary) | 20 TEST(TracedValueTest, FlatDictionary) |
22 { | 21 { |
23 std::unique_ptr<TracedValue> value = TracedValue::create(); | 22 OwnPtr<TracedValue> value = TracedValue::create(); |
24 value->setInteger("int", 2014); | 23 value->setInteger("int", 2014); |
25 value->setDouble("double", 0.0); | 24 value->setDouble("double", 0.0); |
26 value->setBoolean("bool", true); | 25 value->setBoolean("bool", true); |
27 value->setString("string", "string"); | 26 value->setString("string", "string"); |
28 | 27 |
29 std::unique_ptr<base::Value> parsed = parseTracedValue(std::move(value)); | 28 std::unique_ptr<base::Value> parsed = parseTracedValue(std::move(value)); |
30 base::DictionaryValue* dictionary; | 29 base::DictionaryValue* dictionary; |
31 ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); | 30 ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); |
32 int intValue; | 31 int intValue; |
33 EXPECT_TRUE(dictionary->GetInteger("int", &intValue)); | 32 EXPECT_TRUE(dictionary->GetInteger("int", &intValue)); |
34 EXPECT_EQ(2014, intValue); | 33 EXPECT_EQ(2014, intValue); |
35 double doubleValue; | 34 double doubleValue; |
36 EXPECT_TRUE(dictionary->GetDouble("double", &doubleValue)); | 35 EXPECT_TRUE(dictionary->GetDouble("double", &doubleValue)); |
37 EXPECT_EQ(0.0, doubleValue); | 36 EXPECT_EQ(0.0, doubleValue); |
38 std::string stringValue; | 37 std::string stringValue; |
39 EXPECT_TRUE(dictionary->GetString("string", &stringValue)); | 38 EXPECT_TRUE(dictionary->GetString("string", &stringValue)); |
40 EXPECT_EQ("string", stringValue); | 39 EXPECT_EQ("string", stringValue); |
41 } | 40 } |
42 | 41 |
43 TEST(TracedValueTest, Hierarchy) | 42 TEST(TracedValueTest, Hierarchy) |
44 { | 43 { |
45 std::unique_ptr<TracedValue> value = TracedValue::create(); | 44 OwnPtr<TracedValue> value = TracedValue::create(); |
46 value->setInteger("i0", 2014); | 45 value->setInteger("i0", 2014); |
47 value->beginDictionary("dict1"); | 46 value->beginDictionary("dict1"); |
48 value->setInteger("i1", 2014); | 47 value->setInteger("i1", 2014); |
49 value->beginDictionary("dict2"); | 48 value->beginDictionary("dict2"); |
50 value->setBoolean("b2", false); | 49 value->setBoolean("b2", false); |
51 value->endDictionary(); | 50 value->endDictionary(); |
52 value->setString("s1", "foo"); | 51 value->setString("s1", "foo"); |
53 value->endDictionary(); | 52 value->endDictionary(); |
54 value->setDouble("d0", 0.0); | 53 value->setDouble("d0", 0.0); |
55 value->setBoolean("b0", true); | 54 value->setBoolean("b0", true); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 int i2; | 95 int i2; |
97 EXPECT_TRUE(a1d2->GetInteger("i2", &i2)); | 96 EXPECT_TRUE(a1d2->GetInteger("i2", &i2)); |
98 EXPECT_EQ(3, i2); | 97 EXPECT_EQ(3, i2); |
99 std::string s0; | 98 std::string s0; |
100 EXPECT_TRUE(dictionary->GetString("s0", &s0)); | 99 EXPECT_TRUE(dictionary->GetString("s0", &s0)); |
101 EXPECT_EQ("foo", s0); | 100 EXPECT_EQ("foo", s0); |
102 } | 101 } |
103 | 102 |
104 TEST(TracedValueTest, Escape) | 103 TEST(TracedValueTest, Escape) |
105 { | 104 { |
106 std::unique_ptr<TracedValue> value = TracedValue::create(); | 105 OwnPtr<TracedValue> value = TracedValue::create(); |
107 value->setString("s0", "value0\\"); | 106 value->setString("s0", "value0\\"); |
108 value->setString("s1", "value\n1"); | 107 value->setString("s1", "value\n1"); |
109 value->setString("s2", "\"value2\""); | 108 value->setString("s2", "\"value2\""); |
110 value->setString("s3\\", "value3"); | 109 value->setString("s3\\", "value3"); |
111 value->setString("\"s4\"", "value4"); | 110 value->setString("\"s4\"", "value4"); |
112 | 111 |
113 std::unique_ptr<base::Value> parsed = parseTracedValue(std::move(value)); | 112 std::unique_ptr<base::Value> parsed = parseTracedValue(std::move(value)); |
114 base::DictionaryValue* dictionary; | 113 base::DictionaryValue* dictionary; |
115 ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); | 114 ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); |
116 std::string s0; | 115 std::string s0; |
117 EXPECT_TRUE(dictionary->GetString("s0", &s0)); | 116 EXPECT_TRUE(dictionary->GetString("s0", &s0)); |
118 EXPECT_EQ("value0\\", s0); | 117 EXPECT_EQ("value0\\", s0); |
119 std::string s1; | 118 std::string s1; |
120 EXPECT_TRUE(dictionary->GetString("s1", &s1)); | 119 EXPECT_TRUE(dictionary->GetString("s1", &s1)); |
121 EXPECT_EQ("value\n1", s1); | 120 EXPECT_EQ("value\n1", s1); |
122 std::string s2; | 121 std::string s2; |
123 EXPECT_TRUE(dictionary->GetString("s2", &s2)); | 122 EXPECT_TRUE(dictionary->GetString("s2", &s2)); |
124 EXPECT_EQ("\"value2\"", s2); | 123 EXPECT_EQ("\"value2\"", s2); |
125 std::string s3; | 124 std::string s3; |
126 EXPECT_TRUE(dictionary->GetString("s3\\", &s3)); | 125 EXPECT_TRUE(dictionary->GetString("s3\\", &s3)); |
127 EXPECT_EQ("value3", s3); | 126 EXPECT_EQ("value3", s3); |
128 std::string s4; | 127 std::string s4; |
129 EXPECT_TRUE(dictionary->GetString("\"s4\"", &s4)); | 128 EXPECT_TRUE(dictionary->GetString("\"s4\"", &s4)); |
130 EXPECT_EQ("value4", s4); | 129 EXPECT_EQ("value4", s4); |
131 } | 130 } |
132 | 131 |
133 } // namespace blink | 132 } // namespace blink |
OLD | NEW |