Remove use of JSONReader::DeprecatedRead from activity_log
[chromium-blink-merge.git] / base / trace_event / trace_event_argument_unittest.cc
blobcb1cf2ef1d57cac30abdb073799eab29f28a9270
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"
9 namespace base {
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);
20 EXPECT_EQ(
21 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}",
22 json);
25 TEST(TraceEventArgumentTest, Hierarchy) {
26 scoped_refptr<TracedValue> value = new TracedValue();
27 value->SetInteger("i0", 2014);
28 value->BeginDictionary("dict1");
29 value->SetInteger("i1", 2014);
30 value->BeginDictionary("dict2");
31 value->SetBoolean("b2", false);
32 value->EndDictionary();
33 value->SetString("s1", "foo");
34 value->EndDictionary();
35 value->SetDouble("d0", 0.0);
36 value->SetBoolean("b0", true);
37 value->BeginArray("a1");
38 value->AppendInteger(1);
39 value->AppendBoolean(true);
40 value->BeginDictionary();
41 value->SetInteger("i2", 3);
42 value->EndDictionary();
43 value->EndArray();
44 value->SetString("s0", "foo");
45 std::string json;
46 value->AppendAsTraceFormat(&json);
47 EXPECT_EQ(
48 "{\"a1\":[1,true,{\"i2\":3}],\"b0\":true,\"d0\":0.0,\"dict1\":{\"dict2\":"
49 "{\"b2\":false},\"i1\":2014,\"s1\":\"foo\"},\"i0\":2014,\"s0\":"
50 "\"foo\"}",
51 json);
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';
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();
73 std::string json;
74 value->AppendAsTraceFormat(&json);
75 EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString + "\",\"c\":[\"" +
76 kLongString2 + "\",\"\",{\"a\":\"" + kLongString3 + "\"}]}",
77 json);
80 TEST(TraceEventArgumentTest, PassBaseValue) {
81 FundamentalValue int_value(42);
82 FundamentalValue bool_value(true);
83 FundamentalValue double_value(42.0f);
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");
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());
98 scoped_refptr<TracedValue> value = new TracedValue();
99 value->BeginDictionary("outer_dict");
100 value->SetValue("inner_list", list_value.Pass());
101 value->EndDictionary();
103 dict_value.reset();
104 list_value.reset();
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);
114 TEST(TraceEventArgumentTest, PassTracedValue) {
115 auto dict_value = make_scoped_refptr(new TracedValue);
116 dict_value->SetInteger("a", 1);
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();
124 dict_value->SetValue("e", *nested_dict_value);
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);
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);
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);
145 } // namespace trace_event
146 } // namespace base