| 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/bind.h" |
| 6 #include "base/callback.h" |
| 5 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 6 #include "base/logging.h" | 8 #include "base/logging.h" |
| 7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/test/logging_utils.h" |
| 8 | 11 |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 namespace logging { | 15 namespace logging { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 using ::testing::Return; | 19 using ::testing::Return; |
| 17 | 20 |
| 18 // Needs to be global since log assert handlers can't maintain state. | 21 // Needs to be global since log assert handlers can't maintain state. |
| 19 int log_sink_call_count = 0; | 22 int log_sink_call_count = 0; |
| 20 | 23 |
| 21 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 24 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
| 22 void LogSink(const std::string& str) { | 25 void LogSink(const char* file, |
| 26 int line, |
| 27 const std::string& message, |
| 28 const std::string& stack_trace) { |
| 23 ++log_sink_call_count; | 29 ++log_sink_call_count; |
| 24 } | 30 } |
| 25 #endif | 31 #endif |
| 26 | 32 |
| 27 // Class to make sure any manipulations we do to the min log level are | 33 // Class to make sure any manipulations we do to the min log level are |
| 28 // contained (i.e., do not affect other unit tests). | 34 // contained (i.e., do not affect other unit tests). |
| 29 class LogStateSaver { | 35 class LogStateSaver { |
| 30 public: | 36 public: |
| 31 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} | 37 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} |
| 32 | 38 |
| 33 ~LogStateSaver() { | 39 ~LogStateSaver() { |
| 34 SetMinLogLevel(old_min_log_level_); | 40 SetMinLogLevel(old_min_log_level_); |
| 35 SetLogAssertHandler(NULL); | |
| 36 log_sink_call_count = 0; | 41 log_sink_call_count = 0; |
| 37 } | 42 } |
| 38 | 43 |
| 39 private: | 44 private: |
| 40 int old_min_log_level_; | 45 int old_min_log_level_; |
| 41 | 46 |
| 42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | 47 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 class LoggingTest : public testing::Test { | 50 class LoggingTest : public testing::Test { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 176 |
| 172 // Official builds have CHECKs directly call BreakDebugger. | 177 // Official builds have CHECKs directly call BreakDebugger. |
| 173 #if !defined(OFFICIAL_BUILD) | 178 #if !defined(OFFICIAL_BUILD) |
| 174 | 179 |
| 175 TEST_F(LoggingTest, CheckStreamsAreLazy) { | 180 TEST_F(LoggingTest, CheckStreamsAreLazy) { |
| 176 MockLogSource mock_log_source, uncalled_mock_log_source; | 181 MockLogSource mock_log_source, uncalled_mock_log_source; |
| 177 EXPECT_CALL(mock_log_source, Log()).Times(8). | 182 EXPECT_CALL(mock_log_source, Log()).Times(8). |
| 178 WillRepeatedly(Return("check message")); | 183 WillRepeatedly(Return("check message")); |
| 179 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); | 184 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); |
| 180 | 185 |
| 181 SetLogAssertHandler(&LogSink); | 186 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
| 182 | 187 |
| 183 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); | 188 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); |
| 184 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); | 189 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); |
| 185 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) | 190 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) |
| 186 << uncalled_mock_log_source.Log(); | 191 << uncalled_mock_log_source.Log(); |
| 187 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) | 192 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) |
| 188 << mock_log_source.Log(); | 193 << mock_log_source.Log(); |
| 189 } | 194 } |
| 190 | 195 |
| 191 #endif | 196 #endif |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 230 } |
| 226 void DcheckEmptyFunction2() {} | 231 void DcheckEmptyFunction2() {} |
| 227 | 232 |
| 228 TEST_F(LoggingTest, Dcheck) { | 233 TEST_F(LoggingTest, Dcheck) { |
| 229 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 234 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 230 // Release build. | 235 // Release build. |
| 231 EXPECT_FALSE(DCHECK_IS_ON()); | 236 EXPECT_FALSE(DCHECK_IS_ON()); |
| 232 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 237 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
| 233 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) | 238 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
| 234 // Release build with real DCHECKS. | 239 // Release build with real DCHECKS. |
| 235 SetLogAssertHandler(&LogSink); | 240 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
| 236 EXPECT_TRUE(DCHECK_IS_ON()); | 241 EXPECT_TRUE(DCHECK_IS_ON()); |
| 237 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 242 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
| 238 #else | 243 #else |
| 239 // Debug build. | 244 // Debug build. |
| 240 SetLogAssertHandler(&LogSink); | 245 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
| 241 EXPECT_TRUE(DCHECK_IS_ON()); | 246 EXPECT_TRUE(DCHECK_IS_ON()); |
| 242 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 247 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
| 243 #endif | 248 #endif |
| 244 | 249 |
| 245 EXPECT_EQ(0, log_sink_call_count); | 250 EXPECT_EQ(0, log_sink_call_count); |
| 246 DCHECK(false); | 251 DCHECK(false); |
| 247 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 252 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
| 248 DPCHECK(false); | 253 DPCHECK(false); |
| 249 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 254 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); |
| 250 DCHECK_EQ(0, 1); | 255 DCHECK_EQ(0, 1); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 std::wstring wstr = L"Hello World"; | 346 std::wstring wstr = L"Hello World"; |
| 342 std::ostringstream ostr; | 347 std::ostringstream ostr; |
| 343 ostr << wstr; | 348 ostr << wstr; |
| 344 EXPECT_EQ("Hello World", ostr.str()); | 349 EXPECT_EQ("Hello World", ostr.str()); |
| 345 } | 350 } |
| 346 } // namespace nested_test | 351 } // namespace nested_test |
| 347 | 352 |
| 348 } // namespace | 353 } // namespace |
| 349 | 354 |
| 350 } // namespace logging | 355 } // namespace logging |
| OLD | NEW |