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

Side by Side Diff: base/test/mock_log.h

Issue 966423003: Moving ScopedMockLog from net/test to base/test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@separate-port-range
Patch Set: Addressed Ricardo's code review feedback. Created 5 years, 9 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
« no previous file with comments | « base/test/BUILD.gn ('k') | base/test/mock_log.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 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 NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ 5 #ifndef BASE_TEST_MOCK_LOG_H_
6 #define NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ 6 #define BASE_TEST_MOCK_LOG_H_
7
8 #include <string>
7 9
8 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/synchronization/lock.h"
9 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 14
12 namespace net { 15 namespace base {
13 namespace test { 16 namespace test {
14 17
15 // A ScopedMockLog object intercepts LOG() messages issued during its 18 // A MockLog object intercepts LOG() messages issued during its lifespan. Using
16 // lifespan. Using this together with gMock, it's very easy to test 19 // this together with gMock, it's very easy to test how a piece of code calls
17 // how a piece of code calls LOG(). The typical usage: 20 // LOG(). The typical usage:
18 // 21 //
19 // TEST(FooTest, LogsCorrectly) { 22 // TEST(FooTest, LogsCorrectly) {
20 // ScopedMockLog log; 23 // MockLog log;
21 // 24 //
22 // // We expect the WARNING "Something bad!" exactly twice. 25 // // We expect the WARNING "Something bad!" exactly twice.
23 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) 26 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!"))
24 // .Times(2); 27 // .Times(2);
25 // 28 //
26 // // We allow foo.cc to call LOG(INFO) any number of times. 29 // // We allow foo.cc to call LOG(INFO) any number of times.
27 // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _)) 30 // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _))
28 // .Times(AnyNumber()); 31 // .Times(AnyNumber());
29 // 32 //
30 // log.StartCapturingLogs(); // Call this after done setting expectations. 33 // log.StartCapturingLogs(); // Call this after done setting expectations.
31 // Foo(); // Exercises the code under test. 34 // Foo(); // Exercises the code under test.
32 // } 35 // }
33 // 36 //
34 // CAVEAT: base/logging does not allow a thread to call LOG() again 37 // CAVEAT: base/logging does not allow a thread to call LOG() again when it's
35 // when it's already inside a LOG() call. Doing so will cause a 38 // already inside a LOG() call. Doing so will cause a deadlock. Therefore,
36 // deadlock. Therefore, it's the user's responsibility to not call 39 // it's the user's responsibility to not call LOG() in an action triggered by
37 // LOG() in an action triggered by ScopedMockLog::Log(). You may call 40 // MockLog::Log(). You may call RAW_LOG() instead.
38 // RAW_LOG() instead. 41 class MockLog {
39 class ScopedMockLog {
40 public: 42 public:
41 // Creates a ScopedMockLog object that is not capturing logs. 43 // Creates a MockLog object that is not capturing logs. If it were to start
42 // If it were to start to capture logs, it could be a problem if 44 // to capture logs, it could be a problem if some other threads already exist
43 // some other threads already exist and are logging, as the user 45 // and are logging, as the user hasn't had a chance to set up expectation on
44 // hasn't had a chance to set up expectation on this object yet 46 // this object yet (calling a mock method before setting the expectation is
45 // (calling a mock method before setting the expectation is
46 // UNDEFINED behavior). 47 // UNDEFINED behavior).
47 ScopedMockLog(); 48 MockLog();
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 ~MockLog();
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.
54 // 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 // StopCapturingLogs() at the same time.
57 void StartCapturingLogs(); 55 void StartCapturingLogs();
58 56
59 // Stops log capturing if the object is capturing logs. Otherwise 57 // Stops log capturing if the object is capturing logs. Otherwise crashes.
60 // 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 // this method if another thread may be calling it or
63 // StartCapturingLogs() at the same time.
64 void StopCapturingLogs(); 58 void StopCapturingLogs();
65 59
66 // Sets the Log Message Handler that gets passed every log message before 60 // Log method is invoked for every log message before it's sent to other log
67 // it's sent to other log destinations (if any). 61 // destinations (if any). The method should return true to signal that it
68 // Returns true to signal that it handled the message and the message 62 // handled the message and the message should not be sent to other log
69 // should not be sent to other log destinations. 63 // destinations.
70 MOCK_METHOD5(Log, bool(int severity, 64 MOCK_METHOD5(Log,
71 const char* file, 65 bool(int severity,
72 int line, 66 const char* file,
73 size_t message_start, 67 int line,
74 const std::string& str)); 68 size_t message_start,
69 const std::string& str));
75 70
76 private: 71 private:
77 // The currently active scoped mock log. 72 // The currently active mock log.
78 static ScopedMockLog* g_instance_; 73 static MockLog* g_instance_;
74
75 // Lock protecting access to g_instance_.
76 static Lock g_lock;
79 77
80 // Static function which is set as the logging message handler. 78 // Static function which is set as the logging message handler.
81 // Called once for each message. 79 // Called once for each message.
82 static bool LogMessageHandler(int severity, 80 static bool LogMessageHandler(int severity,
83 const char* file, 81 const char* file,
84 int line, 82 int line,
85 size_t message_start, 83 size_t message_start,
86 const std::string& str); 84 const std::string& str);
87 85
88 // True if this object is currently capturing logs. 86 // True if this object is currently capturing logs.
89 bool is_capturing_logs_; 87 bool is_capturing_logs_;
90 88
91 // The previous handler to restore when the ScopedMockLog is destroyed. 89 // The previous handler to restore when the MockLog is destroyed.
92 logging::LogMessageHandlerFunction previous_handler_; 90 logging::LogMessageHandlerFunction previous_handler_;
91
92 DISALLOW_COPY_AND_ASSIGN(MockLog);
93 }; 93 };
94 94
95 } // namespace test 95 } // namespace test
96 } // namespace net 96 } // namespace base
97 97
98 #endif // NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ 98 #endif // BASE_TEST_MOCK_LOG_H_
OLDNEW
« no previous file with comments | « base/test/BUILD.gn ('k') | base/test/mock_log.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698