OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "blimp/common/proto/blimp_message.pb.h" | |
6 #include "blimp/common/proto/input.pb.h" | |
7 #include "blimp/common/proto/navigation.pb.h" | |
8 #include "blimp/net/blimp_message_multiplexer.h" | |
9 #include "blimp/net/test_common.h" | |
10 #include "net/base/net_errors.h" | |
11 #include "net/base/test_completion_callback.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using testing::_; | |
16 using testing::DoAll; | |
17 using testing::Ref; | |
18 using testing::SaveArg; | |
19 | |
20 namespace blimp { | |
21 namespace { | |
22 | |
23 class BlimpMessageMultiplexerTest : public testing::Test { | |
24 public: | |
25 BlimpMessageMultiplexerTest() | |
26 : multiplexer_(&mock_output_processor_), | |
27 input_message_(new BlimpMessage), | |
28 navigation_message_(new BlimpMessage), | |
29 input_processor_(multiplexer_.CreateSender(BlimpMessage::kInput)), | |
30 navigation_processor_( | |
31 multiplexer_.CreateSender(BlimpMessage::kNavigation)) {} | |
32 | |
33 void SetUp() override { | |
34 EXPECT_CALL(mock_output_processor_, MockableProcessMessage(_, _)) | |
35 .WillRepeatedly( | |
36 DoAll(SaveArg<0>(&captured_message_), SaveArg<1>(&captured_cb_))); | |
37 | |
38 input_message_->mutable_input()->set_type( | |
39 InputMessage::Type_GestureScrollBegin); | |
40 navigation_message_->mutable_navigation()->set_type( | |
41 NavigationMessage::LOAD_URL); | |
42 } | |
43 | |
44 protected: | |
45 MockBlimpMessageProcessor mock_output_processor_; | |
46 BlimpMessageMultiplexer multiplexer_; | |
47 std::unique_ptr<BlimpMessage> input_message_; | |
48 std::unique_ptr<BlimpMessage> navigation_message_; | |
49 BlimpMessage captured_message_; | |
50 net::CompletionCallback captured_cb_; | |
51 std::unique_ptr<BlimpMessageProcessor> input_processor_; | |
52 std::unique_ptr<BlimpMessageProcessor> navigation_processor_; | |
53 }; | |
54 | |
55 // Verify that each sender propagates its types and copies the message payload | |
56 // correctly. | |
57 TEST_F(BlimpMessageMultiplexerTest, TypeSetByMux) { | |
58 net::TestCompletionCallback cb_1; | |
59 input_processor_->ProcessMessage(std::move(input_message_), cb_1.callback()); | |
60 EXPECT_EQ(BlimpMessage::kInput, captured_message_.feature_case()); | |
61 EXPECT_EQ(InputMessage::Type_GestureScrollBegin, | |
62 captured_message_.input().type()); | |
63 captured_cb_.Run(net::OK); | |
64 EXPECT_EQ(net::OK, cb_1.WaitForResult()); | |
65 | |
66 net::TestCompletionCallback cb_2; | |
67 navigation_processor_->ProcessMessage(std::move(navigation_message_), | |
68 cb_2.callback()); | |
69 EXPECT_EQ(BlimpMessage::kNavigation, captured_message_.feature_case()); | |
70 EXPECT_EQ(NavigationMessage::LOAD_URL, captured_message_.navigation().type()); | |
71 captured_cb_.Run(net::ERR_FAILED); | |
72 EXPECT_EQ(net::ERR_FAILED, cb_2.WaitForResult()); | |
73 } | |
74 | |
75 // Verify that the multiplexer allows the caller to supply a message type. | |
76 TEST_F(BlimpMessageMultiplexerTest, TypeSetByCaller) { | |
77 input_message_->mutable_input(); | |
78 | |
79 net::TestCompletionCallback cb_1; | |
80 input_processor_->ProcessMessage(std::move(input_message_), cb_1.callback()); | |
81 EXPECT_EQ(BlimpMessage::kInput, captured_message_.feature_case()); | |
82 EXPECT_EQ(InputMessage::Type_GestureScrollBegin, | |
83 captured_message_.input().type()); | |
84 captured_cb_.Run(net::OK); | |
85 EXPECT_EQ(net::OK, cb_1.WaitForResult()); | |
86 } | |
87 | |
88 // Verify that senders for a given type can be torn down and recreated. | |
89 TEST_F(BlimpMessageMultiplexerTest, SenderTransience) { | |
90 net::TestCompletionCallback cb_3; | |
91 input_processor_ = multiplexer_.CreateSender(BlimpMessage::kInput); | |
92 input_processor_->ProcessMessage(std::move(input_message_), cb_3.callback()); | |
93 EXPECT_EQ(BlimpMessage::kInput, captured_message_.feature_case()); | |
94 EXPECT_EQ(InputMessage::Type_GestureScrollBegin, | |
95 captured_message_.input().type()); | |
96 captured_cb_.Run(net::OK); | |
97 EXPECT_EQ(net::OK, cb_3.WaitForResult()); | |
98 } | |
99 | |
100 // Verify that there is no limit on the number of senders for a given type. | |
101 TEST_F(BlimpMessageMultiplexerTest, SenderMultiplicity) { | |
102 net::TestCompletionCallback cb_4; | |
103 std::unique_ptr<BlimpMessageProcessor> input_processor_2 = | |
104 multiplexer_.CreateSender(BlimpMessage::kInput); | |
105 input_processor_2->ProcessMessage(std::move(input_message_), cb_4.callback()); | |
106 EXPECT_EQ(BlimpMessage::kInput, captured_message_.feature_case()); | |
107 EXPECT_EQ(InputMessage::Type_GestureScrollBegin, | |
108 captured_message_.input().type()); | |
109 captured_cb_.Run(net::ERR_INVALID_ARGUMENT); | |
110 EXPECT_EQ(net::ERR_INVALID_ARGUMENT, cb_4.WaitForResult()); | |
111 } | |
112 | |
113 } // namespace | |
114 } // namespace blimp | |
OLD | NEW |