Chromium Code Reviews| Index: base/trace_event_value_unittest.cc |
| diff --git a/base/trace_event_value_unittest.cc b/base/trace_event_value_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9ae70a93cd8ad5bc7d6d7a18c717819493308468 |
| --- /dev/null |
| +++ b/base/trace_event_value_unittest.cc |
| @@ -0,0 +1,143 @@ |
| +// 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/trace_event_value.h" |
| +#include "base/traced_object.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace base { |
| + |
| + // 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(STATIC_STR("TestTracedObjectJson")); |
| + traced_value->EndArray(); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestTracedObject); |
| + }; |
| + |
| + TEST(TracedValue, AllSupportedTypesTest) { |
| + // Testing the simplest kind of array, pushing a TestTracedObject. |
| + 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); |
| + EXPECT_EQ("{{\"TestTracedObjectJson\"}}", out_str); |
|
dsinclair
2013/07/23 14:56:29
This should be [["TestTracedObjectJson"]]
rterrazas
2013/08/01 08:01:26
Done.
|
| + |
| + // Testing the simplest kind of dictionary, pushing a TestTracedObject. |
| + TestTracedObject traced_object; |
| + |
| + TracedValue dict_of_one; |
| + dict_of_one.BeginDictionary(); |
| + dict_of_one.Push(STATIC_STR("string_key"), traced_object); |
| + dict_of_one.EndDictionary(); |
| + |
| + std::string dict_of_one_json; |
| + dict_of_one.AppendAsTraceFormat(&dict_of_one_json); |
| + EXPECT_EQ("{\"string_key\": {\"TestTracedObjectJson\"}}", |
| + dict_of_one_json); |
| + |
| + // Testing an array that has all supported primitive types. |
| + TracedValue array_of_all_types; |
| + array_of_all_types.BeginArray(); |
| + array_of_all_types.Push(STATIC_STR("One")); |
| + array_of_all_types.Push(std::string("Two")); |
| + int third = 3; |
| + array_of_all_types.Push(third); |
| + int64 fourth = 4; |
| + array_of_all_types.Push(fourth); |
| + uint32 fifth = 5; |
| + array_of_all_types.Push(fifth); |
| + uint64 sixth = 6; |
| + array_of_all_types.Push(sixth); |
| + float seventh = 7.0; |
| + array_of_all_types.Push(seventh); |
| + array_of_all_types.Push(true); |
| + array_of_all_types.Push(traced_object); |
| + array_of_all_types.EndArray(); |
| + |
| + std::string array_of_all_types_json; |
| + array_of_all_types.AppendAsTraceFormat(&array_of_all_types_json); |
| + EXPECT_EQ("{\"One\", \"Two\", 3, 4, 5, 6, 7.000000, true, "\ |
| + "{\"TestTracedObjectJson\"}}", array_of_all_types_json); |
| + |
| + TracedValue dict_of_all_types; |
| + dict_of_all_types.BeginDictionary(); |
| + dict_of_all_types.Push(STATIC_STR("StaticStrValue"), STATIC_STR("Value")); |
| + dict_of_all_types.Push(STATIC_STR("StdStringValue"), std::string("Value")); |
| + dict_of_all_types.Push(STATIC_STR("Int32Value"), third); |
| + dict_of_all_types.Push(STATIC_STR("Int64Value"), fourth); |
| + dict_of_all_types.Push(STATIC_STR("UInt32Value"), fifth); |
| + dict_of_all_types.Push(STATIC_STR("UInt64Value"), sixth); |
| + dict_of_all_types.Push(STATIC_STR("FloatValue"), seventh); |
| + dict_of_all_types.Push(STATIC_STR("BoolValue"), false); |
| + dict_of_all_types.Push(STATIC_STR("TracedObjectValue"), traced_object); |
| + dict_of_all_types.EndDictionary(); |
| + |
| + std::string dict_of_all_types_json; |
| + dict_of_all_types.AppendAsTraceFormat(&dict_of_all_types_json); |
| + EXPECT_EQ("{\"StaticStrValue\": \"Value\", "\ |
|
nduca
2013/07/23 04:54:04
one thing you could do to simplify your test is us
|
| + "\"StdStringValue\": \"Value\", "\ |
| + "\"Int32Value\": 3, "\ |
| + "\"Int64Value\": 4, "\ |
| + "\"UInt32Value\": 5, "\ |
| + "\"UInt64Value\": 6, "\ |
| + "\"FloatValue\": 7.000000, "\ |
| + "\"BoolValue\": false, "\ |
| + "\"TracedObjectValue\": {\"TestTracedObjectJson\"}}", |
| + dict_of_all_types_json); |
| + } |
| + |
| + TEST(TracedValue, ObjectWithNestedValuesTest) { |
|
rterrazas
2013/07/21 22:23:49
Here I'm basically testing that the support for ne
|
| + TracedValue composite_value; |
| + // Begins main dict. |
| + composite_value.BeginDictionary(); |
| + // Begins inner dictionary. |
| + composite_value.BeginDictionary(STATIC_STR("Inner dictionary")); |
| + composite_value.Push(STATIC_STR("InnerKey1"), STATIC_STR("InnerVal")); |
| + composite_value.Push(STATIC_STR("InnerKey2"), 12345); |
| + // Begins array in inner dictionary. |
| + composite_value.BeginArray(STATIC_STR("InnerArray")); |
| + composite_value.Push(STATIC_STR("I can't believe this is not butter!")); |
| + composite_value.Push(STATIC_STR("bar")); |
| + // Begin dictionary in the array. |
| + composite_value.BeginDictionary(); |
| + composite_value.Push(STATIC_STR("DictInArrayKey"), |
| + STATIC_STR("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); |
| + EXPECT_EQ("{\"Inner dictionary\": {"\ |
| + "\"InnerKey1\": \"InnerVal\", "\ |
| + "\"InnerKey2\": 12345, "\ |
| + "\"InnerArray\": {"\ |
| + "\"I can't believe this is not butter!\", "\ |
| + "\"bar\", "\ |
| + "{\"DictInArrayKey\": \"DictInArrayVal\"}, "\ |
| + "1}}}", |
| + composite_value_json); |
| + } |
| +} // Namespace base. |
| + |