Remove APIPermission::kFileSystemWriteDirectory
[chromium-blink-merge.git] / base / trace_event / trace_config_unittest.cc
blobfdfb8803f48102cd7feddcfee0e9446f36965786
1 // Copyright (c) 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/trace_event/memory_dump_manager.h"
6 #include "base/trace_event/trace_config.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace base {
10 namespace trace_event {
12 namespace {
14 const char kDefaultTraceConfigString[] =
15 "{"
16 "\"enable_argument_filter\":false,"
17 "\"enable_sampling\":false,"
18 "\"enable_systrace\":false,"
19 "\"excluded_categories\":[\"*Debug\",\"*Test\"],"
20 "\"record_mode\":\"record-until-full\""
21 "}";
23 const char kMemoryDumpTraceConfigString[] =
24 "{"
25 "\"enable_argument_filter\":false,"
26 "\"enable_sampling\":false,"
27 "\"enable_systrace\":false,"
28 "\"included_categories\":["
29 "\"disabled-by-default-memory-infra\""
30 "],"
31 "\"memory_dump_config\":{"
32 "\"triggers\":["
33 "{"
34 "\"mode\":\"light\","
35 "\"periodic_interval_ms\":200"
36 "},"
37 "{"
38 "\"mode\":\"detailed\","
39 "\"periodic_interval_ms\":2000"
40 "}"
41 "]"
42 "},"
43 "\"record_mode\":\"record-until-full\""
44 "}";
46 const char kTraceConfigStringWithEmptyTriggers[] =
47 "{"
48 "\"enable_argument_filter\":false,"
49 "\"enable_sampling\":false,"
50 "\"enable_systrace\":false,"
51 "\"included_categories\":["
52 "\"disabled-by-default-memory-infra\""
53 "],"
54 "\"memory_dump_config\":{"
55 "\"triggers\":["
56 "]"
57 "},"
58 "\"record_mode\":\"record-until-full\""
59 "}";
61 } // namespace
63 TEST(TraceConfigTest, TraceConfigFromValidLegacyFormat) {
64 // From trace options strings
65 TraceConfig config("", "record-until-full");
66 EXPECT_EQ(RECORD_UNTIL_FULL, config.GetTraceRecordMode());
67 EXPECT_FALSE(config.IsSamplingEnabled());
68 EXPECT_FALSE(config.IsSystraceEnabled());
69 EXPECT_FALSE(config.IsArgumentFilterEnabled());
70 EXPECT_STREQ("record-until-full", config.ToTraceOptionsString().c_str());
72 config = TraceConfig("", "record-continuously");
73 EXPECT_EQ(RECORD_CONTINUOUSLY, config.GetTraceRecordMode());
74 EXPECT_FALSE(config.IsSamplingEnabled());
75 EXPECT_FALSE(config.IsSystraceEnabled());
76 EXPECT_FALSE(config.IsArgumentFilterEnabled());
77 EXPECT_STREQ("record-continuously", config.ToTraceOptionsString().c_str());
79 config = TraceConfig("", "trace-to-console");
80 EXPECT_EQ(ECHO_TO_CONSOLE, config.GetTraceRecordMode());
81 EXPECT_FALSE(config.IsSamplingEnabled());
82 EXPECT_FALSE(config.IsSystraceEnabled());
83 EXPECT_FALSE(config.IsArgumentFilterEnabled());
84 EXPECT_STREQ("trace-to-console", config.ToTraceOptionsString().c_str());
86 config = TraceConfig("", "record-as-much-as-possible");
87 EXPECT_EQ(RECORD_AS_MUCH_AS_POSSIBLE, config.GetTraceRecordMode());
88 EXPECT_FALSE(config.IsSamplingEnabled());
89 EXPECT_FALSE(config.IsSystraceEnabled());
90 EXPECT_FALSE(config.IsArgumentFilterEnabled());
91 EXPECT_STREQ("record-as-much-as-possible",
92 config.ToTraceOptionsString().c_str());
94 config = TraceConfig("", "record-until-full, enable-sampling");
95 EXPECT_EQ(RECORD_UNTIL_FULL, config.GetTraceRecordMode());
96 EXPECT_TRUE(config.IsSamplingEnabled());
97 EXPECT_FALSE(config.IsSystraceEnabled());
98 EXPECT_FALSE(config.IsArgumentFilterEnabled());
99 EXPECT_STREQ("record-until-full,enable-sampling",
100 config.ToTraceOptionsString().c_str());
102 config = TraceConfig("", "enable-systrace, record-continuously");
103 EXPECT_EQ(RECORD_CONTINUOUSLY, config.GetTraceRecordMode());
104 EXPECT_FALSE(config.IsSamplingEnabled());
105 EXPECT_TRUE(config.IsSystraceEnabled());
106 EXPECT_FALSE(config.IsArgumentFilterEnabled());
107 EXPECT_STREQ("record-continuously,enable-systrace",
108 config.ToTraceOptionsString().c_str());
110 config = TraceConfig("", "enable-argument-filter,record-as-much-as-possible");
111 EXPECT_EQ(RECORD_AS_MUCH_AS_POSSIBLE, config.GetTraceRecordMode());
112 EXPECT_FALSE(config.IsSamplingEnabled());
113 EXPECT_FALSE(config.IsSystraceEnabled());
114 EXPECT_TRUE(config.IsArgumentFilterEnabled());
115 EXPECT_STREQ("record-as-much-as-possible,enable-argument-filter",
116 config.ToTraceOptionsString().c_str());
118 config = TraceConfig(
120 "enable-systrace,trace-to-console,enable-sampling,enable-argument-filter");
121 EXPECT_EQ(ECHO_TO_CONSOLE, config.GetTraceRecordMode());
122 EXPECT_TRUE(config.IsSamplingEnabled());
123 EXPECT_TRUE(config.IsSystraceEnabled());
124 EXPECT_TRUE(config.IsArgumentFilterEnabled());
125 EXPECT_STREQ(
126 "trace-to-console,enable-sampling,enable-systrace,enable-argument-filter",
127 config.ToTraceOptionsString().c_str());
129 config = TraceConfig(
130 "", "record-continuously, record-until-full, trace-to-console");
131 EXPECT_EQ(ECHO_TO_CONSOLE, config.GetTraceRecordMode());
132 EXPECT_FALSE(config.IsSamplingEnabled());
133 EXPECT_FALSE(config.IsSystraceEnabled());
134 EXPECT_FALSE(config.IsArgumentFilterEnabled());
135 EXPECT_STREQ("trace-to-console", config.ToTraceOptionsString().c_str());
137 // From TraceRecordMode
138 config = TraceConfig("", RECORD_UNTIL_FULL);
139 EXPECT_EQ(RECORD_UNTIL_FULL, config.GetTraceRecordMode());
140 EXPECT_FALSE(config.IsSamplingEnabled());
141 EXPECT_FALSE(config.IsSystraceEnabled());
142 EXPECT_FALSE(config.IsArgumentFilterEnabled());
143 EXPECT_STREQ("record-until-full", config.ToTraceOptionsString().c_str());
145 config = TraceConfig("", RECORD_CONTINUOUSLY);
146 EXPECT_EQ(RECORD_CONTINUOUSLY, config.GetTraceRecordMode());
147 EXPECT_FALSE(config.IsSamplingEnabled());
148 EXPECT_FALSE(config.IsSystraceEnabled());
149 EXPECT_FALSE(config.IsArgumentFilterEnabled());
150 EXPECT_STREQ("record-continuously", config.ToTraceOptionsString().c_str());
152 config = TraceConfig("", ECHO_TO_CONSOLE);
153 EXPECT_EQ(ECHO_TO_CONSOLE, config.GetTraceRecordMode());
154 EXPECT_FALSE(config.IsSamplingEnabled());
155 EXPECT_FALSE(config.IsSystraceEnabled());
156 EXPECT_FALSE(config.IsArgumentFilterEnabled());
157 EXPECT_STREQ("trace-to-console", config.ToTraceOptionsString().c_str());
159 config = TraceConfig("", RECORD_AS_MUCH_AS_POSSIBLE);
160 EXPECT_EQ(RECORD_AS_MUCH_AS_POSSIBLE, config.GetTraceRecordMode());
161 EXPECT_FALSE(config.IsSamplingEnabled());
162 EXPECT_FALSE(config.IsSystraceEnabled());
163 EXPECT_FALSE(config.IsArgumentFilterEnabled());
164 EXPECT_STREQ("record-as-much-as-possible",
165 config.ToTraceOptionsString().c_str());
167 // From category filter strings
168 config = TraceConfig("-*Debug,-*Test", "");
169 EXPECT_STREQ("-*Debug,-*Test", config.ToCategoryFilterString().c_str());
171 config = TraceConfig("included,-excluded,inc_pattern*,-exc_pattern*", "");
172 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
173 config.ToCategoryFilterString().c_str());
175 config = TraceConfig("only_inc_cat", "");
176 EXPECT_STREQ("only_inc_cat", config.ToCategoryFilterString().c_str());
178 config = TraceConfig("-only_exc_cat", "");
179 EXPECT_STREQ("-only_exc_cat", config.ToCategoryFilterString().c_str());
181 config = TraceConfig("disabled-by-default-cc,-excluded", "");
182 EXPECT_STREQ("disabled-by-default-cc,-excluded",
183 config.ToCategoryFilterString().c_str());
185 config = TraceConfig("disabled-by-default-cc,included", "");
186 EXPECT_STREQ("included,disabled-by-default-cc",
187 config.ToCategoryFilterString().c_str());
189 config = TraceConfig("DELAY(test.Delay1;16),included", "");
190 EXPECT_STREQ("included,DELAY(test.Delay1;16)",
191 config.ToCategoryFilterString().c_str());
193 // From both trace options and category filter strings
194 config = TraceConfig("", "");
195 EXPECT_EQ(RECORD_UNTIL_FULL, config.GetTraceRecordMode());
196 EXPECT_FALSE(config.IsSamplingEnabled());
197 EXPECT_FALSE(config.IsSystraceEnabled());
198 EXPECT_FALSE(config.IsArgumentFilterEnabled());
199 EXPECT_STREQ("", config.ToCategoryFilterString().c_str());
200 EXPECT_STREQ("record-until-full", config.ToTraceOptionsString().c_str());
202 config = TraceConfig("included,-excluded,inc_pattern*,-exc_pattern*",
203 "enable-systrace, trace-to-console, enable-sampling");
204 EXPECT_EQ(ECHO_TO_CONSOLE, config.GetTraceRecordMode());
205 EXPECT_TRUE(config.IsSamplingEnabled());
206 EXPECT_TRUE(config.IsSystraceEnabled());
207 EXPECT_FALSE(config.IsArgumentFilterEnabled());
208 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
209 config.ToCategoryFilterString().c_str());
210 EXPECT_STREQ("trace-to-console,enable-sampling,enable-systrace",
211 config.ToTraceOptionsString().c_str());
213 // From both trace options and category filter strings with spaces.
214 config = TraceConfig(" included , -excluded, inc_pattern*, ,-exc_pattern* ",
215 "enable-systrace, ,trace-to-console, enable-sampling ");
216 EXPECT_EQ(ECHO_TO_CONSOLE, config.GetTraceRecordMode());
217 EXPECT_TRUE(config.IsSamplingEnabled());
218 EXPECT_TRUE(config.IsSystraceEnabled());
219 EXPECT_FALSE(config.IsArgumentFilterEnabled());
220 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
221 config.ToCategoryFilterString().c_str());
222 EXPECT_STREQ("trace-to-console,enable-sampling,enable-systrace",
223 config.ToTraceOptionsString().c_str());
225 // From category filter string and TraceRecordMode
226 config = TraceConfig("included,-excluded,inc_pattern*,-exc_pattern*",
227 RECORD_CONTINUOUSLY);
228 EXPECT_EQ(RECORD_CONTINUOUSLY, config.GetTraceRecordMode());
229 EXPECT_FALSE(config.IsSystraceEnabled());
230 EXPECT_FALSE(config.IsSamplingEnabled());
231 EXPECT_FALSE(config.IsArgumentFilterEnabled());
232 EXPECT_STREQ("included,inc_pattern*,-excluded,-exc_pattern*",
233 config.ToCategoryFilterString().c_str());
234 EXPECT_STREQ("record-continuously", config.ToTraceOptionsString().c_str());
237 TEST(TraceConfigTest, TraceConfigFromInvalidLegacyStrings) {
238 TraceConfig config("", "foo-bar-baz");
239 EXPECT_EQ(RECORD_UNTIL_FULL, config.GetTraceRecordMode());
240 EXPECT_FALSE(config.IsSamplingEnabled());
241 EXPECT_FALSE(config.IsSystraceEnabled());
242 EXPECT_FALSE(config.IsArgumentFilterEnabled());
243 EXPECT_STREQ("", config.ToCategoryFilterString().c_str());
244 EXPECT_STREQ("record-until-full", config.ToTraceOptionsString().c_str());
246 config = TraceConfig("arbitrary-category", "foo-bar-baz, enable-systrace");
247 EXPECT_EQ(RECORD_UNTIL_FULL, config.GetTraceRecordMode());
248 EXPECT_FALSE(config.IsSamplingEnabled());
249 EXPECT_TRUE(config.IsSystraceEnabled());
250 EXPECT_FALSE(config.IsArgumentFilterEnabled());
251 EXPECT_STREQ("arbitrary-category", config.ToCategoryFilterString().c_str());
252 EXPECT_STREQ("record-until-full,enable-systrace",
253 config.ToTraceOptionsString().c_str());
255 const char* const configs[] = {
257 "DELAY(",
258 "DELAY(;",
259 "DELAY(;)",
260 "DELAY(test.Delay)",
261 "DELAY(test.Delay;)"
263 for (size_t i = 0; i < arraysize(configs); i++) {
264 TraceConfig tc(configs[i], "");
265 EXPECT_EQ(0u, tc.GetSyntheticDelayValues().size());
269 TEST(TraceConfigTest, ConstructDefaultTraceConfig) {
270 // Make sure that upon an empty string, we fall back to the default config.
271 TraceConfig tc;
272 EXPECT_STREQ(kDefaultTraceConfigString, tc.ToString().c_str());
273 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
274 EXPECT_FALSE(tc.IsSamplingEnabled());
275 EXPECT_FALSE(tc.IsSystraceEnabled());
276 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
277 EXPECT_STREQ("-*Debug,-*Test", tc.ToCategoryFilterString().c_str());
279 EXPECT_FALSE(tc.IsCategoryEnabled("Category1"));
280 EXPECT_FALSE(tc.IsCategoryEnabled("not-excluded-category"));
281 EXPECT_FALSE(tc.IsCategoryEnabled("CategoryTest"));
282 EXPECT_FALSE(tc.IsCategoryEnabled("CategoryDebug"));
283 EXPECT_FALSE(tc.IsCategoryEnabled("disabled-by-default-cc"));
285 EXPECT_TRUE(tc.IsCategoryGroupEnabled("Category1"));
286 EXPECT_TRUE(tc.IsCategoryGroupEnabled("not-excluded-category"));
287 EXPECT_FALSE(tc.IsCategoryGroupEnabled("CategoryTest"));
288 EXPECT_FALSE(tc.IsCategoryGroupEnabled("CategoryDebug"));
289 EXPECT_FALSE(tc.IsCategoryGroupEnabled("disabled-by-default-cc"));
291 EXPECT_TRUE(tc.IsCategoryGroupEnabled("Category1,CategoryDebug"));
292 EXPECT_TRUE(tc.IsCategoryGroupEnabled("CategoryDebug,Category1"));
293 EXPECT_TRUE(tc.IsCategoryGroupEnabled("CategoryTest,not-excluded-category"));
294 EXPECT_FALSE(tc.IsCategoryGroupEnabled("CategoryDebug,CategoryTest"));
297 TEST(TraceConfigTest, TraceConfigFromValidString) {
298 // Using some non-empty config string.
299 const char config_string[] =
301 "\"enable_argument_filter\":true,"
302 "\"enable_sampling\":true,"
303 "\"enable_systrace\":true,"
304 "\"excluded_categories\":[\"excluded\",\"exc_pattern*\"],"
305 "\"included_categories\":[\"included\","
306 "\"inc_pattern*\","
307 "\"disabled-by-default-cc\"],"
308 "\"record_mode\":\"record-continuously\","
309 "\"synthetic_delays\":[\"test.Delay1;16\",\"test.Delay2;32\"]"
310 "}";
311 TraceConfig tc(config_string);
313 EXPECT_STREQ(config_string, tc.ToString().c_str());
314 EXPECT_EQ(RECORD_CONTINUOUSLY, tc.GetTraceRecordMode());
315 EXPECT_TRUE(tc.IsSamplingEnabled());
316 EXPECT_TRUE(tc.IsSystraceEnabled());
317 EXPECT_TRUE(tc.IsArgumentFilterEnabled());
318 EXPECT_STREQ("included,inc_pattern*,disabled-by-default-cc,-excluded,"
319 "-exc_pattern*,DELAY(test.Delay1;16),DELAY(test.Delay2;32)",
320 tc.ToCategoryFilterString().c_str());
322 EXPECT_TRUE(tc.IsCategoryEnabled("included"));
323 EXPECT_TRUE(tc.IsCategoryEnabled("inc_pattern_category"));
324 EXPECT_TRUE(tc.IsCategoryEnabled("disabled-by-default-cc"));
325 EXPECT_FALSE(tc.IsCategoryEnabled("excluded"));
326 EXPECT_FALSE(tc.IsCategoryEnabled("exc_pattern_category"));
327 EXPECT_FALSE(tc.IsCategoryEnabled("disabled-by-default-others"));
328 EXPECT_FALSE(tc.IsCategoryEnabled("not-excluded-nor-included"));
330 EXPECT_TRUE(tc.IsCategoryGroupEnabled("included"));
331 EXPECT_TRUE(tc.IsCategoryGroupEnabled("inc_pattern_category"));
332 EXPECT_TRUE(tc.IsCategoryGroupEnabled("disabled-by-default-cc"));
333 EXPECT_FALSE(tc.IsCategoryGroupEnabled("excluded"));
334 EXPECT_FALSE(tc.IsCategoryGroupEnabled("exc_pattern_category"));
335 EXPECT_FALSE(tc.IsCategoryGroupEnabled("disabled-by-default-others"));
336 EXPECT_FALSE(tc.IsCategoryGroupEnabled("not-excluded-nor-included"));
338 EXPECT_TRUE(tc.IsCategoryGroupEnabled("included,excluded"));
339 EXPECT_FALSE(tc.IsCategoryGroupEnabled("excluded,exc_pattern_category"));
340 EXPECT_TRUE(tc.IsCategoryGroupEnabled("included,DELAY(test.Delay1;16)"));
341 EXPECT_FALSE(tc.IsCategoryGroupEnabled("DELAY(test.Delay1;16)"));
343 EXPECT_EQ(2u, tc.GetSyntheticDelayValues().size());
344 EXPECT_STREQ("test.Delay1;16", tc.GetSyntheticDelayValues()[0].c_str());
345 EXPECT_STREQ("test.Delay2;32", tc.GetSyntheticDelayValues()[1].c_str());
347 const char config_string_2[] = "{\"included_categories\":[\"*\"]}";
348 TraceConfig tc2(config_string_2);
349 EXPECT_TRUE(tc2.IsCategoryEnabled("non-disabled-by-default-pattern"));
350 EXPECT_FALSE(tc2.IsCategoryEnabled("disabled-by-default-pattern"));
351 EXPECT_TRUE(tc2.IsCategoryGroupEnabled("non-disabled-by-default-pattern"));
352 EXPECT_FALSE(tc2.IsCategoryGroupEnabled("disabled-by-default-pattern"));
354 // Clear
355 tc.Clear();
356 EXPECT_STREQ(tc.ToString().c_str(),
358 "\"enable_argument_filter\":false,"
359 "\"enable_sampling\":false,"
360 "\"enable_systrace\":false,"
361 "\"record_mode\":\"record-until-full\""
362 "}");
365 TEST(TraceConfigTest, TraceConfigFromInvalidString) {
366 // The config string needs to be a dictionary correctly formatted as a JSON
367 // string. Otherwise, it will fall back to the default initialization.
368 TraceConfig tc("");
369 EXPECT_STREQ(kDefaultTraceConfigString, tc.ToString().c_str());
370 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
371 EXPECT_FALSE(tc.IsSamplingEnabled());
372 EXPECT_FALSE(tc.IsSystraceEnabled());
373 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
374 EXPECT_STREQ("-*Debug,-*Test", tc.ToCategoryFilterString().c_str());
376 tc = TraceConfig("This is an invalid config string.");
377 EXPECT_STREQ(kDefaultTraceConfigString, tc.ToString().c_str());
378 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
379 EXPECT_FALSE(tc.IsSamplingEnabled());
380 EXPECT_FALSE(tc.IsSystraceEnabled());
381 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
382 EXPECT_STREQ("-*Debug,-*Test", tc.ToCategoryFilterString().c_str());
384 tc = TraceConfig("[\"This\", \"is\", \"not\", \"a\", \"dictionary\"]");
385 EXPECT_STREQ(kDefaultTraceConfigString, tc.ToString().c_str());
386 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
387 EXPECT_FALSE(tc.IsSamplingEnabled());
388 EXPECT_FALSE(tc.IsSystraceEnabled());
389 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
390 EXPECT_STREQ("-*Debug,-*Test", tc.ToCategoryFilterString().c_str());
392 tc = TraceConfig("{\"record_mode\": invalid-value-needs-double-quote}");
393 EXPECT_STREQ(kDefaultTraceConfigString, tc.ToString().c_str());
394 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
395 EXPECT_FALSE(tc.IsSamplingEnabled());
396 EXPECT_FALSE(tc.IsSystraceEnabled());
397 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
398 EXPECT_STREQ("-*Debug,-*Test", tc.ToCategoryFilterString().c_str());
400 // If the config string a dictionary formatted as a JSON string, it will
401 // initialize TraceConfig with best effort.
402 tc = TraceConfig("{}");
403 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
404 EXPECT_FALSE(tc.IsSamplingEnabled());
405 EXPECT_FALSE(tc.IsSystraceEnabled());
406 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
407 EXPECT_STREQ("", tc.ToCategoryFilterString().c_str());
409 tc = TraceConfig("{\"arbitrary-key\":\"arbitrary-value\"}");
410 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
411 EXPECT_FALSE(tc.IsSamplingEnabled());
412 EXPECT_FALSE(tc.IsSystraceEnabled());
413 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
414 EXPECT_STREQ("", tc.ToCategoryFilterString().c_str());
416 const char invalid_config_string[] =
418 "\"enable_sampling\":\"true\","
419 "\"enable_systrace\":1,"
420 "\"excluded_categories\":[\"excluded\"],"
421 "\"included_categories\":\"not a list\","
422 "\"record_mode\":\"arbitrary-mode\","
423 "\"synthetic_delays\":[\"test.Delay1;16\","
424 "\"invalid-delay\","
425 "\"test.Delay2;32\"]"
426 "}";
427 tc = TraceConfig(invalid_config_string);
428 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
429 EXPECT_FALSE(tc.IsSamplingEnabled());
430 EXPECT_FALSE(tc.IsSystraceEnabled());
431 EXPECT_FALSE(tc.IsArgumentFilterEnabled());
432 EXPECT_STREQ("-excluded,DELAY(test.Delay1;16),DELAY(test.Delay2;32)",
433 tc.ToCategoryFilterString().c_str());
435 const char invalid_config_string_2[] =
437 "\"included_categories\":[\"category\",\"disabled-by-default-pattern\"],"
438 "\"excluded_categories\":[\"category\",\"disabled-by-default-pattern\"]"
439 "}";
440 tc = TraceConfig(invalid_config_string_2);
441 EXPECT_TRUE(tc.IsCategoryEnabled("category"));
442 EXPECT_TRUE(tc.IsCategoryEnabled("disabled-by-default-pattern"));
443 EXPECT_TRUE(tc.IsCategoryGroupEnabled("category"));
444 EXPECT_TRUE(tc.IsCategoryGroupEnabled("disabled-by-default-pattern"));
447 TEST(TraceConfigTest, MergingTraceConfigs) {
448 // Merge
449 TraceConfig tc;
450 TraceConfig tc2("included,-excluded,inc_pattern*,-exc_pattern*", "");
451 tc.Merge(tc2);
452 EXPECT_STREQ("{"
453 "\"enable_argument_filter\":false,"
454 "\"enable_sampling\":false,"
455 "\"enable_systrace\":false,"
456 "\"excluded_categories\":["
457 "\"*Debug\",\"*Test\",\"excluded\",\"exc_pattern*\""
458 "],"
459 "\"record_mode\":\"record-until-full\""
460 "}",
461 tc.ToString().c_str());
463 tc = TraceConfig("DELAY(test.Delay1;16)", "");
464 tc2 = TraceConfig("DELAY(test.Delay2;32)", "");
465 tc.Merge(tc2);
466 EXPECT_EQ(2u, tc.GetSyntheticDelayValues().size());
467 EXPECT_STREQ("test.Delay1;16", tc.GetSyntheticDelayValues()[0].c_str());
468 EXPECT_STREQ("test.Delay2;32", tc.GetSyntheticDelayValues()[1].c_str());
471 TEST(TraceConfigTest, IsCategoryGroupEnabled) {
472 // Enabling a disabled- category does not require all categories to be traced
473 // to be included.
474 TraceConfig tc("disabled-by-default-cc,-excluded", "");
475 EXPECT_STREQ("disabled-by-default-cc,-excluded",
476 tc.ToCategoryFilterString().c_str());
477 EXPECT_TRUE(tc.IsCategoryGroupEnabled("disabled-by-default-cc"));
478 EXPECT_TRUE(tc.IsCategoryGroupEnabled("some_other_group"));
479 EXPECT_FALSE(tc.IsCategoryGroupEnabled("excluded"));
481 // Enabled a disabled- category and also including makes all categories to
482 // be traced require including.
483 tc = TraceConfig("disabled-by-default-cc,included", "");
484 EXPECT_STREQ("included,disabled-by-default-cc",
485 tc.ToCategoryFilterString().c_str());
486 EXPECT_TRUE(tc.IsCategoryGroupEnabled("disabled-by-default-cc"));
487 EXPECT_TRUE(tc.IsCategoryGroupEnabled("included"));
488 EXPECT_FALSE(tc.IsCategoryGroupEnabled("other_included"));
491 TEST(TraceConfigTest, IsEmptyOrContainsLeadingOrTrailingWhitespace) {
492 // Test that IsEmptyOrContainsLeadingOrTrailingWhitespace actually catches
493 // categories that are explicitly forbidden.
494 // This method is called in a DCHECK to assert that we don't have these types
495 // of strings as categories.
496 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
497 " bad_category "));
498 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
499 " bad_category"));
500 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
501 "bad_category "));
502 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
503 " bad_category"));
504 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
505 "bad_category "));
506 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
507 " bad_category "));
508 EXPECT_TRUE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
509 ""));
510 EXPECT_FALSE(TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
511 "good_category"));
514 TEST(TraceConfigTest, SetTraceOptionValues) {
515 TraceConfig tc;
516 EXPECT_EQ(RECORD_UNTIL_FULL, tc.GetTraceRecordMode());
517 EXPECT_FALSE(tc.IsSamplingEnabled());
518 EXPECT_FALSE(tc.IsSystraceEnabled());
520 tc.SetTraceRecordMode(RECORD_AS_MUCH_AS_POSSIBLE);
521 EXPECT_EQ(RECORD_AS_MUCH_AS_POSSIBLE, tc.GetTraceRecordMode());
523 tc.EnableSampling();
524 EXPECT_TRUE(tc.IsSamplingEnabled());
526 tc.EnableSystrace();
527 EXPECT_TRUE(tc.IsSystraceEnabled());
530 TEST(TraceConfigTest, TraceConfigFromMemoryConfigString) {
531 TraceConfig tc(kMemoryDumpTraceConfigString);
532 EXPECT_STREQ(kMemoryDumpTraceConfigString, tc.ToString().c_str());
533 EXPECT_TRUE(tc.IsCategoryGroupEnabled(MemoryDumpManager::kTraceCategory));
534 EXPECT_EQ(2u, tc.memory_dump_config_.size());
536 EXPECT_EQ(200u, tc.memory_dump_config_[0].periodic_interval_ms);
537 EXPECT_EQ(MemoryDumpArgs::LevelOfDetail::LOW,
538 tc.memory_dump_config_[0].level_of_detail);
540 EXPECT_EQ(2000u, tc.memory_dump_config_[1].periodic_interval_ms);
541 EXPECT_EQ(MemoryDumpArgs::LevelOfDetail::HIGH,
542 tc.memory_dump_config_[1].level_of_detail);
545 TEST(TraceConfigTest, EmptyMemoryDumpConfigTest) {
546 // Empty trigger list should also be specified when converting back to string.
547 TraceConfig tc(kTraceConfigStringWithEmptyTriggers);
548 EXPECT_STREQ(kTraceConfigStringWithEmptyTriggers, tc.ToString().c_str());
549 EXPECT_EQ(0u, tc.memory_dump_config_.size());
552 TEST(TraceConfigTest, LegacyStringToMemoryDumpConfig) {
553 TraceConfig tc(MemoryDumpManager::kTraceCategory, "");
554 EXPECT_TRUE(tc.IsCategoryGroupEnabled(MemoryDumpManager::kTraceCategory));
555 EXPECT_NE(std::string::npos, tc.ToString().find("memory_dump_config"));
556 EXPECT_EQ(2u, tc.memory_dump_config_.size());
559 } // namespace trace_event
560 } // namespace base