mock_log.h 3.3 KB

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