test_log_listener_safe.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 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_FUCHSIA_TEST_LOG_LISTENER_SAFE_H_
  5. #define BASE_FUCHSIA_TEST_LOG_LISTENER_SAFE_H_
  6. #include <fuchsia/logger/cpp/fidl_test_base.h>
  7. #include <lib/fidl/cpp/binding.h>
  8. #include <lib/zx/time.h>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/containers/circular_deque.h"
  14. #include "base/strings/string_piece.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace base {
  17. // LogListenerSafe implementation that invokes a caller-supplied callback for
  18. // each received message.
  19. // Note that messages will be delivered in order of receipt from the system
  20. // logger, starting with any recent messages that the logging service had
  21. // cached, i.e. including messages that may pre-date this log-listener being
  22. // created.
  23. class TestLogListenerSafe final
  24. : public fuchsia::logger::testing::LogListenerSafe_TestBase {
  25. public:
  26. using OnLogMessageCallback =
  27. base::RepeatingCallback<void(const fuchsia::logger::LogMessage&)>;
  28. TestLogListenerSafe();
  29. ~TestLogListenerSafe() override;
  30. TestLogListenerSafe(const TestLogListenerSafe&) = delete;
  31. TestLogListenerSafe& operator=(const TestLogListenerSafe&) = delete;
  32. // Sets a callback to be invoked with every message received via Log().
  33. void set_on_log_message(OnLogMessageCallback callback);
  34. private:
  35. // LogListenerSafe implementation.
  36. void Log(fuchsia::logger::LogMessage message, LogCallback callback) override;
  37. void LogMany(std::vector<fuchsia::logger::LogMessage> messages,
  38. LogManyCallback callback) override;
  39. void Done() override;
  40. void NotImplemented_(const std::string& name) override;
  41. OnLogMessageCallback on_log_message_;
  42. };
  43. // Helper that manages a TestLogListenerSafe to simplify running the message
  44. // loop until specific messages are received.
  45. // Messages received prior to ListenToLog() being called will be silently
  46. // ignored.
  47. class SimpleTestLogListener {
  48. public:
  49. SimpleTestLogListener();
  50. ~SimpleTestLogListener();
  51. SimpleTestLogListener(const SimpleTestLogListener&) = delete;
  52. SimpleTestLogListener& operator=(const SimpleTestLogListener&) = delete;
  53. // Attaches this instance to receive data matching |options|, from |log|.
  54. void ListenToLog(fuchsia::logger::Log* log,
  55. std::unique_ptr<fuchsia::logger::LogFilterOptions> options);
  56. // Runs the message loop until a log message containing |expected_string| is
  57. // received, and returns it. Returns |absl::nullopt| if |binding_| disconnects
  58. // without the |expected_string| having been logged.
  59. absl::optional<fuchsia::logger::LogMessage> RunUntilMessageReceived(
  60. base::StringPiece expected_string);
  61. private:
  62. // Pushes |message| to the |logged_messages_| queue, or to |on_log_message_|.
  63. void PushLoggedMessage(const fuchsia::logger::LogMessage& message);
  64. // Used to ignore messages with timestamps prior to this listener's creation.
  65. zx::time ignore_before_;
  66. TestLogListenerSafe listener_;
  67. fidl::Binding<fuchsia::logger::LogListenerSafe> binding_;
  68. base::circular_deque<fuchsia::logger::LogMessage> logged_messages_;
  69. TestLogListenerSafe::OnLogMessageCallback on_log_message_;
  70. };
  71. } // namespace base
  72. #endif // BASE_FUCHSIA_TEST_LOG_LISTENER_SAFE_H_