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 "chromeos/device_event_log_impl.h"
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"
19 namespace device_event_log
{
23 const size_t kDefaultMaxEvents
= 100;
24 LogLevel kDefaultLevel
= LOG_LEVEL_EVENT
;
25 LogType kDefaultType
= LOG_TYPE_NETWORK
;
29 class DeviceEventLogTest
: public testing::Test
{
31 DeviceEventLogTest() {}
33 void SetUp() override
{
34 impl_
.reset(new DeviceEventLogImpl(kDefaultMaxEvents
));
37 void TearDown() override
{ impl_
.reset(); }
40 std::string
SkipTime(const std::string
& input
) {
42 std::vector
<std::string
> lines
;
43 base::SplitString(input
, '\n', &lines
);
44 for (size_t i
= 0; i
< lines
.size(); ++i
) {
45 size_t n
= lines
[i
].find(']');
46 if (n
!= std::string::npos
)
47 output
+= "[time] " + lines
[i
].substr(n
+ 2) + '\n';
54 size_t CountLines(const std::string
& input
) {
55 return std::count(input
.begin(), input
.end(), '\n');
58 std::string
GetLogString(StringOrder order
,
59 const std::string
& format
,
62 return impl_
->GetAsString(order
, format
, "", max_level
, max_events
);
65 std::string
GetOrderedString(StringOrder order
, size_t max_events
) {
66 return impl_
->GetAsString(order
, "file", "", kDefaultLevel
, max_events
);
69 std::string
GetLogStringForType(const std::string
& types
) {
70 return impl_
->GetAsString(OLDEST_FIRST
, "type", types
, kDefaultLevel
, 0);
73 void AddNetworkEntry(const char* file
,
76 const std::string
& event
) {
77 impl_
->AddEntry(file
, line
, kDefaultType
, level
, event
);
80 void AddTestEvent(LogLevel level
, const std::string
& event
) {
81 AddNetworkEntry("file", 0, level
, event
);
84 void AddEventType(LogType type
, const std::string
& event
) {
85 impl_
->AddEntry("file", 0, type
, kDefaultLevel
, event
);
88 size_t GetMaxEntries() const { return impl_
->max_entries(); }
90 scoped_ptr
<DeviceEventLogImpl
> impl_
;
93 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogTest
);
96 TEST_F(DeviceEventLogTest
, TestNetworkEvents
) {
97 std::string output_none
= GetOrderedString(OLDEST_FIRST
, 0);
98 EXPECT_EQ("No Log Entries.", output_none
);
100 LogLevel level
= kDefaultLevel
;
101 AddNetworkEntry("file1", 1, level
, "event1");
102 AddNetworkEntry("file2", 2, level
, "event2");
103 AddNetworkEntry("file3", 3, level
, "event3");
104 AddNetworkEntry("file3", 3, level
, "event3");
106 const std::string
expected_output_oldest_first(
109 "file3:3 event3 (2)\n");
110 std::string output_oldest_first
= GetOrderedString(OLDEST_FIRST
, 0);
111 EXPECT_EQ(expected_output_oldest_first
, output_oldest_first
);
113 const std::string
expected_output_oldest_first_short(
115 "file3:3 event3 (2)\n");
116 std::string output_oldest_first_short
= GetOrderedString(OLDEST_FIRST
, 2);
117 EXPECT_EQ(expected_output_oldest_first_short
, output_oldest_first_short
);
119 const std::string
expected_output_newest_first(
120 "file3:3 event3 (2)\n"
123 std::string output_newest_first
= GetOrderedString(NEWEST_FIRST
, 0);
124 EXPECT_EQ(expected_output_newest_first
, output_newest_first
);
126 const std::string
expected_output_newest_first_short(
127 "file3:3 event3 (2)\n"
129 std::string output_newest_first_short
= GetOrderedString(NEWEST_FIRST
, 2);
130 EXPECT_EQ(expected_output_newest_first_short
, output_newest_first_short
);
133 TEST_F(DeviceEventLogTest
, TestMaxEntries
) {
134 const size_t max_entries
= GetMaxEntries();
135 const size_t entries_to_add
= max_entries
+ 3;
136 for (size_t i
= 0; i
< entries_to_add
; ++i
) {
137 AddTestEvent(LOG_LEVEL_EVENT
, base::StringPrintf("event_%" PRIuS
, i
));
139 std::string output
= GetOrderedString(OLDEST_FIRST
, 0);
140 size_t output_lines
= CountLines(output
);
141 EXPECT_EQ(max_entries
, output_lines
);
144 TEST_F(DeviceEventLogTest
, TestStringFormat
) {
145 AddTestEvent(LOG_LEVEL_ERROR
, "event0");
146 EXPECT_EQ("file:0 event0\n",
147 GetLogString(OLDEST_FIRST
, "file", kDefaultLevel
, 1));
148 EXPECT_EQ("[time] event0\n",
149 SkipTime(GetLogString(OLDEST_FIRST
, "time", kDefaultLevel
, 1)));
150 EXPECT_EQ("event0\n", GetLogString(OLDEST_FIRST
, "", kDefaultLevel
, 1));
151 EXPECT_EQ("<b><i>event0</i></b>\n",
152 GetLogString(OLDEST_FIRST
, "html", kDefaultLevel
, 1));
154 "[time] file:0 event0\n",
155 SkipTime(GetLogString(OLDEST_FIRST
, "file,time", kDefaultLevel
, 1)));
157 AddTestEvent(LOG_LEVEL_DEBUG
, "event1");
158 EXPECT_EQ("[time] file:0 <i>event1</i>\n",
159 SkipTime(GetLogString(OLDEST_FIRST
, "file,time,html",
160 LOG_LEVEL_DEBUG
, 1)));
162 AddTestEvent(kDefaultLevel
, "event2");
163 EXPECT_EQ("Network: file:0 event2\n",
164 GetLogString(OLDEST_FIRST
, "file,type", kDefaultLevel
, 1));
167 TEST_F(DeviceEventLogTest
, TestLogLevel
) {
168 AddTestEvent(LOG_LEVEL_ERROR
, "error1");
169 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
170 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
171 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
172 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
173 AddTestEvent(LOG_LEVEL_DEBUG
, "debug6");
176 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
177 EXPECT_EQ(3u, CountLines(out
));
178 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 0);
179 EXPECT_EQ(5u, CountLines(out
));
180 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0);
181 EXPECT_EQ(6u, CountLines(out
));
183 // Test max_level. Get only the ERROR entries.
184 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
185 EXPECT_EQ("error1\nerror2\nerror4\n", out
);
188 TEST_F(DeviceEventLogTest
, TestMaxEvents
) {
189 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
190 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
191 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
192 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
193 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
196 EXPECT_EQ("error4\n", GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
198 EXPECT_EQ("error2\nerror4\n",
199 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
201 EXPECT_EQ("event3\nerror4\nevent5\n",
202 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
204 EXPECT_EQ("error2\nevent3\nerror4\nevent5\n",
205 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
207 EXPECT_EQ("event1\nerror2\nevent3\nerror4\nevent5\n",
208 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
211 EXPECT_EQ("error4\n", GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
213 EXPECT_EQ("error4\nerror2\n",
214 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
216 EXPECT_EQ("event5\nerror4\nevent3\n",
217 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
219 EXPECT_EQ("event5\nerror4\nevent3\nerror2\n",
220 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
222 EXPECT_EQ("event5\nerror4\nevent3\nerror2\nevent1\n",
223 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
226 TEST_F(DeviceEventLogTest
, TestMaxErrors
) {
227 const int kMaxTestEntries
= 4;
228 impl_
.reset(new DeviceEventLogImpl(kMaxTestEntries
));
229 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
230 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
231 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
232 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
233 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
234 AddTestEvent(LOG_LEVEL_EVENT
, "event6");
235 EXPECT_EQ("error2\nerror4\nevent5\nevent6\n",
236 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0));
239 TEST_F(DeviceEventLogTest
, TestType
) {
240 AddEventType(LOG_TYPE_NETWORK
, "event1");
241 AddEventType(LOG_TYPE_POWER
, "event2");
242 AddEventType(LOG_TYPE_NETWORK
, "event3");
243 AddEventType(LOG_TYPE_POWER
, "event4");
244 AddEventType(LOG_TYPE_NETWORK
, "event5");
245 AddEventType(LOG_TYPE_NETWORK
, "event6");
247 "Network: event1\nNetwork: event3\nNetwork: event5\nNetwork: event6\n",
248 GetLogStringForType("network"));
249 const std::string
power_events("Power: event2\nPower: event4\n");
250 EXPECT_EQ(power_events
, GetLogStringForType("power"));
251 EXPECT_EQ(power_events
, GetLogStringForType("non-network"));
252 const std::string
all_events(
258 "Network: event6\n");
259 EXPECT_EQ(all_events
, GetLogStringForType("network,power"));
260 EXPECT_EQ(all_events
, GetLogStringForType(""));
263 } // namespace device_event_log
265 } // namespace chromeos