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"
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/format_macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/test/test_simple_task_runner.h"
17 #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
;
26 const char kFileName
[] = "file";
28 // Calls GetAsString on the task thread and sets s_string_result. Make sure
29 // that task_runner_->RunUntilIdle() is called before using s_string_result.
30 std::string s_string_result
;
31 void CallGetAsString(DeviceEventLogImpl
* impl
,
33 const std::string
& format
,
34 const std::string
& types
,
38 impl
->GetAsString(order
, format
, types
, max_level
, max_events
);
43 class DeviceEventLogTest
: public testing::Test
{
45 DeviceEventLogTest() : task_runner_(new base::TestSimpleTaskRunner()) {}
47 void SetUp() override
{
48 impl_
.reset(new DeviceEventLogImpl(task_runner_
, kDefaultMaxEvents
));
51 void TearDown() override
{ impl_
.reset(); }
54 std::string
SkipTime(const std::string
& input
) {
56 for (const std::string
& line
: base::SplitString(
57 input
, "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
)) {
58 size_t n
= line
.find(']');
59 if (n
!= std::string::npos
)
60 output
+= "[time] " + line
.substr(n
+ 2) + '\n';
67 size_t CountLines(const std::string
& input
) {
68 return std::count(input
.begin(), input
.end(), '\n');
71 std::string
GetAsString(StringOrder order
,
72 const std::string
& format
,
73 const std::string
& types
,
76 task_runner_
->PostTask(
78 base::Bind(&CallGetAsString
,
79 impl_
.get(), order
, format
, types
, max_level
, max_events
));
80 task_runner_
->RunUntilIdle();
81 return s_string_result
;
84 std::string
GetLogString(StringOrder order
,
85 const std::string
& format
,
88 return GetAsString(order
, format
, "", max_level
, max_events
);
91 std::string
GetOrderedString(StringOrder order
, size_t max_events
) {
92 return GetAsString(order
, "file", "", kDefaultLevel
, max_events
);
95 std::string
GetLogStringForType(const std::string
& types
) {
96 return GetAsString(OLDEST_FIRST
, "type", types
, kDefaultLevel
, 0);
99 void AddNetworkEntry(const char* file
,
102 const std::string
& event
) {
103 impl_
->AddEntry(file
, line
, kDefaultType
, level
, event
);
104 task_runner_
->RunUntilIdle();
107 void AddTestEvent(LogLevel level
, const std::string
& event
) {
108 AddNetworkEntry(kFileName
, 0, level
, event
);
111 void AddEventType(LogType type
, const std::string
& event
) {
112 impl_
->AddEntry(kFileName
, 0, type
, kDefaultLevel
, event
);
113 task_runner_
->RunUntilIdle();
116 size_t GetMaxEntries() const { return impl_
->max_entries(); }
118 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
119 scoped_ptr
<DeviceEventLogImpl
> impl_
;
122 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogTest
);
125 TEST_F(DeviceEventLogTest
, TestNetworkEvents
) {
126 std::string output_none
= GetOrderedString(OLDEST_FIRST
, 0);
127 EXPECT_EQ("No Log Entries.", output_none
);
129 LogLevel level
= kDefaultLevel
;
130 static const char kFile1
[] = "file1";
131 static const char kFile2
[] = "file2";
132 static const char kFile3
[] = "file3";
133 AddNetworkEntry(kFile1
, 1, level
, "event1");
134 AddNetworkEntry(kFile2
, 2, level
, "event2");
135 AddNetworkEntry(kFile3
, 3, level
, "event3");
136 AddNetworkEntry(kFile3
, 3, level
, "event3");
138 const std::string
expected_output_oldest_first(
141 "file3:3 event3 (2)\n");
142 std::string output_oldest_first
= GetOrderedString(OLDEST_FIRST
, 0);
143 EXPECT_EQ(expected_output_oldest_first
, output_oldest_first
);
145 const std::string
expected_output_oldest_first_short(
147 "file3:3 event3 (2)\n");
148 std::string output_oldest_first_short
= GetOrderedString(OLDEST_FIRST
, 2);
149 EXPECT_EQ(expected_output_oldest_first_short
, output_oldest_first_short
);
151 const std::string
expected_output_newest_first(
152 "file3:3 event3 (2)\n"
155 std::string output_newest_first
= GetOrderedString(NEWEST_FIRST
, 0);
156 EXPECT_EQ(expected_output_newest_first
, output_newest_first
);
158 const std::string
expected_output_newest_first_short(
159 "file3:3 event3 (2)\n"
161 std::string output_newest_first_short
= GetOrderedString(NEWEST_FIRST
, 2);
162 EXPECT_EQ(expected_output_newest_first_short
, output_newest_first_short
);
165 TEST_F(DeviceEventLogTest
, TestMaxEntries
) {
166 const size_t max_entries
= GetMaxEntries();
167 const size_t entries_to_add
= max_entries
+ 3;
168 for (size_t i
= 0; i
< entries_to_add
; ++i
) {
169 AddTestEvent(LOG_LEVEL_EVENT
, base::StringPrintf("event_%" PRIuS
, i
));
171 std::string output
= GetOrderedString(OLDEST_FIRST
, 0);
172 size_t output_lines
= CountLines(output
);
173 EXPECT_EQ(max_entries
, output_lines
);
176 TEST_F(DeviceEventLogTest
, TestStringFormat
) {
177 AddTestEvent(LOG_LEVEL_ERROR
, "event0");
178 EXPECT_EQ("file:0 event0\n",
179 GetLogString(OLDEST_FIRST
, "file", kDefaultLevel
, 1));
180 EXPECT_EQ("[time] event0\n",
181 SkipTime(GetLogString(OLDEST_FIRST
, "time", kDefaultLevel
, 1)));
182 EXPECT_EQ("event0\n", GetLogString(OLDEST_FIRST
, "", kDefaultLevel
, 1));
183 EXPECT_EQ("<b><i>event0</i></b>\n",
184 GetLogString(OLDEST_FIRST
, "html", kDefaultLevel
, 1));
186 "[time] file:0 event0\n",
187 SkipTime(GetLogString(OLDEST_FIRST
, "file,time", kDefaultLevel
, 1)));
189 AddTestEvent(LOG_LEVEL_DEBUG
, "event1");
190 EXPECT_EQ("[time] file:0 <i>event1</i>\n",
191 SkipTime(GetLogString(OLDEST_FIRST
, "file,time,html",
192 LOG_LEVEL_DEBUG
, 1)));
194 AddTestEvent(kDefaultLevel
, "event2");
195 EXPECT_EQ("Network: file:0 event2\n",
196 GetLogString(OLDEST_FIRST
, "file,type", kDefaultLevel
, 1));
199 TEST_F(DeviceEventLogTest
, TestLogLevel
) {
200 AddTestEvent(LOG_LEVEL_ERROR
, "error1");
201 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
202 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
203 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
204 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
205 AddTestEvent(LOG_LEVEL_DEBUG
, "debug6");
208 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
209 EXPECT_EQ(3u, CountLines(out
));
210 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 0);
211 EXPECT_EQ(5u, CountLines(out
));
212 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0);
213 EXPECT_EQ(6u, CountLines(out
));
215 // Test max_level. Get only the ERROR entries.
216 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
217 EXPECT_EQ("error1\nerror2\nerror4\n", out
);
220 TEST_F(DeviceEventLogTest
, TestMaxEvents
) {
221 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
222 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
223 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
224 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
225 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
228 EXPECT_EQ("error4\n", GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
230 EXPECT_EQ("error2\nerror4\n",
231 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
233 EXPECT_EQ("event3\nerror4\nevent5\n",
234 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
236 EXPECT_EQ("error2\nevent3\nerror4\nevent5\n",
237 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
239 EXPECT_EQ("event1\nerror2\nevent3\nerror4\nevent5\n",
240 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
243 EXPECT_EQ("error4\n", GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
245 EXPECT_EQ("error4\nerror2\n",
246 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
248 EXPECT_EQ("event5\nerror4\nevent3\n",
249 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
251 EXPECT_EQ("event5\nerror4\nevent3\nerror2\n",
252 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
254 EXPECT_EQ("event5\nerror4\nevent3\nerror2\nevent1\n",
255 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
258 TEST_F(DeviceEventLogTest
, TestMaxErrors
) {
259 const int kMaxTestEntries
= 4;
260 impl_
.reset(new DeviceEventLogImpl(task_runner_
, kMaxTestEntries
));
261 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
262 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
263 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
264 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
265 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
266 AddTestEvent(LOG_LEVEL_EVENT
, "event6");
267 EXPECT_EQ("error2\nerror4\nevent5\nevent6\n",
268 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0));
271 TEST_F(DeviceEventLogTest
, TestType
) {
272 AddEventType(LOG_TYPE_NETWORK
, "event1");
273 AddEventType(LOG_TYPE_POWER
, "event2");
274 AddEventType(LOG_TYPE_NETWORK
, "event3");
275 AddEventType(LOG_TYPE_POWER
, "event4");
276 AddEventType(LOG_TYPE_NETWORK
, "event5");
277 AddEventType(LOG_TYPE_NETWORK
, "event6");
279 "Network: event1\nNetwork: event3\nNetwork: event5\nNetwork: event6\n",
280 GetLogStringForType("network"));
281 const std::string
power_events("Power: event2\nPower: event4\n");
282 EXPECT_EQ(power_events
, GetLogStringForType("power"));
283 EXPECT_EQ(power_events
, GetLogStringForType("non-network"));
284 const std::string
all_events(
290 "Network: event6\n");
291 EXPECT_EQ(all_events
, GetLogStringForType("network,power"));
292 EXPECT_EQ(all_events
, GetLogStringForType(""));
295 } // namespace device_event_log