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