xref: /aosp_15_r20/external/openscreen/cast/streaming/testing/simple_message_port.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
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 
5 #ifndef CAST_STREAMING_TESTING_SIMPLE_MESSAGE_PORT_H_
6 #define CAST_STREAMING_TESTING_SIMPLE_MESSAGE_PORT_H_
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include "cast/common/public/message_port.h"
13 #include "cast/streaming/message_fields.h"
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16 
17 namespace openscreen {
18 namespace cast {
19 
20 class SimpleMessagePort : public MessagePort {
21  public:
SimpleMessagePort(const std::string & destination_id)22   explicit SimpleMessagePort(const std::string& destination_id)
23       : destination_id_(destination_id) {}
24 
25   ~SimpleMessagePort() override = default;
SetClient(MessagePort::Client * client,std::string client_sender_id)26   void SetClient(MessagePort::Client* client,
27                  std::string client_sender_id) override {
28     client_ = client;
29   }
30 
ResetClient()31   void ResetClient() override { client_ = nullptr; }
32 
ReceiveMessage(const std::string & message)33   void ReceiveMessage(const std::string& message) {
34     ReceiveMessage(kCastWebrtcNamespace, message);
35   }
36 
ReceiveMessage(const std::string & namespace_,const std::string & message)37   void ReceiveMessage(const std::string& namespace_,
38                       const std::string& message) {
39     ReceiveMessage(destination_id_, namespace_, message);
40   }
41 
ReceiveMessage(const std::string & sender_id,const std::string & namespace_,const std::string & message)42   void ReceiveMessage(const std::string& sender_id,
43                       const std::string& namespace_,
44                       const std::string& message) {
45     ASSERT_NE(client_, nullptr);
46     client_->OnMessage(sender_id, namespace_, message);
47   }
48 
ReceiveError(Error error)49   void ReceiveError(Error error) {
50     ASSERT_NE(client_, nullptr);
51     client_->OnError(error);
52   }
53 
PostMessage(const std::string & sender_id,const std::string & message_namespace,const std::string & message)54   void PostMessage(const std::string& sender_id,
55                    const std::string& message_namespace,
56                    const std::string& message) override {
57     posted_messages_.emplace_back(message);
58   }
59 
posted_messages()60   const std::vector<std::string> posted_messages() const {
61     return posted_messages_;
62   }
63 
64  private:
65   MessagePort::Client* client_ = nullptr;
66   std::string destination_id_;
67   std::vector<std::string> posted_messages_;
68 };
69 
70 }  // namespace cast
71 }  // namespace openscreen
72 
73 #endif  // CAST_STREAMING_TESTING_SIMPLE_MESSAGE_PORT_H_
74