Web MIDI: send back error information to blink on starting sessions
[chromium-blink-merge.git] / net / base / net_log_logger_unittest.cc
blob5d587dabd5fdf9326d8c9ebf6f6f4cdc7b39ef8e
1 // Copyright 2013 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 "net/base/net_log_logger.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_reader.h"
11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace net {
16 class NetLogLoggerTest : public testing::Test {
17 public:
18 virtual void SetUp() {
19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
20 log_path_ = temp_dir_.path().AppendASCII("NetLogFile");
23 protected:
24 base::ScopedTempDir temp_dir_;
25 base::FilePath log_path_;
28 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) {
30 // Create and destroy a logger.
31 FILE* file = base::OpenFile(log_path_, "w");
32 ASSERT_TRUE(file);
33 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants());
34 NetLogLogger logger(file, *constants);
37 std::string input;
38 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
40 base::JSONReader reader;
41 scoped_ptr<base::Value> root(reader.ReadToValue(input));
42 ASSERT_TRUE(root) << reader.GetErrorMessage();
44 base::DictionaryValue* dict;
45 ASSERT_TRUE(root->GetAsDictionary(&dict));
46 base::ListValue* events;
47 ASSERT_TRUE(dict->GetList("events", &events));
48 ASSERT_EQ(0u, events->GetSize());
51 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) {
53 FILE* file = base::OpenFile(log_path_, "w");
54 ASSERT_TRUE(file);
55 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants());
56 NetLogLogger logger(file, *constants);
58 const int kDummyId = 1;
59 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId);
60 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE,
61 source,
62 NetLog::PHASE_BEGIN,
63 base::TimeTicks::Now(),
64 NULL);
65 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
66 logger.OnAddEntry(entry);
69 std::string input;
70 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
72 base::JSONReader reader;
73 scoped_ptr<base::Value> root(reader.ReadToValue(input));
74 ASSERT_TRUE(root) << reader.GetErrorMessage();
76 base::DictionaryValue* dict;
77 ASSERT_TRUE(root->GetAsDictionary(&dict));
78 base::ListValue* events;
79 ASSERT_TRUE(dict->GetList("events", &events));
80 ASSERT_EQ(1u, events->GetSize());
83 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) {
85 FILE* file = base::OpenFile(log_path_, "w");
86 ASSERT_TRUE(file);
87 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants());
88 NetLogLogger logger(file, *constants);
90 const int kDummyId = 1;
91 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId);
92 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE,
93 source,
94 NetLog::PHASE_BEGIN,
95 base::TimeTicks::Now(),
96 NULL);
97 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
99 // Add the entry multiple times.
100 logger.OnAddEntry(entry);
101 logger.OnAddEntry(entry);
104 std::string input;
105 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
107 base::JSONReader reader;
108 scoped_ptr<base::Value> root(reader.ReadToValue(input));
109 ASSERT_TRUE(root) << reader.GetErrorMessage();
111 base::DictionaryValue* dict;
112 ASSERT_TRUE(root->GetAsDictionary(&dict));
113 base::ListValue* events;
114 ASSERT_TRUE(dict->GetList("events", &events));
115 ASSERT_EQ(2u, events->GetSize());
118 } // namespace net