Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / components / device_event_log / device_event_log_impl_unittest.cc
blobb6eff23fce12858cfb311b44918ff67d29ccf178
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
3 // found in the LICENSE file.
5 #include "components/device_event_log/device_event_log_impl.h"
7 #include <algorithm>
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/format_macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/stringprintf.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace device_event_log {
19 namespace {
21 const size_t kDefaultMaxEvents = 100;
22 LogLevel kDefaultLevel = LOG_LEVEL_EVENT;
23 LogType kDefaultType = LOG_TYPE_NETWORK;
25 } // namespace
27 class DeviceEventLogTest : public testing::Test {
28 public:
29 DeviceEventLogTest() {}
31 void SetUp() override {
32 impl_.reset(new DeviceEventLogImpl(kDefaultMaxEvents));
35 void TearDown() override { impl_.reset(); }
37 protected:
38 std::string SkipTime(const std::string& input) {
39 std::string output;
40 std::vector<std::string> lines;
41 base::SplitString(input, '\n', &lines);
42 for (size_t i = 0; i < lines.size(); ++i) {
43 size_t n = lines[i].find(']');
44 if (n != std::string::npos)
45 output += "[time] " + lines[i].substr(n + 2) + '\n';
46 else
47 output += lines[i];
49 return output;
52 size_t CountLines(const std::string& input) {
53 return std::count(input.begin(), input.end(), '\n');
56 std::string GetLogString(StringOrder order,
57 const std::string& format,
58 LogLevel max_level,
59 size_t max_events) {
60 return impl_->GetAsString(order, format, "", max_level, max_events);
63 std::string GetOrderedString(StringOrder order, size_t max_events) {
64 return impl_->GetAsString(order, "file", "", kDefaultLevel, max_events);
67 std::string GetLogStringForType(const std::string& types) {
68 return impl_->GetAsString(OLDEST_FIRST, "type", types, kDefaultLevel, 0);
71 void AddNetworkEntry(const char* file,
72 int line,
73 LogLevel level,
74 const std::string& event) {
75 impl_->AddEntry(file, line, kDefaultType, level, event);
78 void AddTestEvent(LogLevel level, const std::string& event) {
79 AddNetworkEntry("file", 0, level, event);
82 void AddEventType(LogType type, const std::string& event) {
83 impl_->AddEntry("file", 0, type, kDefaultLevel, event);
86 size_t GetMaxEntries() const { return impl_->max_entries(); }
88 scoped_ptr<DeviceEventLogImpl> impl_;
90 private:
91 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogTest);
94 TEST_F(DeviceEventLogTest, TestNetworkEvents) {
95 std::string output_none = GetOrderedString(OLDEST_FIRST, 0);
96 EXPECT_EQ("No Log Entries.", output_none);
98 LogLevel level = kDefaultLevel;
99 AddNetworkEntry("file1", 1, level, "event1");
100 AddNetworkEntry("file2", 2, level, "event2");
101 AddNetworkEntry("file3", 3, level, "event3");
102 AddNetworkEntry("file3", 3, level, "event3");
104 const std::string expected_output_oldest_first(
105 "file1:1 event1\n"
106 "file2:2 event2\n"
107 "file3:3 event3 (2)\n");
108 std::string output_oldest_first = GetOrderedString(OLDEST_FIRST, 0);
109 EXPECT_EQ(expected_output_oldest_first, output_oldest_first);
111 const std::string expected_output_oldest_first_short(
112 "file2:2 event2\n"
113 "file3:3 event3 (2)\n");
114 std::string output_oldest_first_short = GetOrderedString(OLDEST_FIRST, 2);
115 EXPECT_EQ(expected_output_oldest_first_short, output_oldest_first_short);
117 const std::string expected_output_newest_first(
118 "file3:3 event3 (2)\n"
119 "file2:2 event2\n"
120 "file1:1 event1\n");
121 std::string output_newest_first = GetOrderedString(NEWEST_FIRST, 0);
122 EXPECT_EQ(expected_output_newest_first, output_newest_first);
124 const std::string expected_output_newest_first_short(
125 "file3:3 event3 (2)\n"
126 "file2:2 event2\n");
127 std::string output_newest_first_short = GetOrderedString(NEWEST_FIRST, 2);
128 EXPECT_EQ(expected_output_newest_first_short, output_newest_first_short);
131 TEST_F(DeviceEventLogTest, TestMaxEntries) {
132 const size_t max_entries = GetMaxEntries();
133 const size_t entries_to_add = max_entries + 3;
134 for (size_t i = 0; i < entries_to_add; ++i) {
135 AddTestEvent(LOG_LEVEL_EVENT, base::StringPrintf("event_%" PRIuS, i));
137 std::string output = GetOrderedString(OLDEST_FIRST, 0);
138 size_t output_lines = CountLines(output);
139 EXPECT_EQ(max_entries, output_lines);
142 TEST_F(DeviceEventLogTest, TestStringFormat) {
143 AddTestEvent(LOG_LEVEL_ERROR, "event0");
144 EXPECT_EQ("file:0 event0\n",
145 GetLogString(OLDEST_FIRST, "file", kDefaultLevel, 1));
146 EXPECT_EQ("[time] event0\n",
147 SkipTime(GetLogString(OLDEST_FIRST, "time", kDefaultLevel, 1)));
148 EXPECT_EQ("event0\n", GetLogString(OLDEST_FIRST, "", kDefaultLevel, 1));
149 EXPECT_EQ("<b><i>event0</i></b>\n",
150 GetLogString(OLDEST_FIRST, "html", kDefaultLevel, 1));
151 EXPECT_EQ(
152 "[time] file:0 event0\n",
153 SkipTime(GetLogString(OLDEST_FIRST, "file,time", kDefaultLevel, 1)));
155 AddTestEvent(LOG_LEVEL_DEBUG, "event1");
156 EXPECT_EQ("[time] file:0 <i>event1</i>\n",
157 SkipTime(GetLogString(OLDEST_FIRST, "file,time,html",
158 LOG_LEVEL_DEBUG, 1)));
160 AddTestEvent(kDefaultLevel, "event2");
161 EXPECT_EQ("Network: file:0 event2\n",
162 GetLogString(OLDEST_FIRST, "file,type", kDefaultLevel, 1));
165 TEST_F(DeviceEventLogTest, TestLogLevel) {
166 AddTestEvent(LOG_LEVEL_ERROR, "error1");
167 AddTestEvent(LOG_LEVEL_ERROR, "error2");
168 AddTestEvent(LOG_LEVEL_EVENT, "event3");
169 AddTestEvent(LOG_LEVEL_ERROR, "error4");
170 AddTestEvent(LOG_LEVEL_EVENT, "event5");
171 AddTestEvent(LOG_LEVEL_DEBUG, "debug6");
173 std::string out;
174 out = GetLogString(OLDEST_FIRST, "", LOG_LEVEL_ERROR, 0);
175 EXPECT_EQ(3u, CountLines(out));
176 out = GetLogString(OLDEST_FIRST, "", LOG_LEVEL_EVENT, 0);
177 EXPECT_EQ(5u, CountLines(out));
178 out = GetLogString(OLDEST_FIRST, "", LOG_LEVEL_DEBUG, 0);
179 EXPECT_EQ(6u, CountLines(out));
181 // Test max_level. Get only the ERROR entries.
182 out = GetLogString(OLDEST_FIRST, "", LOG_LEVEL_ERROR, 0);
183 EXPECT_EQ("error1\nerror2\nerror4\n", out);
186 TEST_F(DeviceEventLogTest, TestMaxEvents) {
187 AddTestEvent(LOG_LEVEL_EVENT, "event1");
188 AddTestEvent(LOG_LEVEL_ERROR, "error2");
189 AddTestEvent(LOG_LEVEL_EVENT, "event3");
190 AddTestEvent(LOG_LEVEL_ERROR, "error4");
191 AddTestEvent(LOG_LEVEL_EVENT, "event5");
193 // Oldest first
194 EXPECT_EQ("error4\n", GetLogString(OLDEST_FIRST, "", LOG_LEVEL_ERROR, 1));
196 EXPECT_EQ("error2\nerror4\n",
197 GetLogString(OLDEST_FIRST, "", LOG_LEVEL_ERROR, 2));
199 EXPECT_EQ("event3\nerror4\nevent5\n",
200 GetLogString(OLDEST_FIRST, "", LOG_LEVEL_EVENT, 3));
202 EXPECT_EQ("error2\nevent3\nerror4\nevent5\n",
203 GetLogString(OLDEST_FIRST, "", LOG_LEVEL_EVENT, 4));
205 EXPECT_EQ("event1\nerror2\nevent3\nerror4\nevent5\n",
206 GetLogString(OLDEST_FIRST, "", LOG_LEVEL_EVENT, 5));
208 // Newest first
209 EXPECT_EQ("error4\n", GetLogString(NEWEST_FIRST, "", LOG_LEVEL_ERROR, 1));
211 EXPECT_EQ("error4\nerror2\n",
212 GetLogString(NEWEST_FIRST, "", LOG_LEVEL_ERROR, 2));
214 EXPECT_EQ("event5\nerror4\nevent3\n",
215 GetLogString(NEWEST_FIRST, "", LOG_LEVEL_EVENT, 3));
217 EXPECT_EQ("event5\nerror4\nevent3\nerror2\n",
218 GetLogString(NEWEST_FIRST, "", LOG_LEVEL_EVENT, 4));
220 EXPECT_EQ("event5\nerror4\nevent3\nerror2\nevent1\n",
221 GetLogString(NEWEST_FIRST, "", LOG_LEVEL_EVENT, 5));
224 TEST_F(DeviceEventLogTest, TestMaxErrors) {
225 const int kMaxTestEntries = 4;
226 impl_.reset(new DeviceEventLogImpl(kMaxTestEntries));
227 AddTestEvent(LOG_LEVEL_EVENT, "event1");
228 AddTestEvent(LOG_LEVEL_ERROR, "error2");
229 AddTestEvent(LOG_LEVEL_EVENT, "event3");
230 AddTestEvent(LOG_LEVEL_ERROR, "error4");
231 AddTestEvent(LOG_LEVEL_EVENT, "event5");
232 AddTestEvent(LOG_LEVEL_EVENT, "event6");
233 EXPECT_EQ("error2\nerror4\nevent5\nevent6\n",
234 GetLogString(OLDEST_FIRST, "", LOG_LEVEL_DEBUG, 0));
237 TEST_F(DeviceEventLogTest, TestType) {
238 AddEventType(LOG_TYPE_NETWORK, "event1");
239 AddEventType(LOG_TYPE_POWER, "event2");
240 AddEventType(LOG_TYPE_NETWORK, "event3");
241 AddEventType(LOG_TYPE_POWER, "event4");
242 AddEventType(LOG_TYPE_NETWORK, "event5");
243 AddEventType(LOG_TYPE_NETWORK, "event6");
244 EXPECT_EQ(
245 "Network: event1\nNetwork: event3\nNetwork: event5\nNetwork: event6\n",
246 GetLogStringForType("network"));
247 const std::string power_events("Power: event2\nPower: event4\n");
248 EXPECT_EQ(power_events, GetLogStringForType("power"));
249 EXPECT_EQ(power_events, GetLogStringForType("non-network"));
250 const std::string all_events(
251 "Network: event1\n"
252 "Power: event2\n"
253 "Network: event3\n"
254 "Power: event4\n"
255 "Network: event5\n"
256 "Network: event6\n");
257 EXPECT_EQ(all_events, GetLogStringForType("network,power"));
258 EXPECT_EQ(all_events, GetLogStringForType(""));
261 } // namespace device_event_log