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 std::vector
<std::string
> lines
;
57 base::SplitString(input
, '\n', &lines
);
58 for (size_t i
= 0; i
< lines
.size(); ++i
) {
59 size_t n
= lines
[i
].find(']');
60 if (n
!= std::string::npos
)
61 output
+= "[time] " + lines
[i
].substr(n
+ 2) + '\n';
68 size_t CountLines(const std::string
& input
) {
69 return std::count(input
.begin(), input
.end(), '\n');
72 std::string
GetAsString(StringOrder order
,
73 const std::string
& format
,
74 const std::string
& types
,
77 task_runner_
->PostTask(
79 base::Bind(&CallGetAsString
,
80 impl_
.get(), order
, format
, types
, max_level
, max_events
));
81 task_runner_
->RunUntilIdle();
82 return s_string_result
;
85 std::string
GetLogString(StringOrder order
,
86 const std::string
& format
,
89 return GetAsString(order
, format
, "", max_level
, max_events
);
92 std::string
GetOrderedString(StringOrder order
, size_t max_events
) {
93 return GetAsString(order
, "file", "", kDefaultLevel
, max_events
);
96 std::string
GetLogStringForType(const std::string
& types
) {
97 return GetAsString(OLDEST_FIRST
, "type", types
, kDefaultLevel
, 0);
100 void AddNetworkEntry(const char* file
,
103 const std::string
& event
) {
104 impl_
->AddEntry(file
, line
, kDefaultType
, level
, event
);
105 task_runner_
->RunUntilIdle();
108 void AddTestEvent(LogLevel level
, const std::string
& event
) {
109 AddNetworkEntry(kFileName
, 0, level
, event
);
112 void AddEventType(LogType type
, const std::string
& event
) {
113 impl_
->AddEntry(kFileName
, 0, type
, kDefaultLevel
, event
);
114 task_runner_
->RunUntilIdle();
117 size_t GetMaxEntries() const { return impl_
->max_entries(); }
119 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
120 scoped_ptr
<DeviceEventLogImpl
> impl_
;
123 DISALLOW_COPY_AND_ASSIGN(DeviceEventLogTest
);
126 TEST_F(DeviceEventLogTest
, TestNetworkEvents
) {
127 std::string output_none
= GetOrderedString(OLDEST_FIRST
, 0);
128 EXPECT_EQ("No Log Entries.", output_none
);
130 LogLevel level
= kDefaultLevel
;
131 static const char kFile1
[] = "file1";
132 static const char kFile2
[] = "file2";
133 static const char kFile3
[] = "file3";
134 AddNetworkEntry(kFile1
, 1, level
, "event1");
135 AddNetworkEntry(kFile2
, 2, level
, "event2");
136 AddNetworkEntry(kFile3
, 3, level
, "event3");
137 AddNetworkEntry(kFile3
, 3, level
, "event3");
139 const std::string
expected_output_oldest_first(
142 "file3:3 event3 (2)\n");
143 std::string output_oldest_first
= GetOrderedString(OLDEST_FIRST
, 0);
144 EXPECT_EQ(expected_output_oldest_first
, output_oldest_first
);
146 const std::string
expected_output_oldest_first_short(
148 "file3:3 event3 (2)\n");
149 std::string output_oldest_first_short
= GetOrderedString(OLDEST_FIRST
, 2);
150 EXPECT_EQ(expected_output_oldest_first_short
, output_oldest_first_short
);
152 const std::string
expected_output_newest_first(
153 "file3:3 event3 (2)\n"
156 std::string output_newest_first
= GetOrderedString(NEWEST_FIRST
, 0);
157 EXPECT_EQ(expected_output_newest_first
, output_newest_first
);
159 const std::string
expected_output_newest_first_short(
160 "file3:3 event3 (2)\n"
162 std::string output_newest_first_short
= GetOrderedString(NEWEST_FIRST
, 2);
163 EXPECT_EQ(expected_output_newest_first_short
, output_newest_first_short
);
166 TEST_F(DeviceEventLogTest
, TestMaxEntries
) {
167 const size_t max_entries
= GetMaxEntries();
168 const size_t entries_to_add
= max_entries
+ 3;
169 for (size_t i
= 0; i
< entries_to_add
; ++i
) {
170 AddTestEvent(LOG_LEVEL_EVENT
, base::StringPrintf("event_%" PRIuS
, i
));
172 std::string output
= GetOrderedString(OLDEST_FIRST
, 0);
173 size_t output_lines
= CountLines(output
);
174 EXPECT_EQ(max_entries
, output_lines
);
177 TEST_F(DeviceEventLogTest
, TestStringFormat
) {
178 AddTestEvent(LOG_LEVEL_ERROR
, "event0");
179 EXPECT_EQ("file:0 event0\n",
180 GetLogString(OLDEST_FIRST
, "file", kDefaultLevel
, 1));
181 EXPECT_EQ("[time] event0\n",
182 SkipTime(GetLogString(OLDEST_FIRST
, "time", kDefaultLevel
, 1)));
183 EXPECT_EQ("event0\n", GetLogString(OLDEST_FIRST
, "", kDefaultLevel
, 1));
184 EXPECT_EQ("<b><i>event0</i></b>\n",
185 GetLogString(OLDEST_FIRST
, "html", kDefaultLevel
, 1));
187 "[time] file:0 event0\n",
188 SkipTime(GetLogString(OLDEST_FIRST
, "file,time", kDefaultLevel
, 1)));
190 AddTestEvent(LOG_LEVEL_DEBUG
, "event1");
191 EXPECT_EQ("[time] file:0 <i>event1</i>\n",
192 SkipTime(GetLogString(OLDEST_FIRST
, "file,time,html",
193 LOG_LEVEL_DEBUG
, 1)));
195 AddTestEvent(kDefaultLevel
, "event2");
196 EXPECT_EQ("Network: file:0 event2\n",
197 GetLogString(OLDEST_FIRST
, "file,type", kDefaultLevel
, 1));
200 TEST_F(DeviceEventLogTest
, TestLogLevel
) {
201 AddTestEvent(LOG_LEVEL_ERROR
, "error1");
202 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
203 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
204 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
205 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
206 AddTestEvent(LOG_LEVEL_DEBUG
, "debug6");
209 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
210 EXPECT_EQ(3u, CountLines(out
));
211 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 0);
212 EXPECT_EQ(5u, CountLines(out
));
213 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0);
214 EXPECT_EQ(6u, CountLines(out
));
216 // Test max_level. Get only the ERROR entries.
217 out
= GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 0);
218 EXPECT_EQ("error1\nerror2\nerror4\n", out
);
221 TEST_F(DeviceEventLogTest
, TestMaxEvents
) {
222 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
223 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
224 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
225 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
226 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
229 EXPECT_EQ("error4\n", GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
231 EXPECT_EQ("error2\nerror4\n",
232 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
234 EXPECT_EQ("event3\nerror4\nevent5\n",
235 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
237 EXPECT_EQ("error2\nevent3\nerror4\nevent5\n",
238 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
240 EXPECT_EQ("event1\nerror2\nevent3\nerror4\nevent5\n",
241 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
244 EXPECT_EQ("error4\n", GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 1));
246 EXPECT_EQ("error4\nerror2\n",
247 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_ERROR
, 2));
249 EXPECT_EQ("event5\nerror4\nevent3\n",
250 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 3));
252 EXPECT_EQ("event5\nerror4\nevent3\nerror2\n",
253 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 4));
255 EXPECT_EQ("event5\nerror4\nevent3\nerror2\nevent1\n",
256 GetLogString(NEWEST_FIRST
, "", LOG_LEVEL_EVENT
, 5));
259 TEST_F(DeviceEventLogTest
, TestMaxErrors
) {
260 const int kMaxTestEntries
= 4;
261 impl_
.reset(new DeviceEventLogImpl(task_runner_
, kMaxTestEntries
));
262 AddTestEvent(LOG_LEVEL_EVENT
, "event1");
263 AddTestEvent(LOG_LEVEL_ERROR
, "error2");
264 AddTestEvent(LOG_LEVEL_EVENT
, "event3");
265 AddTestEvent(LOG_LEVEL_ERROR
, "error4");
266 AddTestEvent(LOG_LEVEL_EVENT
, "event5");
267 AddTestEvent(LOG_LEVEL_EVENT
, "event6");
268 EXPECT_EQ("error2\nerror4\nevent5\nevent6\n",
269 GetLogString(OLDEST_FIRST
, "", LOG_LEVEL_DEBUG
, 0));
272 TEST_F(DeviceEventLogTest
, TestType
) {
273 AddEventType(LOG_TYPE_NETWORK
, "event1");
274 AddEventType(LOG_TYPE_POWER
, "event2");
275 AddEventType(LOG_TYPE_NETWORK
, "event3");
276 AddEventType(LOG_TYPE_POWER
, "event4");
277 AddEventType(LOG_TYPE_NETWORK
, "event5");
278 AddEventType(LOG_TYPE_NETWORK
, "event6");
280 "Network: event1\nNetwork: event3\nNetwork: event5\nNetwork: event6\n",
281 GetLogStringForType("network"));
282 const std::string
power_events("Power: event2\nPower: event4\n");
283 EXPECT_EQ(power_events
, GetLogStringForType("power"));
284 EXPECT_EQ(power_events
, GetLogStringForType("non-network"));
285 const std::string
all_events(
291 "Network: event6\n");
292 EXPECT_EQ(all_events
, GetLogStringForType("network,power"));
293 EXPECT_EQ(all_events
, GetLogStringForType(""));
296 } // namespace device_event_log