[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / tracing / background_tracing_manager_browsertest.cc
blobb9d98806a0d413a5e0c60cccf63348595de393ed
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/bind.h"
6 #include "base/metrics/histogram_macros.h"
7 #include "base/strings/pattern.h"
8 #include "base/trace_event/trace_event.h"
9 #include "content/browser/tracing/background_tracing_rule.h"
10 #include "content/public/browser/background_tracing_manager.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/public/test/test_utils.h"
14 #include "third_party/zlib/zlib.h"
16 namespace content {
18 class BackgroundTracingManagerBrowserTest : public ContentBrowserTest {
19 public:
20 BackgroundTracingManagerBrowserTest() {}
22 private:
23 DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerBrowserTest);
26 class BackgroundTracingManagerUploadConfigWrapper {
27 public:
28 BackgroundTracingManagerUploadConfigWrapper(const base::Closure& callback)
29 : callback_(callback), receive_count_(0) {
30 receive_callback_ =
31 base::Bind(&BackgroundTracingManagerUploadConfigWrapper::Upload,
32 base::Unretained(this));
35 void Upload(const scoped_refptr<base::RefCountedString>& file_contents,
36 scoped_ptr<base::DictionaryValue> metadata,
37 base::Callback<void()> done_callback) {
38 receive_count_ += 1;
39 EXPECT_TRUE(file_contents);
41 size_t compressed_length = file_contents->data().length();
42 const size_t kOutputBufferLength = 10 * 1024 * 1024;
43 std::vector<char> output_str(kOutputBufferLength);
45 z_stream stream = {0};
46 stream.avail_in = compressed_length;
47 stream.avail_out = kOutputBufferLength;
48 stream.next_in = (Bytef*)&file_contents->data()[0];
49 stream.next_out = (Bytef*)vector_as_array(&output_str);
51 // 16 + MAX_WBITS means only decoding gzip encoded streams, and using
52 // the biggest window size, according to zlib.h
53 int result = inflateInit2(&stream, 16 + MAX_WBITS);
54 EXPECT_EQ(Z_OK, result);
55 result = inflate(&stream, Z_FINISH);
56 int bytes_written = kOutputBufferLength - stream.avail_out;
58 inflateEnd(&stream);
59 EXPECT_EQ(Z_STREAM_END, result);
61 last_file_contents_.assign(vector_as_array(&output_str), bytes_written);
62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
63 base::Bind(done_callback));
64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
65 base::Bind(callback_));
68 bool TraceHasMatchingString(const char* str) {
69 return last_file_contents_.find(str) != std::string::npos;
72 int get_receive_count() const { return receive_count_; }
74 const BackgroundTracingManager::ReceiveCallback& get_receive_callback()
75 const {
76 return receive_callback_;
79 private:
80 BackgroundTracingManager::ReceiveCallback receive_callback_;
81 base::Closure callback_;
82 int receive_count_;
83 std::string last_file_contents_;
86 void StartedFinalizingCallback(base::Closure callback,
87 bool expected,
88 bool value) {
89 EXPECT_EQ(expected, value);
90 if (!callback.is_null())
91 callback.Run();
94 scoped_ptr<BackgroundTracingConfig> CreatePreemptiveConfig() {
95 base::DictionaryValue dict;
97 dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
98 dict.SetString("category", "BENCHMARK");
100 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
102 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
103 rules_dict->SetString("rule", "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED");
104 rules_dict->SetString("trigger_name", "preemptive_test");
105 rules_list->Append(rules_dict.Pass());
107 dict.Set("configs", rules_list.Pass());
109 scoped_ptr<BackgroundTracingConfig> config(
110 BackgroundTracingConfigImpl::FromDict(&dict));
112 EXPECT_TRUE(config);
113 return config.Pass();
116 scoped_ptr<BackgroundTracingConfig> CreateReactiveConfig() {
117 base::DictionaryValue dict;
119 dict.SetString("mode", "REACTIVE_TRACING_MODE");
121 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
123 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
124 rules_dict->SetString("rule", "TRACE_ON_NAVIGATION_UNTIL_TRIGGER_OR_FULL");
125 rules_dict->SetString("trigger_name", "reactive_test");
126 rules_dict->SetString("category", "BENCHMARK");
127 rules_list->Append(rules_dict.Pass());
129 dict.Set("configs", rules_list.Pass());
131 scoped_ptr<BackgroundTracingConfig> config(
132 BackgroundTracingConfigImpl::FromDict(&dict));
134 EXPECT_TRUE(config);
135 return config.Pass();
138 void SetupBackgroundTracingManager() {
139 content::BackgroundTracingManager::GetInstance()
140 ->InvalidateTriggerHandlesForTesting();
143 void DisableScenarioWhenIdle() {
144 BackgroundTracingManager::GetInstance()->SetActiveScenario(
145 NULL, BackgroundTracingManager::ReceiveCallback(),
146 BackgroundTracingManager::NO_DATA_FILTERING);
149 // This tests that the endpoint receives the final trace data.
150 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
151 ReceiveTraceFinalContentsOnTrigger) {
153 SetupBackgroundTracingManager();
155 base::RunLoop run_loop;
156 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
157 run_loop.QuitClosure());
159 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
161 BackgroundTracingManager::TriggerHandle handle =
162 BackgroundTracingManager::
163 GetInstance()->RegisterTriggerType("preemptive_test");
165 BackgroundTracingManager::GetInstance()->SetActiveScenario(
166 config.Pass(), upload_config_wrapper.get_receive_callback(),
167 BackgroundTracingManager::NO_DATA_FILTERING);
169 BackgroundTracingManager::GetInstance()->WhenIdle(
170 base::Bind(&DisableScenarioWhenIdle));
172 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
173 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
175 run_loop.Run();
177 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
181 // This tests triggering more than once still only gathers once.
182 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
183 CallTriggersMoreThanOnceOnlyGatherOnce) {
185 SetupBackgroundTracingManager();
187 base::RunLoop run_loop;
188 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
189 run_loop.QuitClosure());
191 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
193 content::BackgroundTracingManager::TriggerHandle handle =
194 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
195 "preemptive_test");
197 BackgroundTracingManager::GetInstance()->SetActiveScenario(
198 config.Pass(), upload_config_wrapper.get_receive_callback(),
199 BackgroundTracingManager::NO_DATA_FILTERING);
201 BackgroundTracingManager::GetInstance()->WhenIdle(
202 base::Bind(&DisableScenarioWhenIdle));
204 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
205 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
206 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
207 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), false));
209 run_loop.Run();
211 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
215 namespace {
217 bool IsTraceEventArgsWhitelisted(const char* category_group_name,
218 const char* event_name) {
219 if (base::MatchPattern(category_group_name, "benchmark") &&
220 base::MatchPattern(event_name, "whitelisted")) {
221 return true;
224 return false;
227 } // namespace
229 // This tests that non-whitelisted args get stripped if required.
230 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
231 NoWhitelistedArgsStripped) {
232 SetupBackgroundTracingManager();
234 base::trace_event::TraceLog::GetInstance()->SetArgumentFilterPredicate(
235 base::Bind(&IsTraceEventArgsWhitelisted));
237 base::RunLoop wait_for_upload;
238 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
239 wait_for_upload.QuitClosure());
241 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
243 content::BackgroundTracingManager::TriggerHandle handle =
244 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
245 "preemptive_test");
247 base::RunLoop wait_for_activated;
248 BackgroundTracingManager::GetInstance()->SetTracingEnabledCallbackForTesting(
249 wait_for_activated.QuitClosure());
250 EXPECT_TRUE(BackgroundTracingManager::GetInstance()->SetActiveScenario(
251 config.Pass(), upload_config_wrapper.get_receive_callback(),
252 BackgroundTracingManager::ANONYMIZE_DATA));
254 wait_for_activated.Run();
256 TRACE_EVENT1("benchmark", "whitelisted", "find_this", 1);
257 TRACE_EVENT1("benchmark", "not_whitelisted", "this_not_found", 1);
259 BackgroundTracingManager::GetInstance()->WhenIdle(
260 base::Bind(&DisableScenarioWhenIdle));
262 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
263 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
265 wait_for_upload.Run();
267 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
268 EXPECT_TRUE(upload_config_wrapper.TraceHasMatchingString("{"));
269 EXPECT_TRUE(upload_config_wrapper.TraceHasMatchingString("find_this"));
270 EXPECT_TRUE(!upload_config_wrapper.TraceHasMatchingString("this_not_found"));
273 // This tests subprocesses (like a navigating renderer) which gets told to
274 // provide a argument-filtered trace and has no predicate in place to do the
275 // filtering (in this case, only the browser process gets it set), will crash
276 // rather than return potential PII.
277 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
278 CrashWhenSubprocessWithoutArgumentFilter) {
279 SetupBackgroundTracingManager();
281 base::trace_event::TraceLog::GetInstance()->SetArgumentFilterPredicate(
282 base::Bind(&IsTraceEventArgsWhitelisted));
284 base::RunLoop wait_for_upload;
285 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
286 wait_for_upload.QuitClosure());
288 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
290 content::BackgroundTracingManager::TriggerHandle handle =
291 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
292 "preemptive_test");
294 base::RunLoop wait_for_activated;
295 BackgroundTracingManager::GetInstance()->SetTracingEnabledCallbackForTesting(
296 wait_for_activated.QuitClosure());
297 EXPECT_TRUE(BackgroundTracingManager::GetInstance()->SetActiveScenario(
298 config.Pass(), upload_config_wrapper.get_receive_callback(),
299 BackgroundTracingManager::ANONYMIZE_DATA));
301 wait_for_activated.Run();
303 NavigateToURL(shell(), GetTestUrl("", "about:blank"));
305 BackgroundTracingManager::GetInstance()->WhenIdle(
306 base::Bind(&DisableScenarioWhenIdle));
308 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
309 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
311 wait_for_upload.Run();
313 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
314 // We should *not* receive anything at all from the renderer,
315 // the process should've crashed rather than letting that happen.
316 EXPECT_TRUE(!upload_config_wrapper.TraceHasMatchingString("CrRendererMain"));
319 // This tests multiple triggers still only gathers once.
320 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
321 CallMultipleTriggersOnlyGatherOnce) {
323 SetupBackgroundTracingManager();
325 base::RunLoop run_loop;
326 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
327 run_loop.QuitClosure());
329 base::DictionaryValue dict;
330 dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
331 dict.SetString("category", "BENCHMARK");
333 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
335 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
336 rules_dict->SetString("rule", "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED");
337 rules_dict->SetString("trigger_name", "test1");
338 rules_list->Append(rules_dict.Pass());
341 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
342 rules_dict->SetString("rule", "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED");
343 rules_dict->SetString("trigger_name", "test2");
344 rules_list->Append(rules_dict.Pass());
347 dict.Set("configs", rules_list.Pass());
349 scoped_ptr<BackgroundTracingConfig> config(
350 BackgroundTracingConfigImpl::FromDict(&dict));
351 EXPECT_TRUE(config);
353 BackgroundTracingManager::TriggerHandle handle1 =
354 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test1");
355 BackgroundTracingManager::TriggerHandle handle2 =
356 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test2");
358 BackgroundTracingManager::GetInstance()->SetActiveScenario(
359 config.Pass(), upload_config_wrapper.get_receive_callback(),
360 BackgroundTracingManager::NO_DATA_FILTERING);
362 BackgroundTracingManager::GetInstance()->WhenIdle(
363 base::Bind(&DisableScenarioWhenIdle));
365 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
366 handle1, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
367 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
368 handle2,
369 base::Bind(&StartedFinalizingCallback, base::Closure(), false));
371 run_loop.Run();
373 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
377 // This tests that you can't trigger without a scenario set.
378 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
379 CannotTriggerWithoutScenarioSet) {
381 SetupBackgroundTracingManager();
383 base::RunLoop run_loop;
384 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
385 (base::Closure()));
387 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
389 content::BackgroundTracingManager::TriggerHandle handle =
390 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
391 "preemptive_test");
393 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
394 handle,
395 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
397 run_loop.Run();
399 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
403 // This tests that no trace is triggered with a handle that isn't specified
404 // in the config.
405 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
406 DoesNotTriggerWithWrongHandle) {
408 SetupBackgroundTracingManager();
410 base::RunLoop run_loop;
411 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
412 (base::Closure()));
414 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
416 content::BackgroundTracingManager::TriggerHandle handle =
417 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
418 "does_not_exist");
420 BackgroundTracingManager::GetInstance()->SetActiveScenario(
421 config.Pass(), upload_config_wrapper.get_receive_callback(),
422 BackgroundTracingManager::NO_DATA_FILTERING);
424 BackgroundTracingManager::GetInstance()->WhenIdle(
425 base::Bind(&DisableScenarioWhenIdle));
427 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
428 handle,
429 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
431 run_loop.Run();
433 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
437 // This tests that no trace is triggered with an invalid handle.
438 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
439 DoesNotTriggerWithInvalidHandle) {
441 SetupBackgroundTracingManager();
443 base::RunLoop run_loop;
444 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
445 (base::Closure()));
447 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
449 content::BackgroundTracingManager::TriggerHandle handle =
450 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
451 "preemptive_test");
453 content::BackgroundTracingManager::GetInstance()
454 ->InvalidateTriggerHandlesForTesting();
456 BackgroundTracingManager::GetInstance()->SetActiveScenario(
457 config.Pass(), upload_config_wrapper.get_receive_callback(),
458 BackgroundTracingManager::NO_DATA_FILTERING);
460 BackgroundTracingManager::GetInstance()->WhenIdle(
461 base::Bind(&DisableScenarioWhenIdle));
463 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
464 handle,
465 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
467 run_loop.Run();
469 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
473 // This tests that histogram triggers for preemptive mode configs.
474 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
475 ReceiveTraceSucceedsOnHigherHistogramSample) {
477 SetupBackgroundTracingManager();
479 base::RunLoop run_loop;
481 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
482 run_loop.QuitClosure());
484 base::DictionaryValue dict;
485 dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
486 dict.SetString("category", "BENCHMARK");
488 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
490 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
491 rules_dict->SetString(
492 "rule", "MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE");
493 rules_dict->SetString("histogram_name", "fake");
494 rules_dict->SetInteger("histogram_value", 1);
495 rules_list->Append(rules_dict.Pass());
498 dict.Set("configs", rules_list.Pass());
500 scoped_ptr<BackgroundTracingConfig> config(
501 BackgroundTracingConfigImpl::FromDict(&dict));
502 EXPECT_TRUE(config);
504 BackgroundTracingManager::GetInstance()->SetActiveScenario(
505 config.Pass(), upload_config_wrapper.get_receive_callback(),
506 BackgroundTracingManager::NO_DATA_FILTERING);
508 // Our reference value is "1", so a value of "2" should trigger a trace.
509 LOCAL_HISTOGRAM_COUNTS("fake", 2);
511 run_loop.Run();
513 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
517 // This tests that histogram values < reference value don't trigger.
518 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
519 ReceiveTraceFailsOnLowerHistogramSample) {
521 SetupBackgroundTracingManager();
523 base::RunLoop run_loop;
525 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
526 run_loop.QuitClosure());
528 base::DictionaryValue dict;
529 dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
530 dict.SetString("category", "BENCHMARK");
532 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
534 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
535 rules_dict->SetString(
536 "rule", "MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE");
537 rules_dict->SetString("histogram_name", "fake");
538 rules_dict->SetInteger("histogram_value", 1);
539 rules_list->Append(rules_dict.Pass());
542 dict.Set("configs", rules_list.Pass());
544 scoped_ptr<BackgroundTracingConfig> config(
545 BackgroundTracingConfigImpl::FromDict(&dict));
546 EXPECT_TRUE(config);
548 BackgroundTracingManager::GetInstance()->SetActiveScenario(
549 config.Pass(), upload_config_wrapper.get_receive_callback(),
550 BackgroundTracingManager::NO_DATA_FILTERING);
552 // This should fail to trigger a trace since the sample value < the
553 // the reference value above.
554 LOCAL_HISTOGRAM_COUNTS("fake", 0);
556 run_loop.RunUntilIdle();
558 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
562 // This tests that invalid preemptive mode configs will fail.
563 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
564 SetActiveScenarioFailsWithInvalidPreemptiveConfig) {
566 SetupBackgroundTracingManager();
568 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
569 (base::Closure()));
571 base::DictionaryValue dict;
572 dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
573 dict.SetString("category", "BENCHMARK");
575 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
577 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
578 rules_dict->SetString("rule", "INVALID_RULE");
579 rules_list->Append(rules_dict.Pass());
582 dict.Set("configs", rules_list.Pass());
584 scoped_ptr<BackgroundTracingConfig> config(
585 BackgroundTracingConfigImpl::FromDict(&dict));
586 // An invalid config should always return a nullptr here.
587 EXPECT_FALSE(config);
591 // This tests that reactive mode records and terminates with timeout.
592 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
593 ReactiveTimeoutTermination) {
595 SetupBackgroundTracingManager();
597 base::RunLoop run_loop;
598 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
599 run_loop.QuitClosure());
601 scoped_ptr<BackgroundTracingConfig> config = CreateReactiveConfig();
603 BackgroundTracingManager::TriggerHandle handle =
604 BackgroundTracingManager::
605 GetInstance()->RegisterTriggerType("reactive_test");
607 BackgroundTracingManager::GetInstance()->SetActiveScenario(
608 config.Pass(), upload_config_wrapper.get_receive_callback(),
609 BackgroundTracingManager::NO_DATA_FILTERING);
611 BackgroundTracingManager::GetInstance()->WhenIdle(
612 base::Bind(&DisableScenarioWhenIdle));
614 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
615 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
617 BackgroundTracingManager::GetInstance()->FireTimerForTesting();
619 run_loop.Run();
621 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
625 // This tests that reactive mode records and terminates with a second trigger.
626 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
627 ReactiveSecondTriggerTermination) {
629 SetupBackgroundTracingManager();
631 base::RunLoop run_loop;
632 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
633 run_loop.QuitClosure());
635 scoped_ptr<BackgroundTracingConfig> config = CreateReactiveConfig();
637 BackgroundTracingManager::TriggerHandle handle =
638 BackgroundTracingManager::
639 GetInstance()->RegisterTriggerType("reactive_test");
641 BackgroundTracingManager::GetInstance()->SetActiveScenario(
642 config.Pass(), upload_config_wrapper.get_receive_callback(),
643 BackgroundTracingManager::NO_DATA_FILTERING);
645 BackgroundTracingManager::GetInstance()->WhenIdle(
646 base::Bind(&DisableScenarioWhenIdle));
648 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
649 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
650 // second trigger to terminate.
651 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
652 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
654 run_loop.Run();
656 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
660 // This tests that reactive mode only terminates with the same trigger.
661 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
662 ReactiveSecondTriggerMustMatchForTermination) {
664 SetupBackgroundTracingManager();
666 base::RunLoop run_loop;
667 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
668 run_loop.QuitClosure());
670 base::DictionaryValue dict;
671 dict.SetString("mode", "REACTIVE_TRACING_MODE");
673 scoped_ptr<base::ListValue> rules_list(new base::ListValue());
675 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
676 rules_dict->SetString("rule",
677 "TRACE_ON_NAVIGATION_UNTIL_TRIGGER_OR_FULL");
678 rules_dict->SetString("trigger_name", "reactive_test1");
679 rules_dict->SetString("category", "BENCHMARK");
680 rules_list->Append(rules_dict.Pass());
683 scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
684 rules_dict->SetString("rule",
685 "TRACE_ON_NAVIGATION_UNTIL_TRIGGER_OR_FULL");
686 rules_dict->SetString("trigger_name", "reactive_test2");
687 rules_dict->SetString("category", "BENCHMARK");
688 rules_list->Append(rules_dict.Pass());
690 dict.Set("configs", rules_list.Pass());
692 scoped_ptr<BackgroundTracingConfig> config(
693 BackgroundTracingConfigImpl::FromDict(&dict));
695 BackgroundTracingManager::TriggerHandle handle1 =
696 BackgroundTracingManager::GetInstance()->RegisterTriggerType(
697 "reactive_test1");
698 BackgroundTracingManager::TriggerHandle handle2 =
699 BackgroundTracingManager::GetInstance()->RegisterTriggerType(
700 "reactive_test2");
702 BackgroundTracingManager::GetInstance()->SetActiveScenario(
703 config.Pass(), upload_config_wrapper.get_receive_callback(),
704 BackgroundTracingManager::NO_DATA_FILTERING);
706 BackgroundTracingManager::GetInstance()->WhenIdle(
707 base::Bind(&DisableScenarioWhenIdle));
709 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
710 handle1, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
712 // This is expected to fail since we triggered with handle1.
713 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
714 handle2,
715 base::Bind(&StartedFinalizingCallback, base::Closure(), false));
717 // second trigger to terminate.
718 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
719 handle1, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
721 run_loop.Run();
723 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
727 // This tests a third trigger in reactive more does not start another trace.
728 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
729 ReactiveThirdTriggerTimeout) {
731 SetupBackgroundTracingManager();
733 base::RunLoop run_loop;
734 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
735 run_loop.QuitClosure());
737 scoped_ptr<BackgroundTracingConfig> config = CreateReactiveConfig();
739 BackgroundTracingManager::TriggerHandle handle =
740 BackgroundTracingManager::
741 GetInstance()->RegisterTriggerType("reactive_test");
743 BackgroundTracingManager::GetInstance()->SetActiveScenario(
744 config.Pass(), upload_config_wrapper.get_receive_callback(),
745 BackgroundTracingManager::NO_DATA_FILTERING);
747 BackgroundTracingManager::GetInstance()->WhenIdle(
748 base::Bind(&DisableScenarioWhenIdle));
750 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
751 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
752 // second trigger to terminate.
753 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
754 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
755 // third trigger to trigger again, fails as it is still gathering.
756 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
757 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), false));
759 run_loop.Run();
761 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
765 } // namespace content