Skeleton for BackgroundTracingManager.
[chromium-blink-merge.git] / content / browser / tracing / background_tracing_manager_browsertest.cc
blobf353264f911aa59fb7b36d62422afce812058982
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 "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"
13 namespace content {
15 class BackgroundTracingManagerBrowserTest : public ContentBrowserTest {
16 public:
17 BackgroundTracingManagerBrowserTest() {}
19 private:
20 DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerBrowserTest);
23 class BackgroundTracingManagerUploadConfigWrapper {
24 public:
25 BackgroundTracingManagerUploadConfigWrapper(const base::Closure& callback)
26 : callback_(callback), receive_count_(0) {
27 receive_callback_ =
28 base::Bind(&BackgroundTracingManagerUploadConfigWrapper::Upload,
29 base::Unretained(this));
32 void Upload(const base::RefCountedString* file_contents,
33 base::Callback<void()> done_callback) {
34 receive_count_ += 1;
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()
45 const {
46 return receive_callback_;
49 private:
50 BackgroundTracingManager::ReceiveCallback receive_callback_;
51 base::Closure callback_;
52 int receive_count_;
55 void StartedFinalizingCallback(base::Closure callback,
56 bool expected,
57 bool value) {
58 EXPECT_EQ(expected, value);
59 if (!callback.is_null())
60 callback.Run();
63 scoped_ptr<BackgroundTracingPreemptiveConfig> CreatePreemptiveConfig() {
64 scoped_ptr<BackgroundTracingPreemptiveConfig> config(
65 new BackgroundTracingPreemptiveConfig());
67 BackgroundTracingPreemptiveConfig::MonitoringRule rule;
68 rule.type =
69 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED;
70 rule.named_trigger_info.trigger_name = "test";
72 config->configs.push_back(rule);
74 return config.Pass();
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));
112 run_loop.Run();
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(
133 "test");
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));
146 run_loop.Run();
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;
166 rule.type =
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(
188 handle2,
189 base::Bind(&StartedFinalizingCallback, base::Closure(), false));
191 run_loop.Run();
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(
205 (base::Closure()));
207 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
209 content::BackgroundTracingManager::TriggerHandle handle =
210 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
211 "test");
213 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
214 handle,
215 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
217 run_loop.Run();
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
224 // in the config.
225 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
226 DoesNotTriggerWithWrongHandle) {
228 SetupBackgroundTracingManager();
230 base::RunLoop run_loop;
231 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
232 (base::Closure()));
234 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
235 CreatePreemptiveConfig();
237 content::BackgroundTracingManager::TriggerHandle handle =
238 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
239 "does_not_exist");
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(
248 handle,
249 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
251 run_loop.Run();
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(
265 (base::Closure()));
267 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
268 CreatePreemptiveConfig();
270 content::BackgroundTracingManager::TriggerHandle handle =
271 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
272 "test");
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(
284 handle,
285 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
287 run_loop.Run();
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(
300 (base::Closure()));
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(
326 (base::Closure()));
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