| 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 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 namespace trace_event { | 16 namespace trace_event { |
| 16 | 17 |
| 17 TEST(TraceEventArgumentTest, FlatDictionary) { | 18 TEST(TraceEventArgumentTest, FlatDictionary) { |
| 18 scoped_ptr<TracedValue> value(new TracedValue()); | 19 std::unique_ptr<TracedValue> value(new TracedValue()); |
| 19 value->SetInteger("int", 2014); | 20 value->SetInteger("int", 2014); |
| 20 value->SetDouble("double", 0.0); | 21 value->SetDouble("double", 0.0); |
| 21 value->SetBoolean("bool", true); | 22 value->SetBoolean("bool", true); |
| 22 value->SetString("string", "string"); | 23 value->SetString("string", "string"); |
| 23 std::string json = "PREFIX"; | 24 std::string json = "PREFIX"; |
| 24 value->AppendAsTraceFormat(&json); | 25 value->AppendAsTraceFormat(&json); |
| 25 EXPECT_EQ( | 26 EXPECT_EQ( |
| 26 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}", | 27 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}", |
| 27 json); | 28 json); |
| 28 } | 29 } |
| 29 | 30 |
| 30 TEST(TraceEventArgumentTest, NoDotPathExpansion) { | 31 TEST(TraceEventArgumentTest, NoDotPathExpansion) { |
| 31 scoped_ptr<TracedValue> value(new TracedValue()); | 32 std::unique_ptr<TracedValue> value(new TracedValue()); |
| 32 value->SetInteger("in.t", 2014); | 33 value->SetInteger("in.t", 2014); |
| 33 value->SetDouble("doub.le", 0.0); | 34 value->SetDouble("doub.le", 0.0); |
| 34 value->SetBoolean("bo.ol", true); | 35 value->SetBoolean("bo.ol", true); |
| 35 value->SetString("str.ing", "str.ing"); | 36 value->SetString("str.ing", "str.ing"); |
| 36 std::string json; | 37 std::string json; |
| 37 value->AppendAsTraceFormat(&json); | 38 value->AppendAsTraceFormat(&json); |
| 38 EXPECT_EQ( | 39 EXPECT_EQ( |
| 39 "{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}", | 40 "{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}", |
| 40 json); | 41 json); |
| 41 } | 42 } |
| 42 | 43 |
| 43 TEST(TraceEventArgumentTest, Hierarchy) { | 44 TEST(TraceEventArgumentTest, Hierarchy) { |
| 44 scoped_ptr<TracedValue> value(new TracedValue()); | 45 std::unique_ptr<TracedValue> value(new TracedValue()); |
| 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 15 matching lines...) Expand all Loading... |
| 70 } | 71 } |
| 71 | 72 |
| 72 TEST(TraceEventArgumentTest, LongStrings) { | 73 TEST(TraceEventArgumentTest, LongStrings) { |
| 73 std::string kLongString = "supercalifragilisticexpialidocious"; | 74 std::string kLongString = "supercalifragilisticexpialidocious"; |
| 74 std::string kLongString2 = "0123456789012345678901234567890123456789"; | 75 std::string kLongString2 = "0123456789012345678901234567890123456789"; |
| 75 char kLongString3[4096]; | 76 char kLongString3[4096]; |
| 76 for (size_t i = 0; i < sizeof(kLongString3); ++i) | 77 for (size_t i = 0; i < sizeof(kLongString3); ++i) |
| 77 kLongString3[i] = 'a' + (i % 25); | 78 kLongString3[i] = 'a' + (i % 25); |
| 78 kLongString3[sizeof(kLongString3) - 1] = '\0'; | 79 kLongString3[sizeof(kLongString3) - 1] = '\0'; |
| 79 | 80 |
| 80 scoped_ptr<TracedValue> value(new TracedValue()); | 81 std::unique_ptr<TracedValue> value(new TracedValue()); |
| 81 value->SetString("a", "short"); | 82 value->SetString("a", "short"); |
| 82 value->SetString("b", kLongString); | 83 value->SetString("b", kLongString); |
| 83 value->BeginArray("c"); | 84 value->BeginArray("c"); |
| 84 value->AppendString(kLongString2); | 85 value->AppendString(kLongString2); |
| 85 value->AppendString(""); | 86 value->AppendString(""); |
| 86 value->BeginDictionary(); | 87 value->BeginDictionary(); |
| 87 value->SetString("a", kLongString3); | 88 value->SetString("a", kLongString3); |
| 88 value->EndDictionary(); | 89 value->EndDictionary(); |
| 89 value->EndArray(); | 90 value->EndArray(); |
| 90 | 91 |
| 91 std::string json; | 92 std::string json; |
| 92 value->AppendAsTraceFormat(&json); | 93 value->AppendAsTraceFormat(&json); |
| 93 EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString + "\",\"c\":[\"" + | 94 EXPECT_EQ("{\"a\":\"short\",\"b\":\"" + kLongString + "\",\"c\":[\"" + |
| 94 kLongString2 + "\",\"\",{\"a\":\"" + kLongString3 + "\"}]}", | 95 kLongString2 + "\",\"\",{\"a\":\"" + kLongString3 + "\"}]}", |
| 95 json); | 96 json); |
| 96 } | 97 } |
| 97 | 98 |
| 98 TEST(TraceEventArgumentTest, PassBaseValue) { | 99 TEST(TraceEventArgumentTest, PassBaseValue) { |
| 99 FundamentalValue int_value(42); | 100 FundamentalValue int_value(42); |
| 100 FundamentalValue bool_value(true); | 101 FundamentalValue bool_value(true); |
| 101 FundamentalValue double_value(42.0f); | 102 FundamentalValue double_value(42.0f); |
| 102 | 103 |
| 103 auto dict_value = make_scoped_ptr(new DictionaryValue); | 104 auto dict_value = base::WrapUnique(new DictionaryValue); |
| 104 dict_value->SetBoolean("bool", true); | 105 dict_value->SetBoolean("bool", true); |
| 105 dict_value->SetInteger("int", 42); | 106 dict_value->SetInteger("int", 42); |
| 106 dict_value->SetDouble("double", 42.0f); | 107 dict_value->SetDouble("double", 42.0f); |
| 107 dict_value->SetString("string", std::string("a") + "b"); | 108 dict_value->SetString("string", std::string("a") + "b"); |
| 108 dict_value->SetString("string", std::string("a") + "b"); | 109 dict_value->SetString("string", std::string("a") + "b"); |
| 109 | 110 |
| 110 auto list_value = make_scoped_ptr(new ListValue); | 111 auto list_value = base::WrapUnique(new ListValue); |
| 111 list_value->AppendBoolean(false); | 112 list_value->AppendBoolean(false); |
| 112 list_value->AppendInteger(1); | 113 list_value->AppendInteger(1); |
| 113 list_value->AppendString("in_list"); | 114 list_value->AppendString("in_list"); |
| 114 list_value->Append(std::move(dict_value)); | 115 list_value->Append(std::move(dict_value)); |
| 115 | 116 |
| 116 scoped_ptr<TracedValue> value(new TracedValue()); | 117 std::unique_ptr<TracedValue> value(new TracedValue()); |
| 117 value->BeginDictionary("outer_dict"); | 118 value->BeginDictionary("outer_dict"); |
| 118 value->SetValue("inner_list", std::move(list_value)); | 119 value->SetValue("inner_list", std::move(list_value)); |
| 119 value->EndDictionary(); | 120 value->EndDictionary(); |
| 120 | 121 |
| 121 dict_value.reset(); | 122 dict_value.reset(); |
| 122 list_value.reset(); | 123 list_value.reset(); |
| 123 | 124 |
| 124 std::string json; | 125 std::string json; |
| 125 value->AppendAsTraceFormat(&json); | 126 value->AppendAsTraceFormat(&json); |
| 126 EXPECT_EQ( | 127 EXPECT_EQ( |
| 127 "{\"outer_dict\":{\"inner_list\":[false,1,\"in_list\",{\"bool\":true," | 128 "{\"outer_dict\":{\"inner_list\":[false,1,\"in_list\",{\"bool\":true," |
| 128 "\"double\":42.0,\"int\":42,\"string\":\"ab\"}]}}", | 129 "\"double\":42.0,\"int\":42,\"string\":\"ab\"}]}}", |
| 129 json); | 130 json); |
| 130 } | 131 } |
| 131 | 132 |
| 132 TEST(TraceEventArgumentTest, PassTracedValue) { | 133 TEST(TraceEventArgumentTest, PassTracedValue) { |
| 133 auto dict_value = make_scoped_ptr(new TracedValue()); | 134 auto dict_value = base::WrapUnique(new TracedValue()); |
| 134 dict_value->SetInteger("a", 1); | 135 dict_value->SetInteger("a", 1); |
| 135 | 136 |
| 136 auto nested_dict_value = make_scoped_ptr(new TracedValue()); | 137 auto nested_dict_value = base::WrapUnique(new TracedValue()); |
| 137 nested_dict_value->SetInteger("b", 2); | 138 nested_dict_value->SetInteger("b", 2); |
| 138 nested_dict_value->BeginArray("c"); | 139 nested_dict_value->BeginArray("c"); |
| 139 nested_dict_value->AppendString("foo"); | 140 nested_dict_value->AppendString("foo"); |
| 140 nested_dict_value->EndArray(); | 141 nested_dict_value->EndArray(); |
| 141 | 142 |
| 142 dict_value->SetValue("e", *nested_dict_value); | 143 dict_value->SetValue("e", *nested_dict_value); |
| 143 | 144 |
| 144 // Check the merged result. | 145 // Check the merged result. |
| 145 std::string json; | 146 std::string json; |
| 146 dict_value->AppendAsTraceFormat(&json); | 147 dict_value->AppendAsTraceFormat(&json); |
| 147 EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json); | 148 EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json); |
| 148 | 149 |
| 149 // Check that the passed nestd dict was left unouthced. | 150 // Check that the passed nestd dict was left unouthced. |
| 150 json = ""; | 151 json = ""; |
| 151 nested_dict_value->AppendAsTraceFormat(&json); | 152 nested_dict_value->AppendAsTraceFormat(&json); |
| 152 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json); | 153 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json); |
| 153 | 154 |
| 154 // And that it is still usable. | 155 // And that it is still usable. |
| 155 nested_dict_value->SetInteger("f", 3); | 156 nested_dict_value->SetInteger("f", 3); |
| 156 nested_dict_value->BeginDictionary("g"); | 157 nested_dict_value->BeginDictionary("g"); |
| 157 nested_dict_value->EndDictionary(); | 158 nested_dict_value->EndDictionary(); |
| 158 json = ""; | 159 json = ""; |
| 159 nested_dict_value->AppendAsTraceFormat(&json); | 160 nested_dict_value->AppendAsTraceFormat(&json); |
| 160 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json); | 161 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json); |
| 161 } | 162 } |
| 162 | 163 |
| 163 } // namespace trace_event | 164 } // namespace trace_event |
| 164 } // namespace base | 165 } // namespace base |
| OLD | NEW |