OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 BASE_TEST_MOCK_LOG_H_ | 5 #ifndef BASE_TEST_MOCK_LOG_H_ |
6 #define BASE_TEST_MOCK_LOG_H_ | 6 #define BASE_TEST_MOCK_LOG_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 | 12 |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 namespace test { | 19 namespace test { |
19 | 20 |
| 21 class MockLogMessageListener; |
| 22 |
20 // A MockLog object intercepts LOG() messages issued during its lifespan. Using | 23 // A MockLog object intercepts LOG() messages issued during its lifespan. Using |
21 // this together with gMock, it's very easy to test how a piece of code calls | 24 // this together with gMock, it's very easy to test how a piece of code calls |
22 // LOG(). The typical usage: | 25 // LOG(). The typical usage: |
23 // | 26 // |
24 // TEST(FooTest, LogsCorrectly) { | 27 // TEST(FooTest, LogsCorrectly) { |
25 // MockLog log; | 28 // MockLog log; |
26 // | 29 // |
27 // // We expect the WARNING "Something bad!" exactly twice. | 30 // // We expect the WARNING "Something bad!" exactly twice. |
28 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) | 31 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) |
29 // .Times(2); | 32 // .Times(2); |
(...skipping 23 matching lines...) Expand all Loading... |
53 ~MockLog(); | 56 ~MockLog(); |
54 | 57 |
55 // Starts log capturing if the object isn't already doing so. | 58 // Starts log capturing if the object isn't already doing so. |
56 // Otherwise crashes. | 59 // Otherwise crashes. |
57 void StartCapturingLogs(); | 60 void StartCapturingLogs(); |
58 | 61 |
59 // Stops log capturing if the object is capturing logs. Otherwise crashes. | 62 // Stops log capturing if the object is capturing logs. Otherwise crashes. |
60 void StopCapturingLogs(); | 63 void StopCapturingLogs(); |
61 | 64 |
62 // Log method is invoked for every log message before it's sent to other log | 65 // Log method is invoked for every log message before it's sent to other log |
63 // destinations (if any). The method should return true to signal that it | 66 // destinations (if any). |
64 // handled the message and the message should not be sent to other log | |
65 // destinations. | |
66 MOCK_METHOD5(Log, | 67 MOCK_METHOD5(Log, |
67 bool(int severity, | 68 void(int severity, |
68 const char* file, | 69 const char* file, |
69 int line, | 70 int line, |
70 size_t message_start, | 71 size_t message_start, |
71 const std::string& str)); | 72 const std::string& str)); |
72 | 73 |
73 private: | 74 private: |
74 // The currently active mock log. | 75 std::unique_ptr<MockLogMessageListener> listener_; |
75 static MockLog* g_instance_; | |
76 | |
77 // Lock protecting access to g_instance_. | |
78 static Lock g_lock; | |
79 | |
80 // Static function which is set as the logging message handler. | |
81 // Called once for each message. | |
82 static bool LogMessageHandler(int severity, | |
83 const char* file, | |
84 int line, | |
85 size_t message_start, | |
86 const std::string& str); | |
87 | 76 |
88 // True if this object is currently capturing logs. | 77 // True if this object is currently capturing logs. |
89 bool is_capturing_logs_; | 78 bool is_capturing_logs_; |
90 | 79 |
91 // The previous handler to restore when the MockLog is destroyed. | |
92 logging::LogMessageHandlerFunction previous_handler_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(MockLog); | 80 DISALLOW_COPY_AND_ASSIGN(MockLog); |
95 }; | 81 }; |
96 | 82 |
97 } // namespace test | 83 } // namespace test |
98 } // namespace base | 84 } // namespace base |
99 | 85 |
100 #endif // BASE_TEST_MOCK_LOG_H_ | 86 #endif // BASE_TEST_MOCK_LOG_H_ |
OLD | NEW |