1 // Copyright (c) 2012 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/network/network_event_log.h"
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/format_macros.h"
12 #include "base/stringprintf.h"
13 #include "base/strings/string_split.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class NetworkEventLogTest
: public testing::Test
{
20 NetworkEventLogTest() {
23 virtual void SetUp() OVERRIDE
{
24 network_event_log::Initialize();
27 virtual void TearDown() OVERRIDE
{
28 network_event_log::Shutdown();
32 std::string
SkipTime(const std::string
& input
) {
34 std::vector
<std::string
> lines
;
35 base::SplitString(input
, '\n', &lines
);
36 for (size_t i
= 0; i
< lines
.size(); ++i
) {
37 size_t n
= lines
[i
].find(']');
38 if (n
!= std::string::npos
)
39 output
+= lines
[i
].substr(n
+2) + '\n';
44 size_t CountLines(const std::string
& input
) {
45 return std::count(input
.begin(), input
.end(), '\n');
49 DISALLOW_COPY_AND_ASSIGN(NetworkEventLogTest
);
52 TEST_F(NetworkEventLogTest
, TestNetworkEvents
) {
53 std::string output_none
= network_event_log::GetAsString(
54 network_event_log::OLDEST_FIRST
, 0);
55 EXPECT_EQ("No Log Entries.", output_none
);
57 network_event_log::AddEntry("module1", "event1", "description1");
58 network_event_log::AddEntry("module2", "event2", "description2");
59 network_event_log::AddEntry("module3", "event3", "description3");
60 network_event_log::AddEntry("module3", "event3", "description3");
62 const std::string
expected_output_oldest_first(
63 "module1:event1: description1\n"
64 "module2:event2: description2\n"
65 "module3:event3: description3 (2)\n");
66 std::string output_oldest_first
= network_event_log::GetAsString(
67 network_event_log::OLDEST_FIRST
, 0);
68 output_oldest_first
= SkipTime(output_oldest_first
);
69 EXPECT_EQ(expected_output_oldest_first
, output_oldest_first
);
71 const std::string
expected_output_oldest_first_short(
72 "module2:event2: description2\n"
73 "module3:event3: description3 (2)\n");
74 std::string output_oldest_first_short
= network_event_log::GetAsString(
75 network_event_log::OLDEST_FIRST
, 2);
76 output_oldest_first_short
= SkipTime(output_oldest_first_short
);
77 EXPECT_EQ(expected_output_oldest_first_short
, output_oldest_first_short
);
79 const std::string
expected_output_newest_first(
80 "module3:event3: description3 (2)\n"
81 "module2:event2: description2\n"
82 "module1:event1: description1\n");
83 std::string output_newest_first
= network_event_log::GetAsString(
84 network_event_log::NEWEST_FIRST
, 0);
85 output_newest_first
= SkipTime(output_newest_first
);
86 EXPECT_EQ(expected_output_newest_first
, output_newest_first
);
88 const std::string
expected_output_newest_first_short(
89 "module3:event3: description3 (2)\n"
90 "module2:event2: description2\n");
91 std::string output_newest_first_short
= network_event_log::GetAsString(
92 network_event_log::NEWEST_FIRST
, 2);
93 output_newest_first_short
= SkipTime(output_newest_first_short
);
94 EXPECT_EQ(expected_output_newest_first_short
, output_newest_first_short
);
97 TEST_F(NetworkEventLogTest
, TestMaxNetworkEvents
) {
98 const size_t entries_to_add
=
99 network_event_log::kMaxNetworkEventLogEntries
+ 3;
100 for (size_t i
= 0; i
< entries_to_add
; ++i
)
101 network_event_log::AddEntry("test",
102 base::StringPrintf("event_%"PRIuS
, i
), "");
104 std::string output
= GetAsString(network_event_log::OLDEST_FIRST
, 0);
105 size_t output_lines
= CountLines(output
);
106 EXPECT_EQ(network_event_log::kMaxNetworkEventLogEntries
, output_lines
);
109 } // namespace chromeos