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
, kDefaultType
, max_level
,
66 std::string
GetOrderedString(StringOrder order
, size_t max_events
) {
67 return impl_
->GetAsString(order
, "file", kDefaultType
, kDefaultLevel
,
71 std::string
GetLogStringForType(LogType type
) {
72 return impl_
->GetAsString(OLDEST_FIRST
, "type", type
, kDefaultLevel
, 0);
75 void AddNetworkEntry(const char* file
,
78 const std::string
& event
) {
79 impl_
->AddEntry(file
, line
, kDefaultType
, level
, event
);
82 void AddTestEvent(LogLevel level
, const std::string
& event
) {
83 AddNetworkEntry("file", 0, level
, event
);
86 void AddEventType(LogType type
, const std::string
& event
) {
87 impl_
->AddEntry("file", 0, type
, kDefaultLevel
, event
);
90 size_t GetMaxEntries() const { return impl_
->max_entries(); }
92 scoped_ptr
<DeviceEventLogImpl
> impl_
;
95 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogTest
);
98 TEST_F(DeviceEventLogTest
, TestNetworkEvents
) {
99 std::string output_none
= GetOrderedString(OLDEST_FIRST
, 0);
100 EXPECT_EQ("No Log Entries.", output_none
);
102 LogLevel level
= kDefaultLevel
;
103 AddNetworkEntry("file1", 1, level
, "event1");
104 AddNetworkEntry("file2", 2, level
, "event2");
105 AddNetworkEntry("file3", 3, level
, "event3");
106 AddNetworkEntry("file3", 3, level
, "event3");
108 const std::string
expected_output_oldest_first(
111 "file3:3 event3 (2)\n");
112 std::string output_oldest_first
= GetOrderedString(OLDEST_FIRST
, 0);
113 EXPECT_EQ(expected_output_oldest_first
, output_oldest_first
);
115 const std::string
expected_output_oldest_first_short(
117 "file3:3 event3 (2)\n");
118 std::string output_oldest_first_short
= GetOrderedString(OLDEST_FIRST
, 2);
119 EXPECT_EQ(expected_output_oldest_first_short
, output_oldest_first_short
);
121 const std::string
expected_output_newest_first(
122 "file3:3 event3 (2)\n"
125 std::string output_newest_first
= GetOrderedString(NEWEST_FIRST
, 0);
126 EXPECT_EQ(expected_output_newest_first
, output_newest_first
);
128 const std::string
expected_output_newest_first_short(
129 "file3:3 event3 (2)\n"
131 std::string output_newest_first_short
= GetOrderedString(NEWEST_FIRST
, 2);
132 EXPECT_EQ(expected_output_newest_first_short
, output_newest_first_short
);
135 TEST_F(DeviceEventLogTest
, TestMaxEntries
) {
136 const size_t max_entries
= GetMaxEntries();
137 const size_t entries_to_add
= max_entries
+ 3;
138 for (size_t i
= 0; i
< entries_to_add
; ++i
) {
139 AddTestEvent(LOG_LEVEL_EVENT
, base::StringPrintf("event_%" PRIuS
, i
));
141 std::string output
= GetOrderedString(OLDEST_FIRST
, 0);
142 size_t output_lines
= CountLines(output
);
143 EXPECT_EQ(max_entries
, output_lines
);
146 TEST_F(DeviceEventLogTest
, TestStringFormat
) {
147 AddTestEvent(LOG_LEVEL_ERROR
, "event0");
148 EXPECT_EQ("file:0 event0\n",
149 GetLogString(OLDEST_FIRST
, "file", kDefaultLevel
, 1));
150 EXPECT_EQ("[time] event0\n",
151 SkipTime(GetLogString(OLDEST_FIRST
, "time", kDefaultLevel
, 1)));
152 EXPECT_EQ("event0\n", GetLogString(OLDEST_FIRST
, "", kDefaultLevel
, 1));
153 EXPECT_EQ("<b><i>event0</i></b>\n",
154 GetLogString(OLDEST_FIRST
, "html", kDefaultLevel
, 1));
156 "[time] file:0 event0\n",
157 SkipTime(GetLogString(OLDEST_FIRST
, "file,time", kDefaultLevel
, 1)));
159 AddTestEvent(LOG_LEVEL_DEBUG
, "event1");
160 EXPECT_EQ("[time] file:0 <i>event1</i>\n",
161 SkipTime(GetLogString(OLDEST_FIRST
, "file,time,html",
162 LOG_LEVEL_DEBUG
, 1)));
164 AddTestEvent(kDefaultLevel
, "event2");
165 EXPECT_EQ("Network: file:0 event2\n",
166 GetLogString(OLDEST_FIRST
, "file,type", kDefaultLevel
, 1));
169 TEST_F(DeviceEventLogTest
, TestLogLevel
) {
170 AddTestEvent(LOG_LEVEL_ERROR
, "error1");
171 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
172 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
173 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
174 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
175 AddTestEvent(LOG_LEVEL_DEBUG
, "debug6");
178 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
179 EXPECT_EQ(3u, CountLines(out
));
180 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 0);
181 EXPECT_EQ(5u, CountLines(out
));
182 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0);
183 EXPECT_EQ(6u, CountLines(out
));
185 // Test max_level. Get only the ERROR entries.
186 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
187 EXPECT_EQ("error1\nerror2\nerror4\n", out
);
190 TEST_F(DeviceEventLogTest
, TestMaxEvents
) {
191 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
192 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
193 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
194 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
195 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
198 EXPECT_EQ("error4\n", GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
200 EXPECT_EQ("error2\nerror4\n",
201 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
203 EXPECT_EQ("event3\nerror4\nevent5\n",
204 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
206 EXPECT_EQ("error2\nevent3\nerror4\nevent5\n",
207 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
209 EXPECT_EQ("event1\nerror2\nevent3\nerror4\nevent5\n",
210 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
213 EXPECT_EQ("error4\n", GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
215 EXPECT_EQ("error4\nerror2\n",
216 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
218 EXPECT_EQ("event5\nerror4\nevent3\n",
219 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
221 EXPECT_EQ("event5\nerror4\nevent3\nerror2\n",
222 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
224 EXPECT_EQ("event5\nerror4\nevent3\nerror2\nevent1\n",
225 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
228 TEST_F(DeviceEventLogTest
, TestMaxErrors
) {
229 const int kMaxTestEntries
= 4;
230 impl_
.reset(new DeviceEventLogImpl(kMaxTestEntries
));
231 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
232 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
233 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
234 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
235 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
236 AddTestEvent(LOG_LEVEL_EVENT
, "event6");
237 EXPECT_EQ("error2\nerror4\nevent5\nevent6\n",
238 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0));
241 TEST_F(DeviceEventLogTest
, TestType
) {
242 AddEventType(LOG_TYPE_NETWORK
, "event1");
243 AddEventType(LOG_TYPE_POWER
, "event2");
244 AddEventType(LOG_TYPE_NETWORK
, "event3");
245 AddEventType(LOG_TYPE_POWER
, "event4");
246 AddEventType(LOG_TYPE_NETWORK
, "event5");
247 AddEventType(LOG_TYPE_NETWORK
, "event6");
249 "Network: event1\nNetwork: event3\nNetwork: event5\nNetwork: event6\n",
250 GetLogStringForType(LOG_TYPE_NETWORK
));
251 EXPECT_EQ("Power: event2\nPower: event4\n",
252 GetLogStringForType(LOG_TYPE_POWER
));
253 EXPECT_EQ("Power: event2\nPower: event4\n",
254 GetLogStringForType(LOG_TYPE_NON_NETWORK
));
262 GetLogStringForType(LOG_TYPE_ALL
));
265 } // namespace device_event_log
267 } // namespace chromeos