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