Chromium Code Reviews| 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/values.h" | 11 #include "base/values.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 namespace trace_event { | 15 namespace trace_event { |
| 16 | 16 |
| 17 TEST(TraceEventArgumentTest, FlatDictionary) { | 17 TEST(TraceEventArgumentTest, FlatDictionary) { |
| 18 scoped_refptr<TracedValue> value = new TracedValue(); | 18 scoped_ptr<TracedValue> value(new TracedValue()); |
| 19 value->SetInteger("int", 2014); | 19 value->SetInteger("int", 2014); |
| 20 value->SetDouble("double", 0.0); | 20 value->SetDouble("double", 0.0); |
| 21 value->SetBoolean("bool", true); | 21 value->SetBoolean("bool", true); |
| 22 value->SetString("string", "string"); | 22 value->SetString("string", "string"); |
| 23 std::string json = "PREFIX"; | 23 std::string json = "PREFIX"; |
| 24 value->AppendAsTraceFormat(&json); | 24 value->AppendAsTraceFormat(&json); |
| 25 EXPECT_EQ( | 25 EXPECT_EQ( |
| 26 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}", | 26 "PREFIX{\"bool\":true,\"double\":0.0,\"int\":2014,\"string\":\"string\"}", |
| 27 json); | 27 json); |
| 28 } | 28 } |
| 29 | 29 |
| 30 TEST(TraceEventArgumentTest, NoDotPathExpansion) { | 30 TEST(TraceEventArgumentTest, NoDotPathExpansion) { |
| 31 scoped_refptr<TracedValue> value = new TracedValue(); | 31 scoped_ptr<TracedValue> value(new TracedValue()); |
| 32 value->SetInteger("in.t", 2014); | 32 value->SetInteger("in.t", 2014); |
| 33 value->SetDouble("doub.le", 0.0); | 33 value->SetDouble("doub.le", 0.0); |
| 34 value->SetBoolean("bo.ol", true); | 34 value->SetBoolean("bo.ol", true); |
| 35 value->SetString("str.ing", "str.ing"); | 35 value->SetString("str.ing", "str.ing"); |
| 36 std::string json; | 36 std::string json; |
| 37 value->AppendAsTraceFormat(&json); | 37 value->AppendAsTraceFormat(&json); |
| 38 EXPECT_EQ( | 38 EXPECT_EQ( |
| 39 "{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}", | 39 "{\"bo.ol\":true,\"doub.le\":0.0,\"in.t\":2014,\"str.ing\":\"str.ing\"}", |
| 40 json); | 40 json); |
| 41 } | 41 } |
| 42 | 42 |
| 43 TEST(TraceEventArgumentTest, Hierarchy) { | 43 TEST(TraceEventArgumentTest, Hierarchy) { |
| 44 scoped_refptr<TracedValue> value = new TracedValue(); | 44 scoped_ptr<TracedValue> value(new TracedValue()); |
| 45 value->SetInteger("i0", 2014); | 45 value->SetInteger("i0", 2014); |
| 46 value->BeginDictionary("dict1"); | 46 value->BeginDictionary("dict1"); |
| 47 value->SetInteger("i1", 2014); | 47 value->SetInteger("i1", 2014); |
| 48 value->BeginDictionary("dict2"); | 48 value->BeginDictionary("dict2"); |
| 49 value->SetBoolean("b2", false); | 49 value->SetBoolean("b2", false); |
| 50 value->EndDictionary(); | 50 value->EndDictionary(); |
| 51 value->SetString("s1", "foo"); | 51 value->SetString("s1", "foo"); |
| 52 value->EndDictionary(); | 52 value->EndDictionary(); |
| 53 value->SetDouble("d0", 0.0); | 53 value->SetDouble("d0", 0.0); |
| 54 value->SetBoolean("b0", true); | 54 value->SetBoolean("b0", true); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 70 } | 70 } |
| 71 | 71 |
| 72 TEST(TraceEventArgumentTest, LongStrings) { | 72 TEST(TraceEventArgumentTest, LongStrings) { |
| 73 std::string kLongString = "supercalifragilisticexpialidocious"; | 73 std::string kLongString = "supercalifragilisticexpialidocious"; |
| 74 std::string kLongString2 = "0123456789012345678901234567890123456789"; | 74 std::string kLongString2 = "0123456789012345678901234567890123456789"; |
| 75 char kLongString3[4096]; | 75 char kLongString3[4096]; |
| 76 for (size_t i = 0; i < sizeof(kLongString3); ++i) | 76 for (size_t i = 0; i < sizeof(kLongString3); ++i) |
| 77 kLongString3[i] = 'a' + (i % 25); | 77 kLongString3[i] = 'a' + (i % 25); |
| 78 kLongString3[sizeof(kLongString3) - 1] = '\0'; | 78 kLongString3[sizeof(kLongString3) - 1] = '\0'; |
| 79 | 79 |
| 80 scoped_refptr<TracedValue> value = new TracedValue(); | 80 scoped_ptr<TracedValue> value(new TracedValue()); |
| 81 value->SetString("a", "short"); | 81 value->SetString("a", "short"); |
| 82 value->SetString("b", kLongString); | 82 value->SetString("b", kLongString); |
| 83 value->BeginArray("c"); | 83 value->BeginArray("c"); |
| 84 value->AppendString(kLongString2); | 84 value->AppendString(kLongString2); |
| 85 value->AppendString(""); | 85 value->AppendString(""); |
| 86 value->BeginDictionary(); | 86 value->BeginDictionary(); |
| 87 value->SetString("a", kLongString3); | 87 value->SetString("a", kLongString3); |
| 88 value->EndDictionary(); | 88 value->EndDictionary(); |
| 89 value->EndArray(); | 89 value->EndArray(); |
| 90 | 90 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 106 dict_value->SetDouble("double", 42.0f); | 106 dict_value->SetDouble("double", 42.0f); |
| 107 dict_value->SetString("string", std::string("a") + "b"); | 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 | 109 |
| 110 auto list_value = make_scoped_ptr(new ListValue); | 110 auto list_value = make_scoped_ptr(new ListValue); |
| 111 list_value->AppendBoolean(false); | 111 list_value->AppendBoolean(false); |
| 112 list_value->AppendInteger(1); | 112 list_value->AppendInteger(1); |
| 113 list_value->AppendString("in_list"); | 113 list_value->AppendString("in_list"); |
| 114 list_value->Append(std::move(dict_value)); | 114 list_value->Append(std::move(dict_value)); |
| 115 | 115 |
| 116 scoped_refptr<TracedValue> value = new TracedValue(); | 116 scoped_ptr<TracedValue> value(new TracedValue()); |
| 117 value->BeginDictionary("outer_dict"); | 117 value->BeginDictionary("outer_dict"); |
| 118 value->SetValue("inner_list", std::move(list_value)); | 118 value->SetValue("inner_list", std::move(list_value)); |
| 119 value->EndDictionary(); | 119 value->EndDictionary(); |
| 120 | 120 |
| 121 dict_value.reset(); | 121 dict_value.reset(); |
| 122 list_value.reset(); | 122 list_value.reset(); |
| 123 | 123 |
| 124 std::string json; | 124 std::string json; |
| 125 value->AppendAsTraceFormat(&json); | 125 value->AppendAsTraceFormat(&json); |
| 126 EXPECT_EQ( | 126 EXPECT_EQ( |
| 127 "{\"outer_dict\":{\"inner_list\":[false,1,\"in_list\",{\"bool\":true," | 127 "{\"outer_dict\":{\"inner_list\":[false,1,\"in_list\",{\"bool\":true," |
| 128 "\"double\":42.0,\"int\":42,\"string\":\"ab\"}]}}", | 128 "\"double\":42.0,\"int\":42,\"string\":\"ab\"}]}}", |
| 129 json); | 129 json); |
| 130 } | 130 } |
| 131 | 131 |
| 132 TEST(TraceEventArgumentTest, PassTracedValue) { | 132 TEST(TraceEventArgumentTest, PassTracedValue) { |
| 133 auto dict_value = make_scoped_refptr(new TracedValue); | 133 auto dict_value = make_scoped_ptr(new TracedValue); |
|
oystein (OOO til 10th of July)
2016/02/26 19:46:55
nit: inconsistent usage of 'new TracedValue' vs 'n
Primiano Tucci (use gerrit)
2016/02/26 22:52:50
Fixed (even if I think is a broader problem, shoul
| |
| 134 dict_value->SetInteger("a", 1); | 134 dict_value->SetInteger("a", 1); |
| 135 | 135 |
| 136 auto nested_dict_value = make_scoped_refptr(new TracedValue); | 136 auto nested_dict_value = make_scoped_ptr(new TracedValue); |
| 137 nested_dict_value->SetInteger("b", 2); | 137 nested_dict_value->SetInteger("b", 2); |
| 138 nested_dict_value->BeginArray("c"); | 138 nested_dict_value->BeginArray("c"); |
| 139 nested_dict_value->AppendString("foo"); | 139 nested_dict_value->AppendString("foo"); |
| 140 nested_dict_value->EndArray(); | 140 nested_dict_value->EndArray(); |
| 141 | 141 |
| 142 dict_value->SetValue("e", *nested_dict_value); | 142 dict_value->SetValue("e", *nested_dict_value); |
| 143 | 143 |
| 144 // Check the merged result. | 144 // Check the merged result. |
| 145 std::string json; | 145 std::string json; |
| 146 dict_value->AppendAsTraceFormat(&json); | 146 dict_value->AppendAsTraceFormat(&json); |
| 147 EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json); | 147 EXPECT_EQ("{\"a\":1,\"e\":{\"b\":2,\"c\":[\"foo\"]}}", json); |
| 148 | 148 |
| 149 // Check that the passed nestd dict was left unouthced. | 149 // Check that the passed nestd dict was left unouthced. |
| 150 json = ""; | 150 json = ""; |
| 151 nested_dict_value->AppendAsTraceFormat(&json); | 151 nested_dict_value->AppendAsTraceFormat(&json); |
| 152 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json); | 152 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"]}", json); |
| 153 | 153 |
| 154 // And that it is still usable. | 154 // And that it is still usable. |
| 155 nested_dict_value->SetInteger("f", 3); | 155 nested_dict_value->SetInteger("f", 3); |
| 156 nested_dict_value->BeginDictionary("g"); | 156 nested_dict_value->BeginDictionary("g"); |
| 157 nested_dict_value->EndDictionary(); | 157 nested_dict_value->EndDictionary(); |
| 158 json = ""; | 158 json = ""; |
| 159 nested_dict_value->AppendAsTraceFormat(&json); | 159 nested_dict_value->AppendAsTraceFormat(&json); |
| 160 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json); | 160 EXPECT_EQ("{\"b\":2,\"c\":[\"foo\"],\"f\":3,\"g\":{}}", json); |
| 161 } | 161 } |
| 162 | 162 |
| 163 } // namespace trace_event | 163 } // namespace trace_event |
| 164 } // namespace base | 164 } // namespace base |
| OLD | NEW |