• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
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/trace_conversion_helper.h"
6 
7 #include <stddef.h>
8 
9 #include <utility>
10 
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 namespace trace_event {
17 
TEST(TraceEventArgumentTest,SetTracedValueArgParameterPack)18 TEST(TraceEventArgumentTest, SetTracedValueArgParameterPack) {
19   std::unique_ptr<TracedValue> value(new TracedValue());
20   TracedValue* raw_value = value.get();
21   SetTracedValueArg(raw_value, "not_traced");
22   SetTracedValueArg(raw_value, "single_value", 42);
23   SetTracedValueArg(raw_value, "pack", 42, false, 0.0, "hello");
24   std::string json;
25   value->AppendAsTraceFormat(&json);
26   EXPECT_EQ(
27       "{"
28       "\"single_value\":42,"
29       "\"pack\":42,"
30       "\"pack\":false,"
31       "\"pack\":0.0,"
32       "\"pack\":\"hello\""
33       "}",
34       json);
35 }
36 
TEST(TraceEventArgumentTest,SetTracedValueArgCompatibleTypes)37 TEST(TraceEventArgumentTest, SetTracedValueArgCompatibleTypes) {
38   std::unique_ptr<TracedValue> value(new TracedValue());
39   TracedValue* raw_value = value.get();
40   SetTracedValueArg(raw_value, "float_literal", 0.0f);
41   float my_float = 0.0f;
42   SetTracedValueArg(raw_value, "my_float", my_float);
43   char my_char = 13;
44   SetTracedValueArg(raw_value, "my_char", my_char);
45   std::string json;
46   value->AppendAsTraceFormat(&json);
47   EXPECT_EQ(
48       "{"
49       "\"float_literal\":0.0,"
50       "\"my_float\":0.0,"
51       "\"my_char\":13"
52       "}",
53       json);
54 }
55 
56 class UseFallback {};
57 
TEST(TraceEventArgumentTest,SetTracedValueArgBasicTypes)58 TEST(TraceEventArgumentTest, SetTracedValueArgBasicTypes) {
59   std::unique_ptr<TracedValue> value(new TracedValue());
60   TracedValue* raw_value = value.get();
61   SetTracedValueArg(raw_value, "my_int", 1);
62   SetTracedValueArg(raw_value, "my_double", 0.1);
63   SetTracedValueArg(raw_value, "my_bool", false);
64   SetTracedValueArg(raw_value, "my_literal", "hello");
65   SetTracedValueArg(raw_value, "my_string",
66                     std::string("wonderful_") + std::string("world"));
67   SetTracedValueArg(raw_value, "my_void_ptr", static_cast<void*>(nullptr));
68   SetTracedValueArg(raw_value, "use_fallback", UseFallback());
69   UseFallback usefallback_with_variable;
70   SetTracedValueArg(raw_value, "use_fallback", usefallback_with_variable);
71   std::string json;
72   value->AppendAsTraceFormat(&json);
73   EXPECT_EQ(
74       "{\"my_int\":1,"
75       "\"my_double\":0.1,"
76       "\"my_bool\":false,"
77       "\"my_literal\":\"hello\","
78       "\"my_string\":\"wonderful_world\","
79       "\"my_void_ptr\":\"0x0\","
80       "\"use_fallback\":\"\\u003Cvalue>\","
81       "\"use_fallback\":\"\\u003Cvalue>\""
82       "}",
83       json);
84 }
85 
TEST(TraceEventConversionHelperTest,OstreamValueToString)86 TEST(TraceEventConversionHelperTest, OstreamValueToString) {
87   std::string zero = internal::OstreamValueToString(0);
88   EXPECT_EQ("0", zero);
89 }
90 
TEST(TraceEventConversionHelperTest,UseFallback)91 TEST(TraceEventConversionHelperTest, UseFallback) {
92   std::string answer = ValueToString(UseFallback(), "fallback");
93   EXPECT_EQ("fallback", answer);
94 }
95 
96 // std::ostream::operator<<
TEST(TraceEventConversionHelperTest,StdOstream)97 TEST(TraceEventConversionHelperTest, StdOstream) {
98   const char* literal = "hello literal";
99   EXPECT_EQ(literal, ValueToString(literal));
100   std::string str = "hello std::string";
101   EXPECT_EQ(str, ValueToString(str));
102   EXPECT_EQ("1", ValueToString(true));
103 }
104 
105 // base::NumberToString
TEST(TraceEventConversionHelperTest,Number)106 TEST(TraceEventConversionHelperTest, Number) {
107   EXPECT_EQ("3.14159", ValueToString(3.14159));
108   EXPECT_EQ("0", ValueToString(0.f));
109   EXPECT_EQ("42", ValueToString(42));
110 }
111 
112 class UseToString {
113  public:
ToString() const114   std::string ToString() const { return "UseToString::ToString"; }
115 };
116 
TEST(TraceEventConversionHelperTest,UseToString)117 TEST(TraceEventConversionHelperTest, UseToString) {
118   std::string answer = ValueToString(UseToString());
119   EXPECT_EQ("UseToString::ToString", answer);
120 }
121 
122 class UseFallbackNonConstTostring {
123  public:
ToString()124   std::string ToString() { return "don't return me, not const"; }
125 };
126 
TEST(TraceEventConversionHelperTest,UseFallbackNonConstToString)127 TEST(TraceEventConversionHelperTest, UseFallbackNonConstToString) {
128   std::string answer = ValueToString(UseFallbackNonConstTostring(), "fallback");
129   EXPECT_EQ("fallback", answer);
130 }
131 
132 class ConfusingToStringAPI {
133  public:
ToString() const134   ConfusingToStringAPI ToString() const { return ConfusingToStringAPI(); }
135 };
136 
TEST(TraceEventConversionHelperTest,ConfusingToStringAPI)137 TEST(TraceEventConversionHelperTest, ConfusingToStringAPI) {
138   std::string answer = ValueToString(ConfusingToStringAPI(), "fallback");
139   EXPECT_EQ("fallback", answer);
140 }
141 
142 // std::ostream::operator<<
TEST(TraceEventConversionHelperTest,UseOstreamOperator)143 TEST(TraceEventConversionHelperTest, UseOstreamOperator) {
144   // Test that the output is the same as when calling OstreamValueToString.
145   // Different platforms may represent the pointer differently, thus we don't
146   // compare with a value.
147   EXPECT_EQ(internal::OstreamValueToString((void*)0x123),
148             ValueToString((void*)0x123));
149 }
150 
151 class UseOperatorLessLess {};
152 
operator <<(std::ostream & os,const UseOperatorLessLess &)153 std::ostream& operator<<(std::ostream& os, const UseOperatorLessLess&) {
154   os << "UseOperatorLessLess";
155   return os;
156 }
157 
TEST(TraceEventConversionHelperTest,UseOperatorLessLess)158 TEST(TraceEventConversionHelperTest, UseOperatorLessLess) {
159   std::string answer = ValueToString(UseOperatorLessLess());
160   EXPECT_EQ("UseOperatorLessLess", answer);
161 }
162 
163 class HasBoth {
164  public:
ToString() const165   std::string ToString() const { return "HasBoth::ToString"; }
166 };
167 
operator <<(std::ostream & os,const HasBoth &)168 std::ostream& operator<<(std::ostream& os, const HasBoth&) {
169   os << "HasBoth::OperatorLessLess";
170   return os;
171 }
172 
TEST(TraceEventConversionHelperTest,HasBoth)173 TEST(TraceEventConversionHelperTest, HasBoth) {
174   std::string answer = ValueToString(HasBoth());
175   EXPECT_EQ("HasBoth::ToString", answer);
176 }
177 
178 class HasData {
179  public:
data() const180   const char* data() const { return "HasData"; }
181 };
182 
TEST(TraceEventConversionHelperTest,HasData)183 TEST(TraceEventConversionHelperTest, HasData) {
184   std::string answer = ValueToString(HasData());
185   EXPECT_EQ("HasData", answer);
186 }
187 
188 class HasNonConstData {
189  public:
data()190   const char* data() { return "HasNonConstData"; }
191 };
192 
TEST(TraceEventConversionHelperTest,HasNonConstData)193 TEST(TraceEventConversionHelperTest, HasNonConstData) {
194   std::string answer = ValueToString(HasNonConstData(), "fallback");
195   EXPECT_EQ("fallback", answer);
196 }
197 
198 class HasDataOfWrongType {
199  public:
data()200   void data() {}
201 };
202 
TEST(TraceEventConversionHelperTest,HasDataOfWrongType)203 TEST(TraceEventConversionHelperTest, HasDataOfWrongType) {
204   std::string answer = ValueToString(HasDataOfWrongType(), "fallback");
205   EXPECT_EQ("fallback", answer);
206 }
207 
208 }  // namespace trace_event
209 }  // namespace base
210