Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 | 8 |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 class LoggingTest : public testing::Test { | 45 class LoggingTest : public testing::Test { |
| 46 private: | 46 private: |
| 47 LogStateSaver log_state_saver_; | 47 LogStateSaver log_state_saver_; |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 class MockLogSource { | 50 class MockLogSource { |
| 51 public: | 51 public: |
| 52 MOCK_METHOD0(Log, const char*()); | 52 MOCK_METHOD0(Log, const char*()); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // Message handler used by test launcher to debug CHECK/DCHECK. | |
| 56 // This object will temporary disable message handler for test. | |
|
dcheng
2017/01/20 08:26:23
Nit: Scoper object to temporarily clear any log me
alex-ac
2017/01/20 12:11:16
Done.
| |
| 57 class MockLogMessageHandler { | |
| 58 public: | |
| 59 MockLogMessageHandler() : saved_handler_(GetLogMessageHandler()) { | |
| 60 SetLogMessageHandler(nullptr); | |
| 61 } | |
| 62 | |
| 63 ~MockLogMessageHandler() { SetLogMessageHandler(saved_handler_); } | |
| 64 | |
| 65 private: | |
| 66 LogMessageHandlerFunction saved_handler_; | |
| 67 }; | |
| 68 | |
| 55 TEST_F(LoggingTest, BasicLogging) { | 69 TEST_F(LoggingTest, BasicLogging) { |
| 56 MockLogSource mock_log_source; | 70 MockLogSource mock_log_source; |
| 57 EXPECT_CALL(mock_log_source, Log()).Times(DEBUG_MODE ? 16 : 8). | 71 EXPECT_CALL(mock_log_source, Log()).Times(DEBUG_MODE ? 16 : 8). |
| 58 WillRepeatedly(Return("log message")); | 72 WillRepeatedly(Return("log message")); |
| 59 | 73 |
| 60 SetMinLogLevel(LOG_INFO); | 74 SetMinLogLevel(LOG_INFO); |
| 61 | 75 |
| 62 EXPECT_TRUE(LOG_IS_ON(INFO)); | 76 EXPECT_TRUE(LOG_IS_ON(INFO)); |
| 63 // As of g++-4.5, the first argument to EXPECT_EQ cannot be a | 77 // As of g++-4.5, the first argument to EXPECT_EQ cannot be a |
| 64 // constant expression. | 78 // constant expression. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 DLOG_IF(INFO, true) << mock_log_source.Log(); | 159 DLOG_IF(INFO, true) << mock_log_source.Log(); |
| 146 DPLOG(INFO) << mock_log_source.Log(); | 160 DPLOG(INFO) << mock_log_source.Log(); |
| 147 DPLOG_IF(INFO, true) << mock_log_source.Log(); | 161 DPLOG_IF(INFO, true) << mock_log_source.Log(); |
| 148 DVLOG(1) << mock_log_source.Log(); | 162 DVLOG(1) << mock_log_source.Log(); |
| 149 DVLOG_IF(1, true) << mock_log_source.Log(); | 163 DVLOG_IF(1, true) << mock_log_source.Log(); |
| 150 DVPLOG(1) << mock_log_source.Log(); | 164 DVPLOG(1) << mock_log_source.Log(); |
| 151 DVPLOG_IF(1, true) << mock_log_source.Log(); | 165 DVPLOG_IF(1, true) << mock_log_source.Log(); |
| 152 } | 166 } |
| 153 | 167 |
| 154 TEST_F(LoggingTest, LoggingIsLazyByDestination) { | 168 TEST_F(LoggingTest, LoggingIsLazyByDestination) { |
| 169 MockLogMessageHandler mock_handler; | |
| 155 MockLogSource mock_log_source; | 170 MockLogSource mock_log_source; |
| 156 MockLogSource mock_log_source_error; | 171 MockLogSource mock_log_source_error; |
| 157 EXPECT_CALL(mock_log_source, Log()).Times(0); | 172 EXPECT_CALL(mock_log_source, Log()).Times(0); |
| 158 | 173 |
| 159 // Severity >= ERROR is always printed to stderr. | 174 // Severity >= ERROR is always printed to stderr. |
| 160 EXPECT_CALL(mock_log_source_error, Log()).Times(1). | 175 EXPECT_CALL(mock_log_source_error, Log()).Times(1). |
| 161 WillRepeatedly(Return("log message")); | 176 WillRepeatedly(Return("log message")); |
| 162 | 177 |
| 163 LoggingSettings settings; | 178 LoggingSettings settings; |
| 164 settings.logging_dest = LOG_NONE; | 179 settings.logging_dest = LOG_NONE; |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 std::wstring wstr = L"Hello World"; | 356 std::wstring wstr = L"Hello World"; |
| 342 std::ostringstream ostr; | 357 std::ostringstream ostr; |
| 343 ostr << wstr; | 358 ostr << wstr; |
| 344 EXPECT_EQ("Hello World", ostr.str()); | 359 EXPECT_EQ("Hello World", ostr.str()); |
| 345 } | 360 } |
| 346 } // namespace nested_test | 361 } // namespace nested_test |
| 347 | 362 |
| 348 } // namespace | 363 } // namespace |
| 349 | 364 |
| 350 } // namespace logging | 365 } // namespace logging |
| OLD | NEW |