aboutsummaryrefslogtreecommitdiff
path: root/cast/streaming/testing/simple_message_port.h
blob: 24e289f7435f5b540625ea3d242e5cf88468cfe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CAST_STREAMING_TESTING_SIMPLE_MESSAGE_PORT_H_
#define CAST_STREAMING_TESTING_SIMPLE_MESSAGE_PORT_H_

#include <string>
#include <utility>
#include <vector>

#include "cast/common/public/message_port.h"
#include "cast/streaming/message_fields.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace openscreen {
namespace cast {

class SimpleMessagePort : public MessagePort {
 public:
  explicit SimpleMessagePort(const std::string& destination_id)
      : destination_id_(destination_id) {}

  ~SimpleMessagePort() override = default;
  void SetClient(MessagePort::Client* client,
                 std::string client_sender_id) override {
    client_ = client;
  }

  void ResetClient() override { client_ = nullptr; }

  void ReceiveMessage(const std::string& message) {
    ReceiveMessage(kCastWebrtcNamespace, message);
  }

  void ReceiveMessage(const std::string& namespace_,
                      const std::string& message) {
    ReceiveMessage(destination_id_, namespace_, message);
  }

  void ReceiveMessage(const std::string& sender_id,
                      const std::string& namespace_,
                      const std::string& message) {
    ASSERT_NE(client_, nullptr);
    client_->OnMessage(sender_id, namespace_, message);
  }

  void ReceiveError(Error error) {
    ASSERT_NE(client_, nullptr);
    client_->OnError(error);
  }

  void PostMessage(const std::string& sender_id,
                   const std::string& message_namespace,
                   const std::string& message) override {
    posted_messages_.emplace_back(message);
  }

  const std::vector<std::string> posted_messages() const {
    return posted_messages_;
  }

 private:
  MessagePort::Client* client_ = nullptr;
  std::string destination_id_;
  std::vector<std::string> posted_messages_;
};

}  // namespace cast
}  // namespace openscreen

#endif  // CAST_STREAMING_TESTING_SIMPLE_MESSAGE_PORT_H_