// Copyright 2015 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef BASE_TEST_MOCK_LOG_H_ #define BASE_TEST_MOCK_LOG_H_ #include <stddef.h> #include <string> #include "base/logging.h" #include "base/synchronization/lock.h" #include "testing/gmock/include/gmock/gmock.h" namespace base { namespace test { // A MockLog object intercepts LOG() messages issued during its lifespan. Using // this together with gMock, it's very easy to test how a piece of code calls // LOG(). The typical usage: // // TEST(FooTest, LogsCorrectly) { // MockLog log; // // // We expect the WARNING "Something bad!" exactly twice. // EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) // .Times(2); // // // We allow foo.cc to call LOG(INFO) any number of times. // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _)) // .Times(AnyNumber()); // // log.StartCapturingLogs(); // Call this after done setting expectations. // Foo(); // Exercises the code under test. // } // // CAVEAT: base/logging does not allow a thread to call LOG() again when it's // already inside a LOG() call. Doing so will cause a deadlock. Therefore, // it's the user's responsibility to not call LOG() in an action triggered by // MockLog::Log(). You may call RAW_LOG() instead. class MockLog { … }; } // namespace test } // namespace base #endif // BASE_TEST_MOCK_LOG_H_