Chromium Code Reviews| Index: content/renderer/presentation/presentation_connection_proxy_unittest.cc |
| diff --git a/content/renderer/presentation/presentation_connection_proxy_unittest.cc b/content/renderer/presentation/presentation_connection_proxy_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3f1cd558f15f2da03ab5a12aa7f04e5212da00f6 |
| --- /dev/null |
| +++ b/content/renderer/presentation/presentation_connection_proxy_unittest.cc |
| @@ -0,0 +1,133 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/run_loop.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "content/renderer/presentation/presentation_connection_proxy.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationConnection.h" |
| +#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationController.h" |
| + |
| +using ::testing::_; |
| + |
| +namespace content { |
| + |
| +class TestPresentationConnection : public blink::WebPresentationConnection { |
| + public: |
| + void bindProxy( |
| + std::unique_ptr<blink::WebPresentationConnectionProxy> proxy) override { |
| + proxy_ = std::move(proxy); |
| + } |
| + MOCK_METHOD1(didReceiveTextMessage, void(const blink::WebString& message)); |
| + MOCK_METHOD2(didReceiveBinaryMessage, |
| + void(const uint8_t* data, size_t length)); |
| + MOCK_METHOD1(didChangeState, void(blink::WebPresentationConnectionState)); |
| + |
| + std::unique_ptr<blink::WebPresentationConnectionProxy> proxy_; |
| +}; |
| + |
| +class PresentationConnectionProxyTest : public ::testing::Test { |
|
mark a. foltz
2017/01/23 20:18:42
Nice unit test :-)
|
| + public: |
| + PresentationConnectionProxyTest() {} |
| + ~PresentationConnectionProxyTest() override {} |
| + |
| + void SetUp() override { |
| + // Set some test data. |
| + controller_connection_ = base::MakeUnique<TestPresentationConnection>(); |
| + receiver_connection_ = base::MakeUnique<TestPresentationConnection>(); |
| + } |
| + |
| + void TearDown() override { |
| + controller_connection_.reset(); |
| + receiver_connection_.reset(); |
| + } |
| + |
| + void SetUpPresentationConnectionProxy() { |
|
mark a. foltz
2017/01/23 20:18:42
Should this just be part of SetUp()?
zhaobin
2017/01/24 01:23:24
Done.
|
| + controller_connection_proxy_ = |
| + new ControllerConnectionProxy(controller_connection_.get()); |
| + controller_connection_->bindProxy( |
| + base::WrapUnique(controller_connection_proxy_)); |
| + receiver_connection_proxy_ = |
| + new ReceiverConnectionProxy(receiver_connection_.get()); |
| + receiver_connection_->bindProxy( |
| + base::WrapUnique(receiver_connection_proxy_)); |
| + |
| + EXPECT_CALL( |
| + *controller_connection_, |
| + didChangeState(blink::WebPresentationConnectionState::Connected)); |
| + EXPECT_CALL( |
| + *receiver_connection_, |
| + didChangeState(blink::WebPresentationConnectionState::Connected)); |
| + |
| + receiver_connection_proxy_->Bind( |
| + controller_connection_proxy_->MakeRemoteRequest()); |
| + receiver_connection_proxy_->BindTargetConnection( |
| + controller_connection_proxy_->Bind()); |
| + } |
| + |
| + void ExpectSendConnectionMessageCallback(bool success) { |
| + EXPECT_TRUE(success); |
| + } |
| + |
| + protected: |
| + std::unique_ptr<TestPresentationConnection> controller_connection_; |
| + std::unique_ptr<TestPresentationConnection> receiver_connection_; |
| + ControllerConnectionProxy* controller_connection_proxy_; |
| + ReceiverConnectionProxy* receiver_connection_proxy_; |
| + |
| + private: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| +}; |
| + |
| +TEST_F(PresentationConnectionProxyTest, TestSendString) { |
| + SetUpPresentationConnectionProxy(); |
| + |
| + blink::WebString message = blink::WebString::fromUTF8("test message"); |
| + blink::mojom::ConnectionMessagePtr session_message = |
| + blink::mojom::ConnectionMessage::New(); |
| + session_message->type = blink::mojom::PresentationMessageType::TEXT; |
| + session_message->message = message.utf8(); |
| + |
| + base::RunLoop run_loop; |
| + EXPECT_CALL(*receiver_connection_, didReceiveTextMessage(message)); |
| + controller_connection_proxy_->SendConnectionMessage( |
| + std::move(session_message), |
| + base::Bind( |
| + &PresentationConnectionProxyTest::ExpectSendConnectionMessageCallback, |
| + base::Unretained(this))); |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(PresentationConnectionProxyTest, TestSendArrayBuffer) { |
| + SetUpPresentationConnectionProxy(); |
| + |
| + std::vector<uint8_t> expected_data; |
| + expected_data.push_back(42); |
| + expected_data.push_back(36); |
| + |
| + blink::mojom::ConnectionMessagePtr session_message = |
| + blink::mojom::ConnectionMessage::New(); |
| + session_message->type = blink::mojom::PresentationMessageType::BINARY; |
| + session_message->data = expected_data; |
| + |
| + base::RunLoop run_loop; |
| + EXPECT_CALL(*receiver_connection_, didReceiveBinaryMessage(_, _)) |
| + .WillOnce(::testing::Invoke( |
| + [this, &expected_data](const uint8_t* data, size_t length) { |
| + std::vector<uint8_t> message_data(data, data + length); |
| + EXPECT_EQ(expected_data, message_data); |
| + })); |
| + |
| + controller_connection_proxy_->SendConnectionMessage( |
| + std::move(session_message), |
| + base::Bind( |
| + &PresentationConnectionProxyTest::ExpectSendConnectionMessageCallback, |
| + base::Unretained(this))); |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +} // namespace content |