OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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. |
| 4 |
| 5 #include "base/debug/trace_event_value.h" |
| 6 #include "base/debug/trace_event_object.h" |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/values.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace base { |
| 12 namespace debug { |
| 13 |
| 14 // Sample test class to show how we can use the TracedObject interface to |
| 15 // provide a standard way of defining how to push objects of whatever type |
| 16 // into a TracedValue. |
| 17 class TestTracedObject : public TracedObject { |
| 18 public: |
| 19 TestTracedObject() {} |
| 20 virtual ~TestTracedObject() {} |
| 21 |
| 22 virtual void PushInto(TracedValue* traced_value) const OVERRIDE { |
| 23 traced_value->BeginArray(); |
| 24 traced_value->Push("TestTracedObjectJson"); |
| 25 traced_value->EndArray(); |
| 26 } |
| 27 |
| 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(TestTracedObject); |
| 30 }; |
| 31 |
| 32 static ListValue* CreateTestTracedObjectValue() { |
| 33 ListValue* traced_object_list_value = new ListValue(); |
| 34 traced_object_list_value->Append( |
| 35 base::Value::CreateStringValue("TestTracedObjectJson")); |
| 36 return traced_object_list_value; |
| 37 } |
| 38 |
| 39 TEST(TracedValue, AllSupportedTypesTest) { |
| 40 // Testing the simplest kind of array, pushing a TestTracedObject. |
| 41 // TracedValue array. |
| 42 TracedValue array_of_one; |
| 43 array_of_one.BeginArray(); |
| 44 array_of_one.Push(TestTracedObject()); |
| 45 array_of_one.EndArray(); |
| 46 std::string out_str; |
| 47 array_of_one.AppendAsTraceFormat(&out_str); |
| 48 |
| 49 // Expected array. |
| 50 ListValue* list_value = new ListValue(); |
| 51 list_value->Append(CreateTestTracedObjectValue()); |
| 52 std::string expected_list; |
| 53 base::JSONWriter::Write(list_value, &expected_list); |
| 54 delete list_value; |
| 55 |
| 56 EXPECT_EQ(expected_list, out_str); |
| 57 |
| 58 // Testing the simplest kind of dictionary, pushing a TestTracedObject. |
| 59 // TracedValue dictionary. |
| 60 TestTracedObject traced_object_value; |
| 61 const char* test_key = "string_key"; |
| 62 TracedValue dict_of_one; |
| 63 dict_of_one.BeginDictionary(); |
| 64 dict_of_one.Push(test_key, traced_object_value); |
| 65 dict_of_one.EndDictionary(); |
| 66 std::string dict_of_one_json; |
| 67 dict_of_one.AppendAsTraceFormat(&dict_of_one_json); |
| 68 |
| 69 // Expected dictionary. |
| 70 DictionaryValue* expected_dictionary = new DictionaryValue(); |
| 71 expected_dictionary->Set(test_key, CreateTestTracedObjectValue()); |
| 72 std::string expected_dict; |
| 73 base::JSONWriter::Write(expected_dictionary, &expected_dict); |
| 74 delete expected_dictionary; |
| 75 |
| 76 EXPECT_EQ(expected_dict, dict_of_one_json); |
| 77 |
| 78 // Testing an array that has all supported types. |
| 79 // TracedValue array. |
| 80 TracedValue array_of_all_types; |
| 81 array_of_all_types.BeginArray(); |
| 82 const char* static_str_value = "One\""; |
| 83 array_of_all_types.Push(static_str_value); |
| 84 std::string std_string_value("Two\""); |
| 85 array_of_all_types.Push(std_string_value); |
| 86 int int32_value = 3; |
| 87 array_of_all_types.Push(int32_value); |
| 88 int64 int64_value = 4; |
| 89 array_of_all_types.Push(int64_value); |
| 90 uint32 uint32_value = 5; |
| 91 array_of_all_types.Push(uint32_value); |
| 92 uint64 uint64_value = 6; |
| 93 array_of_all_types.Push(uint64_value); |
| 94 float float_value = 7.0; |
| 95 array_of_all_types.Push(float_value); |
| 96 bool bool_value = true; |
| 97 array_of_all_types.Push(bool_value); |
| 98 array_of_all_types.Push(traced_object_value); |
| 99 array_of_all_types.EndArray(); |
| 100 std::string array_of_all_types_json; |
| 101 array_of_all_types.AppendAsTraceFormat(&array_of_all_types_json); |
| 102 |
| 103 // Expected array. |
| 104 ListValue* expected_list_of_all_types = new ListValue(); |
| 105 expected_list_of_all_types->Append(new StringValue(static_str_value)); |
| 106 expected_list_of_all_types->Append(new StringValue(std_string_value)); |
| 107 expected_list_of_all_types->Append( |
| 108 base::Value::CreateIntegerValue(int32_value)); |
| 109 expected_list_of_all_types->Append( |
| 110 base::Value::CreateIntegerValue(int64_value)); |
| 111 expected_list_of_all_types->Append( |
| 112 base::Value::CreateIntegerValue(uint32_value)); |
| 113 expected_list_of_all_types->Append( |
| 114 base::Value::CreateIntegerValue(uint64_value)); |
| 115 expected_list_of_all_types->Append( |
| 116 base::Value::CreateDoubleValue(float_value)); |
| 117 expected_list_of_all_types->Append( |
| 118 base::Value::CreateBooleanValue(bool_value)); |
| 119 expected_list_of_all_types->Append(CreateTestTracedObjectValue()); |
| 120 std::string expected_list_str; |
| 121 base::JSONWriter::Write(expected_list_of_all_types, &expected_list_str); |
| 122 delete expected_list_of_all_types; |
| 123 |
| 124 EXPECT_EQ(expected_list_str, array_of_all_types_json); |
| 125 |
| 126 const char* const_char_ptr_value_key = "1_ConstCharPtrValue"; |
| 127 const char* std_string_value_key = "2_StdStringValue"; |
| 128 const char* int32_value_key = "3_Int32Value"; |
| 129 const char* int64_value_key = "4_Int64Value"; |
| 130 const char* uint32_value_key = "5_UInt32Value"; |
| 131 const char* uint64_value_key = "6_UInt64lValue"; |
| 132 const char* float_value_key = "7_FloatValue"; |
| 133 const char* bool_value_key = "8_BoolValue"; |
| 134 const char* traced_object_value_key = "9_TracedObjectValue"; |
| 135 |
| 136 // Testing a dictionary that has all supported types. |
| 137 // TracedValue dictionary. |
| 138 TracedValue dict_of_all_types; |
| 139 bool_value = false; |
| 140 dict_of_all_types.BeginDictionary(); |
| 141 dict_of_all_types.Push(const_char_ptr_value_key, static_str_value); |
| 142 dict_of_all_types.Push(std_string_value_key, std_string_value); |
| 143 dict_of_all_types.Push(int32_value_key, int32_value); |
| 144 dict_of_all_types.Push(int64_value_key, int64_value); |
| 145 dict_of_all_types.Push(uint32_value_key, uint32_value); |
| 146 dict_of_all_types.Push(uint64_value_key, uint64_value); |
| 147 dict_of_all_types.Push(float_value_key, float_value); |
| 148 dict_of_all_types.Push(bool_value_key, bool_value); |
| 149 dict_of_all_types.Push(traced_object_value_key, traced_object_value); |
| 150 dict_of_all_types.EndDictionary(); |
| 151 |
| 152 // Expected dictionary. |
| 153 DictionaryValue* expected_dict_of_all_types = new DictionaryValue(); |
| 154 expected_dict_of_all_types->Set(const_char_ptr_value_key, |
| 155 new StringValue(static_str_value)); |
| 156 expected_dict_of_all_types->Set(std_string_value_key, |
| 157 new StringValue(std_string_value)); |
| 158 expected_dict_of_all_types->Set(int32_value_key, |
| 159 base::Value::CreateIntegerValue(int32_value)); |
| 160 expected_dict_of_all_types->Set(int64_value_key, |
| 161 base::Value::CreateIntegerValue(int64_value)); |
| 162 expected_dict_of_all_types->Set(uint32_value_key, |
| 163 base::Value::CreateIntegerValue(uint32_value)); |
| 164 expected_dict_of_all_types->Set(uint64_value_key, |
| 165 base::Value::CreateIntegerValue(uint64_value)); |
| 166 expected_dict_of_all_types->Set(float_value_key, |
| 167 base::Value::CreateDoubleValue(float_value)); |
| 168 expected_dict_of_all_types->Set(bool_value_key, |
| 169 base::Value::CreateBooleanValue(bool_value)); |
| 170 expected_dict_of_all_types->Set(traced_object_value_key, |
| 171 CreateTestTracedObjectValue()); |
| 172 std::string expected_dict_str; |
| 173 base::JSONWriter::Write(expected_dict_of_all_types, &expected_dict_str); |
| 174 delete expected_dict_of_all_types; |
| 175 |
| 176 std::string dict_of_all_types_json; |
| 177 dict_of_all_types.AppendAsTraceFormat(&dict_of_all_types_json); |
| 178 EXPECT_EQ(expected_dict_str, dict_of_all_types_json); |
| 179 } |
| 180 |
| 181 TEST(TracedValue, ObjectWithNestedValuesTest) { |
| 182 // TracedValue dictionary. |
| 183 TracedValue composite_value; |
| 184 |
| 185 const char* inner_dictionary_key = "InnerDictionaryKey"; |
| 186 const char* first_inner_key = "1_InnerKey"; |
| 187 const char* second_inner_key = "2_InnerKey"; |
| 188 const char* third_inner_key = "3_InnerKeyEscapedQuote\""; |
| 189 const char* dictionary_inside_array_key = "DictInArrayKey"; |
| 190 |
| 191 // Begins main dict. |
| 192 composite_value.BeginDictionary(); |
| 193 // Begins inner dictionary. |
| 194 composite_value.PushDictionary(inner_dictionary_key); |
| 195 composite_value.Push(first_inner_key, "InnerVal"); |
| 196 composite_value.Push(second_inner_key, 12345); |
| 197 // Begins array in inner dictionary. |
| 198 composite_value.PushArray(third_inner_key); |
| 199 composite_value.Push("foo"); |
| 200 composite_value.Push("bar"); |
| 201 // Begin dictionary in the array. |
| 202 composite_value.BeginDictionary(); |
| 203 composite_value.Push(dictionary_inside_array_key, "DictInArrayVal"); |
| 204 // Ends dictionary in the array. |
| 205 composite_value.EndDictionary(); |
| 206 composite_value.Push(1); |
| 207 // Ens array in inner dictionary. |
| 208 composite_value.EndArray(); |
| 209 // Ends inner dictionary. |
| 210 composite_value.EndDictionary(); |
| 211 // Ends main dict. |
| 212 composite_value.EndDictionary(); |
| 213 std::string composite_value_json; |
| 214 composite_value.AppendAsTraceFormat(&composite_value_json); |
| 215 |
| 216 // Expected dictionary. |
| 217 DictionaryValue* expected_dictionary = new DictionaryValue(); |
| 218 DictionaryValue* inner_dict = new DictionaryValue(); |
| 219 inner_dict->Set(first_inner_key, new StringValue("InnerVal")); |
| 220 inner_dict->Set(second_inner_key, base::Value::CreateIntegerValue(12345)); |
| 221 ListValue* inner_array = new ListValue(); |
| 222 inner_array->Append(new StringValue("foo")); |
| 223 inner_array->Append(new StringValue("bar")); |
| 224 DictionaryValue* dict_in_array = new DictionaryValue(); |
| 225 dict_in_array->Set(dictionary_inside_array_key, |
| 226 new StringValue("DictInArrayVal")); |
| 227 inner_array->Append(dict_in_array); |
| 228 inner_array->Append(base::Value::CreateIntegerValue(1)); |
| 229 inner_dict->Set(third_inner_key, inner_array); |
| 230 expected_dictionary->Set(inner_dictionary_key, inner_dict); |
| 231 std::string expected_json; |
| 232 base::JSONWriter::Write(expected_dictionary, &expected_json); |
| 233 delete expected_dictionary; |
| 234 |
| 235 EXPECT_EQ(expected_json, composite_value_json); |
| 236 } |
| 237 } // namespace debug |
| 238 } // namespace base |
| 239 |
OLD | NEW |