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" |
| 11 | 11 |
| 12 namespace logging { | 12 namespace logging { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 using ::testing::HasSubstr; | |
| 16 using ::testing::Return; | 17 using ::testing::Return; |
| 18 using ::testing::_; | |
| 17 | 19 |
| 18 // Needs to be global since log assert handlers can't maintain state. | 20 // Needs to be global since log assert handlers can't maintain state. |
| 19 int log_sink_call_count = 0; | 21 int log_sink_call_count = 0; |
| 20 | 22 |
| 21 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 23 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
| 22 void LogSink(const std::string& str) { | 24 void LogSink(const std::string& str) { |
| 23 ++log_sink_call_count; | 25 ++log_sink_call_count; |
| 24 } | 26 } |
| 25 #endif | 27 #endif |
| 26 | 28 |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 if (false) | 288 if (false) |
| 287 CHECK_EQ(false, true); // Unreached. | 289 CHECK_EQ(false, true); // Unreached. |
| 288 else | 290 else |
| 289 CHECK_EQ(true, reached = true); // Reached, passed. | 291 CHECK_EQ(true, reached = true); // Reached, passed. |
| 290 ASSERT_TRUE(reached); | 292 ASSERT_TRUE(reached); |
| 291 | 293 |
| 292 if (false) | 294 if (false) |
| 293 CHECK_EQ(false, true); // Unreached. | 295 CHECK_EQ(false, true); // Unreached. |
| 294 } | 296 } |
| 295 | 297 |
| 298 class MockLogMessageHandler : logging::LogMessageHandler { | |
| 299 public: | |
| 300 MOCK_METHOD5(OnMessage, | |
| 301 bool(int severity, | |
| 302 const char* file, | |
| 303 int line, | |
| 304 size_t message_start, | |
| 305 const std::string& str)); | |
| 306 }; | |
| 307 | |
| 308 TEST_F(LoggingTest, LogHandlerSink) { | |
| 309 size_t count = LogMessageHandlerCountForTesting(); | |
| 310 { | |
| 311 MockLogMessageHandler log, log2, log3; | |
| 312 EXPECT_EQ(count + 3, LogMessageHandlerCountForTesting()); | |
| 313 | |
| 314 testing::InSequence s; | |
| 315 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing1"))) | |
| 316 .WillOnce(::testing::Return(false)); | |
| 317 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing1"))) | |
| 318 .WillOnce(::testing::Return(true)); | |
| 319 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing1"))).Times(0); | |
| 320 | |
| 321 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing2"))) | |
| 322 .WillOnce(::testing::Return(false)); | |
| 323 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing2"))) | |
| 324 .WillOnce(::testing::Return(false)); | |
| 325 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing2"))).Times(1); | |
| 326 | |
| 327 LOG(WARNING) << "testing1"; | |
| 328 LOG(WARNING) << "testing2"; | |
| 329 } | |
| 330 EXPECT_EQ(count, LogMessageHandlerCountForTesting()); | |
| 331 } | |
| 332 | |
| 333 class MockLogMessageListener : logging::LogMessageListener { | |
| 334 public: | |
| 335 MockLogMessageListener(std::string& buffer) : buffer_(buffer) {} | |
| 336 void OnMessage(int severity, | |
| 337 const char* file, | |
| 338 int line, | |
| 339 size_t message_start, | |
| 340 const std::string& str) override { | |
| 341 buffer_.append(str); | |
| 342 } | |
| 343 | |
| 344 private: | |
| 345 std::string& buffer_; | |
| 346 }; | |
| 347 | |
| 348 TEST_F(LoggingTest, LogListenerBasic) { | |
| 349 std::string buffer; | |
| 350 MockLogMessageListener log(buffer); | |
| 351 | |
| 352 LOG(INFO) << "testing1"; | |
| 353 LOG(WARNING) << "testing2"; | |
| 354 | |
| 355 EXPECT_NE(std::string::npos, buffer.find("testing1")); | |
| 356 EXPECT_NE(std::string::npos, buffer.find("testing2")); | |
| 357 } | |
| 358 | |
| 359 TEST_F(LoggingTest, LogListenerLifespan) { | |
| 360 size_t count = LogMessageListenerCountForTesting(); | |
| 361 std::string buf1, buf2; | |
| 362 MockLogMessageListener* log1 = nullptr; | |
| 363 { | |
| 364 MockLogMessageListener log2(buf2); | |
| 365 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting()); | |
| 366 | |
| 367 LOG(WARNING) << "testing1"; | |
| 368 log1 = new MockLogMessageListener(buf1); | |
| 369 EXPECT_EQ(count + 2, LogMessageListenerCountForTesting()); | |
| 370 LOG(WARNING) << "testing2"; | |
| 371 } | |
| 372 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting()); | |
| 373 LOG(WARNING) << "testing3"; | |
| 374 delete log1; | |
| 375 EXPECT_EQ(count, LogMessageListenerCountForTesting()); | |
| 376 | |
| 377 EXPECT_EQ(std::string::npos, buf1.find("testing1")); | |
| 378 EXPECT_NE(std::string::npos, buf1.find("testing2")); | |
| 379 EXPECT_NE(std::string::npos, buf1.find("testing3")); | |
| 380 EXPECT_NE(std::string::npos, buf2.find("testing1")); | |
| 381 EXPECT_NE(std::string::npos, buf2.find("testing2")); | |
| 382 EXPECT_EQ(std::string::npos, buf2.find("testing3")); | |
| 383 } | |
| 384 | |
|
wychen
2016/08/17 17:08:54
Note to self: generate message inside OnMessage().
| |
| 296 // Test that defining an operator<< for a type in a namespace doesn't prevent | 385 // Test that defining an operator<< for a type in a namespace doesn't prevent |
| 297 // other code in that namespace from calling the operator<<(ostream, wstring) | 386 // other code in that namespace from calling the operator<<(ostream, wstring) |
| 298 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be | 387 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be |
| 299 // found by ADL, since defining another operator<< prevents name lookup from | 388 // found by ADL, since defining another operator<< prevents name lookup from |
| 300 // looking in the global namespace. | 389 // looking in the global namespace. |
| 301 namespace nested_test { | 390 namespace nested_test { |
| 302 class Streamable {}; | 391 class Streamable {}; |
| 303 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, | 392 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, |
| 304 const Streamable&) { | 393 const Streamable&) { |
| 305 return out << "Streamable"; | 394 return out << "Streamable"; |
| 306 } | 395 } |
| 307 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { | 396 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { |
| 308 std::wstring wstr = L"Hello World"; | 397 std::wstring wstr = L"Hello World"; |
| 309 std::ostringstream ostr; | 398 std::ostringstream ostr; |
| 310 ostr << wstr; | 399 ostr << wstr; |
| 311 EXPECT_EQ("Hello World", ostr.str()); | 400 EXPECT_EQ("Hello World", ostr.str()); |
| 312 } | 401 } |
| 313 } // namespace nested_test | 402 } // namespace nested_test |
| 314 | 403 |
| 315 } // namespace | 404 } // namespace |
| 316 | 405 |
| 317 } // namespace logging | 406 } // namespace logging |
| OLD | NEW |