1 // Copyright 2019 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 #include "cast/common/channel/namespace_router.h"
6
7 #include <utility>
8
9 #include "cast/common/channel/cast_message_handler.h"
10 #include "cast/common/channel/proto/cast_channel.pb.h"
11 #include "cast/common/channel/testing/fake_cast_socket.h"
12 #include "cast/common/channel/testing/mock_cast_message_handler.h"
13 #include "cast/common/channel/virtual_connection_router.h"
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16
17 namespace openscreen {
18 namespace cast {
19 namespace {
20
21 using ::cast::channel::CastMessage;
22 using ::testing::_;
23 using ::testing::Invoke;
24
25 class NamespaceRouterTest : public ::testing::Test {
26 public:
27 protected:
socket()28 CastSocket* socket() { return &fake_socket_.socket; }
29
30 FakeCastSocket fake_socket_;
31 VirtualConnectionRouter vc_router_;
32 NamespaceRouter router_;
33 };
34
35 } // namespace
36
TEST_F(NamespaceRouterTest,NoHandlersNoop)37 TEST_F(NamespaceRouterTest, NoHandlersNoop) {
38 CastMessage message;
39 message.set_namespace_("anzrfcnpr");
40 router_.OnMessage(&vc_router_, socket(), std::move(message));
41 }
42
TEST_F(NamespaceRouterTest,MultipleHandlers)43 TEST_F(NamespaceRouterTest, MultipleHandlers) {
44 MockCastMessageHandler media_handler;
45 MockCastMessageHandler auth_handler;
46 MockCastMessageHandler connection_handler;
47
48 router_.AddNamespaceHandler("media", &media_handler);
49 router_.AddNamespaceHandler("auth", &auth_handler);
50 router_.AddNamespaceHandler("connection", &connection_handler);
51
52 EXPECT_CALL(media_handler, OnMessage(_, _, _)).Times(0);
53 EXPECT_CALL(auth_handler, OnMessage(_, _, _))
54 .WillOnce(Invoke([](VirtualConnectionRouter* router, CastSocket*,
55 CastMessage message) {
56 EXPECT_EQ(message.namespace_(), "auth");
57 }));
58 EXPECT_CALL(connection_handler, OnMessage(_, _, _))
59 .WillOnce(Invoke([](VirtualConnectionRouter* router, CastSocket*,
60 CastMessage message) {
61 EXPECT_EQ(message.namespace_(), "connection");
62 }));
63
64 CastMessage auth_message;
65 auth_message.set_namespace_("auth");
66 router_.OnMessage(&vc_router_, socket(), std::move(auth_message));
67
68 CastMessage connection_message;
69 connection_message.set_namespace_("connection");
70 router_.OnMessage(&vc_router_, socket(), std::move(connection_message));
71 }
72
TEST_F(NamespaceRouterTest,RemoveHandler)73 TEST_F(NamespaceRouterTest, RemoveHandler) {
74 MockCastMessageHandler handler1;
75 MockCastMessageHandler handler2;
76
77 router_.AddNamespaceHandler("one", &handler1);
78 router_.AddNamespaceHandler("two", &handler2);
79
80 router_.RemoveNamespaceHandler("one");
81
82 EXPECT_CALL(handler1, OnMessage(_, _, _)).Times(0);
83 EXPECT_CALL(handler2, OnMessage(_, _, _))
84 .WillOnce(Invoke(
85 [](VirtualConnectionRouter* router, CastSocket* socket,
86 CastMessage message) { EXPECT_EQ("two", message.namespace_()); }));
87
88 CastMessage message1;
89 message1.set_namespace_("one");
90 router_.OnMessage(&vc_router_, socket(), std::move(message1));
91
92 CastMessage message2;
93 message2.set_namespace_("two");
94 router_.OnMessage(&vc_router_, socket(), std::move(message2));
95 }
96
97 } // namespace cast
98 } // namespace openscreen
99