Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ | 5 #ifndef BASE_TEST_SCOPED_MOCK_LOG_H_ |
| 6 #define NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ | 6 #define BASE_TEST_SCOPED_MOCK_LOG_H_ |
| 7 | |
| 8 #include <string> | |
| 7 | 9 |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/synchronization/lock.h" | |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | 13 |
| 12 namespace net { | 14 namespace base { |
| 13 namespace test { | |
|
rvargas (doing something else)
2015/03/03 19:45:34
Almost all files on the new destination are outsid
Łukasz Anforowicz
2015/03/04 00:23:01
Good point. I was torn on this myself and ended u
| |
| 14 | 15 |
| 15 // A ScopedMockLog object intercepts LOG() messages issued during its | 16 // A ScopedMockLog object intercepts LOG() messages issued during its |
| 16 // lifespan. Using this together with gMock, it's very easy to test | 17 // lifespan. Using this together with gMock, it's very easy to test |
| 17 // how a piece of code calls LOG(). The typical usage: | 18 // how a piece of code calls LOG(). The typical usage: |
| 18 // | 19 // |
| 19 // TEST(FooTest, LogsCorrectly) { | 20 // TEST(FooTest, LogsCorrectly) { |
| 20 // ScopedMockLog log; | 21 // ScopedMockLog log; |
| 21 // | 22 // |
| 22 // // We expect the WARNING "Something bad!" exactly twice. | 23 // // We expect the WARNING "Something bad!" exactly twice. |
| 23 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) | 24 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) |
| 24 // .Times(2); | 25 // .Times(2); |
| 25 // | 26 // |
| 26 // // We allow foo.cc to call LOG(INFO) any number of times. | 27 // // We allow foo.cc to call LOG(INFO) any number of times. |
| 27 // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _)) | 28 // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _)) |
| 28 // .Times(AnyNumber()); | 29 // .Times(AnyNumber()); |
| 29 // | 30 // |
| 30 // log.StartCapturingLogs(); // Call this after done setting expectations. | 31 // log.StartCapturingLogs(); // Call this after done setting expectations. |
| 31 // Foo(); // Exercises the code under test. | 32 // Foo(); // Exercises the code under test. |
| 32 // } | 33 // } |
| 33 // | 34 // |
| 34 // CAVEAT: base/logging does not allow a thread to call LOG() again | 35 // CAVEAT: base/logging does not allow a thread to call LOG() again |
| 35 // when it's already inside a LOG() call. Doing so will cause a | 36 // when it's already inside a LOG() call. Doing so will cause a |
| 36 // deadlock. Therefore, it's the user's responsibility to not call | 37 // deadlock. Therefore, it's the user's responsibility to not call |
| 37 // LOG() in an action triggered by ScopedMockLog::Log(). You may call | 38 // LOG() in an action triggered by ScopedMockLog::Log(). You may call |
| 38 // RAW_LOG() instead. | 39 // RAW_LOG() instead. |
| 39 class ScopedMockLog { | 40 class ScopedMockLog { |
|
rvargas (doing something else)
2015/03/03 19:45:34
I generally don't like "Scoped" named classes unle
Łukasz Anforowicz
2015/03/04 00:23:01
Done.
| |
| 40 public: | 41 public: |
| 41 // Creates a ScopedMockLog object that is not capturing logs. | 42 // Creates a ScopedMockLog object that is not capturing logs. |
| 42 // If it were to start to capture logs, it could be a problem if | 43 // If it were to start to capture logs, it could be a problem if |
|
rvargas (doing something else)
2015/03/03 19:45:34
nit: comments should wrap as close to the limit as
Łukasz Anforowicz
2015/03/04 00:23:01
I re-wrapped where it was saving lines and/or wher
| |
| 43 // some other threads already exist and are logging, as the user | 44 // some other threads already exist and are logging, as the user |
| 44 // hasn't had a chance to set up expectation on this object yet | 45 // hasn't had a chance to set up expectation on this object yet |
| 45 // (calling a mock method before setting the expectation is | 46 // (calling a mock method before setting the expectation is |
| 46 // UNDEFINED behavior). | 47 // UNDEFINED behavior). |
| 47 ScopedMockLog(); | 48 ScopedMockLog(); |
| 48 | 49 |
| 49 // When the object is destructed, it stops intercepting logs. | 50 // When the object is destructed, it stops intercepting logs. |
| 50 ~ScopedMockLog(); | 51 ~ScopedMockLog(); |
| 51 | 52 |
| 52 // Starts log capturing if the object isn't already doing so. | 53 // Starts log capturing if the object isn't already doing so. |
| 53 // Otherwise crashes. Usually this method is called in the same | 54 // Otherwise crashes. Usually this method is called in the same |
| 54 // thread that created this object. It is the user's responsibility | 55 // thread that created this object. It is the user's responsibility |
| 55 // to not call this method if another thread may be calling it or | 56 // to not call this method if another thread may be calling it or |
| 56 // StopCapturingLogs() at the same time. | 57 // StopCapturingLogs() at the same time. |
|
rvargas (doing something else)
2015/03/03 19:45:34
This comment looks stale now.
Łukasz Anforowicz
2015/03/04 00:23:01
Oh, right. Thanks.
| |
| 57 void StartCapturingLogs(); | 58 void StartCapturingLogs(); |
| 58 | 59 |
| 59 // Stops log capturing if the object is capturing logs. Otherwise | 60 // Stops log capturing if the object is capturing logs. Otherwise |
| 60 // crashes. Usually this method is called in the same thread that | 61 // crashes. Usually this method is called in the same thread that |
| 61 // created this object. It is the user's responsibility to not call | 62 // created this object. It is the user's responsibility to not call |
| 62 // this method if another thread may be calling it or | 63 // this method if another thread may be calling it or |
| 63 // StartCapturingLogs() at the same time. | 64 // StartCapturingLogs() at the same time. |
| 64 void StopCapturingLogs(); | 65 void StopCapturingLogs(); |
| 65 | 66 |
| 66 // Sets the Log Message Handler that gets passed every log message before | 67 // Sets the Log Message Handler that gets passed every log message before |
|
rvargas (doing something else)
2015/03/03 19:45:34
nit: This is the documentation of logging::SetLogM
Łukasz Anforowicz
2015/03/04 00:23:01
Good catch. I reworded the comment.
| |
| 67 // it's sent to other log destinations (if any). | 68 // it's sent to other log destinations (if any). |
| 68 // Returns true to signal that it handled the message and the message | 69 // Returns true to signal that it handled the message and the message |
| 69 // should not be sent to other log destinations. | 70 // should not be sent to other log destinations. |
| 70 MOCK_METHOD5(Log, bool(int severity, | 71 MOCK_METHOD5(Log, |
| 71 const char* file, | 72 bool(int severity, |
| 72 int line, | 73 const char* file, |
| 73 size_t message_start, | 74 int line, |
| 74 const std::string& str)); | 75 size_t message_start, |
| 76 const std::string& str)); | |
| 75 | 77 |
| 76 private: | 78 private: |
| 77 // The currently active scoped mock log. | 79 // The currently active scoped mock log. |
| 78 static ScopedMockLog* g_instance_; | 80 static ScopedMockLog* g_instance_; |
| 79 | 81 |
| 82 // Lock protecting access to g_instance_. | |
| 83 static Lock g_lock; | |
| 84 | |
| 80 // Static function which is set as the logging message handler. | 85 // Static function which is set as the logging message handler. |
| 81 // Called once for each message. | 86 // Called once for each message. |
| 82 static bool LogMessageHandler(int severity, | 87 static bool LogMessageHandler(int severity, |
| 83 const char* file, | 88 const char* file, |
| 84 int line, | 89 int line, |
| 85 size_t message_start, | 90 size_t message_start, |
| 86 const std::string& str); | 91 const std::string& str); |
| 87 | 92 |
| 88 // True if this object is currently capturing logs. | 93 // True if this object is currently capturing logs. |
| 89 bool is_capturing_logs_; | 94 bool is_capturing_logs_; |
| 90 | 95 |
| 91 // The previous handler to restore when the ScopedMockLog is destroyed. | 96 // The previous handler to restore when the ScopedMockLog is destroyed. |
| 92 logging::LogMessageHandlerFunction previous_handler_; | 97 logging::LogMessageHandlerFunction previous_handler_; |
| 93 }; | 98 }; |
|
rvargas (doing something else)
2015/03/03 19:45:34
DISALLOW_COPY_AND_ASSIGN ?
Łukasz Anforowicz
2015/03/04 00:23:01
Done.
| |
| 94 | 99 |
| 95 } // namespace test | 100 } // namespace base |
| 96 } // namespace net | |
| 97 | 101 |
| 98 #endif // NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ | 102 #endif // BASE_TEST_SCOPED_MOCK_LOG_H_ |
| OLD | NEW |