blob: 6a3c5b05ebcdd8e96a72e47ab96b2ab643edd37a [file] [log] [blame]
xunjielieb290602014-09-16 17:13:181// Copyright 2014 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
eroman87c53d62015-04-02 06:51:075#include "net/log/trace_net_log_observer.h"
xunjielieb290602014-09-16 17:13:186
danakja9850e12016-04-18 22:28:087#include <memory>
xunjielieb290602014-09-16 17:13:188#include <string>
9#include <vector>
10
xunjielieb290602014-09-16 17:13:1811#include "base/json/json_reader.h"
12#include "base/logging.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/ref_counted_memory.h"
xunjielieb290602014-09-16 17:13:1815#include "base/run_loop.h"
16#include "base/strings/stringprintf.h"
oysteine17de7452015-07-27 21:49:2717#include "base/trace_event/trace_buffer.h"
primiano4775e5012015-01-30 16:40:3118#include "base/trace_event/trace_event.h"
19#include "base/trace_event/trace_event_impl.h"
xunjielieb290602014-09-16 17:13:1820#include "base/values.h"
mikecirone8b85c432016-09-08 19:11:0021#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1922#include "net/log/net_log_parameters_callback.h"
mikecirone8b85c432016-09-08 19:11:0023#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1924#include "net/log/net_log_with_source.h"
vishal.b62985ca92015-04-17 08:45:5125#include "net/log/test_net_log.h"
mmenke43758e62015-05-04 21:09:4626#include "net/log/test_net_log_entry.h"
xunjielieb290602014-09-16 17:13:1827#include "testing/gtest/include/gtest/gtest.h"
28
ssid759dd8c2015-02-09 21:25:3929using base::trace_event::TraceLog;
xunjielieb290602014-09-16 17:13:1830
31namespace net {
32
33namespace {
34
xunjieli1d2e6f52014-10-21 20:05:0135// TraceLog category for NetLog events.
xunjieli654aa662015-02-26 16:14:3336const char kNetLogTracingCategory[] = "netlog";
xunjieli1d2e6f52014-10-21 20:05:0137
xunjielieb290602014-09-16 17:13:1838struct TraceEntryInfo {
39 std::string category;
40 std::string id;
41 std::string phase;
42 std::string name;
43 std::string source_type;
44};
45
46TraceEntryInfo GetTraceEntryInfoFromValue(const base::DictionaryValue& value) {
47 TraceEntryInfo info;
48 EXPECT_TRUE(value.GetString("cat", &info.category));
49 EXPECT_TRUE(value.GetString("id", &info.id));
50 EXPECT_TRUE(value.GetString("ph", &info.phase));
51 EXPECT_TRUE(value.GetString("name", &info.name));
52 EXPECT_TRUE(value.GetString("args.source_type", &info.source_type));
53
54 return info;
55}
56
57class TraceNetLogObserverTest : public testing::Test {
58 public:
59 TraceNetLogObserverTest() {
60 TraceLog* tracelog = TraceLog::GetInstance();
61 DCHECK(tracelog);
62 DCHECK(!tracelog->IsEnabled());
63 trace_buffer_.SetOutputCallback(json_output_.GetCallback());
64 trace_net_log_observer_.reset(new TraceNetLogObserver());
65 trace_events_.reset(new base::ListValue());
66 }
67
dcheng67be2b1f2014-10-27 21:47:2968 ~TraceNetLogObserverTest() override {
xunjielieb290602014-09-16 17:13:1869 DCHECK(!TraceLog::GetInstance()->IsEnabled());
70 }
71
72 void OnTraceDataCollected(
73 base::RunLoop* run_loop,
74 const scoped_refptr<base::RefCountedString>& events_str,
75 bool has_more_events) {
76 DCHECK(trace_events_->empty());
77 trace_buffer_.Start();
78 trace_buffer_.AddFragment(events_str->data());
79 trace_buffer_.Finish();
80
danakja9850e12016-04-18 22:28:0881 std::unique_ptr<base::Value> trace_value;
olli.raula624ee8f2015-09-01 18:54:4682 trace_value = base::JSONReader::Read(
xunjielieb290602014-09-16 17:13:1883 json_output_.json_output,
olli.raula624ee8f2015-09-01 18:54:4684 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN);
xunjielieb290602014-09-16 17:13:1885
86 ASSERT_TRUE(trace_value) << json_output_.json_output;
87 base::ListValue* trace_events = NULL;
88 ASSERT_TRUE(trace_value->GetAsList(&trace_events));
89
90 trace_events_ = FilterNetLogTraceEvents(*trace_events);
91
92 if (!has_more_events)
93 run_loop->Quit();
94 }
95
96 static void EnableTraceLog() {
xunjieli1d2e6f52014-10-21 20:05:0197 TraceLog::GetInstance()->SetEnabled(
zhenwd601ddc52015-06-02 21:46:3498 base::trace_event::TraceConfig(kNetLogTracingCategory, ""),
99 TraceLog::RECORDING_MODE);
xunjielieb290602014-09-16 17:13:18100 }
101
102 void EndTraceAndFlush() {
103 base::RunLoop run_loop;
104 TraceLog::GetInstance()->SetDisabled();
105 TraceLog::GetInstance()->Flush(
106 base::Bind(&TraceNetLogObserverTest::OnTraceDataCollected,
eroman87c53d62015-04-02 06:51:07107 base::Unretained(this), base::Unretained(&run_loop)));
xunjielieb290602014-09-16 17:13:18108 run_loop.Run();
109 }
110
111 void set_trace_net_log_observer(TraceNetLogObserver* trace_net_log_observer) {
112 trace_net_log_observer_.reset(trace_net_log_observer);
113 }
114
danakja9850e12016-04-18 22:28:08115 static std::unique_ptr<base::ListValue> FilterNetLogTraceEvents(
xunjielieb290602014-09-16 17:13:18116 const base::ListValue& trace_events) {
danakja9850e12016-04-18 22:28:08117 std::unique_ptr<base::ListValue> filtered_trace_events(
118 new base::ListValue());
xunjielieb290602014-09-16 17:13:18119 for (size_t i = 0; i < trace_events.GetSize(); i++) {
120 const base::DictionaryValue* dict = NULL;
121 if (!trace_events.GetDictionary(i, &dict)) {
122 ADD_FAILURE() << "Unexpected non-dictionary event in trace_events";
123 continue;
124 }
125 std::string category;
126 if (!dict->GetString("cat", &category)) {
127 ADD_FAILURE()
128 << "Unexpected item without a category field in trace_events";
129 continue;
130 }
xunjieli1d2e6f52014-10-21 20:05:01131 if (category != kNetLogTracingCategory)
xunjielieb290602014-09-16 17:13:18132 continue;
dcheng53a55db2016-06-21 20:24:42133 filtered_trace_events->Append(dict->CreateDeepCopy());
xunjielieb290602014-09-16 17:13:18134 }
dchengc7eeda422015-12-26 03:56:48135 return filtered_trace_events;
xunjielieb290602014-09-16 17:13:18136 }
137
eroman87c53d62015-04-02 06:51:07138 base::ListValue* trace_events() const { return trace_events_.get(); }
xunjielieb290602014-09-16 17:13:18139
vishal.b62985ca92015-04-17 08:45:51140 TestNetLog* net_log() { return &net_log_; }
xunjielieb290602014-09-16 17:13:18141
142 TraceNetLogObserver* trace_net_log_observer() const {
143 return trace_net_log_observer_.get();
144 }
145
146 private:
danakja9850e12016-04-18 22:28:08147 std::unique_ptr<base::ListValue> trace_events_;
ssid759dd8c2015-02-09 21:25:39148 base::trace_event::TraceResultBuffer trace_buffer_;
149 base::trace_event::TraceResultBuffer::SimpleOutput json_output_;
vishal.b62985ca92015-04-17 08:45:51150 TestNetLog net_log_;
danakja9850e12016-04-18 22:28:08151 std::unique_ptr<TraceNetLogObserver> trace_net_log_observer_;
xunjielieb290602014-09-16 17:13:18152};
153
154TEST_F(TraceNetLogObserverTest, TracingNotEnabled) {
155 trace_net_log_observer()->WatchForTraceStart(net_log());
mikecirone8b85c432016-09-08 19:11:00156 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
xunjielieb290602014-09-16 17:13:18157
158 EndTraceAndFlush();
159 trace_net_log_observer()->StopWatchForTraceStart();
160
161 EXPECT_EQ(0u, trace_events()->GetSize());
162}
163
164TEST_F(TraceNetLogObserverTest, TraceEventCaptured) {
mmenke43758e62015-05-04 21:09:46165 TestNetLogEntry::List entries;
xunjielieb290602014-09-16 17:13:18166 net_log()->GetEntries(&entries);
167 EXPECT_TRUE(entries.empty());
168
169 trace_net_log_observer()->WatchForTraceStart(net_log());
170 EnableTraceLog();
tfarina42834112016-09-22 13:38:20171 NetLogWithSource net_log_with_source =
172 NetLogWithSource::Make(net_log(), net::NetLogSourceType::NONE);
mikecirone8b85c432016-09-08 19:11:00173 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED);
tfarina42834112016-09-22 13:38:20174 net_log_with_source.BeginEvent(NetLogEventType::URL_REQUEST_START_JOB);
175 net_log_with_source.EndEvent(NetLogEventType::REQUEST_ALIVE);
xunjielieb290602014-09-16 17:13:18176
177 net_log()->GetEntries(&entries);
178 EXPECT_EQ(3u, entries.size());
179 EndTraceAndFlush();
180 trace_net_log_observer()->StopWatchForTraceStart();
181 EXPECT_EQ(3u, trace_events()->GetSize());
182 const base::DictionaryValue* item1 = NULL;
183 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1));
184 const base::DictionaryValue* item2 = NULL;
185 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2));
186 const base::DictionaryValue* item3 = NULL;
187 ASSERT_TRUE(trace_events()->GetDictionary(2, &item3));
188
189 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1);
190 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2);
191 TraceEntryInfo actual_item3 = GetTraceEntryInfoFromValue(*item3);
xunjieli1d2e6f52014-10-21 20:05:01192 EXPECT_EQ(kNetLogTracingCategory, actual_item1.category);
xunjielieb290602014-09-16 17:13:18193 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id);
194 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT),
195 actual_item1.phase);
mikecirone8b85c432016-09-08 19:11:00196 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::CANCELLED),
xunjielieb290602014-09-16 17:13:18197 actual_item1.name);
198 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type),
199 actual_item1.source_type);
200
xunjieli1d2e6f52014-10-21 20:05:01201 EXPECT_EQ(kNetLogTracingCategory, actual_item2.category);
xunjielieb290602014-09-16 17:13:18202 EXPECT_EQ(base::StringPrintf("0x%d", entries[1].source.id), actual_item2.id);
203 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN),
204 actual_item2.phase);
mikecirone8b85c432016-09-08 19:11:00205 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::URL_REQUEST_START_JOB),
xunjielieb290602014-09-16 17:13:18206 actual_item2.name);
207 EXPECT_EQ(NetLog::SourceTypeToString(entries[1].source.type),
208 actual_item2.source_type);
209
xunjieli1d2e6f52014-10-21 20:05:01210 EXPECT_EQ(kNetLogTracingCategory, actual_item3.category);
xunjielieb290602014-09-16 17:13:18211 EXPECT_EQ(base::StringPrintf("0x%d", entries[2].source.id), actual_item3.id);
212 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_END),
213 actual_item3.phase);
mikecirone8b85c432016-09-08 19:11:00214 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::REQUEST_ALIVE),
xunjielieb290602014-09-16 17:13:18215 actual_item3.name);
216 EXPECT_EQ(NetLog::SourceTypeToString(entries[2].source.type),
217 actual_item3.source_type);
218}
219
220TEST_F(TraceNetLogObserverTest, EnableAndDisableTracing) {
221 trace_net_log_observer()->WatchForTraceStart(net_log());
222 EnableTraceLog();
mikecirone8b85c432016-09-08 19:11:00223 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED);
xunjielieb290602014-09-16 17:13:18224 TraceLog::GetInstance()->SetDisabled();
mikecirone8b85c432016-09-08 19:11:00225 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
xunjielieb290602014-09-16 17:13:18226 EnableTraceLog();
mikecirone8b85c432016-09-08 19:11:00227 net_log()->AddGlobalEntry(NetLogEventType::URL_REQUEST_START_JOB);
xunjielieb290602014-09-16 17:13:18228
229 EndTraceAndFlush();
230 trace_net_log_observer()->StopWatchForTraceStart();
231
mmenke43758e62015-05-04 21:09:46232 TestNetLogEntry::List entries;
xunjielieb290602014-09-16 17:13:18233 net_log()->GetEntries(&entries);
234 EXPECT_EQ(3u, entries.size());
235 EXPECT_EQ(2u, trace_events()->GetSize());
236 const base::DictionaryValue* item1 = NULL;
237 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1));
238 const base::DictionaryValue* item2 = NULL;
239 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2));
240
241 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1);
242 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2);
xunjieli1d2e6f52014-10-21 20:05:01243 EXPECT_EQ(kNetLogTracingCategory, actual_item1.category);
xunjielieb290602014-09-16 17:13:18244 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id);
245 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT),
246 actual_item1.phase);
mikecirone8b85c432016-09-08 19:11:00247 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::CANCELLED),
xunjielieb290602014-09-16 17:13:18248 actual_item1.name);
249 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type),
250 actual_item1.source_type);
251
xunjieli1d2e6f52014-10-21 20:05:01252 EXPECT_EQ(kNetLogTracingCategory, actual_item2.category);
xunjielieb290602014-09-16 17:13:18253 EXPECT_EQ(base::StringPrintf("0x%d", entries[2].source.id), actual_item2.id);
254 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT),
255 actual_item2.phase);
mikecirone8b85c432016-09-08 19:11:00256 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::URL_REQUEST_START_JOB),
xunjielieb290602014-09-16 17:13:18257 actual_item2.name);
258 EXPECT_EQ(NetLog::SourceTypeToString(entries[2].source.type),
259 actual_item2.source_type);
260}
261
262TEST_F(TraceNetLogObserverTest, DestroyObserverWhileTracing) {
263 trace_net_log_observer()->WatchForTraceStart(net_log());
264 EnableTraceLog();
mikecirone8b85c432016-09-08 19:11:00265 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED);
xunjielieb290602014-09-16 17:13:18266 trace_net_log_observer()->StopWatchForTraceStart();
267 set_trace_net_log_observer(NULL);
mikecirone8b85c432016-09-08 19:11:00268 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
xunjielieb290602014-09-16 17:13:18269
270 EndTraceAndFlush();
271
mmenke43758e62015-05-04 21:09:46272 TestNetLogEntry::List entries;
xunjielieb290602014-09-16 17:13:18273 net_log()->GetEntries(&entries);
274 EXPECT_EQ(2u, entries.size());
275 EXPECT_EQ(1u, trace_events()->GetSize());
276
277 const base::DictionaryValue* item1 = NULL;
278 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1));
279
280 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1);
xunjieli1d2e6f52014-10-21 20:05:01281 EXPECT_EQ(kNetLogTracingCategory, actual_item1.category);
xunjielieb290602014-09-16 17:13:18282 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id);
283 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT),
284 actual_item1.phase);
mikecirone8b85c432016-09-08 19:11:00285 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::CANCELLED),
xunjielieb290602014-09-16 17:13:18286 actual_item1.name);
287 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type),
288 actual_item1.source_type);
289}
290
291TEST_F(TraceNetLogObserverTest, DestroyObserverWhileNotTracing) {
292 trace_net_log_observer()->WatchForTraceStart(net_log());
mikecirone8b85c432016-09-08 19:11:00293 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED);
xunjielieb290602014-09-16 17:13:18294 trace_net_log_observer()->StopWatchForTraceStart();
295 set_trace_net_log_observer(NULL);
mikecirone8b85c432016-09-08 19:11:00296 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
297 net_log()->AddGlobalEntry(NetLogEventType::URL_REQUEST_START_JOB);
xunjielieb290602014-09-16 17:13:18298
299 EndTraceAndFlush();
300
mmenke43758e62015-05-04 21:09:46301 TestNetLogEntry::List entries;
xunjielieb290602014-09-16 17:13:18302 net_log()->GetEntries(&entries);
303 EXPECT_EQ(3u, entries.size());
304 EXPECT_EQ(0u, trace_events()->GetSize());
305}
306
307TEST_F(TraceNetLogObserverTest, CreateObserverAfterTracingStarts) {
308 set_trace_net_log_observer(NULL);
309 EnableTraceLog();
310 set_trace_net_log_observer(new TraceNetLogObserver());
311 trace_net_log_observer()->WatchForTraceStart(net_log());
mikecirone8b85c432016-09-08 19:11:00312 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED);
xunjielieb290602014-09-16 17:13:18313 trace_net_log_observer()->StopWatchForTraceStart();
mikecirone8b85c432016-09-08 19:11:00314 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
315 net_log()->AddGlobalEntry(NetLogEventType::URL_REQUEST_START_JOB);
xunjielieb290602014-09-16 17:13:18316
317 EndTraceAndFlush();
318
mmenke43758e62015-05-04 21:09:46319 TestNetLogEntry::List entries;
xunjielieb290602014-09-16 17:13:18320 net_log()->GetEntries(&entries);
321 EXPECT_EQ(3u, entries.size());
lizeb9da0f1a62016-12-02 09:26:48322 EXPECT_EQ(1u, trace_events()->GetSize());
323}
324
325TEST_F(TraceNetLogObserverTest,
326 CreateObserverAfterTracingStartsDisabledCategory) {
327 set_trace_net_log_observer(nullptr);
328
329 std::string disabled_netlog_category =
330 std::string("-") + kNetLogTracingCategory;
331 TraceLog::GetInstance()->SetEnabled(
332 base::trace_event::TraceConfig(disabled_netlog_category, ""),
333 TraceLog::RECORDING_MODE);
334
335 set_trace_net_log_observer(new TraceNetLogObserver());
336 trace_net_log_observer()->WatchForTraceStart(net_log());
337 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED);
338 trace_net_log_observer()->StopWatchForTraceStart();
339 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
340 net_log()->AddGlobalEntry(NetLogEventType::URL_REQUEST_START_JOB);
341
342 EndTraceAndFlush();
343
344 TestNetLogEntry::List entries;
345 net_log()->GetEntries(&entries);
346 EXPECT_EQ(3u, entries.size());
xunjielieb290602014-09-16 17:13:18347 EXPECT_EQ(0u, trace_events()->GetSize());
348}
349
350TEST_F(TraceNetLogObserverTest, EventsWithAndWithoutParameters) {
351 trace_net_log_observer()->WatchForTraceStart(net_log());
352 EnableTraceLog();
mikecironef22f9812016-10-04 03:40:19353 NetLogParametersCallback net_log_callback;
xunjielieb290602014-09-16 17:13:18354 std::string param = "bar";
355 net_log_callback = NetLog::StringCallback("foo", &param);
356
mikecirone8b85c432016-09-08 19:11:00357 net_log()->AddGlobalEntry(NetLogEventType::CANCELLED, net_log_callback);
358 net_log()->AddGlobalEntry(NetLogEventType::REQUEST_ALIVE);
xunjielieb290602014-09-16 17:13:18359
360 EndTraceAndFlush();
361 trace_net_log_observer()->StopWatchForTraceStart();
362
mmenke43758e62015-05-04 21:09:46363 TestNetLogEntry::List entries;
xunjielieb290602014-09-16 17:13:18364 net_log()->GetEntries(&entries);
365 EXPECT_EQ(2u, entries.size());
366 EXPECT_EQ(2u, trace_events()->GetSize());
367 const base::DictionaryValue* item1 = NULL;
368 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1));
369 const base::DictionaryValue* item2 = NULL;
370 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2));
371
372 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1);
373 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2);
xunjieli1d2e6f52014-10-21 20:05:01374 EXPECT_EQ(kNetLogTracingCategory, actual_item1.category);
xunjielieb290602014-09-16 17:13:18375 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id);
376 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT),
377 actual_item1.phase);
mikecirone8b85c432016-09-08 19:11:00378 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::CANCELLED),
xunjielieb290602014-09-16 17:13:18379 actual_item1.name);
380 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type),
381 actual_item1.source_type);
382
xunjieli1d2e6f52014-10-21 20:05:01383 EXPECT_EQ(kNetLogTracingCategory, actual_item2.category);
xunjielieb290602014-09-16 17:13:18384 EXPECT_EQ(base::StringPrintf("0x%d", entries[1].source.id), actual_item2.id);
385 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT),
386 actual_item2.phase);
mikecirone8b85c432016-09-08 19:11:00387 EXPECT_EQ(NetLog::EventTypeToString(NetLogEventType::REQUEST_ALIVE),
xunjielieb290602014-09-16 17:13:18388 actual_item2.name);
389 EXPECT_EQ(NetLog::SourceTypeToString(entries[1].source.type),
390 actual_item2.source_type);
391
392 std::string item1_params;
393 std::string item2_params;
394 EXPECT_TRUE(item1->GetString("args.params.foo", &item1_params));
395 EXPECT_EQ("bar", item1_params);
396
397 EXPECT_TRUE(item2->GetString("args.params", &item2_params));
398 EXPECT_TRUE(item2_params.empty());
399}
400
lizebbe0aa512016-12-05 18:57:33401TEST(TraceNetLogObserverCategoryTest, DisabledCategory) {
402 TraceNetLogObserver observer;
403 NetLog net_log;
404 observer.WatchForTraceStart(&net_log);
405
406 EXPECT_FALSE(net_log.IsCapturing());
407
408 std::string disabled_netlog_category =
409 std::string("-") + kNetLogTracingCategory;
410 TraceLog::GetInstance()->SetEnabled(
411 base::trace_event::TraceConfig(disabled_netlog_category, ""),
412 TraceLog::RECORDING_MODE);
413
414 EXPECT_FALSE(net_log.IsCapturing());
415 observer.StopWatchForTraceStart();
416 EXPECT_FALSE(net_log.IsCapturing());
417
418 TraceLog::GetInstance()->SetDisabled();
419}
420
421TEST(TraceNetLogObserverCategoryTest, EnabledCategory) {
422 TraceNetLogObserver observer;
423 NetLog net_log;
424 observer.WatchForTraceStart(&net_log);
425
426 EXPECT_FALSE(net_log.IsCapturing());
427
428 TraceLog::GetInstance()->SetEnabled(
429 base::trace_event::TraceConfig(kNetLogTracingCategory, ""),
430 TraceLog::RECORDING_MODE);
431
432 EXPECT_TRUE(net_log.IsCapturing());
433 observer.StopWatchForTraceStart();
434 EXPECT_FALSE(net_log.IsCapturing());
435
436 TraceLog::GetInstance()->SetDisabled();
437}
438
xunjielieb290602014-09-16 17:13:18439} // namespace
440
441} // namespace net