Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: content/browser/tracing/background_tracing_manager_browsertest.cc

Issue 1089253003: Re-land first pass BackgroundTracingManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Scoped_ptr Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/tracing/background_tracing_manager_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
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"
12
13 namespace content {
14
15 class BackgroundTracingManagerBrowserTest : public ContentBrowserTest {
16 public:
17 BackgroundTracingManagerBrowserTest() {}
18
19 private:
20 DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerBrowserTest);
21 };
22
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));
30 }
31
32 void Upload(const base::RefCountedString* file_contents,
33 base::Callback<void()> done_callback) {
34 receive_count_ += 1;
35
36 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
37 base::Bind(done_callback));
38 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
39 base::Bind(callback_));
40 }
41
42 int get_receive_count() const { return receive_count_; }
43
44 const BackgroundTracingManager::ReceiveCallback& get_receive_callback()
45 const {
46 return receive_callback_;
47 }
48
49 private:
50 BackgroundTracingManager::ReceiveCallback receive_callback_;
51 base::Closure callback_;
52 int receive_count_;
53 };
54
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();
61 }
62
63 scoped_ptr<BackgroundTracingPreemptiveConfig> CreatePreemptiveConfig() {
64 scoped_ptr<BackgroundTracingPreemptiveConfig> config(
65 new BackgroundTracingPreemptiveConfig());
66
67 BackgroundTracingPreemptiveConfig::MonitoringRule rule;
68 rule.type =
69 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED;
70 rule.named_trigger_info.trigger_name = "test";
71
72 config->configs.push_back(rule);
73
74 return config.Pass();
75 }
76
77 void SetupBackgroundTracingManager() {
78 content::BackgroundTracingManager::GetInstance()
79 ->InvalidateTriggerHandlesForTesting();
80 }
81
82 void DisableScenarioWhenIdle() {
83 BackgroundTracingManager::GetInstance()->SetActiveScenario(
84 NULL, BackgroundTracingManager::ReceiveCallback(), false);
85 }
86
87 // This tests that the endpoint receives the final trace data.
88 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
89 ReceiveTraceFinalContentsOnTrigger) {
90 {
91 SetupBackgroundTracingManager();
92
93 base::RunLoop run_loop;
94 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
95 run_loop.QuitClosure());
96
97 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
98 CreatePreemptiveConfig();
99
100 BackgroundTracingManager::TriggerHandle handle =
101 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test");
102
103 BackgroundTracingManager::GetInstance()->SetActiveScenario(
104 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
105
106 BackgroundTracingManager::GetInstance()->WhenIdle(
107 base::Bind(&DisableScenarioWhenIdle));
108
109 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
110 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
111
112 run_loop.Run();
113
114 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
115 }
116 }
117
118 // This tests triggering more than once still only gathers once.
119 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
120 CallTriggersMoreThanOnceOnlyGatherOnce) {
121 {
122 SetupBackgroundTracingManager();
123
124 base::RunLoop run_loop;
125 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
126 run_loop.QuitClosure());
127
128 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
129 CreatePreemptiveConfig();
130
131 content::BackgroundTracingManager::TriggerHandle handle =
132 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
133 "test");
134
135 BackgroundTracingManager::GetInstance()->SetActiveScenario(
136 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
137
138 BackgroundTracingManager::GetInstance()->WhenIdle(
139 base::Bind(&DisableScenarioWhenIdle));
140
141 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
142 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), true));
143 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
144 handle, base::Bind(&StartedFinalizingCallback, base::Closure(), false));
145
146 run_loop.Run();
147
148 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
149 }
150 }
151
152 // This tests multiple triggers still only gathers once.
153 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
154 CallMultipleTriggersOnlyGatherOnce) {
155 {
156 SetupBackgroundTracingManager();
157
158 base::RunLoop run_loop;
159 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
160 run_loop.QuitClosure());
161
162 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
163 CreatePreemptiveConfig();
164
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);
170
171 rule.named_trigger_info.trigger_name = "test2";
172 config->configs.push_back(rule);
173
174 BackgroundTracingManager::TriggerHandle handle1 =
175 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test1");
176 BackgroundTracingManager::TriggerHandle handle2 =
177 BackgroundTracingManager::GetInstance()->RegisterTriggerType("test2");
178
179 BackgroundTracingManager::GetInstance()->SetActiveScenario(
180 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
181
182 BackgroundTracingManager::GetInstance()->WhenIdle(
183 base::Bind(&DisableScenarioWhenIdle));
184
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));
190
191 run_loop.Run();
192
193 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 1);
194 }
195 }
196
197 // This tests that you can't trigger without a scenario set.
198 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
199 CannotTriggerWithoutScenarioSet) {
200 {
201 SetupBackgroundTracingManager();
202
203 base::RunLoop run_loop;
204 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
205 (base::Closure()));
206
207 scoped_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
208
209 content::BackgroundTracingManager::TriggerHandle handle =
210 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
211 "test");
212
213 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
214 handle,
215 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
216
217 run_loop.Run();
218
219 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
220 }
221 }
222
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) {
227 {
228 SetupBackgroundTracingManager();
229
230 base::RunLoop run_loop;
231 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
232 (base::Closure()));
233
234 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
235 CreatePreemptiveConfig();
236
237 content::BackgroundTracingManager::TriggerHandle handle =
238 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
239 "does_not_exist");
240
241 BackgroundTracingManager::GetInstance()->SetActiveScenario(
242 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
243
244 BackgroundTracingManager::GetInstance()->WhenIdle(
245 base::Bind(&DisableScenarioWhenIdle));
246
247 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
248 handle,
249 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
250
251 run_loop.Run();
252
253 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
254 }
255 }
256
257 // This tests that no trace is triggered with an invalid handle.
258 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
259 DoesNotTriggerWithInvalidHandle) {
260 {
261 SetupBackgroundTracingManager();
262
263 base::RunLoop run_loop;
264 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
265 (base::Closure()));
266
267 scoped_ptr<BackgroundTracingPreemptiveConfig> config =
268 CreatePreemptiveConfig();
269
270 content::BackgroundTracingManager::TriggerHandle handle =
271 content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
272 "test");
273
274 content::BackgroundTracingManager::GetInstance()
275 ->InvalidateTriggerHandlesForTesting();
276
277 BackgroundTracingManager::GetInstance()->SetActiveScenario(
278 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
279
280 BackgroundTracingManager::GetInstance()->WhenIdle(
281 base::Bind(&DisableScenarioWhenIdle));
282
283 BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
284 handle,
285 base::Bind(&StartedFinalizingCallback, run_loop.QuitClosure(), false));
286
287 run_loop.Run();
288
289 EXPECT_TRUE(upload_config_wrapper.get_receive_count() == 0);
290 }
291 }
292
293 // This tests that reactive mode configs will fail.
294 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
295 DoesNotAllowPreemptiveConfigThatsNotManual) {
296 {
297 SetupBackgroundTracingManager();
298
299 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
300 (base::Closure()));
301
302 scoped_ptr<BackgroundTracingPreemptiveConfig> config(
303 new content::BackgroundTracingPreemptiveConfig());
304
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);
311
312 bool result = BackgroundTracingManager::GetInstance()->SetActiveScenario(
313 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
314
315 EXPECT_FALSE(result);
316 }
317 }
318
319 // This tests that reactive mode configs will fail.
320 IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
321 DoesNotAllowReactiveConfig) {
322 {
323 SetupBackgroundTracingManager();
324
325 BackgroundTracingManagerUploadConfigWrapper upload_config_wrapper(
326 (base::Closure()));
327
328 scoped_ptr<BackgroundTracingConfig> config(
329 new BackgroundTracingReactiveConfig());
330
331 bool result = BackgroundTracingManager::GetInstance()->SetActiveScenario(
332 config.Pass(), upload_config_wrapper.get_receive_callback(), true);
333
334 EXPECT_FALSE(result);
335 }
336 }
337
338 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/tracing/background_tracing_manager_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698