Index: base/test/mock_log.h |
diff --git a/net/test/scoped_mock_log.h b/base/test/mock_log.h |
similarity index 41% |
rename from net/test/scoped_mock_log.h |
rename to base/test/mock_log.h |
index e1edfcccde8cd5150708a792043799b41cc8cc00..315ef1fb2f088d121b92d27c68d32cd020513899 100644 |
--- a/net/test/scoped_mock_log.h |
+++ b/base/test/mock_log.h |
@@ -1,23 +1,26 @@ |
-// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#ifndef NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ |
-#define NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ |
+#ifndef BASE_TEST_MOCK_LOG_H_ |
+#define BASE_TEST_MOCK_LOG_H_ |
+ |
+#include <string> |
#include "base/logging.h" |
+#include "base/macros.h" |
+#include "base/synchronization/lock.h" |
#include "testing/gmock/include/gmock/gmock.h" |
-#include "testing/gtest/include/gtest/gtest.h" |
-namespace net { |
+namespace base { |
namespace test { |
-// A ScopedMockLog 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: |
+// 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) { |
-// ScopedMockLog log; |
+// MockLog log; |
// |
// // We expect the WARNING "Something bad!" exactly twice. |
// EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) |
@@ -31,51 +34,46 @@ namespace test { |
// 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 ScopedMockLog::Log(). You may call |
-// RAW_LOG() instead. |
-class ScopedMockLog { |
+// 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 { |
public: |
- // Creates a ScopedMockLog object that is not capturing logs. |
- // If it were to start to capture logs, it could be a problem if |
- // some other threads already exist and are logging, as the user |
- // hasn't had a chance to set up expectation on this object yet |
- // (calling a mock method before setting the expectation is |
+ // Creates a MockLog object that is not capturing logs. If it were to start |
+ // to capture logs, it could be a problem if some other threads already exist |
+ // and are logging, as the user hasn't had a chance to set up expectation on |
+ // this object yet (calling a mock method before setting the expectation is |
// UNDEFINED behavior). |
- ScopedMockLog(); |
+ MockLog(); |
// When the object is destructed, it stops intercepting logs. |
- ~ScopedMockLog(); |
+ ~MockLog(); |
// Starts log capturing if the object isn't already doing so. |
- // Otherwise crashes. Usually this method is called in the same |
- // thread that created this object. It is the user's responsibility |
- // to not call this method if another thread may be calling it or |
- // StopCapturingLogs() at the same time. |
+ // Otherwise crashes. |
void StartCapturingLogs(); |
- // Stops log capturing if the object is capturing logs. Otherwise |
- // crashes. Usually this method is called in the same thread that |
- // created this object. It is the user's responsibility to not call |
- // this method if another thread may be calling it or |
- // StartCapturingLogs() at the same time. |
+ // Stops log capturing if the object is capturing logs. Otherwise crashes. |
void StopCapturingLogs(); |
- // Sets the Log Message Handler that gets passed every log message before |
- // it's sent to other log destinations (if any). |
- // Returns true to signal that it handled the message and the message |
- // should not be sent to other log destinations. |
- MOCK_METHOD5(Log, bool(int severity, |
- const char* file, |
- int line, |
- size_t message_start, |
- const std::string& str)); |
+ // Log method is invoked for every log message before it's sent to other log |
+ // destinations (if any). The method should return true to signal that it |
+ // handled the message and the message should not be sent to other log |
+ // destinations. |
+ MOCK_METHOD5(Log, |
+ bool(int severity, |
+ const char* file, |
+ int line, |
+ size_t message_start, |
+ const std::string& str)); |
private: |
- // The currently active scoped mock log. |
- static ScopedMockLog* g_instance_; |
+ // The currently active mock log. |
+ static MockLog* g_instance_; |
+ |
+ // Lock protecting access to g_instance_. |
+ static Lock g_lock; |
// Static function which is set as the logging message handler. |
// Called once for each message. |
@@ -88,11 +86,13 @@ class ScopedMockLog { |
// True if this object is currently capturing logs. |
bool is_capturing_logs_; |
- // The previous handler to restore when the ScopedMockLog is destroyed. |
+ // The previous handler to restore when the MockLog is destroyed. |
logging::LogMessageHandlerFunction previous_handler_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockLog); |
}; |
} // namespace test |
-} // namespace net |
+} // namespace base |
-#endif // NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ |
+#endif // BASE_TEST_MOCK_LOG_H_ |