| 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 "content/browser/presentation/presentation_service_impl.h" | 5 #include "content/browser/presentation/presentation_service_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <iterator> |
| 10 #include <memory> | 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <utility> | 13 #include <utility> |
| 13 #include <vector> | 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/location.h" | 16 #include "base/location.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "content/public/browser/navigation_handle.h" | 21 #include "content/public/browser/navigation_handle.h" |
| 21 #include "content/public/browser/presentation_service_delegate.h" | 22 #include "content/public/browser/presentation_service_delegate.h" |
| 22 #include "content/public/common/presentation_constants.h" | 23 #include "content/public/common/presentation_connection_message.h" |
| 23 #include "content/public/common/presentation_session.h" | 24 #include "content/public/common/presentation_session.h" |
| 24 #include "content/test/test_render_frame_host.h" | 25 #include "content/test/test_render_frame_host.h" |
| 25 #include "content/test/test_render_view_host.h" | 26 #include "content/test/test_render_view_host.h" |
| 26 #include "content/test/test_web_contents.h" | 27 #include "content/test/test_web_contents.h" |
| 27 #include "mojo/public/cpp/bindings/interface_ptr.h" | 28 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
| 29 | 30 |
| 30 using ::testing::_; | 31 using ::testing::_; |
| 32 using ::testing::ByRef; |
| 31 using ::testing::Eq; | 33 using ::testing::Eq; |
| 34 using ::testing::Invoke; |
| 32 using ::testing::InvokeWithoutArgs; | 35 using ::testing::InvokeWithoutArgs; |
| 33 using ::testing::Mock; | 36 using ::testing::Mock; |
| 34 using ::testing::Return; | 37 using ::testing::Return; |
| 35 using ::testing::SaveArg; | 38 using ::testing::SaveArg; |
| 39 using ::testing::WithArgs; |
| 36 | 40 |
| 37 namespace content { | 41 namespace content { |
| 38 | 42 |
| 39 namespace { | 43 namespace { |
| 40 | 44 |
| 41 // Matches Mojo structs. | |
| 42 MATCHER_P(Equals, expected, "") { | |
| 43 return expected.Equals(arg); | |
| 44 } | |
| 45 | |
| 46 // Matches content::PresentationSessionInfo. | 45 // Matches content::PresentationSessionInfo. |
| 47 MATCHER_P(SessionInfoEquals, expected, "") { | 46 MATCHER_P(SessionInfoEquals, expected, "") { |
| 48 return expected.presentation_url == arg.presentation_url && | 47 return expected.presentation_url == arg.presentation_url && |
| 49 expected.presentation_id == arg.presentation_id; | 48 expected.presentation_id == arg.presentation_id; |
| 50 } | 49 } |
| 51 | 50 |
| 52 const char kPresentationId[] = "presentationId"; | 51 const char kPresentationId[] = "presentationId"; |
| 53 const char kPresentationUrl1[] = "http://foo.com/index.html"; | 52 const char kPresentationUrl1[] = "http://foo.com/index.html"; |
| 54 const char kPresentationUrl2[] = "http://example.com/index.html"; | 53 const char kPresentationUrl2[] = "http://example.com/index.html"; |
| 55 const char kPresentationUrl3[] = "http://example.net/index.html"; | 54 const char kPresentationUrl3[] = "http://example.net/index.html"; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 void(int render_process_id, | 108 void(int render_process_id, |
| 110 int render_frame_id, | 109 int render_frame_id, |
| 111 const std::string& presentation_id)); | 110 const std::string& presentation_id)); |
| 112 MOCK_METHOD3(Terminate, | 111 MOCK_METHOD3(Terminate, |
| 113 void(int render_process_id, | 112 void(int render_process_id, |
| 114 int render_frame_id, | 113 int render_frame_id, |
| 115 const std::string& presentation_id)); | 114 const std::string& presentation_id)); |
| 116 MOCK_METHOD4(ListenForConnectionMessages, | 115 MOCK_METHOD4(ListenForConnectionMessages, |
| 117 void(int render_process_id, | 116 void(int render_process_id, |
| 118 int render_frame_id, | 117 int render_frame_id, |
| 119 const content::PresentationSessionInfo& session, | 118 const PresentationSessionInfo& session, |
| 120 const PresentationConnectionMessageCallback& message_cb)); | 119 const PresentationConnectionMessageCallback& message_cb)); |
| 121 MOCK_METHOD5(SendMessageRawPtr, | 120 // PresentationConnectionMessage is move-only. |
| 121 void SendMessage(int render_process_id, |
| 122 int render_frame_id, |
| 123 const PresentationSessionInfo& session, |
| 124 PresentationConnectionMessage message, |
| 125 const SendMessageCallback& send_message_cb) { |
| 126 SendMessageInternal(render_process_id, render_frame_id, session, message, |
| 127 send_message_cb); |
| 128 } |
| 129 MOCK_METHOD5(SendMessageInternal, |
| 122 void(int render_process_id, | 130 void(int render_process_id, |
| 123 int render_frame_id, | 131 int render_frame_id, |
| 124 const content::PresentationSessionInfo& session, | 132 const PresentationSessionInfo& session, |
| 125 PresentationConnectionMessage* message_request, | 133 const PresentationConnectionMessage& message, |
| 126 const SendMessageCallback& send_message_cb)); | 134 const SendMessageCallback& send_message_cb)); |
| 127 void SendMessage( | 135 MOCK_METHOD4( |
| 128 int render_process_id, | 136 ListenForConnectionStateChange, |
| 129 int render_frame_id, | 137 void(int render_process_id, |
| 130 const content::PresentationSessionInfo& session, | 138 int render_frame_id, |
| 131 std::unique_ptr<PresentationConnectionMessage> message_request, | 139 const PresentationSessionInfo& connection, |
| 132 const SendMessageCallback& send_message_cb) override { | 140 const PresentationConnectionStateChangedCallback& state_changed_cb)); |
| 133 SendMessageRawPtr(render_process_id, render_frame_id, session, | |
| 134 message_request.release(), send_message_cb); | |
| 135 } | |
| 136 MOCK_METHOD4(ListenForConnectionStateChange, | |
| 137 void(int render_process_id, | |
| 138 int render_frame_id, | |
| 139 const content::PresentationSessionInfo& connection, | |
| 140 const content::PresentationConnectionStateChangedCallback& | |
| 141 state_changed_cb)); | |
| 142 | 141 |
| 143 void ConnectToPresentation( | 142 void ConnectToPresentation( |
| 144 int render_process_id, | 143 int render_process_id, |
| 145 int render_frame_id, | 144 int render_frame_id, |
| 146 const content::PresentationSessionInfo& session, | 145 const PresentationSessionInfo& session, |
| 147 PresentationConnectionPtr controller_conn_ptr, | 146 PresentationConnectionPtr controller_conn_ptr, |
| 148 PresentationConnectionRequest receiver_conn_request) override { | 147 PresentationConnectionRequest receiver_conn_request) override { |
| 149 RegisterOffscreenPresentationConnectionRaw( | 148 RegisterOffscreenPresentationConnectionRaw( |
| 150 render_process_id, render_frame_id, session, controller_conn_ptr.get()); | 149 render_process_id, render_frame_id, session, controller_conn_ptr.get()); |
| 151 } | 150 } |
| 152 | 151 |
| 153 MOCK_METHOD4(RegisterOffscreenPresentationConnectionRaw, | 152 MOCK_METHOD4(RegisterOffscreenPresentationConnectionRaw, |
| 154 void(int render_process_id, | 153 void(int render_process_id, |
| 155 int render_frame_id, | 154 int render_frame_id, |
| 156 const content::PresentationSessionInfo& session, | 155 const PresentationSessionInfo& session, |
| 157 blink::mojom::PresentationConnection* connection)); | 156 blink::mojom::PresentationConnection* connection)); |
| 158 | 157 |
| 159 void set_screen_availability_listening_supported(bool value) { | 158 void set_screen_availability_listening_supported(bool value) { |
| 160 screen_availability_listening_supported_ = value; | 159 screen_availability_listening_supported_ = value; |
| 161 } | 160 } |
| 162 | 161 |
| 163 private: | 162 private: |
| 164 bool screen_availability_listening_supported_ = true; | 163 bool screen_availability_listening_supported_ = true; |
| 165 }; | 164 }; |
| 166 | 165 |
| 167 class MockReceiverPresentationServiceDelegate | 166 class MockReceiverPresentationServiceDelegate |
| 168 : public ReceiverPresentationServiceDelegate { | 167 : public ReceiverPresentationServiceDelegate { |
| 169 public: | 168 public: |
| 170 MOCK_METHOD3(AddObserver, | 169 MOCK_METHOD3(AddObserver, |
| 171 void(int render_process_id, | 170 void(int render_process_id, |
| 172 int render_frame_id, | 171 int render_frame_id, |
| 173 PresentationServiceDelegate::Observer* observer)); | 172 PresentationServiceDelegate::Observer* observer)); |
| 174 MOCK_METHOD2(RemoveObserver, | 173 MOCK_METHOD2(RemoveObserver, |
| 175 void(int render_process_id, int render_frame_id)); | 174 void(int render_process_id, int render_frame_id)); |
| 176 MOCK_METHOD2(Reset, void(int render_process_id, int routing_id)); | 175 MOCK_METHOD2(Reset, void(int render_process_id, int routing_id)); |
| 177 MOCK_METHOD1(RegisterReceiverConnectionAvailableCallback, | 176 MOCK_METHOD1(RegisterReceiverConnectionAvailableCallback, |
| 178 void(const content::ReceiverConnectionAvailableCallback&)); | 177 void(const ReceiverConnectionAvailableCallback&)); |
| 179 }; | 178 }; |
| 180 | 179 |
| 181 class MockPresentationConnection : public blink::mojom::PresentationConnection { | 180 class MockPresentationConnection : public blink::mojom::PresentationConnection { |
| 182 public: | 181 public: |
| 183 void OnMessage(blink::mojom::ConnectionMessagePtr message, | 182 // PresentationConnectionMessage is move-only. |
| 184 const base::Callback<void(bool)>& send_message_cb) override { | 183 void OnMessage(PresentationConnectionMessage message, |
| 185 OnConnectionMessageReceived(*message); | 184 const base::Callback<void(bool)>& send_message_cb) { |
| 185 OnMessageInternal(message, send_message_cb); |
| 186 } | 186 } |
| 187 MOCK_METHOD1(OnConnectionMessageReceived, | 187 MOCK_METHOD2(OnMessageInternal, |
| 188 void(const blink::mojom::ConnectionMessage& message)); | 188 void(const PresentationConnectionMessage& message, |
| 189 const base::Callback<void(bool)>& send_message_cb)); |
| 189 MOCK_METHOD1(DidChangeState, void(PresentationConnectionState state)); | 190 MOCK_METHOD1(DidChangeState, void(PresentationConnectionState state)); |
| 190 MOCK_METHOD0(OnClose, void()); | 191 MOCK_METHOD0(OnClose, void()); |
| 191 }; | 192 }; |
| 192 | 193 |
| 193 class MockPresentationServiceClient | 194 class MockPresentationServiceClient |
| 194 : public blink::mojom::PresentationServiceClient { | 195 : public blink::mojom::PresentationServiceClient { |
| 195 public: | 196 public: |
| 196 MOCK_METHOD2(OnScreenAvailabilityUpdated, | 197 MOCK_METHOD2(OnScreenAvailabilityUpdated, |
| 197 void(const GURL& url, bool available)); | 198 void(const GURL& url, bool available)); |
| 198 MOCK_METHOD2(OnConnectionStateChanged, | 199 MOCK_METHOD2(OnConnectionStateChanged, |
| 199 void(const content::PresentationSessionInfo& connection, | 200 void(const PresentationSessionInfo& connection, |
| 200 PresentationConnectionState new_state)); | 201 PresentationConnectionState new_state)); |
| 201 MOCK_METHOD3(OnConnectionClosed, | 202 MOCK_METHOD3(OnConnectionClosed, |
| 202 void(const content::PresentationSessionInfo& connection, | 203 void(const PresentationSessionInfo& connection, |
| 203 PresentationConnectionCloseReason reason, | 204 PresentationConnectionCloseReason reason, |
| 204 const std::string& message)); | 205 const std::string& message)); |
| 205 MOCK_METHOD1(OnScreenAvailabilityNotSupported, void(const GURL& url)); | 206 MOCK_METHOD1(OnScreenAvailabilityNotSupported, void(const GURL& url)); |
| 206 | 207 // PresentationConnectionMessage is move-only. |
| 207 void OnConnectionMessagesReceived( | 208 void OnConnectionMessagesReceived( |
| 208 const content::PresentationSessionInfo& session_info, | 209 const PresentationSessionInfo& session_info, |
| 209 std::vector<blink::mojom::ConnectionMessagePtr> messages) override { | 210 std::vector<PresentationConnectionMessage> messages) { |
| 210 messages_received_ = std::move(messages); | 211 OnConnectionMessagesReceivedInternal(session_info, messages); |
| 211 MessagesReceived(); | |
| 212 } | 212 } |
| 213 MOCK_METHOD0(MessagesReceived, void()); | 213 MOCK_METHOD2( |
| 214 | 214 OnConnectionMessagesReceivedInternal, |
| 215 void(const PresentationSessionInfo& session_info, |
| 216 const std::vector<PresentationConnectionMessage>& messages)); |
| 215 MOCK_METHOD1(OnDefaultSessionStarted, | 217 MOCK_METHOD1(OnDefaultSessionStarted, |
| 216 void(const content::PresentationSessionInfo& session_info)); | 218 void(const PresentationSessionInfo& session_info)); |
| 217 | 219 |
| 218 void OnReceiverConnectionAvailable( | 220 void OnReceiverConnectionAvailable( |
| 219 const content::PresentationSessionInfo& session_info, | 221 const PresentationSessionInfo& session_info, |
| 220 blink::mojom::PresentationConnectionPtr controller_conn_ptr, | 222 blink::mojom::PresentationConnectionPtr controller_conn_ptr, |
| 221 blink::mojom::PresentationConnectionRequest receiver_conn_request) | 223 blink::mojom::PresentationConnectionRequest receiver_conn_request) |
| 222 override { | 224 override { |
| 223 OnReceiverConnectionAvailable(session_info); | 225 OnReceiverConnectionAvailable(session_info); |
| 224 } | 226 } |
| 225 MOCK_METHOD1(OnReceiverConnectionAvailable, | 227 MOCK_METHOD1(OnReceiverConnectionAvailable, |
| 226 void(const content::PresentationSessionInfo& session_info)); | 228 void(const PresentationSessionInfo& session_info)); |
| 227 | |
| 228 std::vector<blink::mojom::ConnectionMessagePtr> messages_received_; | |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 231 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
| 232 public: | 232 public: |
| 233 PresentationServiceImplTest() | 233 PresentationServiceImplTest() |
| 234 : presentation_url1_(GURL(kPresentationUrl1)), | 234 : presentation_url1_(GURL(kPresentationUrl1)), |
| 235 presentation_url2_(GURL(kPresentationUrl2)), | 235 presentation_url2_(GURL(kPresentationUrl2)), |
| 236 presentation_url3_(GURL(kPresentationUrl3)) {} | 236 presentation_url3_(GURL(kPresentationUrl3)) {} |
| 237 | 237 |
| 238 void SetUp() override { | 238 void SetUp() override { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 262 void TearDown() override { | 262 void TearDown() override { |
| 263 service_ptr_.reset(); | 263 service_ptr_.reset(); |
| 264 if (service_impl_.get()) { | 264 if (service_impl_.get()) { |
| 265 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); | 265 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
| 266 service_impl_.reset(); | 266 service_impl_.reset(); |
| 267 } | 267 } |
| 268 RenderViewHostImplTestHarness::TearDown(); | 268 RenderViewHostImplTestHarness::TearDown(); |
| 269 } | 269 } |
| 270 | 270 |
| 271 void Navigate(bool main_frame) { | 271 void Navigate(bool main_frame) { |
| 272 content::RenderFrameHost* rfh = main_rfh(); | 272 RenderFrameHost* rfh = main_rfh(); |
| 273 content::RenderFrameHostTester* rfh_tester = | 273 RenderFrameHostTester* rfh_tester = RenderFrameHostTester::For(rfh); |
| 274 content::RenderFrameHostTester::For(rfh); | |
| 275 if (!main_frame) | 274 if (!main_frame) |
| 276 rfh = rfh_tester->AppendChild("subframe"); | 275 rfh = rfh_tester->AppendChild("subframe"); |
| 277 std::unique_ptr<NavigationHandle> navigation_handle = | 276 std::unique_ptr<NavigationHandle> navigation_handle = |
| 278 NavigationHandle::CreateNavigationHandleForTesting( | 277 NavigationHandle::CreateNavigationHandleForTesting( |
| 279 GURL(), rfh, true); | 278 GURL(), rfh, true); |
| 280 // Destructor calls DidFinishNavigation. | 279 // Destructor calls DidFinishNavigation. |
| 281 } | 280 } |
| 282 | 281 |
| 283 void ListenForScreenAvailabilityAndWait(const GURL& url, | 282 void ListenForScreenAvailabilityAndWait(const GURL& url, |
| 284 bool delegate_success) { | 283 bool delegate_success) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 } | 327 } |
| 329 | 328 |
| 330 void ExpectCleanState() { | 329 void ExpectCleanState() { |
| 331 EXPECT_TRUE(service_impl_->default_presentation_urls_.empty()); | 330 EXPECT_TRUE(service_impl_->default_presentation_urls_.empty()); |
| 332 EXPECT_EQ( | 331 EXPECT_EQ( |
| 333 service_impl_->screen_availability_listeners_.find(presentation_url1_), | 332 service_impl_->screen_availability_listeners_.find(presentation_url1_), |
| 334 service_impl_->screen_availability_listeners_.end()); | 333 service_impl_->screen_availability_listeners_.end()); |
| 335 } | 334 } |
| 336 | 335 |
| 337 void ExpectNewSessionCallbackSuccess( | 336 void ExpectNewSessionCallbackSuccess( |
| 338 const base::Optional<content::PresentationSessionInfo>& info, | 337 const base::Optional<PresentationSessionInfo>& info, |
| 339 const base::Optional<content::PresentationError>& error) { | 338 const base::Optional<PresentationError>& error) { |
| 340 EXPECT_TRUE(info); | 339 EXPECT_TRUE(info); |
| 341 EXPECT_FALSE(error); | 340 EXPECT_FALSE(error); |
| 342 if (!run_loop_quit_closure_.is_null()) | 341 if (!run_loop_quit_closure_.is_null()) |
| 343 run_loop_quit_closure_.Run(); | 342 run_loop_quit_closure_.Run(); |
| 344 } | 343 } |
| 345 | 344 |
| 346 void ExpectNewSessionCallbackError( | 345 void ExpectNewSessionCallbackError( |
| 347 const base::Optional<content::PresentationSessionInfo>& info, | 346 const base::Optional<PresentationSessionInfo>& info, |
| 348 const base::Optional<content::PresentationError>& error) { | 347 const base::Optional<PresentationError>& error) { |
| 349 EXPECT_FALSE(info); | 348 EXPECT_FALSE(info); |
| 350 EXPECT_TRUE(error); | 349 EXPECT_TRUE(error); |
| 351 if (!run_loop_quit_closure_.is_null()) | 350 if (!run_loop_quit_closure_.is_null()) |
| 352 run_loop_quit_closure_.Run(); | 351 run_loop_quit_closure_.Run(); |
| 353 } | 352 } |
| 354 | 353 |
| 355 void ExpectConnectionMessages( | 354 void ExpectConnectionMessages( |
| 356 const std::vector<blink::mojom::ConnectionMessagePtr>& expected_msgs, | 355 const std::vector<PresentationConnectionMessage>& expected_msgs, |
| 357 const std::vector<blink::mojom::ConnectionMessagePtr>& actual_msgs) { | 356 const std::vector<PresentationConnectionMessage>& actual_msgs) { |
| 358 EXPECT_EQ(expected_msgs.size(), actual_msgs.size()); | 357 EXPECT_EQ(expected_msgs.size(), actual_msgs.size()); |
| 359 for (size_t i = 0; i < actual_msgs.size(); ++i) | 358 for (size_t i = 0; i < actual_msgs.size(); ++i) |
| 360 EXPECT_TRUE(expected_msgs[i].Equals(actual_msgs[i])); | 359 EXPECT_EQ(expected_msgs[i], actual_msgs[i]); |
| 361 } | |
| 362 | |
| 363 void RunListenForConnectionMessages(const std::string& text_msg, | |
| 364 const std::vector<uint8_t>& binary_data, | |
| 365 bool pass_ownership) { | |
| 366 std::vector<blink::mojom::ConnectionMessagePtr> expected_msgs(2); | |
| 367 expected_msgs[0] = blink::mojom::ConnectionMessage::New(); | |
| 368 expected_msgs[0]->type = blink::mojom::PresentationMessageType::TEXT; | |
| 369 expected_msgs[0]->message = text_msg; | |
| 370 expected_msgs[1] = blink::mojom::ConnectionMessage::New(); | |
| 371 expected_msgs[1]->type = blink::mojom::PresentationMessageType::BINARY; | |
| 372 expected_msgs[1]->data = binary_data; | |
| 373 | |
| 374 content::PresentationSessionInfo session(presentation_url1_, | |
| 375 kPresentationId); | |
| 376 | |
| 377 PresentationConnectionMessageCallback message_cb; | |
| 378 { | |
| 379 base::RunLoop run_loop; | |
| 380 EXPECT_CALL(mock_delegate_, ListenForConnectionMessages(_, _, _, _)) | |
| 381 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | |
| 382 SaveArg<3>(&message_cb))); | |
| 383 service_ptr_->ListenForConnectionMessages(session); | |
| 384 run_loop.Run(); | |
| 385 } | |
| 386 | |
| 387 std::vector<std::unique_ptr<PresentationConnectionMessage>> messages; | |
| 388 std::unique_ptr<content::PresentationConnectionMessage> message; | |
| 389 message.reset(new content::PresentationConnectionMessage( | |
| 390 PresentationMessageType::TEXT)); | |
| 391 message->message = text_msg; | |
| 392 messages.push_back(std::move(message)); | |
| 393 message.reset(new content::PresentationConnectionMessage( | |
| 394 PresentationMessageType::BINARY)); | |
| 395 message->data.reset(new std::vector<uint8_t>(binary_data)); | |
| 396 messages.push_back(std::move(message)); | |
| 397 | |
| 398 std::vector<blink::mojom::ConnectionMessagePtr> actual_msgs; | |
| 399 { | |
| 400 base::RunLoop run_loop; | |
| 401 EXPECT_CALL(mock_client_, MessagesReceived()) | |
| 402 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
| 403 message_cb.Run(std::move(messages), pass_ownership); | |
| 404 run_loop.Run(); | |
| 405 } | |
| 406 ExpectConnectionMessages(expected_msgs, mock_client_.messages_received_); | |
| 407 } | 360 } |
| 408 | 361 |
| 409 MockPresentationServiceDelegate mock_delegate_; | 362 MockPresentationServiceDelegate mock_delegate_; |
| 410 MockReceiverPresentationServiceDelegate mock_receiver_delegate_; | 363 MockReceiverPresentationServiceDelegate mock_receiver_delegate_; |
| 411 | 364 |
| 412 std::unique_ptr<PresentationServiceImpl> service_impl_; | 365 std::unique_ptr<PresentationServiceImpl> service_impl_; |
| 413 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_; | 366 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_; |
| 414 | 367 |
| 415 MockPresentationServiceClient mock_client_; | 368 MockPresentationServiceClient mock_client_; |
| 416 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> | 369 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 EXPECT_CALL(mock_delegate_, | 444 EXPECT_CALL(mock_delegate_, |
| 492 SetDefaultPresentationUrls(_, _, presentation_urls_, _)) | 445 SetDefaultPresentationUrls(_, _, presentation_urls_, _)) |
| 493 .Times(1); | 446 .Times(1); |
| 494 | 447 |
| 495 service_impl_->SetDefaultPresentationUrls(presentation_urls_); | 448 service_impl_->SetDefaultPresentationUrls(presentation_urls_); |
| 496 | 449 |
| 497 // Sets different DPUs. | 450 // Sets different DPUs. |
| 498 std::vector<GURL> more_urls = presentation_urls_; | 451 std::vector<GURL> more_urls = presentation_urls_; |
| 499 more_urls.push_back(presentation_url3_); | 452 more_urls.push_back(presentation_url3_); |
| 500 | 453 |
| 501 content::PresentationSessionStartedCallback callback; | 454 PresentationSessionStartedCallback callback; |
| 502 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls(_, _, more_urls, _)) | 455 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls(_, _, more_urls, _)) |
| 503 .WillOnce(SaveArg<3>(&callback)); | 456 .WillOnce(SaveArg<3>(&callback)); |
| 504 service_impl_->SetDefaultPresentationUrls(more_urls); | 457 service_impl_->SetDefaultPresentationUrls(more_urls); |
| 505 | 458 |
| 506 content::PresentationSessionInfo session_info(presentation_url2_, | 459 PresentationSessionInfo session_info(presentation_url2_, kPresentationId); |
| 507 kPresentationId); | |
| 508 | 460 |
| 509 base::RunLoop run_loop; | 461 base::RunLoop run_loop; |
| 510 EXPECT_CALL(mock_client_, | 462 EXPECT_CALL(mock_client_, |
| 511 OnDefaultSessionStarted(SessionInfoEquals(session_info))) | 463 OnDefaultSessionStarted(SessionInfoEquals(session_info))) |
| 512 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 464 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 513 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)); | 465 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)); |
| 514 callback.Run( | 466 callback.Run(PresentationSessionInfo(presentation_url2_, kPresentationId)); |
| 515 content::PresentationSessionInfo(presentation_url2_, kPresentationId)); | |
| 516 run_loop.Run(); | 467 run_loop.Run(); |
| 517 } | 468 } |
| 518 | 469 |
| 519 TEST_F(PresentationServiceImplTest, ListenForConnectionStateChange) { | 470 TEST_F(PresentationServiceImplTest, ListenForConnectionStateChange) { |
| 520 content::PresentationSessionInfo connection(presentation_url1_, | 471 PresentationSessionInfo connection(presentation_url1_, kPresentationId); |
| 521 kPresentationId); | 472 PresentationConnectionStateChangedCallback state_changed_cb; |
| 522 content::PresentationConnectionStateChangedCallback state_changed_cb; | |
| 523 // Trigger state change. It should be propagated back up to |mock_client_|. | 473 // Trigger state change. It should be propagated back up to |mock_client_|. |
| 524 content::PresentationSessionInfo presentation_connection(presentation_url1_, | 474 PresentationSessionInfo presentation_connection(presentation_url1_, |
| 525 kPresentationId); | 475 kPresentationId); |
| 526 | 476 |
| 527 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) | 477 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) |
| 528 .WillOnce(SaveArg<3>(&state_changed_cb)); | 478 .WillOnce(SaveArg<3>(&state_changed_cb)); |
| 529 service_impl_->ListenForConnectionStateChange(connection); | 479 service_impl_->ListenForConnectionStateChange(connection); |
| 530 | 480 |
| 531 { | 481 { |
| 532 base::RunLoop run_loop; | 482 base::RunLoop run_loop; |
| 533 EXPECT_CALL(mock_client_, | 483 EXPECT_CALL(mock_client_, OnConnectionStateChanged( |
| 534 OnConnectionStateChanged( | 484 SessionInfoEquals(presentation_connection), |
| 535 SessionInfoEquals(presentation_connection), | 485 PRESENTATION_CONNECTION_STATE_TERMINATED)) |
| 536 content::PRESENTATION_CONNECTION_STATE_TERMINATED)) | |
| 537 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 486 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 538 state_changed_cb.Run(PresentationConnectionStateChangeInfo( | 487 state_changed_cb.Run(PresentationConnectionStateChangeInfo( |
| 539 PRESENTATION_CONNECTION_STATE_TERMINATED)); | 488 PRESENTATION_CONNECTION_STATE_TERMINATED)); |
| 540 run_loop.Run(); | 489 run_loop.Run(); |
| 541 } | 490 } |
| 542 } | 491 } |
| 543 | 492 |
| 544 TEST_F(PresentationServiceImplTest, ListenForConnectionClose) { | 493 TEST_F(PresentationServiceImplTest, ListenForConnectionClose) { |
| 545 content::PresentationSessionInfo connection(presentation_url1_, | 494 PresentationSessionInfo connection(presentation_url1_, kPresentationId); |
| 546 kPresentationId); | 495 PresentationConnectionStateChangedCallback state_changed_cb; |
| 547 content::PresentationConnectionStateChangedCallback state_changed_cb; | |
| 548 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) | 496 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) |
| 549 .WillOnce(SaveArg<3>(&state_changed_cb)); | 497 .WillOnce(SaveArg<3>(&state_changed_cb)); |
| 550 service_impl_->ListenForConnectionStateChange(connection); | 498 service_impl_->ListenForConnectionStateChange(connection); |
| 551 | 499 |
| 552 // Trigger connection close. It should be propagated back up to | 500 // Trigger connection close. It should be propagated back up to |
| 553 // |mock_client_|. | 501 // |mock_client_|. |
| 554 content::PresentationSessionInfo presentation_connection(presentation_url1_, | 502 PresentationSessionInfo presentation_connection(presentation_url1_, |
| 555 kPresentationId); | 503 kPresentationId); |
| 556 { | 504 { |
| 557 base::RunLoop run_loop; | 505 base::RunLoop run_loop; |
| 558 PresentationConnectionStateChangeInfo closed_info( | 506 PresentationConnectionStateChangeInfo closed_info( |
| 559 PRESENTATION_CONNECTION_STATE_CLOSED); | 507 PRESENTATION_CONNECTION_STATE_CLOSED); |
| 560 closed_info.close_reason = PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY; | 508 closed_info.close_reason = PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY; |
| 561 closed_info.message = "Foo"; | 509 closed_info.message = "Foo"; |
| 562 | 510 |
| 563 EXPECT_CALL(mock_client_, | 511 EXPECT_CALL(mock_client_, |
| 564 OnConnectionClosed( | 512 OnConnectionClosed( |
| 565 SessionInfoEquals(presentation_connection), | 513 SessionInfoEquals(presentation_connection), |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 } | 608 } |
| 661 | 609 |
| 662 TEST_F(PresentationServiceImplTest, Terminate) { | 610 TEST_F(PresentationServiceImplTest, Terminate) { |
| 663 service_ptr_->Terminate(presentation_url1_, kPresentationId); | 611 service_ptr_->Terminate(presentation_url1_, kPresentationId); |
| 664 base::RunLoop run_loop; | 612 base::RunLoop run_loop; |
| 665 EXPECT_CALL(mock_delegate_, Terminate(_, _, Eq(kPresentationId))) | 613 EXPECT_CALL(mock_delegate_, Terminate(_, _, Eq(kPresentationId))) |
| 666 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 614 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 667 run_loop.Run(); | 615 run_loop.Run(); |
| 668 } | 616 } |
| 669 | 617 |
| 670 TEST_F(PresentationServiceImplTest, ListenForConnectionMessagesPassed) { | |
| 671 std::string text_msg("123"); | |
| 672 std::vector<uint8_t> binary_data(3, '\1'); | |
| 673 RunListenForConnectionMessages(text_msg, binary_data, true); | |
| 674 } | |
| 675 | |
| 676 TEST_F(PresentationServiceImplTest, ListenForConnectionMessagesCopied) { | |
| 677 std::string text_msg("123"); | |
| 678 std::vector<uint8_t> binary_data(3, '\1'); | |
| 679 RunListenForConnectionMessages(text_msg, binary_data, false); | |
| 680 } | |
| 681 | |
| 682 TEST_F(PresentationServiceImplTest, ListenForConnectionMessagesWithEmptyMsg) { | |
| 683 std::string text_msg(""); | |
| 684 std::vector<uint8_t> binary_data; | |
| 685 RunListenForConnectionMessages(text_msg, binary_data, false); | |
| 686 } | |
| 687 | |
| 688 TEST_F(PresentationServiceImplTest, SetPresentationConnection) { | 618 TEST_F(PresentationServiceImplTest, SetPresentationConnection) { |
| 689 content::PresentationSessionInfo session(presentation_url1_, kPresentationId); | 619 PresentationSessionInfo session(presentation_url1_, kPresentationId); |
| 690 | 620 |
| 691 blink::mojom::PresentationConnectionPtr connection; | 621 blink::mojom::PresentationConnectionPtr connection; |
| 692 MockPresentationConnection mock_presentation_connection; | 622 MockPresentationConnection mock_presentation_connection; |
| 693 mojo::Binding<blink::mojom::PresentationConnection> connection_binding( | 623 mojo::Binding<blink::mojom::PresentationConnection> connection_binding( |
| 694 &mock_presentation_connection, mojo::MakeRequest(&connection)); | 624 &mock_presentation_connection, mojo::MakeRequest(&connection)); |
| 695 blink::mojom::PresentationConnectionPtr receiver_connection; | 625 blink::mojom::PresentationConnectionPtr receiver_connection; |
| 696 auto request = mojo::MakeRequest(&receiver_connection); | 626 auto request = mojo::MakeRequest(&receiver_connection); |
| 697 | 627 |
| 698 content::PresentationSessionInfo expected(presentation_url1_, | 628 PresentationSessionInfo expected(presentation_url1_, kPresentationId); |
| 699 kPresentationId); | |
| 700 EXPECT_CALL(mock_delegate_, RegisterOffscreenPresentationConnectionRaw( | 629 EXPECT_CALL(mock_delegate_, RegisterOffscreenPresentationConnectionRaw( |
| 701 _, _, SessionInfoEquals(expected), _)); | 630 _, _, SessionInfoEquals(expected), _)); |
| 702 | 631 |
| 703 service_impl_->SetPresentationConnection(session, std::move(connection), | 632 service_impl_->SetPresentationConnection(session, std::move(connection), |
| 704 std::move(request)); | 633 std::move(request)); |
| 705 } | 634 } |
| 706 | 635 |
| 707 TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) { | 636 TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) { |
| 708 MockReceiverPresentationServiceDelegate mock_receiver_delegate; | 637 MockReceiverPresentationServiceDelegate mock_receiver_delegate; |
| 709 | 638 |
| 710 PresentationServiceImpl service_impl(contents()->GetMainFrame(), contents(), | 639 PresentationServiceImpl service_impl(contents()->GetMainFrame(), contents(), |
| 711 nullptr, &mock_receiver_delegate); | 640 nullptr, &mock_receiver_delegate); |
| 712 | 641 |
| 713 ReceiverConnectionAvailableCallback callback; | 642 ReceiverConnectionAvailableCallback callback; |
| 714 EXPECT_CALL(mock_receiver_delegate, | 643 EXPECT_CALL(mock_receiver_delegate, |
| 715 RegisterReceiverConnectionAvailableCallback(_)) | 644 RegisterReceiverConnectionAvailableCallback(_)) |
| 716 .WillOnce(SaveArg<0>(&callback)); | 645 .WillOnce(SaveArg<0>(&callback)); |
| 717 | 646 |
| 718 blink::mojom::PresentationServiceClientPtr client_ptr; | 647 blink::mojom::PresentationServiceClientPtr client_ptr; |
| 719 client_binding_.reset( | 648 client_binding_.reset( |
| 720 new mojo::Binding<blink::mojom::PresentationServiceClient>( | 649 new mojo::Binding<blink::mojom::PresentationServiceClient>( |
| 721 &mock_client_, mojo::MakeRequest(&client_ptr))); | 650 &mock_client_, mojo::MakeRequest(&client_ptr))); |
| 722 service_impl.controller_delegate_ = nullptr; | 651 service_impl.controller_delegate_ = nullptr; |
| 723 service_impl.SetClient(std::move(client_ptr)); | 652 service_impl.SetClient(std::move(client_ptr)); |
| 724 EXPECT_FALSE(callback.is_null()); | 653 EXPECT_FALSE(callback.is_null()); |
| 725 | 654 |
| 726 // NO-OP for ControllerPresentationServiceDelegate API functions | 655 // NO-OP for ControllerPresentationServiceDelegate API functions |
| 727 EXPECT_CALL(mock_delegate_, ListenForConnectionMessages(_, _, _, _)).Times(0); | 656 EXPECT_CALL(mock_delegate_, ListenForConnectionMessages(_, _, _, _)).Times(0); |
| 728 | 657 |
| 729 content::PresentationSessionInfo session(presentation_url1_, kPresentationId); | 658 PresentationSessionInfo session(presentation_url1_, kPresentationId); |
| 730 service_impl.ListenForConnectionMessages(session); | 659 service_impl.ListenForConnectionMessages(session); |
| 731 } | 660 } |
| 732 | 661 |
| 733 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { | 662 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { |
| 734 EXPECT_CALL(mock_delegate_, StartSession(_, _, presentation_urls_, _, _)) | 663 EXPECT_CALL(mock_delegate_, StartSession(_, _, presentation_urls_, _, _)) |
| 735 .Times(1); | 664 .Times(1); |
| 736 service_ptr_->StartSession(presentation_urls_, base::Bind(&DoNothing)); | 665 service_ptr_->StartSession(presentation_urls_, base::Bind(&DoNothing)); |
| 737 | 666 |
| 738 // This request should fail immediately, since there is already a StartSession | 667 // This request should fail immediately, since there is already a StartSession |
| 739 // in progress. | 668 // in progress. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 770 mock_delegate_.set_screen_availability_listening_supported(false); | 699 mock_delegate_.set_screen_availability_listening_supported(false); |
| 771 base::RunLoop run_loop; | 700 base::RunLoop run_loop; |
| 772 EXPECT_CALL(mock_client_, | 701 EXPECT_CALL(mock_client_, |
| 773 OnScreenAvailabilityNotSupported(presentation_url1_)) | 702 OnScreenAvailabilityNotSupported(presentation_url1_)) |
| 774 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 703 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 775 ListenForScreenAvailabilityAndWait(presentation_url1_, false); | 704 ListenForScreenAvailabilityAndWait(presentation_url1_, false); |
| 776 run_loop.Run(); | 705 run_loop.Run(); |
| 777 } | 706 } |
| 778 | 707 |
| 779 } // namespace content | 708 } // namespace content |
| OLD | NEW |