xref: /aosp_15_r20/external/openscreen/cast/streaming/testing/message_pipe.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 // The testing message pipe is intended to be used for integrating
6 // sender and receiver sessions (or other consumers of MessagePort).
7 
8 #ifndef CAST_STREAMING_TESTING_MESSAGE_PIPE_H_
9 #define CAST_STREAMING_TESTING_MESSAGE_PIPE_H_
10 
11 #include <string>
12 #include <utility>
13 #include <vector>
14 
15 #include "cast/common/public/message_port.h"
16 #include "cast/streaming/message_fields.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 
20 namespace openscreen {
21 namespace cast {
22 
23 class MessagePipeEnd : public MessagePort {
24  public:
MessagePipeEnd(std::string destination_id)25   explicit MessagePipeEnd(std::string destination_id)
26       : destination_id_(destination_id) {}
27 
28   ~MessagePipeEnd() override = default;
29 
SetClient(MessagePort::Client * client,std::string client_sender_id)30   void SetClient(MessagePort::Client* client,
31                  std::string client_sender_id) override {
32     sender_id_ = std::move(client_sender_id);
33     client_ = client;
34   }
35 
SetOtherEnd(MessagePipeEnd * other_end)36   void SetOtherEnd(MessagePipeEnd* other_end) {
37     ASSERT_FALSE(other_end_);
38     other_end_ = other_end;
39   }
40 
ResetClient()41   void ResetClient() override { client_ = nullptr; }
42 
ReceiveMessage(const std::string & namespace_,const std::string & message)43   void ReceiveMessage(const std::string& namespace_,
44                       const std::string& message) {
45     ASSERT_NE(client_, nullptr);
46     client_->OnMessage(destination_id_, namespace_, message);
47   }
48 
PostMessage(const std::string & sender_id,const std::string & message_namespace,const std::string & message)49   void PostMessage(const std::string& sender_id,
50                    const std::string& message_namespace,
51                    const std::string& message) override {
52     ASSERT_NE(other_end_, nullptr);
53     other_end_->ReceiveMessage(message_namespace, message);
54   }
55 
56  private:
57   std::string sender_id_;
58   std::string destination_id_;
59   MessagePort::Client* client_ = nullptr;
60   MessagePipeEnd* other_end_ = nullptr;
61 };
62 
63 class MessagePipe {
64  public:
MessagePipe(std::string left_id,std::string right_id)65   explicit MessagePipe(std::string left_id, std::string right_id)
66       : left_id_(std::move(right_id)),
67         right_id_(std::move(left_id)),
68         left_(left_id_),
69         right_(right_id_) {
70     left_.SetOtherEnd(&right_);
71     right_.SetOtherEnd(&left_);
72   }
73 
74   // Access the ends of the pipe, which can be used as a standard
75   // message port.
left()76   MessagePipeEnd* left() { return &left_; }
right()77   MessagePipeEnd* right() { return &right_; }
78 
79  private:
80   std::string left_id_;
81   std::string right_id_;
82   MessagePipeEnd left_;
83   MessagePipeEnd right_;
84 };
85 
86 }  // namespace cast
87 }  // namespace openscreen
88 
89 #endif  // CAST_STREAMING_TESTING_MESSAGE_PIPE_H_
90