Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / test / mock_log.h
blob315ef1fb2f088d121b92d27c68d32cd020513899
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
3 // found in the LICENSE file.
5 #ifndef BASE_TEST_MOCK_LOG_H_
6 #define BASE_TEST_MOCK_LOG_H_
8 #include <string>
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/synchronization/lock.h"
13 #include "testing/gmock/include/gmock/gmock.h"
15 namespace base {
16 namespace test {
18 // A MockLog object intercepts LOG() messages issued during its lifespan. Using
19 // this together with gMock, it's very easy to test how a piece of code calls
20 // LOG(). The typical usage:
22 // TEST(FooTest, LogsCorrectly) {
23 // MockLog log;
25 // // We expect the WARNING "Something bad!" exactly twice.
26 // EXPECT_CALL(log, Log(WARNING, _, "Something bad!"))
27 // .Times(2);
29 // // We allow foo.cc to call LOG(INFO) any number of times.
30 // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _))
31 // .Times(AnyNumber());
33 // log.StartCapturingLogs(); // Call this after done setting expectations.
34 // Foo(); // Exercises the code under test.
35 // }
37 // CAVEAT: base/logging does not allow a thread to call LOG() again when it's
38 // already inside a LOG() call. Doing so will cause a deadlock. Therefore,
39 // it's the user's responsibility to not call LOG() in an action triggered by
40 // MockLog::Log(). You may call RAW_LOG() instead.
41 class MockLog {
42 public:
43 // Creates a MockLog object that is not capturing logs. If it were to start
44 // to capture logs, it could be a problem if some other threads already exist
45 // and are logging, as the user hasn't had a chance to set up expectation on
46 // this object yet (calling a mock method before setting the expectation is
47 // UNDEFINED behavior).
48 MockLog();
50 // When the object is destructed, it stops intercepting logs.
51 ~MockLog();
53 // Starts log capturing if the object isn't already doing so.
54 // Otherwise crashes.
55 void StartCapturingLogs();
57 // Stops log capturing if the object is capturing logs. Otherwise crashes.
58 void StopCapturingLogs();
60 // Log method is invoked for every log message before it's sent to other log
61 // destinations (if any). The method should return true to signal that it
62 // handled the message and the message should not be sent to other log
63 // destinations.
64 MOCK_METHOD5(Log,
65 bool(int severity,
66 const char* file,
67 int line,
68 size_t message_start,
69 const std::string& str));
71 private:
72 // The currently active mock log.
73 static MockLog* g_instance_;
75 // Lock protecting access to g_instance_.
76 static Lock g_lock;
78 // Static function which is set as the logging message handler.
79 // Called once for each message.
80 static bool LogMessageHandler(int severity,
81 const char* file,
82 int line,
83 size_t message_start,
84 const std::string& str);
86 // True if this object is currently capturing logs.
87 bool is_capturing_logs_;
89 // The previous handler to restore when the MockLog is destroyed.
90 logging::LogMessageHandlerFunction previous_handler_;
92 DISALLOW_COPY_AND_ASSIGN(MockLog);
95 } // namespace test
96 } // namespace base
98 #endif // BASE_TEST_MOCK_LOG_H_