Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: base/logging_unittest.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: only keep scoped API Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 unsigned 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")))
320 .Times(0);
321
322 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing2")))
323 .WillOnce(::testing::Return(false));
324 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing2")))
325 .WillOnce(::testing::Return(false));
326 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing2")))
327 .Times(1);
328
329 LOG(WARNING) << "testing1";
330 LOG(WARNING) << "testing2";
331 }
332 EXPECT_EQ(count, LogMessageHandlerCountForTesting());
333 }
334
335 class MockLogMessageListener : logging::LogMessageListener {
336 public:
337 MockLogMessageListener(std::string &buffer): buffer_(buffer) {}
338 void OnMessage(int severity, const char* file, int line,
339 size_t message_start, const std::string& str) override {
340 buffer_.append(str);
341 }
342 private:
343 std::string& buffer_;
344 };
345
346 TEST_F(LoggingTest, LogListenerBasic) {
347 std::string buffer;
348 MockLogMessageListener log(buffer);
349
350 LOG(INFO) << "testing1";
351 LOG(WARNING) << "testing2";
352
353 EXPECT_NE(std::string::npos, buffer.find("testing1"));
354 EXPECT_NE(std::string::npos, buffer.find("testing2"));
355 }
356
357 TEST_F(LoggingTest, LogListenerLifespan) {
358 unsigned count = LogMessageListenerCountForTesting();
359 std::string buf1, buf2;
360 MockLogMessageListener* log1 = nullptr;
361 {
362 MockLogMessageListener log2(buf2);
363 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
364
365 LOG(WARNING) << "testing1";
366 log1 = new MockLogMessageListener(buf1);
367 EXPECT_EQ(count + 2, LogMessageListenerCountForTesting());
368 LOG(WARNING) << "testing2";
369 }
370 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
371 LOG(WARNING) << "testing3";
372 delete log1;
373 EXPECT_EQ(count, LogMessageListenerCountForTesting());
374
375 EXPECT_EQ(std::string::npos, buf1.find("testing1"));
376 EXPECT_NE(std::string::npos, buf1.find("testing2"));
377 EXPECT_NE(std::string::npos, buf1.find("testing3"));
378 EXPECT_NE(std::string::npos, buf2.find("testing1"));
379 EXPECT_NE(std::string::npos, buf2.find("testing2"));
380 EXPECT_EQ(std::string::npos, buf2.find("testing3"));
381 }
382
296 // Test that defining an operator<< for a type in a namespace doesn't prevent 383 // 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) 384 // 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 385 // 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 386 // found by ADL, since defining another operator<< prevents name lookup from
300 // looking in the global namespace. 387 // looking in the global namespace.
301 namespace nested_test { 388 namespace nested_test {
302 class Streamable {}; 389 class Streamable {};
303 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, 390 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out,
304 const Streamable&) { 391 const Streamable&) {
305 return out << "Streamable"; 392 return out << "Streamable";
306 } 393 }
307 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 394 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
308 std::wstring wstr = L"Hello World"; 395 std::wstring wstr = L"Hello World";
309 std::ostringstream ostr; 396 std::ostringstream ostr;
310 ostr << wstr; 397 ostr << wstr;
311 EXPECT_EQ("Hello World", ostr.str()); 398 EXPECT_EQ("Hello World", ostr.str());
312 } 399 }
313 } // namespace nested_test 400 } // namespace nested_test
314 401
315 } // namespace 402 } // namespace
316 403
317 } // namespace logging 404 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698