OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/trace_event/trace_event_argument.h" | 5 #include "base/trace_event/trace_event_argument.h" |
| 6 #include "base/values.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
7 | 8 |
8 namespace base { | 9 namespace base { |
9 namespace trace_event { | 10 namespace trace_event { |
10 | 11 |
11 TEST(TraceEventArgumentTest, FlatDictionary) { | 12 TEST(TraceEventArgumentTest, FlatDictionary) { |
12 scoped_refptr<TracedValue> value = new TracedValue(); | 13 scoped_refptr<TracedValue> value = new TracedValue(); |
13 value->SetInteger("int", 2014); | 14 value->SetInteger("int", 2014); |
14 value->SetDouble("double", 0.0); | 15 value->SetDouble("double", 0.0); |
15 value->SetBoolean("bool", true); | 16 value->SetBoolean("bool", true); |
16 value->SetString("string", "string"); | 17 value->SetString("string", "string"); |
17 std::string json; | 18 std::string json = "PREFIX"; |
18 value->AppendAsTraceFormat(&json); | 19 value->AppendAsTraceFormat(&json); |
19 EXPECT_EQ("{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}", | 20 EXPECT_EQ( |
20 json); | 21 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}", |
| 22 json); |
21 } | 23 } |
22 | 24 |
23 TEST(TraceEventArgumentTest, Hierarchy) { | 25 TEST(TraceEventArgumentTest, Hierarchy) { |
24 scoped_refptr<TracedValue> value = new TracedValue(); | 26 scoped_refptr<TracedValue> value = new TracedValue(); |
25 value->SetInteger("i0", 2014); | 27 value->SetInteger("i0", 2014); |
26 value->BeginDictionary("dict1"); | 28 value->BeginDictionary("dict1"); |
27 value->SetInteger("i1", 2014); | 29 value->SetInteger("i1", 2014); |
28 value->BeginDictionary("dict2"); | 30 value->BeginDictionary("dict2"); |
29 value->SetBoolean("b2", false); | 31 value->SetBoolean("b2", false); |
30 value->EndDictionary(); | 32 value->EndDictionary(); |
(...skipping 11 matching lines...) Expand all Loading... |
42 value->SetString("s0", "foo"); | 44 value->SetString("s0", "foo"); |
43 std::string json; | 45 std::string json; |
44 value->AppendAsTraceFormat(&json); | 46 value->AppendAsTraceFormat(&json); |
45 EXPECT_EQ( | 47 EXPECT_EQ( |
46 "{\"a1\":[1,true,{\"i2\":3}],\"b0\":true,\"d0\":0.0,\"dict1\":{\"dict2\":" | 48 "{\"a1\":[1,true,{\"i2\":3}],\"b0\":true,\"d0\":0.0,\"dict1\":{\"dict2\":" |
47 "{\"b2\":false},\"i1\":2014,\"s1\":\"foo\"},\"i0\":2014,\"s0\":" | 49 "{\"b2\":false},\"i1\":2014,\"s1\":\"foo\"},\"i0\":2014,\"s0\":" |
48 "\"foo\"}", | 50 "\"foo\"}", |
49 json); | 51 json); |
50 } | 52 } |
51 | 53 |
| 54 TEST(TraceEventArgumentTest, LongStrings) { |
| 55 std::string kLongString = "supercalifragilisticexpialidocious"; |
| 56 std::string kLongString2 = "0123456789012345678901234567890123456789"; |
| 57 char kLongString3[4096]; |
| 58 for (size_t i = 0; i < sizeof(kLongString3); ++i) |
| 59 kLongString3[i] = 'a' + (i % 25); |
| 60 kLongString3[sizeof(kLongString3) - 1] = '\0'; |
| 61 |
| 62 scoped_refptr<TracedValue> value = new TracedValue(); |
| 63 value->SetString("a", "short"); |
| 64 value->SetString("b", kLongString); |
| 65 value->BeginArray("c"); |
| 66 value->AppendString(kLongString2); |
| 67 value->AppendString(""); |
| 68 value->BeginDictionary(); |
| 69 value->SetString("a", kLongString3); |
| 70 value->EndDictionary(); |
| 71 value->EndArray(); |
| 72 |
| 73 std::string json; |
| 74 value->AppendAsTraceFormat(&json); |
| 75 EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString + "\",\"c\":[\"" + |
| 76 kLongString2 + "\",\"\",{\"a\":\"" + kLongString3 + "\"}]}", |
| 77 json); |
| 78 } |
| 79 |
| 80 TEST(TraceEventArgumentTest, PassBaseValue) { |
| 81 FundamentalValue int_value(42); |
| 82 FundamentalValue bool_value(true); |
| 83 FundamentalValue double_value(42.0f); |
| 84 |
| 85 auto dict_value = make_scoped_ptr(new DictionaryValue); |
| 86 dict_value->SetBoolean("bool", true); |
| 87 dict_value->SetInteger("int", 42); |
| 88 dict_value->SetDouble("double", 42.0f); |
| 89 dict_value->SetString("string", std::string("a") + "b"); |
| 90 dict_value->SetString("string", std::string("a") + "b"); |
| 91 |
| 92 auto list_value = make_scoped_ptr(new ListValue); |
| 93 list_value->AppendBoolean(false); |
| 94 list_value->AppendInteger(1); |
| 95 list_value->AppendString("in_list"); |
| 96 list_value->Append(dict_value.Pass()); |
| 97 |
| 98 scoped_refptr<TracedValue> value = new TracedValue(); |
| 99 value->BeginDictionary("outer_dict"); |
| 100 value->SetValue("inner_list", list_value.Pass()); |
| 101 value->EndDictionary(); |
| 102 |
| 103 dict_value.reset(); |
| 104 list_value.reset(); |
| 105 |
| 106 std::string json; |
| 107 value->AppendAsTraceFormat(&json); |
| 108 EXPECT_EQ( |
| 109 "{\"outer_dict\":{\"inner_list\":[false,1,\"in_list\",{\"bool\":true," |
| 110 "\"double\":42.0,\"int\":42,\"string\":\"ab\"}]}}", |
| 111 json); |
| 112 } |
| 113 |
| 114 TEST(TraceEventArgumentTest, PassTracedValue) { |
| 115 auto dict_value = make_scoped_refptr(new TracedValue); |
| 116 dict_value->SetInteger("a", 1); |
| 117 |
| 118 auto nested_dict_value = make_scoped_refptr(new TracedValue); |
| 119 nested_dict_value->SetInteger("b", 2); |
| 120 nested_dict_value->BeginArray("c"); |
| 121 nested_dict_value->AppendString("foo"); |
| 122 nested_dict_value->EndArray(); |
| 123 |
| 124 dict_value->SetValue("e", *nested_dict_value); |
| 125 |
| 126 // Check the merged result. |
| 127 std::string json; |
| 128 dict_value->AppendAsTraceFormat(&json); |
| 129 EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json); |
| 130 |
| 131 // Check that the passed nestd dict was left unouthced. |
| 132 json = ""; |
| 133 nested_dict_value->AppendAsTraceFormat(&json); |
| 134 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json); |
| 135 |
| 136 // And that it is still usable. |
| 137 nested_dict_value->SetInteger("f", 3); |
| 138 nested_dict_value->BeginDictionary("g"); |
| 139 nested_dict_value->EndDictionary(); |
| 140 json = ""; |
| 141 nested_dict_value->AppendAsTraceFormat(&json); |
| 142 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json); |
| 143 } |
| 144 |
52 } // namespace trace_event | 145 } // namespace trace_event |
53 } // namespace base | 146 } // namespace base |
OLD | NEW |