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
3 // found in the LICENSE file.
5 #include "base/trace_event/trace_event_argument.h"
6 #include "base/values.h"
7 #include "testing/gtest/include/gtest/gtest.h"
10 namespace trace_event
{
12 TEST(TraceEventArgumentTest
, FlatDictionary
) {
13 scoped_refptr
<TracedValue
> value
= new TracedValue();
14 value
->SetInteger("int", 2014);
15 value
->SetDouble("double", 0.0);
16 value
->SetBoolean("bool", true);
17 value
->SetString("string", "string");
18 std::string json
= "PREFIX";
19 value
->AppendAsTraceFormat(&json
);
21 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}",
25 TEST(TraceEventArgumentTest
, NoDotPathExpansion
) {
26 scoped_refptr
<TracedValue
> value
= new TracedValue();
27 value
->SetInteger("in.t", 2014);
28 value
->SetDouble("doub.le", 0.0);
29 value
->SetBoolean("bo.ol", true);
30 value
->SetString("str.ing", "str.ing");
32 value
->AppendAsTraceFormat(&json
);
34 "{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}",
38 TEST(TraceEventArgumentTest
, Hierarchy
) {
39 scoped_refptr
<TracedValue
> value
= new TracedValue();
40 value
->SetInteger("i0", 2014);
41 value
->BeginDictionary("dict1");
42 value
->SetInteger("i1", 2014);
43 value
->BeginDictionary("dict2");
44 value
->SetBoolean("b2", false);
45 value
->EndDictionary();
46 value
->SetString("s1", "foo");
47 value
->EndDictionary();
48 value
->SetDouble("d0", 0.0);
49 value
->SetBoolean("b0", true);
50 value
->BeginArray("a1");
51 value
->AppendInteger(1);
52 value
->AppendBoolean(true);
53 value
->BeginDictionary();
54 value
->SetInteger("i2", 3);
55 value
->EndDictionary();
57 value
->SetString("s0", "foo");
59 value
->AppendAsTraceFormat(&json
);
61 "{\"a1\":[1,true,{\"i2\":3}],\"b0\":true,\"d0\":0.0,\"dict1\":{\"dict2\":"
62 "{\"b2\":false},\"i1\":2014,\"s1\":\"foo\"},\"i0\":2014,\"s0\":"
67 TEST(TraceEventArgumentTest
, LongStrings
) {
68 std::string kLongString
= "supercalifragilisticexpialidocious";
69 std::string kLongString2
= "0123456789012345678901234567890123456789";
70 char kLongString3
[4096];
71 for (size_t i
= 0; i
< sizeof(kLongString3
); ++i
)
72 kLongString3
[i
] = 'a' + (i
% 25);
73 kLongString3
[sizeof(kLongString3
) - 1] = '\0';
75 scoped_refptr
<TracedValue
> value
= new TracedValue();
76 value
->SetString("a", "short");
77 value
->SetString("b", kLongString
);
78 value
->BeginArray("c");
79 value
->AppendString(kLongString2
);
80 value
->AppendString("");
81 value
->BeginDictionary();
82 value
->SetString("a", kLongString3
);
83 value
->EndDictionary();
87 value
->AppendAsTraceFormat(&json
);
88 EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString
+ "\",\"c\":[\"" +
89 kLongString2
+ "\",\"\",{\"a\":\"" + kLongString3
+ "\"}]}",
93 TEST(TraceEventArgumentTest
, PassBaseValue
) {
94 FundamentalValue
int_value(42);
95 FundamentalValue
bool_value(true);
96 FundamentalValue
double_value(42.0f
);
98 auto dict_value
= make_scoped_ptr(new DictionaryValue
);
99 dict_value
->SetBoolean("bool", true);
100 dict_value
->SetInteger("int", 42);
101 dict_value
->SetDouble("double", 42.0f
);
102 dict_value
->SetString("string", std::string("a") + "b");
103 dict_value
->SetString("string", std::string("a") + "b");
105 auto list_value
= make_scoped_ptr(new ListValue
);
106 list_value
->AppendBoolean(false);
107 list_value
->AppendInteger(1);
108 list_value
->AppendString("in_list");
109 list_value
->Append(dict_value
.Pass());
111 scoped_refptr
<TracedValue
> value
= new TracedValue();
112 value
->BeginDictionary("outer_dict");
113 value
->SetValue("inner_list", list_value
.Pass());
114 value
->EndDictionary();
120 value
->AppendAsTraceFormat(&json
);
122 "{\"outer_dict\":{\"inner_list\":[false,1,\"in_list\",{\"bool\":true,"
123 "\"double\":42.0,\"int\":42,\"string\":\"ab\"}]}}",
127 TEST(TraceEventArgumentTest
, PassTracedValue
) {
128 auto dict_value
= make_scoped_refptr(new TracedValue
);
129 dict_value
->SetInteger("a", 1);
131 auto nested_dict_value
= make_scoped_refptr(new TracedValue
);
132 nested_dict_value
->SetInteger("b", 2);
133 nested_dict_value
->BeginArray("c");
134 nested_dict_value
->AppendString("foo");
135 nested_dict_value
->EndArray();
137 dict_value
->SetValue("e", *nested_dict_value
);
139 // Check the merged result.
141 dict_value
->AppendAsTraceFormat(&json
);
142 EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json
);
144 // Check that the passed nestd dict was left unouthced.
146 nested_dict_value
->AppendAsTraceFormat(&json
);
147 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json
);
149 // And that it is still usable.
150 nested_dict_value
->SetInteger("f", 3);
151 nested_dict_value
->BeginDictionary("g");
152 nested_dict_value
->EndDictionary();
154 nested_dict_value
->AppendAsTraceFormat(&json
);
155 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json
);
158 } // namespace trace_event