OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 <memory> |
| 6 #include <utility> |
| 7 |
| 8 #include "base/run_loop.h" |
| 9 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "content/renderer/presentation/presentation_connection_proxy.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnection.h" |
| 13 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" |
| 14 |
| 15 using ::testing::_; |
| 16 |
| 17 namespace content { |
| 18 |
| 19 class TestPresentationConnection : public blink::WebPresentationConnection { |
| 20 public: |
| 21 void bindProxy( |
| 22 std::unique_ptr<blink::WebPresentationConnectionProxy> proxy) override { |
| 23 proxy_ = std::move(proxy); |
| 24 } |
| 25 MOCK_METHOD1(didReceiveTextMessage, void(const blink::WebString& message)); |
| 26 MOCK_METHOD2(didReceiveBinaryMessage, |
| 27 void(const uint8_t* data, size_t length)); |
| 28 MOCK_METHOD1(didChangeState, void(blink::WebPresentationConnectionState)); |
| 29 |
| 30 std::unique_ptr<blink::WebPresentationConnectionProxy> proxy_; |
| 31 }; |
| 32 |
| 33 class PresentationConnectionProxyTest : public ::testing::Test { |
| 34 public: |
| 35 PresentationConnectionProxyTest() {} |
| 36 ~PresentationConnectionProxyTest() override {} |
| 37 |
| 38 void SetUp() override { |
| 39 // Set some test data. |
| 40 controller_connection_ = base::MakeUnique<TestPresentationConnection>(); |
| 41 receiver_connection_ = base::MakeUnique<TestPresentationConnection>(); |
| 42 } |
| 43 |
| 44 void TearDown() override { |
| 45 controller_connection_.reset(); |
| 46 receiver_connection_.reset(); |
| 47 } |
| 48 |
| 49 void SetUpPresentationConnectionProxy() { |
| 50 controller_connection_proxy_ = |
| 51 new ControllerConnectionProxy(controller_connection_.get()); |
| 52 controller_connection_->bindProxy( |
| 53 base::WrapUnique(controller_connection_proxy_)); |
| 54 receiver_connection_proxy_ = |
| 55 new ReceiverConnectionProxy(receiver_connection_.get()); |
| 56 receiver_connection_->bindProxy( |
| 57 base::WrapUnique(receiver_connection_proxy_)); |
| 58 |
| 59 EXPECT_CALL( |
| 60 *controller_connection_, |
| 61 didChangeState(blink::WebPresentationConnectionState::Connected)); |
| 62 EXPECT_CALL( |
| 63 *receiver_connection_, |
| 64 didChangeState(blink::WebPresentationConnectionState::Connected)); |
| 65 |
| 66 receiver_connection_proxy_->Bind( |
| 67 controller_connection_proxy_->MakeRemoteRequest()); |
| 68 receiver_connection_proxy_->BindTargetConnection( |
| 69 controller_connection_proxy_->Bind()); |
| 70 } |
| 71 |
| 72 void ExpectSendConnectionMessageCallback(bool success) { |
| 73 EXPECT_TRUE(success); |
| 74 } |
| 75 |
| 76 protected: |
| 77 std::unique_ptr<TestPresentationConnection> controller_connection_; |
| 78 std::unique_ptr<TestPresentationConnection> receiver_connection_; |
| 79 ControllerConnectionProxy* controller_connection_proxy_; |
| 80 ReceiverConnectionProxy* receiver_connection_proxy_; |
| 81 |
| 82 private: |
| 83 content::TestBrowserThreadBundle thread_bundle_; |
| 84 }; |
| 85 |
| 86 TEST_F(PresentationConnectionProxyTest, TestSendString) { |
| 87 SetUpPresentationConnectionProxy(); |
| 88 |
| 89 blink::WebString message = blink::WebString::fromUTF8("test message"); |
| 90 blink::mojom::ConnectionMessagePtr session_message = |
| 91 blink::mojom::ConnectionMessage::New(); |
| 92 session_message->type = blink::mojom::PresentationMessageType::TEXT; |
| 93 session_message->message = message.utf8(); |
| 94 |
| 95 base::RunLoop run_loop; |
| 96 EXPECT_CALL(*receiver_connection_, didReceiveTextMessage(message)); |
| 97 controller_connection_proxy_->SendConnectionMessage( |
| 98 std::move(session_message), |
| 99 base::Bind( |
| 100 &PresentationConnectionProxyTest::ExpectSendConnectionMessageCallback, |
| 101 base::Unretained(this))); |
| 102 run_loop.RunUntilIdle(); |
| 103 } |
| 104 |
| 105 TEST_F(PresentationConnectionProxyTest, TestSendArrayBuffer) { |
| 106 SetUpPresentationConnectionProxy(); |
| 107 |
| 108 std::vector<uint8_t> expected_data; |
| 109 expected_data.push_back(42); |
| 110 expected_data.push_back(36); |
| 111 |
| 112 blink::mojom::ConnectionMessagePtr session_message = |
| 113 blink::mojom::ConnectionMessage::New(); |
| 114 session_message->type = blink::mojom::PresentationMessageType::BINARY; |
| 115 session_message->data = expected_data; |
| 116 |
| 117 base::RunLoop run_loop; |
| 118 EXPECT_CALL(*receiver_connection_, didReceiveBinaryMessage(_, _)) |
| 119 .WillOnce(::testing::Invoke( |
| 120 [this, &expected_data](const uint8_t* data, size_t length) { |
| 121 std::vector<uint8_t> message_data(data, data + length); |
| 122 EXPECT_EQ(expected_data, message_data); |
| 123 })); |
| 124 |
| 125 controller_connection_proxy_->SendConnectionMessage( |
| 126 std::move(session_message), |
| 127 base::Bind( |
| 128 &PresentationConnectionProxyTest::ExpectSendConnectionMessageCallback, |
| 129 base::Unretained(this))); |
| 130 run_loop.RunUntilIdle(); |
| 131 } |
| 132 |
| 133 } // namespace content |
OLD | NEW |