Index: base/debug/trace_event_value_unittest.cc |
diff --git a/base/debug/trace_event_value_unittest.cc b/base/debug/trace_event_value_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5df4539dca4092512086754fddc36443f0798be3 |
--- /dev/null |
+++ b/base/debug/trace_event_value_unittest.cc |
@@ -0,0 +1,239 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/debug/trace_event_value.h" |
+#include "base/debug/trace_event_object.h" |
+#include "base/json/json_writer.h" |
+#include "base/values.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+namespace debug { |
+ |
+ // Sample test class to show how we can use the TracedObject interface to |
+ // provide a standard way of defining how to push objects of whatever type |
+ // into a TracedValue. |
+ class TestTracedObject : public TracedObject { |
+ public: |
+ TestTracedObject() {} |
+ virtual ~TestTracedObject() {} |
+ |
+ virtual void PushInto(TracedValue* traced_value) const OVERRIDE { |
+ traced_value->BeginArray(); |
+ traced_value->Push("TestTracedObjectJson"); |
+ traced_value->EndArray(); |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestTracedObject); |
+ }; |
+ |
+ static ListValue* CreateTestTracedObjectValue() { |
+ ListValue* traced_object_list_value = new ListValue(); |
+ traced_object_list_value->Append( |
+ base::Value::CreateStringValue("TestTracedObjectJson")); |
+ return traced_object_list_value; |
+ } |
+ |
+ TEST(TracedValue, AllSupportedTypesTest) { |
+ // Testing the simplest kind of array, pushing a TestTracedObject. |
+ // TracedValue array. |
+ TracedValue array_of_one; |
+ array_of_one.BeginArray(); |
+ array_of_one.Push(TestTracedObject()); |
+ array_of_one.EndArray(); |
+ std::string out_str; |
+ array_of_one.AppendAsTraceFormat(&out_str); |
+ |
+ // Expected array. |
+ ListValue* list_value = new ListValue(); |
+ list_value->Append(CreateTestTracedObjectValue()); |
+ std::string expected_list; |
+ base::JSONWriter::Write(list_value, &expected_list); |
+ delete list_value; |
+ |
+ EXPECT_EQ(expected_list, out_str); |
+ |
+ // Testing the simplest kind of dictionary, pushing a TestTracedObject. |
+ // TracedValue dictionary. |
+ TestTracedObject traced_object_value; |
+ const char* test_key = "string_key"; |
+ TracedValue dict_of_one; |
+ dict_of_one.BeginDictionary(); |
+ dict_of_one.Push(test_key, traced_object_value); |
+ dict_of_one.EndDictionary(); |
+ std::string dict_of_one_json; |
+ dict_of_one.AppendAsTraceFormat(&dict_of_one_json); |
+ |
+ // Expected dictionary. |
+ DictionaryValue* expected_dictionary = new DictionaryValue(); |
+ expected_dictionary->Set(test_key, CreateTestTracedObjectValue()); |
+ std::string expected_dict; |
+ base::JSONWriter::Write(expected_dictionary, &expected_dict); |
+ delete expected_dictionary; |
+ |
+ EXPECT_EQ(expected_dict, dict_of_one_json); |
+ |
+ // Testing an array that has all supported types. |
+ // TracedValue array. |
+ TracedValue array_of_all_types; |
+ array_of_all_types.BeginArray(); |
+ const char* static_str_value = "One\""; |
+ array_of_all_types.Push(static_str_value); |
+ std::string std_string_value("Two\""); |
+ array_of_all_types.Push(std_string_value); |
+ int int32_value = 3; |
+ array_of_all_types.Push(int32_value); |
+ int64 int64_value = 4; |
+ array_of_all_types.Push(int64_value); |
+ uint32 uint32_value = 5; |
+ array_of_all_types.Push(uint32_value); |
+ uint64 uint64_value = 6; |
+ array_of_all_types.Push(uint64_value); |
+ float float_value = 7.0; |
+ array_of_all_types.Push(float_value); |
+ bool bool_value = true; |
+ array_of_all_types.Push(bool_value); |
+ array_of_all_types.Push(traced_object_value); |
+ array_of_all_types.EndArray(); |
+ std::string array_of_all_types_json; |
+ array_of_all_types.AppendAsTraceFormat(&array_of_all_types_json); |
+ |
+ // Expected array. |
+ ListValue* expected_list_of_all_types = new ListValue(); |
+ expected_list_of_all_types->Append(new StringValue(static_str_value)); |
+ expected_list_of_all_types->Append(new StringValue(std_string_value)); |
+ expected_list_of_all_types->Append( |
+ base::Value::CreateIntegerValue(int32_value)); |
+ expected_list_of_all_types->Append( |
+ base::Value::CreateIntegerValue(int64_value)); |
+ expected_list_of_all_types->Append( |
+ base::Value::CreateIntegerValue(uint32_value)); |
+ expected_list_of_all_types->Append( |
+ base::Value::CreateIntegerValue(uint64_value)); |
+ expected_list_of_all_types->Append( |
+ base::Value::CreateDoubleValue(float_value)); |
+ expected_list_of_all_types->Append( |
+ base::Value::CreateBooleanValue(bool_value)); |
+ expected_list_of_all_types->Append(CreateTestTracedObjectValue()); |
+ std::string expected_list_str; |
+ base::JSONWriter::Write(expected_list_of_all_types, &expected_list_str); |
+ delete expected_list_of_all_types; |
+ |
+ EXPECT_EQ(expected_list_str, array_of_all_types_json); |
+ |
+ const char* const_char_ptr_value_key = "1_ConstCharPtrValue"; |
+ const char* std_string_value_key = "2_StdStringValue"; |
+ const char* int32_value_key = "3_Int32Value"; |
+ const char* int64_value_key = "4_Int64Value"; |
+ const char* uint32_value_key = "5_UInt32Value"; |
+ const char* uint64_value_key = "6_UInt64lValue"; |
+ const char* float_value_key = "7_FloatValue"; |
+ const char* bool_value_key = "8_BoolValue"; |
+ const char* traced_object_value_key = "9_TracedObjectValue"; |
+ |
+ // Testing a dictionary that has all supported types. |
+ // TracedValue dictionary. |
+ TracedValue dict_of_all_types; |
+ bool_value = false; |
+ dict_of_all_types.BeginDictionary(); |
+ dict_of_all_types.Push(const_char_ptr_value_key, static_str_value); |
+ dict_of_all_types.Push(std_string_value_key, std_string_value); |
+ dict_of_all_types.Push(int32_value_key, int32_value); |
+ dict_of_all_types.Push(int64_value_key, int64_value); |
+ dict_of_all_types.Push(uint32_value_key, uint32_value); |
+ dict_of_all_types.Push(uint64_value_key, uint64_value); |
+ dict_of_all_types.Push(float_value_key, float_value); |
+ dict_of_all_types.Push(bool_value_key, bool_value); |
+ dict_of_all_types.Push(traced_object_value_key, traced_object_value); |
+ dict_of_all_types.EndDictionary(); |
+ |
+ // Expected dictionary. |
+ DictionaryValue* expected_dict_of_all_types = new DictionaryValue(); |
+ expected_dict_of_all_types->Set(const_char_ptr_value_key, |
+ new StringValue(static_str_value)); |
+ expected_dict_of_all_types->Set(std_string_value_key, |
+ new StringValue(std_string_value)); |
+ expected_dict_of_all_types->Set(int32_value_key, |
+ base::Value::CreateIntegerValue(int32_value)); |
+ expected_dict_of_all_types->Set(int64_value_key, |
+ base::Value::CreateIntegerValue(int64_value)); |
+ expected_dict_of_all_types->Set(uint32_value_key, |
+ base::Value::CreateIntegerValue(uint32_value)); |
+ expected_dict_of_all_types->Set(uint64_value_key, |
+ base::Value::CreateIntegerValue(uint64_value)); |
+ expected_dict_of_all_types->Set(float_value_key, |
+ base::Value::CreateDoubleValue(float_value)); |
+ expected_dict_of_all_types->Set(bool_value_key, |
+ base::Value::CreateBooleanValue(bool_value)); |
+ expected_dict_of_all_types->Set(traced_object_value_key, |
+ CreateTestTracedObjectValue()); |
+ std::string expected_dict_str; |
+ base::JSONWriter::Write(expected_dict_of_all_types, &expected_dict_str); |
+ delete expected_dict_of_all_types; |
+ |
+ std::string dict_of_all_types_json; |
+ dict_of_all_types.AppendAsTraceFormat(&dict_of_all_types_json); |
+ EXPECT_EQ(expected_dict_str, dict_of_all_types_json); |
+ } |
+ |
+ TEST(TracedValue, ObjectWithNestedValuesTest) { |
+ // TracedValue dictionary. |
+ TracedValue composite_value; |
+ |
+ const char* inner_dictionary_key = "InnerDictionaryKey"; |
+ const char* first_inner_key = "1_InnerKey"; |
+ const char* second_inner_key = "2_InnerKey"; |
+ const char* third_inner_key = "3_InnerKeyEscapedQuote\""; |
+ const char* dictionary_inside_array_key = "DictInArrayKey"; |
+ |
+ // Begins main dict. |
+ composite_value.BeginDictionary(); |
+ // Begins inner dictionary. |
+ composite_value.PushDictionary(inner_dictionary_key); |
+ composite_value.Push(first_inner_key, "InnerVal"); |
+ composite_value.Push(second_inner_key, 12345); |
+ // Begins array in inner dictionary. |
+ composite_value.PushArray(third_inner_key); |
+ composite_value.Push("foo"); |
+ composite_value.Push("bar"); |
+ // Begin dictionary in the array. |
+ composite_value.BeginDictionary(); |
+ composite_value.Push(dictionary_inside_array_key, "DictInArrayVal"); |
+ // Ends dictionary in the array. |
+ composite_value.EndDictionary(); |
+ composite_value.Push(1); |
+ // Ens array in inner dictionary. |
+ composite_value.EndArray(); |
+ // Ends inner dictionary. |
+ composite_value.EndDictionary(); |
+ // Ends main dict. |
+ composite_value.EndDictionary(); |
+ std::string composite_value_json; |
+ composite_value.AppendAsTraceFormat(&composite_value_json); |
+ |
+ // Expected dictionary. |
+ DictionaryValue* expected_dictionary = new DictionaryValue(); |
+ DictionaryValue* inner_dict = new DictionaryValue(); |
+ inner_dict->Set(first_inner_key, new StringValue("InnerVal")); |
+ inner_dict->Set(second_inner_key, base::Value::CreateIntegerValue(12345)); |
+ ListValue* inner_array = new ListValue(); |
+ inner_array->Append(new StringValue("foo")); |
+ inner_array->Append(new StringValue("bar")); |
+ DictionaryValue* dict_in_array = new DictionaryValue(); |
+ dict_in_array->Set(dictionary_inside_array_key, |
+ new StringValue("DictInArrayVal")); |
+ inner_array->Append(dict_in_array); |
+ inner_array->Append(base::Value::CreateIntegerValue(1)); |
+ inner_dict->Set(third_inner_key, inner_array); |
+ expected_dictionary->Set(inner_dictionary_key, inner_dict); |
+ std::string expected_json; |
+ base::JSONWriter::Write(expected_dictionary, &expected_json); |
+ delete expected_dictionary; |
+ |
+ EXPECT_EQ(expected_json, composite_value_json); |
+ } |
+} // namespace debug |
+} // namespace base |
+ |