test_message_port_receiver.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 COMPONENTS_CAST_MESSAGE_PORT_TEST_MESSAGE_PORT_RECEIVER_H_
  5. #define COMPONENTS_CAST_MESSAGE_PORT_TEST_MESSAGE_PORT_RECEIVER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/strings/string_piece.h"
  10. #include "components/cast/message_port/message_port.h"
  11. namespace cast_api_bindings {
  12. class TestMessagePortReceiver
  13. : public cast_api_bindings::MessagePort::Receiver {
  14. public:
  15. TestMessagePortReceiver();
  16. ~TestMessagePortReceiver() override;
  17. TestMessagePortReceiver(const TestMessagePortReceiver&) = delete;
  18. TestMessagePortReceiver& operator=(const TestMessagePortReceiver&) = delete;
  19. // Spins a RunLoop until |buffer_| has |message_count| messages.
  20. bool RunUntilMessageCountEqual(size_t message_count);
  21. // Spins a RunLoop until the associated MessagePort is disconnected.
  22. void RunUntilDisconnected();
  23. // Sets the return value of OnMessage
  24. void SetOnMessageResult(bool result);
  25. std::vector<
  26. std::pair<std::string, std::vector<std::unique_ptr<MessagePort>>>>&
  27. buffer() {
  28. return buffer_;
  29. }
  30. private:
  31. // MessagePort::Receiver implementation.
  32. bool OnMessage(base::StringPiece message,
  33. std::vector<std::unique_ptr<MessagePort>> ports) final;
  34. void OnPipeError() final;
  35. std::vector<std::pair<std::string, std::vector<std::unique_ptr<MessagePort>>>>
  36. buffer_;
  37. size_t message_count_target_ = 0;
  38. base::OnceClosure on_receive_satisfied_;
  39. base::OnceClosure on_disconnect_;
  40. bool on_message_result_ = true;
  41. };
  42. } // namespace cast_api_bindings
  43. #endif // COMPONENTS_CAST_MESSAGE_PORT_TEST_MESSAGE_PORT_RECEIVER_H_