1 // Copyright 2015 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.
5 #include "base/json/json_reader.h"
6 #include "base/json/json_writer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "content/public/browser/background_tracing_preemptive_config.h"
10 #include "content/public/browser/background_tracing_reactive_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class BackgroundTracingConfigTest
: public testing::Test
{};
17 scoped_ptr
<BackgroundTracingConfig
> ReadFromJSONString(
18 const std::string
& json_text
) {
19 scoped_ptr
<base::Value
> json_value(base::JSONReader::Read(json_text
));
21 base::DictionaryValue
* dict
= NULL
;
23 json_value
->GetAsDictionary(&dict
);
25 return content::BackgroundTracingConfig::FromDict(dict
);
28 scoped_ptr
<BackgroundTracingPreemptiveConfig
> ReadPreemptiveFromJSONString(
29 const std::string
& json_text
) {
30 return make_scoped_ptr(static_cast<BackgroundTracingPreemptiveConfig
*>(
31 ReadFromJSONString(json_text
).release()));
34 scoped_ptr
<BackgroundTracingReactiveConfig
> ReadReactiveFromJSONString(
35 const std::string
& json_text
) {
36 return make_scoped_ptr(static_cast<BackgroundTracingReactiveConfig
*>(
37 ReadFromJSONString(json_text
).release()));
40 std::string
ConfigToString(const BackgroundTracingConfig
* config
) {
41 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
43 BackgroundTracingConfig::IntoDict(config
, dict
.get());
46 if (base::JSONWriter::Write(*dict
.get(), &results
))
51 TEST_F(BackgroundTracingConfigTest
, ConfigFromInvalidString
) {
52 // Missing or invalid mode
53 EXPECT_FALSE(ReadFromJSONString("{}"));
54 EXPECT_FALSE(ReadFromJSONString("{\"mode\":\"invalid\"}"));
57 TEST_F(BackgroundTracingConfigTest
, PreemptiveConfigFromInvalidString
) {
58 // Missing or invalid category
59 EXPECT_FALSE(ReadFromJSONString("{\"mode\":\"preemptive\"}"));
60 EXPECT_FALSE(ReadFromJSONString(
61 "{\"mode\":\"preemptive\", \"category\": \"invalid\"}"));
63 // Missing or invalid configs
64 EXPECT_FALSE(ReadFromJSONString(
65 "{\"mode\":\"preemptive\", \"category\": \"benchmark\"}"));
66 EXPECT_FALSE(ReadFromJSONString(
67 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
68 "\"configs\": \"\"}"));
69 EXPECT_FALSE(ReadFromJSONString(
70 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
73 // Invalid config entries
74 EXPECT_FALSE(ReadFromJSONString(
75 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
76 "\"configs\": [{}]}"));
77 EXPECT_FALSE(ReadFromJSONString(
78 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
79 "\"configs\": [\"invalid\"]}"));
80 EXPECT_FALSE(ReadFromJSONString(
81 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
82 "\"configs\": [[]]}"));
83 EXPECT_FALSE(ReadFromJSONString(
84 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
85 "\"configs\": [{\"rule\": \"invalid\"}]}"));
87 // Missing or invalid keys for a named trigger.
88 EXPECT_FALSE(ReadFromJSONString(
89 "{\"mode\":\"preemptive\", \"category\": \"benchmark\","
90 "\"configs\": [{\"rule\": \"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\"}]}"));
93 TEST_F(BackgroundTracingConfigTest
, ReactiveConfigFromInvalidString
) {
94 // Missing or invalid configs
95 EXPECT_FALSE(ReadFromJSONString("{\"mode\":\"reactive\"}"));
97 ReadFromJSONString("{\"mode\":\"reactive\", \"configs\": \"invalid\"}"));
98 EXPECT_FALSE(ReadFromJSONString("{\"mode\":\"reactive\", \"configs\": {}}"));
100 // Invalid config entries
102 ReadFromJSONString("{\"mode\":\"reactive\", \"configs\": [{}]}"));
103 EXPECT_FALSE(ReadFromJSONString(
104 "{\"mode\":\"reactive\", \"configs\": [\"invalid\"]}"));
106 // Invalid tracing rule type
107 EXPECT_FALSE(ReadFromJSONString(
108 "{\"mode\":\"reactive\","
109 "\"configs\": [{\"rule\": []}]}"));
110 EXPECT_FALSE(ReadFromJSONString(
111 "{\"mode\":\"reactive\","
112 "\"configs\": [{\"rule\": \"\"}]}"));
113 EXPECT_FALSE(ReadFromJSONString(
114 "{\"mode\":\"reactive\","
115 "\"configs\": [{\"rule\": "
116 "\"trace_for_10s_or_trigger_or_full\"}]}"));
118 EXPECT_FALSE(ReadFromJSONString(
119 "{\"mode\":\"reactive\","
120 "\"configs\": [{\"rule\": "
121 "\"trace_for_10s_or_trigger_or_full\", \"category\": "
123 EXPECT_FALSE(ReadFromJSONString(
124 "{\"mode\":\"reactive\","
125 "\"configs\": [{\"rule\": "
126 "\"trace_for_10s_or_trigger_or_full\", \"category\": "
128 EXPECT_FALSE(ReadFromJSONString(
129 "{\"mode\":\"reactive\","
130 "\"configs\": [{\"rule\": "
131 "\"trace_for_10s_or_trigger_or_full\", \"category\": "
132 "\"benchmark\"}]}"));
134 EXPECT_FALSE(ReadFromJSONString(
135 "{\"mode\":\"reactive\","
136 "\"configs\": [{\"rule\": "
137 "\"trace_for_10s_or_trigger_or_full\", \"category\": "
138 "\"benchmark\", \"trigger_name\": []}]}"));
139 EXPECT_FALSE(ReadFromJSONString(
140 "{\"mode\":\"reactive\","
141 "\"configs\": [{\"rule\": "
142 "\"trace_for_10s_or_trigger_or_full\", \"category\": "
143 "\"benchmark\", \"trigger_name\": 0}]}"));
146 TEST_F(BackgroundTracingConfigTest
, PreemptiveConfigFromValidString
) {
147 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
;
149 config
= ReadPreemptiveFromJSONString(
150 "{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
151 "\"BENCHMARK\",\"configs\": []}");
153 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
);
154 EXPECT_EQ(config
->category_preset
, BackgroundTracingConfig::BENCHMARK
);
155 EXPECT_EQ(config
->configs
.size(), 0u);
157 config
= ReadPreemptiveFromJSONString(
158 "{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
159 "\"BENCHMARK_DEEP\",\"configs\": []}");
161 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
);
162 EXPECT_EQ(config
->category_preset
, BackgroundTracingConfig::BENCHMARK_DEEP
);
163 EXPECT_EQ(config
->configs
.size(), 0u);
165 config
= ReadPreemptiveFromJSONString(
166 "{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
167 "\"BENCHMARK\",\"configs\": [{\"rule\": "
168 "\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\", \"trigger_name\":\"foo\"}]}");
170 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
);
171 EXPECT_EQ(config
->category_preset
, BackgroundTracingConfig::BENCHMARK
);
172 EXPECT_EQ(config
->configs
.size(), 1u);
174 config
->configs
[0].type
,
175 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
);
176 EXPECT_EQ(config
->configs
[0].named_trigger_info
.trigger_name
, "foo");
178 config
= ReadPreemptiveFromJSONString(
179 "{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
180 "\"BENCHMARK\",\"configs\": [{\"rule\": "
181 "\"MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE\", "
182 "\"histogram_name\":\"foo\", \"histogram_value\": 1}]}");
184 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
);
185 EXPECT_EQ(config
->category_preset
, BackgroundTracingConfig::BENCHMARK
);
186 EXPECT_EQ(config
->configs
.size(), 1u);
187 EXPECT_EQ(config
->configs
[0].type
,
188 BackgroundTracingPreemptiveConfig::
189 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE
);
190 EXPECT_EQ(config
->configs
[0].histogram_trigger_info
.histogram_name
, "foo");
191 EXPECT_EQ(config
->configs
[0].histogram_trigger_info
.histogram_value
, 1);
193 config
= ReadPreemptiveFromJSONString(
194 "{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
195 "\"BENCHMARK\",\"configs\": [{\"rule\": "
196 "\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\", \"trigger_name\":\"foo1\"}, "
197 "{\"rule\": \"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\", "
198 "\"trigger_name\":\"foo2\"}]}");
200 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE
);
201 EXPECT_EQ(config
->category_preset
, BackgroundTracingConfig::BENCHMARK
);
202 EXPECT_EQ(config
->configs
.size(), 2u);
204 config
->configs
[0].type
,
205 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
);
206 EXPECT_EQ(config
->configs
[0].named_trigger_info
.trigger_name
, "foo1");
208 config
->configs
[1].type
,
209 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
);
210 EXPECT_EQ(config
->configs
[1].named_trigger_info
.trigger_name
, "foo2");
213 TEST_F(BackgroundTracingConfigTest
, ReactiveConfigFromValidString
) {
214 scoped_ptr
<BackgroundTracingReactiveConfig
> config
;
216 config
= ReadReactiveFromJSONString(
217 "{\"mode\":\"REACTIVE_TRACING_MODE\", \"configs\": []}");
219 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::REACTIVE_TRACING_MODE
);
220 EXPECT_EQ(config
->configs
.size(), 0u);
222 config
= ReadReactiveFromJSONString(
223 "{\"mode\":\"REACTIVE_TRACING_MODE\",\"configs\": [{\"rule\": "
224 "\"TRACE_FOR_10S_OR_TRIGGER_OR_FULL\", "
225 "\"category\": \"BENCHMARK\", \"trigger_name\": \"foo\"}]}");
227 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::REACTIVE_TRACING_MODE
);
228 EXPECT_EQ(config
->configs
.size(), 1u);
229 EXPECT_EQ(config
->configs
[0].type
,
230 BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
);
231 EXPECT_EQ(config
->configs
[0].trigger_name
, "foo");
232 EXPECT_EQ(config
->configs
[0].category_preset
,
233 BackgroundTracingConfig::BENCHMARK
);
235 config
= ReadReactiveFromJSONString(
236 "{\"mode\":\"REACTIVE_TRACING_MODE\",\"configs\": [{\"rule\": "
237 "\"TRACE_FOR_10S_OR_TRIGGER_OR_FULL\", "
238 "\"category\": \"BENCHMARK_DEEP\", \"trigger_name\": \"foo\"}]}");
240 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::REACTIVE_TRACING_MODE
);
241 EXPECT_EQ(config
->configs
.size(), 1u);
242 EXPECT_EQ(config
->configs
[0].type
,
243 BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
);
244 EXPECT_EQ(config
->configs
[0].trigger_name
, "foo");
245 EXPECT_EQ(config
->configs
[0].category_preset
,
246 BackgroundTracingConfig::BENCHMARK_DEEP
);
248 config
= ReadReactiveFromJSONString(
249 "{\"mode\":\"REACTIVE_TRACING_MODE\",\"configs\": [{\"rule\": "
250 "\"TRACE_FOR_10S_OR_TRIGGER_OR_FULL\", "
251 "\"category\": \"BENCHMARK_DEEP\", \"trigger_name\": "
252 "\"foo1\"},{\"rule\": "
253 "\"TRACE_FOR_10S_OR_TRIGGER_OR_FULL\", "
254 "\"category\": \"BENCHMARK_DEEP\", \"trigger_name\": \"foo2\"}]}");
256 EXPECT_EQ(config
->mode
, BackgroundTracingConfig::REACTIVE_TRACING_MODE
);
257 EXPECT_EQ(config
->configs
.size(), 2u);
258 EXPECT_EQ(config
->configs
[0].type
,
259 BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
);
260 EXPECT_EQ(config
->configs
[0].trigger_name
, "foo1");
261 EXPECT_EQ(config
->configs
[0].category_preset
,
262 BackgroundTracingConfig::BENCHMARK_DEEP
);
263 EXPECT_EQ(config
->configs
[1].type
,
264 BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
);
265 EXPECT_EQ(config
->configs
[1].trigger_name
, "foo2");
266 EXPECT_EQ(config
->configs
[1].category_preset
,
267 BackgroundTracingConfig::BENCHMARK_DEEP
);
270 TEST_F(BackgroundTracingConfigTest
, ValidPreemptiveConfigToString
) {
271 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config(
272 new BackgroundTracingPreemptiveConfig());
275 EXPECT_EQ(ConfigToString(config
.get()),
276 "{\"category\":\"BENCHMARK\",\"configs\":[],\"mode\":\"PREEMPTIVE_"
279 // Change category_preset
280 config
->category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
281 EXPECT_EQ(ConfigToString(config
.get()),
282 "{\"category\":\"BENCHMARK_DEEP\",\"configs\":[],\"mode\":"
283 "\"PREEMPTIVE_TRACING_MODE\"}");
286 config
.reset(new BackgroundTracingPreemptiveConfig());
287 config
->category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
289 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
291 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
;
292 rule
.named_trigger_info
.trigger_name
= "foo";
293 config
->configs
.push_back(rule
);
294 EXPECT_EQ(ConfigToString(config
.get()),
295 "{\"category\":\"BENCHMARK_DEEP\",\"configs\":[{\"rule\":"
296 "\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\",\"trigger_name\":"
297 "\"foo\"}],\"mode\":\"PREEMPTIVE_TRACING_MODE\"}");
301 config
.reset(new BackgroundTracingPreemptiveConfig());
302 config
->category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
304 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
306 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
;
307 rule
.named_trigger_info
.trigger_name
= "foo1";
308 config
->configs
.push_back(rule
);
309 rule
.named_trigger_info
.trigger_name
= "foo2";
310 config
->configs
.push_back(rule
);
311 EXPECT_EQ(ConfigToString(config
.get()),
312 "{\"category\":\"BENCHMARK_DEEP\",\"configs\":[{\"rule\":"
313 "\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\",\"trigger_name\":"
314 "\"foo1\"},{\"rule\":\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\","
315 "\"trigger_name\":\"foo2\"}],\"mode\":\"PREEMPTIVE_TRACING_"
320 config
.reset(new BackgroundTracingPreemptiveConfig());
322 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
323 rule
.type
= BackgroundTracingPreemptiveConfig::
324 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE
;
325 rule
.histogram_trigger_info
.histogram_name
= "foo";
326 rule
.histogram_trigger_info
.histogram_value
= 1;
327 config
->configs
.push_back(rule
);
328 EXPECT_EQ(ConfigToString(config
.get()),
329 "{\"category\":\"BENCHMARK\",\"configs\":[{\"histogram_name\":"
330 "\"foo\",\"histogram_value\":1,\"rule\":\"MONITOR_AND_DUMP_WHEN_"
331 "SPECIFIC_HISTOGRAM_AND_VALUE\"}],\"mode\":\"PREEMPTIVE_TRACING_"
336 TEST_F(BackgroundTracingConfigTest
, InvalidPreemptiveConfigToString
) {
337 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
;
340 config
.reset(new BackgroundTracingPreemptiveConfig());
342 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
343 rule
.type
= BackgroundTracingPreemptiveConfig::
344 MONITOR_AND_DUMP_WHEN_BROWSER_STARTUP_COMPLETE
;
345 config
->configs
.push_back(rule
);
346 EXPECT_EQ(ConfigToString(config
.get()),
347 "{\"category\":\"BENCHMARK\",\"configs\":[],\"mode\":"
348 "\"PREEMPTIVE_TRACING_MODE\"}");
352 TEST_F(BackgroundTracingConfigTest
, ValidReactiveConfigToString
) {
353 scoped_ptr
<BackgroundTracingReactiveConfig
> config(
354 new BackgroundTracingReactiveConfig());
357 EXPECT_EQ(ConfigToString(config
.get()),
358 "{\"configs\":[],\"mode\":\"REACTIVE_TRACING_MODE\"}");
361 config
.reset(new BackgroundTracingReactiveConfig());
363 BackgroundTracingReactiveConfig::TracingRule rule
;
364 rule
.type
= BackgroundTracingReactiveConfig::
365 TRACE_FOR_10S_OR_TRIGGER_OR_FULL
;
366 rule
.trigger_name
= "foo";
367 rule
.category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
368 config
->configs
.push_back(rule
);
369 EXPECT_EQ(ConfigToString(config
.get()),
370 "{\"configs\":[{\"category\":\"BENCHMARK_DEEP\",\"rule\":\"TRACE_"
371 "FOR_10S_OR_TRIGGER_OR_FULL\",\"trigger_name\":\"foo\"}],\"mode\""
372 ":\"REACTIVE_TRACING_MODE\"}");
376 config
.reset(new BackgroundTracingReactiveConfig());
378 BackgroundTracingReactiveConfig::TracingRule rule
;
379 rule
.type
= BackgroundTracingReactiveConfig::
380 TRACE_FOR_10S_OR_TRIGGER_OR_FULL
;
381 rule
.trigger_name
= "foo1";
382 rule
.category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
383 config
->configs
.push_back(rule
);
384 rule
.trigger_name
= "foo2";
385 config
->configs
.push_back(rule
);
386 EXPECT_EQ(ConfigToString(config
.get()),
387 "{\"configs\":[{\"category\":\"BENCHMARK_DEEP\",\"rule\":\"TRACE_"
388 "FOR_10S_OR_TRIGGER_OR_FULL\",\"trigger_name\":\"foo1\"},{"
389 "\"category\":\"BENCHMARK_DEEP\",\"rule\":\"TRACE_FOR_10S_OR_"
390 "TRIGGER_OR_FULL\",\"trigger_name\":\"foo2\"}],\"mode\":"
391 "\"REACTIVE_TRACING_MODE\"}");
395 } // namspace content