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/trace_event_value.h" | |
6 #include "base/traced_object.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 | |
9 namespace base { | |
10 | |
11 // Sample test class to show how we can use the TracedObject interface to | |
12 // provide a standard way of defining how to push objects of whatever type | |
13 // into a TracedValue. | |
14 class TestTracedObject : public TracedObject { | |
15 public: | |
16 TestTracedObject() {} | |
17 virtual ~TestTracedObject() {} | |
18 | |
19 virtual void PushInto(TracedValue* traced_value) const OVERRIDE { | |
20 traced_value->BeginArray(); | |
21 traced_value->Push(STATIC_STR("TestTracedObjectJson")); | |
22 traced_value->EndArray(); | |
23 } | |
24 | |
25 private: | |
26 DISALLOW_COPY_AND_ASSIGN(TestTracedObject); | |
27 }; | |
28 | |
29 TEST(TracedValue, AllSupportedTypesTest) { | |
30 // Testing the simplest kind of array, pushing a TestTracedObject. | |
31 TracedValue array_of_one; | |
32 array_of_one.BeginArray(); | |
33 array_of_one.Push(TestTracedObject()); | |
34 array_of_one.EndArray(); | |
35 | |
36 std::string out_str; | |
37 array_of_one.AppendAsTraceFormat(&out_str); | |
38 EXPECT_EQ("{{\"TestTracedObjectJson\"}}", out_str); | |
dsinclair
2013/07/23 14:56:29
This should be [["TestTracedObjectJson"]]
rterrazas
2013/08/01 08:01:26
Done.
| |
39 | |
40 // Testing the simplest kind of dictionary, pushing a TestTracedObject. | |
41 TestTracedObject traced_object; | |
42 | |
43 TracedValue dict_of_one; | |
44 dict_of_one.BeginDictionary(); | |
45 dict_of_one.Push(STATIC_STR("string_key"), traced_object); | |
46 dict_of_one.EndDictionary(); | |
47 | |
48 std::string dict_of_one_json; | |
49 dict_of_one.AppendAsTraceFormat(&dict_of_one_json); | |
50 EXPECT_EQ("{\"string_key\": {\"TestTracedObjectJson\"}}", | |
51 dict_of_one_json); | |
52 | |
53 // Testing an array that has all supported primitive types. | |
54 TracedValue array_of_all_types; | |
55 array_of_all_types.BeginArray(); | |
56 array_of_all_types.Push(STATIC_STR("One")); | |
57 array_of_all_types.Push(std::string("Two")); | |
58 int third = 3; | |
59 array_of_all_types.Push(third); | |
60 int64 fourth = 4; | |
61 array_of_all_types.Push(fourth); | |
62 uint32 fifth = 5; | |
63 array_of_all_types.Push(fifth); | |
64 uint64 sixth = 6; | |
65 array_of_all_types.Push(sixth); | |
66 float seventh = 7.0; | |
67 array_of_all_types.Push(seventh); | |
68 array_of_all_types.Push(true); | |
69 array_of_all_types.Push(traced_object); | |
70 array_of_all_types.EndArray(); | |
71 | |
72 std::string array_of_all_types_json; | |
73 array_of_all_types.AppendAsTraceFormat(&array_of_all_types_json); | |
74 EXPECT_EQ("{\"One\", \"Two\", 3, 4, 5, 6, 7.000000, true, "\ | |
75 "{\"TestTracedObjectJson\"}}", array_of_all_types_json); | |
76 | |
77 TracedValue dict_of_all_types; | |
78 dict_of_all_types.BeginDictionary(); | |
79 dict_of_all_types.Push(STATIC_STR("StaticStrValue"), STATIC_STR("Value")); | |
80 dict_of_all_types.Push(STATIC_STR("StdStringValue"), std::string("Value")); | |
81 dict_of_all_types.Push(STATIC_STR("Int32Value"), third); | |
82 dict_of_all_types.Push(STATIC_STR("Int64Value"), fourth); | |
83 dict_of_all_types.Push(STATIC_STR("UInt32Value"), fifth); | |
84 dict_of_all_types.Push(STATIC_STR("UInt64Value"), sixth); | |
85 dict_of_all_types.Push(STATIC_STR("FloatValue"), seventh); | |
86 dict_of_all_types.Push(STATIC_STR("BoolValue"), false); | |
87 dict_of_all_types.Push(STATIC_STR("TracedObjectValue"), traced_object); | |
88 dict_of_all_types.EndDictionary(); | |
89 | |
90 std::string dict_of_all_types_json; | |
91 dict_of_all_types.AppendAsTraceFormat(&dict_of_all_types_json); | |
92 EXPECT_EQ("{\"StaticStrValue\": \"Value\", "\ | |
nduca
2013/07/23 04:54:04
one thing you could do to simplify your test is us
| |
93 "\"StdStringValue\": \"Value\", "\ | |
94 "\"Int32Value\": 3, "\ | |
95 "\"Int64Value\": 4, "\ | |
96 "\"UInt32Value\": 5, "\ | |
97 "\"UInt64Value\": 6, "\ | |
98 "\"FloatValue\": 7.000000, "\ | |
99 "\"BoolValue\": false, "\ | |
100 "\"TracedObjectValue\": {\"TestTracedObjectJson\"}}", | |
101 dict_of_all_types_json); | |
102 } | |
103 | |
104 TEST(TracedValue, ObjectWithNestedValuesTest) { | |
rterrazas
2013/07/21 22:23:49
Here I'm basically testing that the support for ne
| |
105 TracedValue composite_value; | |
106 // Begins main dict. | |
107 composite_value.BeginDictionary(); | |
108 // Begins inner dictionary. | |
109 composite_value.BeginDictionary(STATIC_STR("Inner dictionary")); | |
110 composite_value.Push(STATIC_STR("InnerKey1"), STATIC_STR("InnerVal")); | |
111 composite_value.Push(STATIC_STR("InnerKey2"), 12345); | |
112 // Begins array in inner dictionary. | |
113 composite_value.BeginArray(STATIC_STR("InnerArray")); | |
114 composite_value.Push(STATIC_STR("I can't believe this is not butter!")); | |
115 composite_value.Push(STATIC_STR("bar")); | |
116 // Begin dictionary in the array. | |
117 composite_value.BeginDictionary(); | |
118 composite_value.Push(STATIC_STR("DictInArrayKey"), | |
119 STATIC_STR("DictInArrayVal")); | |
120 // Ends dictionary in the array. | |
121 composite_value.EndDictionary(); | |
122 composite_value.Push(1); | |
123 // Ens array in inner dictionary. | |
124 composite_value.EndArray(); | |
125 // Ends inner dictionary. | |
126 composite_value.EndDictionary(); | |
127 // Ends main dict. | |
128 composite_value.EndDictionary(); | |
129 | |
130 std::string composite_value_json; | |
131 composite_value.AppendAsTraceFormat(&composite_value_json); | |
132 EXPECT_EQ("{\"Inner dictionary\": {"\ | |
133 "\"InnerKey1\": \"InnerVal\", "\ | |
134 "\"InnerKey2\": 12345, "\ | |
135 "\"InnerArray\": {"\ | |
136 "\"I can't believe this is not butter!\", "\ | |
137 "\"bar\", "\ | |
138 "{\"DictInArrayKey\": \"DictInArrayVal\"}, "\ | |
139 "1}}}", | |
140 composite_value_json); | |
141 } | |
142 } // Namespace base. | |
143 | |
OLD | NEW |