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

Side by Side Diff: chromeos/network/network_event_log_unittest.cc

Issue 14876021: Re-factor network_event_log (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address final nits Created 7 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 | Annotate | Revision Log
« no previous file with comments | « chromeos/network/network_event_log.cc ('k') | chromeos/network/network_handler_callbacks.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chromeos/network/network_event_log.h" 5 #include "chromeos/network/network_event_log.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/format_macros.h" 11 #include "base/format_macros.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace chromeos { 16 namespace chromeos {
17 namespace network_event_log {
18
19 namespace {
20
21 network_event_log::LogLevel kDefaultLevel = network_event_log::LOG_LEVEL_EVENT;
22
23 } // namespace
17 24
18 class NetworkEventLogTest : public testing::Test { 25 class NetworkEventLogTest : public testing::Test {
19 public: 26 public:
20 NetworkEventLogTest() { 27 NetworkEventLogTest() {
21 } 28 }
22 29
23 virtual void SetUp() OVERRIDE { 30 virtual void SetUp() OVERRIDE {
24 network_event_log::Initialize(); 31 network_event_log::Initialize();
25 } 32 }
26 33
27 virtual void TearDown() OVERRIDE { 34 virtual void TearDown() OVERRIDE {
28 network_event_log::Shutdown(); 35 network_event_log::Shutdown();
29 } 36 }
30 37
31 protected: 38 protected:
32 std::string SkipTime(const std::string& input) { 39 std::string SkipTime(const std::string& input) {
33 std::string output; 40 std::string output;
34 std::vector<std::string> lines; 41 std::vector<std::string> lines;
35 base::SplitString(input, '\n', &lines); 42 base::SplitString(input, '\n', &lines);
36 for (size_t i = 0; i < lines.size(); ++i) { 43 for (size_t i = 0; i < lines.size(); ++i) {
37 size_t n = lines[i].find(']'); 44 size_t n = lines[i].find(']');
38 if (n != std::string::npos) 45 if (n != std::string::npos)
39 output += lines[i].substr(n+2) + '\n'; 46 output += "[time] " + lines[i].substr(n+2) + '\n';
47 else
48 output += lines[i];
40 } 49 }
41 return output; 50 return output;
42 } 51 }
43 52
44 size_t CountLines(const std::string& input) { 53 size_t CountLines(const std::string& input) {
45 return std::count(input.begin(), input.end(), '\n'); 54 return std::count(input.begin(), input.end(), '\n');
46 } 55 }
47 56
57 std::string GetLogString(StringOrder order, size_t max_events) {
58 return network_event_log::GetAsString(
59 order, "file,desc", kDefaultLevel, max_events);
60 }
61
48 private: 62 private:
49 DISALLOW_COPY_AND_ASSIGN(NetworkEventLogTest); 63 DISALLOW_COPY_AND_ASSIGN(NetworkEventLogTest);
50 }; 64 };
51 65
52 TEST_F(NetworkEventLogTest, TestNetworkEvents) { 66 TEST_F(NetworkEventLogTest, TestNetworkEvents) {
53 std::string output_none = network_event_log::GetAsString( 67 std::string output_none = GetLogString(OLDEST_FIRST, 0);
54 network_event_log::OLDEST_FIRST, 0);
55 EXPECT_EQ("No Log Entries.", output_none); 68 EXPECT_EQ("No Log Entries.", output_none);
56 69
57 network_event_log::AddEntry("module1", "event1", "description1"); 70 LogLevel level = kDefaultLevel;
58 network_event_log::AddEntry("module2", "event2", "description2"); 71 network_event_log::internal::AddEntry(
59 network_event_log::AddEntry("module3", "event3", "description3"); 72 "file1", 1, level, "event1", "description1");
60 network_event_log::AddEntry("module3", "event3", "description3"); 73 network_event_log::internal::AddEntry(
74 "file2", 2, level, "event2", "description2");
75 network_event_log::internal::AddEntry(
76 "file3", 3, level, "event3", "description3");
77 network_event_log::internal::AddEntry(
78 "file3", 3, level, "event3", "description3");
61 79
62 const std::string expected_output_oldest_first( 80 const std::string expected_output_oldest_first(
63 "module1:event1: description1\n" 81 "file1:1 event1: description1\n"
64 "module2:event2: description2\n" 82 "file2:2 event2: description2\n"
65 "module3:event3: description3 (2)\n"); 83 "file3:3 event3: description3 (2)\n");
66 std::string output_oldest_first = network_event_log::GetAsString( 84 std::string output_oldest_first = GetLogString(OLDEST_FIRST, 0);
67 network_event_log::OLDEST_FIRST, 0);
68 output_oldest_first = SkipTime(output_oldest_first);
69 EXPECT_EQ(expected_output_oldest_first, output_oldest_first); 85 EXPECT_EQ(expected_output_oldest_first, output_oldest_first);
70 86
71 const std::string expected_output_oldest_first_short( 87 const std::string expected_output_oldest_first_short(
72 "module2:event2: description2\n" 88 "file2:2 event2: description2\n"
73 "module3:event3: description3 (2)\n"); 89 "file3:3 event3: description3 (2)\n");
74 std::string output_oldest_first_short = network_event_log::GetAsString( 90 std::string output_oldest_first_short = GetLogString(OLDEST_FIRST, 2);
75 network_event_log::OLDEST_FIRST, 2);
76 output_oldest_first_short = SkipTime(output_oldest_first_short);
77 EXPECT_EQ(expected_output_oldest_first_short, output_oldest_first_short); 91 EXPECT_EQ(expected_output_oldest_first_short, output_oldest_first_short);
78 92
79 const std::string expected_output_newest_first( 93 const std::string expected_output_newest_first(
80 "module3:event3: description3 (2)\n" 94 "file3:3 event3: description3 (2)\n"
81 "module2:event2: description2\n" 95 "file2:2 event2: description2\n"
82 "module1:event1: description1\n"); 96 "file1:1 event1: description1\n");
83 std::string output_newest_first = network_event_log::GetAsString( 97 std::string output_newest_first = GetLogString(NEWEST_FIRST, 0);
84 network_event_log::NEWEST_FIRST, 0);
85 output_newest_first = SkipTime(output_newest_first);
86 EXPECT_EQ(expected_output_newest_first, output_newest_first); 98 EXPECT_EQ(expected_output_newest_first, output_newest_first);
87 99
88 const std::string expected_output_newest_first_short( 100 const std::string expected_output_newest_first_short(
89 "module3:event3: description3 (2)\n" 101 "file3:3 event3: description3 (2)\n"
90 "module2:event2: description2\n"); 102 "file2:2 event2: description2\n");
91 std::string output_newest_first_short = network_event_log::GetAsString( 103 std::string output_newest_first_short = GetLogString(NEWEST_FIRST, 2);
92 network_event_log::NEWEST_FIRST, 2);
93 output_newest_first_short = SkipTime(output_newest_first_short);
94 EXPECT_EQ(expected_output_newest_first_short, output_newest_first_short); 104 EXPECT_EQ(expected_output_newest_first_short, output_newest_first_short);
95 } 105 }
96 106
97 TEST_F(NetworkEventLogTest, TestMaxNetworkEvents) { 107 TEST_F(NetworkEventLogTest, TestMaxNetworkEvents) {
98 const size_t entries_to_add = 108 const size_t entries_to_add =
99 network_event_log::kMaxNetworkEventLogEntries + 3; 109 kMaxNetworkEventLogEntries + 3;
100 for (size_t i = 0; i < entries_to_add; ++i) 110 for (size_t i = 0; i < entries_to_add; ++i) {
101 network_event_log::AddEntry("test", 111 network_event_log::internal::AddEntry(
102 base::StringPrintf("event_%"PRIuS, i), ""); 112 "test", 1, LOG_LEVEL_EVENT,
103 113 base::StringPrintf("event_%"PRIuS, i), "");
104 std::string output = GetAsString(network_event_log::OLDEST_FIRST, 0); 114 }
115 std::string output = GetLogString(OLDEST_FIRST, 0);
105 size_t output_lines = CountLines(output); 116 size_t output_lines = CountLines(output);
106 EXPECT_EQ(network_event_log::kMaxNetworkEventLogEntries, output_lines); 117 EXPECT_EQ(kMaxNetworkEventLogEntries, output_lines);
107 } 118 }
108 119
120 TEST_F(NetworkEventLogTest, TestStringFormat) {
121 network_event_log::internal::AddEntry(
122 "file", 0, LOG_LEVEL_ERROR, "event0", "description");
123 EXPECT_EQ("file:0 event0\n", network_event_log::GetAsString(
124 OLDEST_FIRST, "file", kDefaultLevel, 1));
125 EXPECT_EQ("[time] event0\n", SkipTime(network_event_log::GetAsString(
126 OLDEST_FIRST, "time", kDefaultLevel, 1)));
127 EXPECT_EQ("event0: description\n", network_event_log::GetAsString(
128 OLDEST_FIRST, "desc", kDefaultLevel, 1));
129 EXPECT_EQ("event0\n", network_event_log::GetAsString(
130 OLDEST_FIRST, "", kDefaultLevel, 1));
131 EXPECT_EQ("<b>event0</b>\n", network_event_log::GetAsString(
132 OLDEST_FIRST, "html", kDefaultLevel, 1));
133 EXPECT_EQ("[time] file:0 event0: description\n",
134 SkipTime(network_event_log::GetAsString(
135 OLDEST_FIRST, "file,time,desc", kDefaultLevel, 1)));
136
137 network_event_log::internal::AddEntry(
138 "file", 0, LOG_LEVEL_DEBUG, "event1", "description");
139 EXPECT_EQ("[time] file:0 <i>event1: description</i>\n",
140 SkipTime(network_event_log::GetAsString(
141 OLDEST_FIRST, "file,time,desc,html", LOG_LEVEL_DEBUG, 1)));
142 }
143
144 namespace {
145
146 void AddTestEvent(LogLevel level, const std::string& event) {
147 network_event_log::internal::AddEntry("file", 0, level, event, "description");
148 }
149
150 } // namespace
151
152 TEST_F(NetworkEventLogTest, TestLogLevel) {
153 AddTestEvent(LOG_LEVEL_ERROR, "error1");
154 AddTestEvent(LOG_LEVEL_ERROR, "error2");
155 AddTestEvent(LOG_LEVEL_EVENT, "event3");
156 AddTestEvent(LOG_LEVEL_ERROR, "error4");
157 AddTestEvent(LOG_LEVEL_EVENT, "event5");
158 AddTestEvent(LOG_LEVEL_DEBUG, "debug6");
159
160 std::string output;
161 output = network_event_log::GetAsString(
162 OLDEST_FIRST, "", LOG_LEVEL_ERROR, 0);
163 EXPECT_EQ(3u, CountLines(output));
164 output = network_event_log::GetAsString(
165 OLDEST_FIRST, "", LOG_LEVEL_EVENT, 0);
166 EXPECT_EQ(5u, CountLines(output));
167 output = network_event_log::GetAsString(
168 OLDEST_FIRST, "", LOG_LEVEL_DEBUG, 0);
169 EXPECT_EQ(6u, CountLines(output));
170
171 // Test max_level. Get only the ERROR entries.
172 output = network_event_log::GetAsString(
173 OLDEST_FIRST, "", LOG_LEVEL_ERROR, 0);
174 EXPECT_EQ("error1\nerror2\nerror4\n", output);
175 }
176
177 TEST_F(NetworkEventLogTest, TestMaxEvents) {
178 AddTestEvent(LOG_LEVEL_EVENT, "event1");
179 AddTestEvent(LOG_LEVEL_ERROR, "error2");
180 AddTestEvent(LOG_LEVEL_EVENT, "event3");
181 AddTestEvent(LOG_LEVEL_ERROR, "error4");
182 AddTestEvent(LOG_LEVEL_EVENT, "event5");
183
184 // Oldest first
185 EXPECT_EQ("error4\n", network_event_log::GetAsString(
186 OLDEST_FIRST, "", LOG_LEVEL_ERROR, 1));
187
188 EXPECT_EQ("error2\nerror4\n", network_event_log::GetAsString(
189 OLDEST_FIRST, "", LOG_LEVEL_ERROR, 2));
190
191 EXPECT_EQ("event3\nerror4\nevent5\n", network_event_log::GetAsString(
192 OLDEST_FIRST, "", LOG_LEVEL_EVENT, 3));
193
194 EXPECT_EQ("error2\nevent3\nerror4\nevent5\n", network_event_log::GetAsString(
195 OLDEST_FIRST, "", LOG_LEVEL_EVENT, 4));
196
197 EXPECT_EQ("event1\nerror2\nevent3\nerror4\nevent5\n",
198 network_event_log::GetAsString(
199 OLDEST_FIRST, "", LOG_LEVEL_EVENT, 5));
200
201 // Newest first
202 EXPECT_EQ("error4\n", network_event_log::GetAsString(
203 NEWEST_FIRST, "", LOG_LEVEL_ERROR, 1));
204
205 EXPECT_EQ("error4\nerror2\n", network_event_log::GetAsString(
206 NEWEST_FIRST, "", LOG_LEVEL_ERROR, 2));
207
208 EXPECT_EQ("event5\nerror4\nevent3\n", network_event_log::GetAsString(
209 NEWEST_FIRST, "", LOG_LEVEL_EVENT, 3));
210
211 EXPECT_EQ("event5\nerror4\nevent3\nerror2\n", network_event_log::GetAsString(
212 NEWEST_FIRST, "", LOG_LEVEL_EVENT, 4));
213
214 EXPECT_EQ("event5\nerror4\nevent3\nerror2\nevent1\n",
215 network_event_log::GetAsString(
216 NEWEST_FIRST, "", LOG_LEVEL_EVENT, 5));
217 }
218
219 } // namespace network_event_log
109 } // namespace chromeos 220 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/network_event_log.cc ('k') | chromeos/network/network_handler_callbacks.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698