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.
6 #include "content/public/browser/background_tracing_manager.h"
7 #include "content/public/browser/background_tracing_preemptive_config.h"
8 #include "content/public/browser/background_tracing_reactive_config.h"
9 #include "content/public/test/content_browser_test.h"
10 #include "content/public/test/content_browser_test_utils.h"
11 #include "content/public/test/test_utils.h"
15 class BackgroundTracingManagerBrowserTest
: public ContentBrowserTest
{
17 BackgroundTracingManagerBrowserTest() {}
20 DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerBrowserTest
);
23 class BackgroundTracingManagerUploadConfigWrapper
{
25 BackgroundTracingManagerUploadConfigWrapper(const base::Closure
& callback
)
26 : callback_(callback
), receive_count_(0) {
28 base::Bind(&BackgroundTracingManagerUploadConfigWrapper::Upload
,
29 base::Unretained(this));
32 void Upload(const base::RefCountedString
* file_contents
,
33 base::Callback
<void()> done_callback
) {
36 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
37 base::Bind(done_callback
));
38 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
39 base::Bind(callback_
));
42 int get_receive_count() const { return receive_count_
; }
44 const BackgroundTracingManager::ReceiveCallback
& get_receive_callback()
46 return receive_callback_
;
50 BackgroundTracingManager::ReceiveCallback receive_callback_
;
51 base::Closure callback_
;
55 void StartedFinalizingCallback(base::Closure callback
,
58 EXPECT_EQ(expected
, value
);
59 if (!callback
.is_null())
63 scoped_ptr
<BackgroundTracingPreemptiveConfig
> CreatePreemptiveConfig() {
64 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config(
65 new BackgroundTracingPreemptiveConfig());
67 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
69 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
;
70 rule
.named_trigger_info
.trigger_name
= "test";
72 config
->configs
.push_back(rule
);
77 void SetupBackgroundTracingManager() {
78 content::BackgroundTracingManager::GetInstance()
79 ->InvalidateTriggerHandlesForTesting();
82 void DisableScenarioWhenIdle() {
83 BackgroundTracingManager::GetInstance()->SetActiveScenario(
84 NULL
, BackgroundTracingManager::ReceiveCallback(), false);
87 // This tests that the endpoint receives the final trace data.
88 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
89 ReceiveTraceFinalContentsOnTrigger
) {
91 SetupBackgroundTracingManager();
93 base::RunLoop run_loop
;
94 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
95 run_loop
.QuitClosure());
97 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
=
98 CreatePreemptiveConfig();
100 BackgroundTracingManager::TriggerHandle handle
=
101 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test");
103 BackgroundTracingManager::GetInstance()->SetActiveScenario(
104 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
106 BackgroundTracingManager::GetInstance()->WhenIdle(
107 base::Bind(&DisableScenarioWhenIdle
));
109 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
110 handle
, base::Bind(&StartedFinalizingCallback
, base::Closure(), true));
114 EXPECT_TRUE(upload_config_wrapper
.get_receive_count() == 1);
118 // This tests triggering more than once still only gathers once.
119 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
120 CallTriggersMoreThanOnceOnlyGatherOnce
) {
122 SetupBackgroundTracingManager();
124 base::RunLoop run_loop
;
125 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
126 run_loop
.QuitClosure());
128 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
=
129 CreatePreemptiveConfig();
131 content::BackgroundTracingManager::TriggerHandle handle
=
132 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
135 BackgroundTracingManager::GetInstance()->SetActiveScenario(
136 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
138 BackgroundTracingManager::GetInstance()->WhenIdle(
139 base::Bind(&DisableScenarioWhenIdle
));
141 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
142 handle
, base::Bind(&StartedFinalizingCallback
, base::Closure(), true));
143 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
144 handle
, base::Bind(&StartedFinalizingCallback
, base::Closure(), false));
148 EXPECT_TRUE(upload_config_wrapper
.get_receive_count() == 1);
152 // This tests multiple triggers still only gathers once.
153 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
154 CallMultipleTriggersOnlyGatherOnce
) {
156 SetupBackgroundTracingManager();
158 base::RunLoop run_loop
;
159 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
160 run_loop
.QuitClosure());
162 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
=
163 CreatePreemptiveConfig();
165 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
167 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
;
168 rule
.named_trigger_info
.trigger_name
= "test1";
169 config
->configs
.push_back(rule
);
171 rule
.named_trigger_info
.trigger_name
= "test2";
172 config
->configs
.push_back(rule
);
174 BackgroundTracingManager::TriggerHandle handle1
=
175 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test1");
176 BackgroundTracingManager::TriggerHandle handle2
=
177 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test2");
179 BackgroundTracingManager::GetInstance()->SetActiveScenario(
180 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
182 BackgroundTracingManager::GetInstance()->WhenIdle(
183 base::Bind(&DisableScenarioWhenIdle
));
185 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
186 handle1
, base::Bind(&StartedFinalizingCallback
, base::Closure(), true));
187 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
189 base::Bind(&StartedFinalizingCallback
, base::Closure(), false));
193 EXPECT_TRUE(upload_config_wrapper
.get_receive_count() == 1);
197 // This tests that you can't trigger without a scenario set.
198 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
199 CannotTriggerWithoutScenarioSet
) {
201 SetupBackgroundTracingManager();
203 base::RunLoop run_loop
;
204 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
207 scoped_ptr
<BackgroundTracingConfig
> config
= CreatePreemptiveConfig();
209 content::BackgroundTracingManager::TriggerHandle handle
=
210 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
213 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
215 base::Bind(&StartedFinalizingCallback
, run_loop
.QuitClosure(), false));
219 EXPECT_TRUE(upload_config_wrapper
.get_receive_count() == 0);
223 // This tests that no trace is triggered with a handle that isn't specified
225 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
226 DoesNotTriggerWithWrongHandle
) {
228 SetupBackgroundTracingManager();
230 base::RunLoop run_loop
;
231 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
234 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
=
235 CreatePreemptiveConfig();
237 content::BackgroundTracingManager::TriggerHandle handle
=
238 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
241 BackgroundTracingManager::GetInstance()->SetActiveScenario(
242 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
244 BackgroundTracingManager::GetInstance()->WhenIdle(
245 base::Bind(&DisableScenarioWhenIdle
));
247 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
249 base::Bind(&StartedFinalizingCallback
, run_loop
.QuitClosure(), false));
253 EXPECT_TRUE(upload_config_wrapper
.get_receive_count() == 0);
257 // This tests that no trace is triggered with an invalid handle.
258 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
259 DoesNotTriggerWithInvalidHandle
) {
261 SetupBackgroundTracingManager();
263 base::RunLoop run_loop
;
264 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
267 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config
=
268 CreatePreemptiveConfig();
270 content::BackgroundTracingManager::TriggerHandle handle
=
271 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
274 content::BackgroundTracingManager::GetInstance()
275 ->InvalidateTriggerHandlesForTesting();
277 BackgroundTracingManager::GetInstance()->SetActiveScenario(
278 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
280 BackgroundTracingManager::GetInstance()->WhenIdle(
281 base::Bind(&DisableScenarioWhenIdle
));
283 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
285 base::Bind(&StartedFinalizingCallback
, run_loop
.QuitClosure(), false));
289 EXPECT_TRUE(upload_config_wrapper
.get_receive_count() == 0);
293 // This tests that reactive mode configs will fail.
294 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
295 DoesNotAllowPreemptiveConfigThatsNotManual
) {
297 SetupBackgroundTracingManager();
299 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
302 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config(
303 new content::BackgroundTracingPreemptiveConfig());
305 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
306 rule
.type
= BackgroundTracingPreemptiveConfig::
307 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE
;
308 rule
.histogram_trigger_info
.histogram_name_to_trigger_on
= "fake";
309 rule
.histogram_trigger_info
.histogram_bin_to_trigger_on
= 0;
310 config
->configs
.push_back(rule
);
312 bool result
= BackgroundTracingManager::GetInstance()->SetActiveScenario(
313 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
315 EXPECT_FALSE(result
);
319 // This tests that reactive mode configs will fail.
320 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest
,
321 DoesNotAllowReactiveConfig
) {
323 SetupBackgroundTracingManager();
325 BackgroundTracingManagerUploadConfigWrapper
upload_config_wrapper(
328 scoped_ptr
<BackgroundTracingConfig
> config(
329 new BackgroundTracingReactiveConfig());
331 bool result
= BackgroundTracingManager::GetInstance()->SetActiveScenario(
332 config
.Pass(), upload_config_wrapper
.get_receive_callback(), true);
334 EXPECT_FALSE(result
);
338 } // namespace content