File: openscreen_message_port_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 4,048 bytes parent folder | download | duplicates (6)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/mirroring/service/openscreen_message_port.h"

#include "base/base64.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "components/mirroring/service/value_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/openscreen/src/cast/common/public/message_port.h"

using ::testing::_;
using ::testing::InvokeWithoutArgs;

namespace mirroring {
namespace {

constexpr char kSourceId[] = "sender-123";
constexpr char kDestinationId[] = "receiver-456";
constexpr char kNamespace[] = "namespace";

class MockMessagePortClient : public openscreen::cast::MessagePort::Client {
 public:
  MOCK_METHOD(void,
              OnMessage,
              (const std::string&, const std::string&, const std::string&),
              (override));
  MOCK_METHOD(void, OnError, (const openscreen::Error&), (override));

  const std::string& source_id() override { return source_id_; }

 private:
  std::string source_id_ = kSourceId;
};

}  // anonymous namespace

class OpenscreenMessagePortTest : public ::testing::Test,
                                  public mojom::CastMessageChannel {
 public:
  OpenscreenMessagePortTest() = default;

  void SetUp() override {
    mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel_remote;
    outbound_channel_receiver_.Bind(
        outbound_channel_remote.InitWithNewPipeAndPassReceiver());

    message_port_ = std::make_unique<OpenscreenMessagePort>(
        kSourceId, kDestinationId, std::move(outbound_channel_remote),
        inbound_channel_.BindNewPipeAndPassReceiver());

    message_port_->SetClient(mock_client_);
    task_environment_.RunUntilIdle();
  }

  ~OpenscreenMessagePortTest() override { task_environment_.RunUntilIdle(); }

 protected:
  // mojom::CastMessageChannel implementation (outbound messages).
  void OnMessage(mojom::CastMessagePtr message) override {
    EXPECT_EQ(message->message_namespace, kNamespace);
    std::optional<base::Value> value =
        base::JSONReader::Read(message->json_format_data);
    ASSERT_TRUE(value);
    std::string message_type;
    EXPECT_TRUE(GetString(*value, "type", &message_type));
    OnOutboundMessage(message_type);
  }
  MOCK_METHOD1(OnOutboundMessage, void(const std::string& message_type));

  void OnInboundMessage(const std::string& message_namespace,
                        const std::string& json_format_data) {
    mojom::CastMessagePtr message =
        mojom::CastMessage::New(message_namespace, json_format_data);
    inbound_channel_->OnMessage(std::move(message));
    task_environment_.RunUntilIdle();
  }

  base::test::TaskEnvironment& task_environment() { return task_environment_; }
  MockMessagePortClient& client() { return mock_client_; }
  OpenscreenMessagePort* message_port() { return message_port_.get(); }

 private:
  base::test::TaskEnvironment task_environment_;
  mojo::Receiver<mojom::CastMessageChannel> outbound_channel_receiver_{this};
  mojo::Remote<mojom::CastMessageChannel> inbound_channel_;
  testing::StrictMock<MockMessagePortClient> mock_client_;
  std::unique_ptr<OpenscreenMessagePort> message_port_;
};

TEST_F(OpenscreenMessagePortTest, CanSendMessage) {
  EXPECT_CALL(*this, OnOutboundMessage("OFFER"));
  constexpr char kMessage[] = R"({"type": "OFFER"})";
  message_port()->PostMessage(kDestinationId, kNamespace,
                              std::string(kMessage));
  task_environment().RunUntilIdle();
}

TEST_F(OpenscreenMessagePortTest, CanReceiveMessage) {
  constexpr char kMessage[] = R"({"type": "ANSWER"})";
  EXPECT_CALL(client(), OnMessage(kDestinationId, kNamespace, kMessage));

  OnInboundMessage(kNamespace, std::string(kMessage));
  task_environment().RunUntilIdle();
}

}  // namespace mirroring