| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 <memory> | 5 #include <memory> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "content/public/common/presentation_connection_message.h" |
| 9 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "content/renderer/presentation/presentation_connection_proxy.h" | 11 #include "content/renderer/presentation/presentation_connection_proxy.h" |
| 11 #include "content/renderer/presentation/presentation_dispatcher.h" | 12 #include "content/renderer/presentation/presentation_dispatcher.h" |
| 12 #include "content/renderer/presentation/test_presentation_connection.h" | 13 #include "content/renderer/presentation/test_presentation_connection.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nAvailabilityObserver.h" | 15 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nAvailabilityObserver.h" |
| 15 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnection.h" | 16 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnection.h" |
| 16 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnectionCallbacks.h" | 17 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnectionCallbacks.h" |
| 17 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" | 18 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" |
| 18 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nError.h" | 19 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nError.h" |
| 19 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nReceiver.h" | 20 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nReceiver.h" |
| 20 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nSessionInfo.h" | 21 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nSessionInfo.h" |
| 21 #include "third_party/WebKit/public/web/WebArrayBuffer.h" | 22 #include "third_party/WebKit/public/web/WebArrayBuffer.h" |
| 22 | 23 |
| 23 using ::testing::_; | 24 using ::testing::_; |
| 24 using ::testing::Invoke; | 25 using ::testing::Invoke; |
| 25 using blink::WebArrayBuffer; | 26 using blink::WebArrayBuffer; |
| 26 using blink::WebPresentationAvailabilityCallbacks; | 27 using blink::WebPresentationAvailabilityCallbacks; |
| 27 using blink::WebPresentationAvailabilityObserver; | 28 using blink::WebPresentationAvailabilityObserver; |
| 28 using blink::WebPresentationConnectionCallbacks; | 29 using blink::WebPresentationConnectionCallbacks; |
| 29 using blink::WebPresentationError; | 30 using blink::WebPresentationError; |
| 30 using blink::WebPresentationSessionInfo; | 31 using blink::WebPresentationSessionInfo; |
| 31 using blink::WebString; | 32 using blink::WebString; |
| 32 using blink::WebURL; | 33 using blink::WebURL; |
| 33 using blink::WebVector; | 34 using blink::WebVector; |
| 34 using blink::mojom::PresentationConnection; | 35 using blink::mojom::PresentationConnection; |
| 35 using blink::mojom::PresentationService; | 36 using blink::mojom::PresentationService; |
| 36 using blink::mojom::PresentationServiceClientPtr; | 37 using blink::mojom::PresentationServiceClientPtr; |
| 37 using blink::mojom::ConnectionMessage; | |
| 38 using blink::mojom::ConnectionMessagePtr; | |
| 39 | 38 |
| 40 // TODO(crbug.com/576808): Add test cases for the following: | 39 // TODO(crbug.com/576808): Add test cases for the following: |
| 41 // - State changes | 40 // - State changes |
| 42 // - Messages received | 41 // - Messages received |
| 43 // - Discarding queued messages when the frame navigates | 42 // - Discarding queued messages when the frame navigates |
| 44 // - Screen availability not supported | 43 // - Screen availability not supported |
| 45 // - Default presentation starting | 44 // - Default presentation starting |
| 46 | 45 |
| 47 namespace content { | 46 namespace content { |
| 48 | 47 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 68 MOCK_METHOD1(ListenForScreenAvailability, void(const GURL& availability_url)); | 67 MOCK_METHOD1(ListenForScreenAvailability, void(const GURL& availability_url)); |
| 69 MOCK_METHOD1(StopListeningForScreenAvailability, | 68 MOCK_METHOD1(StopListeningForScreenAvailability, |
| 70 void(const GURL& availability_url)); | 69 void(const GURL& availability_url)); |
| 71 MOCK_METHOD2(StartSession, | 70 MOCK_METHOD2(StartSession, |
| 72 void(const std::vector<GURL>& presentation_urls, | 71 void(const std::vector<GURL>& presentation_urls, |
| 73 const StartSessionCallback& callback)); | 72 const StartSessionCallback& callback)); |
| 74 MOCK_METHOD3(JoinSession, | 73 MOCK_METHOD3(JoinSession, |
| 75 void(const std::vector<GURL>& presentation_urls, | 74 void(const std::vector<GURL>& presentation_urls, |
| 76 const base::Optional<std::string>& presentation_id, | 75 const base::Optional<std::string>& presentation_id, |
| 77 const JoinSessionCallback& callback)); | 76 const JoinSessionCallback& callback)); |
| 78 | |
| 79 MOCK_METHOD2(CloseConnection, | 77 MOCK_METHOD2(CloseConnection, |
| 80 void(const GURL& presentation_url, | 78 void(const GURL& presentation_url, |
| 81 const std::string& presentation_id)); | 79 const std::string& presentation_id)); |
| 82 MOCK_METHOD2(Terminate, | 80 MOCK_METHOD2(Terminate, |
| 83 void(const GURL& presentation_url, | 81 void(const GURL& presentation_url, |
| 84 const std::string& presentation_id)); | 82 const std::string& presentation_id)); |
| 85 | 83 |
| 86 MOCK_METHOD1(ListenForConnectionMessages, | 84 MOCK_METHOD1(ListenForConnectionMessages, |
| 87 void(const PresentationSessionInfo& session_info)); | 85 void(const PresentationSessionInfo& session_info)); |
| 88 | 86 |
| 89 void SetPresentationConnection( | 87 void SetPresentationConnection( |
| 90 const PresentationSessionInfo& session_info, | 88 const PresentationSessionInfo& session_info, |
| 91 blink::mojom::PresentationConnectionPtr controller_conn_ptr, | 89 blink::mojom::PresentationConnectionPtr controller_conn_ptr, |
| 92 blink::mojom::PresentationConnectionRequest receiver_conn_request) | 90 blink::mojom::PresentationConnectionRequest receiver_conn_request) |
| 93 override { | 91 override { |
| 94 SetPresentationConnection(session_info, controller_conn_ptr.get()); | 92 SetPresentationConnection(session_info, controller_conn_ptr.get()); |
| 95 } | 93 } |
| 96 MOCK_METHOD2(SetPresentationConnection, | 94 MOCK_METHOD2(SetPresentationConnection, |
| 97 void(const PresentationSessionInfo& session_info, | 95 void(const PresentationSessionInfo& session_info, |
| 98 PresentationConnection* connection)); | 96 PresentationConnection* connection)); |
| 99 }; | 97 }; |
| 100 | 98 |
| 101 class TestPresentationConnectionProxy : public PresentationConnectionProxy { | 99 class TestPresentationConnectionProxy : public PresentationConnectionProxy { |
| 102 public: | 100 public: |
| 103 TestPresentationConnectionProxy(blink::WebPresentationConnection* connection) | 101 TestPresentationConnectionProxy(blink::WebPresentationConnection* connection) |
| 104 : PresentationConnectionProxy(connection) {} | 102 : PresentationConnectionProxy(connection) {} |
| 105 | 103 |
| 106 void SendConnectionMessage(blink::mojom::ConnectionMessagePtr session_message, | 104 // PresentationConnectionMessage is move-only. |
| 107 const OnMessageCallback& callback) const override { | 105 void SendConnectionMessage(PresentationConnectionMessage message, |
| 108 SendConnectionMessageInternal(session_message.get(), callback); | 106 const OnMessageCallback& cb) const { |
| 107 SendConnectionMessageInternal(message, cb); |
| 109 } | 108 } |
| 110 MOCK_CONST_METHOD2(SendConnectionMessageInternal, | 109 MOCK_CONST_METHOD2(SendConnectionMessageInternal, |
| 111 void(blink::mojom::ConnectionMessage*, | 110 void(const PresentationConnectionMessage&, |
| 112 const OnMessageCallback&)); | 111 const OnMessageCallback&)); |
| 113 MOCK_CONST_METHOD0(close, void()); | 112 MOCK_CONST_METHOD0(close, void()); |
| 114 }; | 113 }; |
| 115 | 114 |
| 116 class TestPresentationReceiver : public blink::WebPresentationReceiver { | 115 class TestPresentationReceiver : public blink::WebPresentationReceiver { |
| 117 public: | 116 public: |
| 118 blink::WebPresentationConnection* onReceiverConnectionAvailable( | 117 blink::WebPresentationConnection* onReceiverConnectionAvailable( |
| 119 const blink::WebPresentationSessionInfo&) override { | 118 const blink::WebPresentationSessionInfo&) override { |
| 120 return &connection_; | 119 return &connection_; |
| 121 } | 120 } |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 run_loop.RunUntilIdle(); | 405 run_loop.RunUntilIdle(); |
| 407 } | 406 } |
| 408 EXPECT_TRUE(connection.proxy()); | 407 EXPECT_TRUE(connection.proxy()); |
| 409 } | 408 } |
| 410 | 409 |
| 411 TEST_F(PresentationDispatcherTest, TestSendString) { | 410 TEST_F(PresentationDispatcherTest, TestSendString) { |
| 412 WebString message = WebString::fromUTF8("test message"); | 411 WebString message = WebString::fromUTF8("test message"); |
| 413 TestPresentationConnection connection; | 412 TestPresentationConnection connection; |
| 414 TestPresentationConnectionProxy connection_proxy(&connection); | 413 TestPresentationConnectionProxy connection_proxy(&connection); |
| 415 | 414 |
| 415 PresentationConnectionMessage expected_message(message.utf8()); |
| 416 |
| 416 base::RunLoop run_loop; | 417 base::RunLoop run_loop; |
| 417 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _)) | 418 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _)) |
| 418 .WillOnce(Invoke([&message](ConnectionMessage* session_message, | 419 .WillOnce(Invoke([&expected_message]( |
| 419 const OnMessageCallback& callback) { | 420 const PresentationConnectionMessage& message_request, |
| 420 EXPECT_EQ(blink::mojom::PresentationMessageType::TEXT, | 421 const OnMessageCallback& callback) { |
| 421 session_message->type); | 422 EXPECT_EQ(message_request, expected_message); |
| 422 EXPECT_EQ(message.utf8(), session_message->message.value()); | 423 callback.Run(true); |
| 423 })); | 424 })); |
| 425 |
| 424 dispatcher_.sendString(url1_, presentation_id_, message, &connection_proxy); | 426 dispatcher_.sendString(url1_, presentation_id_, message, &connection_proxy); |
| 425 run_loop.RunUntilIdle(); | 427 run_loop.RunUntilIdle(); |
| 426 } | 428 } |
| 427 | 429 |
| 428 TEST_F(PresentationDispatcherTest, TestSendArrayBuffer) { | 430 TEST_F(PresentationDispatcherTest, TestSendArrayBuffer) { |
| 431 std::vector<uint8_t> data(array_buffer_data(), |
| 432 array_buffer_data() + array_buffer_.byteLength()); |
| 429 TestPresentationConnection connection; | 433 TestPresentationConnection connection; |
| 430 TestPresentationConnectionProxy connection_proxy(&connection); | 434 TestPresentationConnectionProxy connection_proxy(&connection); |
| 435 PresentationConnectionMessage expected_message(data); |
| 431 | 436 |
| 432 base::RunLoop run_loop; | 437 base::RunLoop run_loop; |
| 433 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _)) | 438 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _)) |
| 434 .WillOnce(Invoke([this](ConnectionMessage* message_request, | 439 .WillOnce(Invoke([&expected_message]( |
| 435 const OnMessageCallback& callback) { | 440 const PresentationConnectionMessage& message_request, |
| 436 std::vector<uint8_t> data( | 441 const OnMessageCallback& callback) { |
| 437 array_buffer_data(), | 442 EXPECT_EQ(message_request, expected_message); |
| 438 array_buffer_data() + array_buffer_.byteLength()); | 443 callback.Run(true); |
| 439 EXPECT_TRUE(message_request->data.has_value()); | |
| 440 EXPECT_EQ(data, message_request->data.value()); | |
| 441 })); | 444 })); |
| 442 dispatcher_.sendArrayBuffer(url1_, presentation_id_, array_buffer_data(), | 445 dispatcher_.sendArrayBuffer(url1_, presentation_id_, array_buffer_data(), |
| 443 array_buffer_.byteLength(), &connection_proxy); | 446 array_buffer_.byteLength(), &connection_proxy); |
| 444 run_loop.RunUntilIdle(); | 447 run_loop.RunUntilIdle(); |
| 445 } | 448 } |
| 446 | 449 |
| 447 TEST_F(PresentationDispatcherTest, TestSendBlobData) { | 450 TEST_F(PresentationDispatcherTest, TestSendBlobData) { |
| 451 std::vector<uint8_t> data(array_buffer_data(), |
| 452 array_buffer_data() + array_buffer_.byteLength()); |
| 448 TestPresentationConnection connection; | 453 TestPresentationConnection connection; |
| 449 TestPresentationConnectionProxy connection_proxy(&connection); | 454 TestPresentationConnectionProxy connection_proxy(&connection); |
| 455 PresentationConnectionMessage expected_message(data); |
| 450 | 456 |
| 451 base::RunLoop run_loop; | 457 base::RunLoop run_loop; |
| 452 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _)) | 458 EXPECT_CALL(connection_proxy, SendConnectionMessageInternal(_, _)) |
| 453 .WillOnce(Invoke([this](ConnectionMessage* message_request, | 459 .WillOnce(Invoke([&expected_message]( |
| 454 const OnMessageCallback& callback) { | 460 const PresentationConnectionMessage& message_request, |
| 455 std::vector<uint8_t> data( | 461 const OnMessageCallback& callback) { |
| 456 array_buffer_data(), | 462 EXPECT_EQ(message_request, expected_message); |
| 457 array_buffer_data() + array_buffer_.byteLength()); | |
| 458 EXPECT_TRUE(message_request->data.has_value()); | |
| 459 EXPECT_EQ(data, message_request->data.value()); | |
| 460 callback.Run(true); | 463 callback.Run(true); |
| 461 })); | 464 })); |
| 462 dispatcher_.sendBlobData(url1_, presentation_id_, array_buffer_data(), | 465 dispatcher_.sendBlobData(url1_, presentation_id_, array_buffer_data(), |
| 463 array_buffer_.byteLength(), &connection_proxy); | 466 array_buffer_.byteLength(), &connection_proxy); |
| 464 run_loop.RunUntilIdle(); | 467 run_loop.RunUntilIdle(); |
| 465 } | 468 } |
| 466 | 469 |
| 467 TEST_F(PresentationDispatcherTest, TestOnReceiverConnectionAvailable) { | 470 TEST_F(PresentationDispatcherTest, TestOnReceiverConnectionAvailable) { |
| 468 PresentationSessionInfo session_info(gurl1_, presentation_id_.utf8()); | 471 PresentationSessionInfo session_info(gurl1_, presentation_id_.utf8()); |
| 469 | 472 |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 | 780 |
| 778 for (auto* mock_observer : mock_observers_) | 781 for (auto* mock_observer : mock_observers_) |
| 779 EXPECT_CALL(*mock_observer, availabilityChanged(false)); | 782 EXPECT_CALL(*mock_observer, availabilityChanged(false)); |
| 780 | 783 |
| 781 base::RunLoop run_loop_2; | 784 base::RunLoop run_loop_2; |
| 782 ChangeURLState(gurl2_, URLState::Unavailable); | 785 ChangeURLState(gurl2_, URLState::Unavailable); |
| 783 run_loop_2.RunUntilIdle(); | 786 run_loop_2.RunUntilIdle(); |
| 784 } | 787 } |
| 785 | 788 |
| 786 } // namespace content | 789 } // namespace content |
| OLD | NEW |