OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/log/trace_net_log_observer.h" | 5 #include "net/log/trace_net_log_observer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
18 #include "base/trace_event/trace_event_impl.h" | 18 #include "base/trace_event/trace_event_impl.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "net/log/capturing_net_log.h" | |
21 #include "net/log/net_log.h" | 20 #include "net/log/net_log.h" |
| 21 #include "net/log/test_net_log.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using base::trace_event::TraceLog; | 24 using base::trace_event::TraceLog; |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // TraceLog category for NetLog events. | 30 // TraceLog category for NetLog events. |
31 const char kNetLogTracingCategory[] = "netlog"; | 31 const char kNetLogTracingCategory[] = "netlog"; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 124 } |
125 if (category != kNetLogTracingCategory) | 125 if (category != kNetLogTracingCategory) |
126 continue; | 126 continue; |
127 filtered_trace_events->Append(dict->DeepCopy()); | 127 filtered_trace_events->Append(dict->DeepCopy()); |
128 } | 128 } |
129 return filtered_trace_events.Pass(); | 129 return filtered_trace_events.Pass(); |
130 } | 130 } |
131 | 131 |
132 base::ListValue* trace_events() const { return trace_events_.get(); } | 132 base::ListValue* trace_events() const { return trace_events_.get(); } |
133 | 133 |
134 CapturingNetLog* net_log() { return &net_log_; } | 134 TestNetLog* net_log() { return &net_log_; } |
135 | 135 |
136 TraceNetLogObserver* trace_net_log_observer() const { | 136 TraceNetLogObserver* trace_net_log_observer() const { |
137 return trace_net_log_observer_.get(); | 137 return trace_net_log_observer_.get(); |
138 } | 138 } |
139 | 139 |
140 private: | 140 private: |
141 scoped_ptr<base::ListValue> trace_events_; | 141 scoped_ptr<base::ListValue> trace_events_; |
142 base::trace_event::TraceResultBuffer trace_buffer_; | 142 base::trace_event::TraceResultBuffer trace_buffer_; |
143 base::trace_event::TraceResultBuffer::SimpleOutput json_output_; | 143 base::trace_event::TraceResultBuffer::SimpleOutput json_output_; |
144 CapturingNetLog net_log_; | 144 TestNetLog net_log_; |
145 scoped_ptr<TraceNetLogObserver> trace_net_log_observer_; | 145 scoped_ptr<TraceNetLogObserver> trace_net_log_observer_; |
146 }; | 146 }; |
147 | 147 |
148 TEST_F(TraceNetLogObserverTest, TracingNotEnabled) { | 148 TEST_F(TraceNetLogObserverTest, TracingNotEnabled) { |
149 trace_net_log_observer()->WatchForTraceStart(net_log()); | 149 trace_net_log_observer()->WatchForTraceStart(net_log()); |
150 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 150 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
151 | 151 |
152 EndTraceAndFlush(); | 152 EndTraceAndFlush(); |
153 trace_net_log_observer()->StopWatchForTraceStart(); | 153 trace_net_log_observer()->StopWatchForTraceStart(); |
154 | 154 |
155 EXPECT_EQ(0u, trace_events()->GetSize()); | 155 EXPECT_EQ(0u, trace_events()->GetSize()); |
156 } | 156 } |
157 | 157 |
158 TEST_F(TraceNetLogObserverTest, TraceEventCaptured) { | 158 TEST_F(TraceNetLogObserverTest, TraceEventCaptured) { |
159 CapturingNetLog::CapturedEntryList entries; | 159 TestNetLog::CapturedEntryList entries; |
160 net_log()->GetEntries(&entries); | 160 net_log()->GetEntries(&entries); |
161 EXPECT_TRUE(entries.empty()); | 161 EXPECT_TRUE(entries.empty()); |
162 | 162 |
163 trace_net_log_observer()->WatchForTraceStart(net_log()); | 163 trace_net_log_observer()->WatchForTraceStart(net_log()); |
164 EnableTraceLog(); | 164 EnableTraceLog(); |
165 BoundNetLog bound_net_log = | 165 BoundNetLog bound_net_log = |
166 BoundNetLog::Make(net_log(), net::NetLog::SOURCE_NONE); | 166 BoundNetLog::Make(net_log(), net::NetLog::SOURCE_NONE); |
167 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); | 167 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
168 bound_net_log.BeginEvent(NetLog::TYPE_URL_REQUEST_START_JOB); | 168 bound_net_log.BeginEvent(NetLog::TYPE_URL_REQUEST_START_JOB); |
169 bound_net_log.EndEvent(NetLog::TYPE_REQUEST_ALIVE); | 169 bound_net_log.EndEvent(NetLog::TYPE_REQUEST_ALIVE); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 EnableTraceLog(); | 216 EnableTraceLog(); |
217 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); | 217 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
218 TraceLog::GetInstance()->SetDisabled(); | 218 TraceLog::GetInstance()->SetDisabled(); |
219 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 219 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
220 EnableTraceLog(); | 220 EnableTraceLog(); |
221 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); | 221 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); |
222 | 222 |
223 EndTraceAndFlush(); | 223 EndTraceAndFlush(); |
224 trace_net_log_observer()->StopWatchForTraceStart(); | 224 trace_net_log_observer()->StopWatchForTraceStart(); |
225 | 225 |
226 CapturingNetLog::CapturedEntryList entries; | 226 TestNetLog::CapturedEntryList entries; |
227 net_log()->GetEntries(&entries); | 227 net_log()->GetEntries(&entries); |
228 EXPECT_EQ(3u, entries.size()); | 228 EXPECT_EQ(3u, entries.size()); |
229 EXPECT_EQ(2u, trace_events()->GetSize()); | 229 EXPECT_EQ(2u, trace_events()->GetSize()); |
230 const base::DictionaryValue* item1 = NULL; | 230 const base::DictionaryValue* item1 = NULL; |
231 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); | 231 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
232 const base::DictionaryValue* item2 = NULL; | 232 const base::DictionaryValue* item2 = NULL; |
233 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); | 233 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); |
234 | 234 |
235 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); | 235 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
236 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); | 236 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); |
(...skipping 19 matching lines...) Expand all Loading... |
256 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileTracing) { | 256 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileTracing) { |
257 trace_net_log_observer()->WatchForTraceStart(net_log()); | 257 trace_net_log_observer()->WatchForTraceStart(net_log()); |
258 EnableTraceLog(); | 258 EnableTraceLog(); |
259 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); | 259 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
260 trace_net_log_observer()->StopWatchForTraceStart(); | 260 trace_net_log_observer()->StopWatchForTraceStart(); |
261 set_trace_net_log_observer(NULL); | 261 set_trace_net_log_observer(NULL); |
262 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 262 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
263 | 263 |
264 EndTraceAndFlush(); | 264 EndTraceAndFlush(); |
265 | 265 |
266 CapturingNetLog::CapturedEntryList entries; | 266 TestNetLog::CapturedEntryList entries; |
267 net_log()->GetEntries(&entries); | 267 net_log()->GetEntries(&entries); |
268 EXPECT_EQ(2u, entries.size()); | 268 EXPECT_EQ(2u, entries.size()); |
269 EXPECT_EQ(1u, trace_events()->GetSize()); | 269 EXPECT_EQ(1u, trace_events()->GetSize()); |
270 | 270 |
271 const base::DictionaryValue* item1 = NULL; | 271 const base::DictionaryValue* item1 = NULL; |
272 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); | 272 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
273 | 273 |
274 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); | 274 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
275 EXPECT_EQ(kNetLogTracingCategory, actual_item1.category); | 275 EXPECT_EQ(kNetLogTracingCategory, actual_item1.category); |
276 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id); | 276 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id); |
277 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), | 277 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
278 actual_item1.phase); | 278 actual_item1.phase); |
279 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED), | 279 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED), |
280 actual_item1.name); | 280 actual_item1.name); |
281 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type), | 281 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type), |
282 actual_item1.source_type); | 282 actual_item1.source_type); |
283 } | 283 } |
284 | 284 |
285 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileNotTracing) { | 285 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileNotTracing) { |
286 trace_net_log_observer()->WatchForTraceStart(net_log()); | 286 trace_net_log_observer()->WatchForTraceStart(net_log()); |
287 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); | 287 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
288 trace_net_log_observer()->StopWatchForTraceStart(); | 288 trace_net_log_observer()->StopWatchForTraceStart(); |
289 set_trace_net_log_observer(NULL); | 289 set_trace_net_log_observer(NULL); |
290 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 290 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
291 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); | 291 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); |
292 | 292 |
293 EndTraceAndFlush(); | 293 EndTraceAndFlush(); |
294 | 294 |
295 CapturingNetLog::CapturedEntryList entries; | 295 TestNetLog::CapturedEntryList entries; |
296 net_log()->GetEntries(&entries); | 296 net_log()->GetEntries(&entries); |
297 EXPECT_EQ(3u, entries.size()); | 297 EXPECT_EQ(3u, entries.size()); |
298 EXPECT_EQ(0u, trace_events()->GetSize()); | 298 EXPECT_EQ(0u, trace_events()->GetSize()); |
299 } | 299 } |
300 | 300 |
301 TEST_F(TraceNetLogObserverTest, CreateObserverAfterTracingStarts) { | 301 TEST_F(TraceNetLogObserverTest, CreateObserverAfterTracingStarts) { |
302 set_trace_net_log_observer(NULL); | 302 set_trace_net_log_observer(NULL); |
303 EnableTraceLog(); | 303 EnableTraceLog(); |
304 set_trace_net_log_observer(new TraceNetLogObserver()); | 304 set_trace_net_log_observer(new TraceNetLogObserver()); |
305 trace_net_log_observer()->WatchForTraceStart(net_log()); | 305 trace_net_log_observer()->WatchForTraceStart(net_log()); |
306 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); | 306 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
307 trace_net_log_observer()->StopWatchForTraceStart(); | 307 trace_net_log_observer()->StopWatchForTraceStart(); |
308 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 308 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
309 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); | 309 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); |
310 | 310 |
311 EndTraceAndFlush(); | 311 EndTraceAndFlush(); |
312 | 312 |
313 CapturingNetLog::CapturedEntryList entries; | 313 TestNetLog::CapturedEntryList entries; |
314 net_log()->GetEntries(&entries); | 314 net_log()->GetEntries(&entries); |
315 EXPECT_EQ(3u, entries.size()); | 315 EXPECT_EQ(3u, entries.size()); |
316 EXPECT_EQ(0u, trace_events()->GetSize()); | 316 EXPECT_EQ(0u, trace_events()->GetSize()); |
317 } | 317 } |
318 | 318 |
319 TEST_F(TraceNetLogObserverTest, EventsWithAndWithoutParameters) { | 319 TEST_F(TraceNetLogObserverTest, EventsWithAndWithoutParameters) { |
320 trace_net_log_observer()->WatchForTraceStart(net_log()); | 320 trace_net_log_observer()->WatchForTraceStart(net_log()); |
321 EnableTraceLog(); | 321 EnableTraceLog(); |
322 NetLog::ParametersCallback net_log_callback; | 322 NetLog::ParametersCallback net_log_callback; |
323 std::string param = "bar"; | 323 std::string param = "bar"; |
324 net_log_callback = NetLog::StringCallback("foo", ¶m); | 324 net_log_callback = NetLog::StringCallback("foo", ¶m); |
325 | 325 |
326 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED, net_log_callback); | 326 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED, net_log_callback); |
327 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); | 327 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
328 | 328 |
329 EndTraceAndFlush(); | 329 EndTraceAndFlush(); |
330 trace_net_log_observer()->StopWatchForTraceStart(); | 330 trace_net_log_observer()->StopWatchForTraceStart(); |
331 | 331 |
332 CapturingNetLog::CapturedEntryList entries; | 332 TestNetLog::CapturedEntryList entries; |
333 net_log()->GetEntries(&entries); | 333 net_log()->GetEntries(&entries); |
334 EXPECT_EQ(2u, entries.size()); | 334 EXPECT_EQ(2u, entries.size()); |
335 EXPECT_EQ(2u, trace_events()->GetSize()); | 335 EXPECT_EQ(2u, trace_events()->GetSize()); |
336 const base::DictionaryValue* item1 = NULL; | 336 const base::DictionaryValue* item1 = NULL; |
337 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); | 337 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
338 const base::DictionaryValue* item2 = NULL; | 338 const base::DictionaryValue* item2 = NULL; |
339 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); | 339 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); |
340 | 340 |
341 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); | 341 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
342 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); | 342 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); |
(...skipping 20 matching lines...) Expand all Loading... |
363 EXPECT_TRUE(item1->GetString("args.params.foo", &item1_params)); | 363 EXPECT_TRUE(item1->GetString("args.params.foo", &item1_params)); |
364 EXPECT_EQ("bar", item1_params); | 364 EXPECT_EQ("bar", item1_params); |
365 | 365 |
366 EXPECT_TRUE(item2->GetString("args.params", &item2_params)); | 366 EXPECT_TRUE(item2->GetString("args.params", &item2_params)); |
367 EXPECT_TRUE(item2_params.empty()); | 367 EXPECT_TRUE(item2_params.empty()); |
368 } | 368 } |
369 | 369 |
370 } // namespace | 370 } // namespace |
371 | 371 |
372 } // namespace net | 372 } // namespace net |
OLD | NEW |