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