Cleanup: Remove webkit entries from .gitignore.
[chromium-blink-merge.git] / components / proximity_auth / logging / logging_unittest.cc
blobe6677b9c62e7b5b64f8eff641d7a0646b59cdc32
1 // Copyright 2015 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/proximity_auth/logging/logging.h"
7 #include "base/lazy_instance.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "components/proximity_auth/logging/log_buffer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace proximity_auth {
14 namespace {
16 const char kLog1[] = "Mahogony destined to make a sturdy table";
17 const char kLog2[] = "Construction grade cedar";
18 const char kLog3[] = "Pine infested by hungry beetles";
20 // Called for every log message added to the standard logging system. The new
21 // log is saved in |g_standard_logs| and consumed so it does not flood stdout.
22 base::LazyInstance<std::vector<std::string>> g_standard_logs =
23 LAZY_INSTANCE_INITIALIZER;
24 bool HandleStandardLogMessage(int severity,
25 const char* file,
26 int line,
27 size_t message_start,
28 const std::string& str) {
29 g_standard_logs.Get().push_back(str);
30 return true;
33 } // namespace
35 class ProximityAuthLoggingTest : public testing::Test {
36 public:
37 ProximityAuthLoggingTest() : previous_handler_(NULL) {}
39 void SetUp() override {
40 LogBuffer::GetInstance()->Clear();
41 g_standard_logs.Get().clear();
43 previous_handler_ = logging::GetLogMessageHandler();
44 logging::SetLogMessageHandler(&HandleStandardLogMessage);
47 void TearDown() override { logging::SetLogMessageHandler(previous_handler_); }
49 private:
50 logging::LogMessageHandlerFunction previous_handler_;
53 TEST_F(ProximityAuthLoggingTest, LogsSavedToBuffer) {
54 int base_line_number = __LINE__;
55 PA_LOG(INFO) << kLog1;
56 PA_LOG(WARNING) << kLog2;
57 PA_LOG(ERROR) << kLog3;
59 auto logs = LogBuffer::GetInstance()->logs();
60 ASSERT_EQ(3u, logs->size());
62 auto iterator = logs->begin();
63 const LogBuffer::LogMessage& log_message1 = *iterator;
64 EXPECT_EQ(kLog1, log_message1.text);
65 EXPECT_EQ(__FILE__, log_message1.file);
66 EXPECT_EQ(base_line_number + 1, log_message1.line);
67 EXPECT_EQ(logging::LOG_INFO, log_message1.severity);
69 ++iterator;
70 const LogBuffer::LogMessage& log_message2 = *iterator;
71 EXPECT_EQ(kLog2, log_message2.text);
72 EXPECT_EQ(__FILE__, log_message2.file);
73 EXPECT_EQ(base_line_number + 2, log_message2.line);
74 EXPECT_EQ(logging::LOG_WARNING, log_message2.severity);
76 ++iterator;
77 const LogBuffer::LogMessage& log_message3 = *iterator;
78 EXPECT_EQ(kLog3, log_message3.text);
79 EXPECT_EQ(__FILE__, log_message3.file);
80 EXPECT_EQ(base_line_number + 3, log_message3.line);
81 EXPECT_EQ(logging::LOG_ERROR, log_message3.severity);
84 TEST_F(ProximityAuthLoggingTest, LogWhenBufferIsFull) {
85 LogBuffer* log_buffer = LogBuffer::GetInstance();
86 EXPECT_EQ(0u, log_buffer->logs()->size());
88 for (size_t i = 0; i < log_buffer->MaxBufferSize(); ++i) {
89 PA_LOG(INFO) << "log " << i;
92 EXPECT_EQ(log_buffer->MaxBufferSize(), log_buffer->logs()->size());
93 PA_LOG(INFO) << kLog1;
94 EXPECT_EQ(log_buffer->MaxBufferSize(), log_buffer->logs()->size());
96 auto iterator = log_buffer->logs()->begin();
97 for (size_t i = 0; i < log_buffer->MaxBufferSize() - 1; ++iterator, ++i) {
98 std::string expected_text = "log " + base::IntToString(i + 1);
99 EXPECT_EQ(expected_text, (*iterator).text);
101 EXPECT_EQ(kLog1, (*iterator).text);
104 TEST_F(ProximityAuthLoggingTest, StandardLogsCreated) {
105 PA_LOG(INFO) << kLog1;
106 PA_LOG(WARNING) << kLog2;
107 PA_LOG(ERROR) << kLog3;
109 ASSERT_EQ(3u, g_standard_logs.Get().size());
110 EXPECT_NE(std::string::npos, g_standard_logs.Get()[0].find(kLog1));
111 EXPECT_NE(std::string::npos, g_standard_logs.Get()[1].find(kLog2));
112 EXPECT_NE(std::string::npos, g_standard_logs.Get()[2].find(kLog3));
115 } // namespace proximity_auth