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 <memory> | 10 #include <memory> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 const char kPresentationId[] = "presentationId"; | 53 const char kPresentationId[] = "presentationId"; |
54 const char kPresentationUrl1[] = "http://foo.com/index.html"; | 54 const char kPresentationUrl1[] = "http://foo.com/index.html"; |
55 const char kPresentationUrl2[] = "http://example.com/index.html"; | 55 const char kPresentationUrl2[] = "http://example.com/index.html"; |
56 const char kPresentationUrl3[] = "http://example.net/index.html"; | 56 const char kPresentationUrl3[] = "http://example.net/index.html"; |
57 | 57 |
58 void DoNothing(blink::mojom::PresentationSessionInfoPtr info, | 58 void DoNothing(blink::mojom::PresentationSessionInfoPtr info, |
59 blink::mojom::PresentationErrorPtr error) {} | 59 blink::mojom::PresentationErrorPtr error) {} |
60 | 60 |
61 } // namespace | 61 } // namespace |
62 | 62 |
63 class MockPresentationServiceDelegate : public PresentationServiceDelegate { | 63 class MockPresentationServiceDelegate |
| 64 : public ControllerPresentationServiceDelegate { |
64 public: | 65 public: |
65 MOCK_METHOD3(AddObserver, | 66 MOCK_METHOD3(AddObserver, |
66 void(int render_process_id, | 67 void(int render_process_id, |
67 int render_frame_id, | 68 int render_frame_id, |
68 PresentationServiceDelegate::Observer* observer)); | 69 PresentationServiceDelegateBase::Observer* observer)); |
69 MOCK_METHOD2(RemoveObserver, | 70 MOCK_METHOD2(RemoveObserver, |
70 void(int render_process_id, int render_frame_id)); | 71 void(int render_process_id, int render_frame_id)); |
71 | 72 |
72 bool AddScreenAvailabilityListener( | 73 bool AddScreenAvailabilityListener( |
73 int render_process_id, | 74 int render_process_id, |
74 int routing_id, | 75 int routing_id, |
75 PresentationScreenAvailabilityListener* listener) override { | 76 PresentationScreenAvailabilityListener* listener) override { |
76 if (!screen_availability_listening_supported_) | 77 if (!screen_availability_listening_supported_) |
77 listener->OnScreenAvailabilityNotSupported(); | 78 listener->OnScreenAvailabilityNotSupported(); |
78 | 79 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 SendMessageRawPtr(render_process_id, render_frame_id, session, | 133 SendMessageRawPtr(render_process_id, render_frame_id, session, |
133 message_request.release(), send_message_cb); | 134 message_request.release(), send_message_cb); |
134 } | 135 } |
135 MOCK_METHOD4(ListenForConnectionStateChange, | 136 MOCK_METHOD4(ListenForConnectionStateChange, |
136 void(int render_process_id, | 137 void(int render_process_id, |
137 int render_frame_id, | 138 int render_frame_id, |
138 const content::PresentationSessionInfo& connection, | 139 const content::PresentationSessionInfo& connection, |
139 const content::PresentationConnectionStateChangedCallback& | 140 const content::PresentationConnectionStateChangedCallback& |
140 state_changed_cb)); | 141 state_changed_cb)); |
141 | 142 |
| 143 void ConnectToOffscreenPresentation( |
| 144 int render_process_id, |
| 145 int render_frame_id, |
| 146 const PresentationSessionInfo& session, |
| 147 PresentationConnectionPtr connection) override { |
| 148 RegisterOffscreenPresentationConnectionRaw( |
| 149 render_process_id, render_frame_id, session, connection.get()); |
| 150 } |
| 151 |
| 152 MOCK_METHOD4(RegisterOffscreenPresentationConnectionRaw, |
| 153 void(int render_process_id, |
| 154 int render_frame_id, |
| 155 const PresentationSessionInfo& session, |
| 156 blink::mojom::PresentationConnection* connection)); |
| 157 |
142 void set_screen_availability_listening_supported(bool value) { | 158 void set_screen_availability_listening_supported(bool value) { |
143 screen_availability_listening_supported_ = value; | 159 screen_availability_listening_supported_ = value; |
144 } | 160 } |
145 | 161 |
146 private: | 162 private: |
147 bool screen_availability_listening_supported_ = true; | 163 bool screen_availability_listening_supported_ = true; |
148 }; | 164 }; |
149 | 165 |
| 166 class MockReceiverPresentationServiceDelegate |
| 167 : public ReceiverPresentationServiceDelegate { |
| 168 public: |
| 169 MOCK_METHOD3(AddObserver, |
| 170 void(int render_process_id, |
| 171 int render_frame_id, |
| 172 PresentationServiceDelegateBase::Observer* observer)); |
| 173 MOCK_METHOD2(RemoveObserver, |
| 174 void(int render_process_id, int render_frame_id)); |
| 175 MOCK_METHOD2(Reset, void(int render_process_id, int routing_id)); |
| 176 MOCK_METHOD1(RegisterReceiverConnectionAvailableCallback, |
| 177 void(const content::ReceiverConnectionAvailableCallback&)); |
| 178 }; |
| 179 |
| 180 class MockPresentationConnection : public blink::mojom::PresentationConnection { |
| 181 public: |
| 182 void SetTargetConnection( |
| 183 blink::mojom::PresentationConnectionPtr connection) override { |
| 184 SetTargetConnection(*connection); |
| 185 } |
| 186 MOCK_METHOD1(SetTargetConnection, |
| 187 void(blink::mojom::PresentationConnection& connection)); |
| 188 |
| 189 void OnMessage(blink::mojom::SessionMessagePtr message) override { |
| 190 OnConnectionMessageReceived(*message); |
| 191 } |
| 192 MOCK_METHOD1(OnConnectionMessageReceived, |
| 193 void(const blink::mojom::SessionMessage& message)); |
| 194 }; |
| 195 |
150 class MockPresentationServiceClient | 196 class MockPresentationServiceClient |
151 : public blink::mojom::PresentationServiceClient { | 197 : public blink::mojom::PresentationServiceClient { |
152 public: | 198 public: |
153 MOCK_METHOD2(OnScreenAvailabilityUpdated, | 199 MOCK_METHOD2(OnScreenAvailabilityUpdated, |
154 void(const GURL& url, bool available)); | 200 void(const GURL& url, bool available)); |
155 void OnConnectionStateChanged( | 201 void OnConnectionStateChanged( |
156 blink::mojom::PresentationSessionInfoPtr connection, | 202 blink::mojom::PresentationSessionInfoPtr connection, |
157 blink::mojom::PresentationConnectionState new_state) override { | 203 blink::mojom::PresentationConnectionState new_state) override { |
158 OnConnectionStateChanged(*connection, new_state); | 204 OnConnectionStateChanged(*connection, new_state); |
159 } | 205 } |
(...skipping 23 matching lines...) Expand all Loading... |
183 MOCK_METHOD0(MessagesReceived, void()); | 229 MOCK_METHOD0(MessagesReceived, void()); |
184 | 230 |
185 void OnDefaultSessionStarted( | 231 void OnDefaultSessionStarted( |
186 blink::mojom::PresentationSessionInfoPtr session_info) override { | 232 blink::mojom::PresentationSessionInfoPtr session_info) override { |
187 OnDefaultSessionStarted(*session_info); | 233 OnDefaultSessionStarted(*session_info); |
188 } | 234 } |
189 MOCK_METHOD1(OnDefaultSessionStarted, | 235 MOCK_METHOD1(OnDefaultSessionStarted, |
190 void(const blink::mojom::PresentationSessionInfo& session_info)); | 236 void(const blink::mojom::PresentationSessionInfo& session_info)); |
191 | 237 |
192 void OnReceiverConnectionAvailable( | 238 void OnReceiverConnectionAvailable( |
193 blink::mojom::PresentationSessionInfoPtr session_info) override { | 239 blink::mojom::PresentationSessionInfoPtr session_info, |
| 240 blink::mojom::PresentationConnectionPtr connection) override { |
194 OnReceiverConnectionAvailable(*session_info); | 241 OnReceiverConnectionAvailable(*session_info); |
195 } | 242 } |
196 MOCK_METHOD1(OnReceiverConnectionAvailable, | 243 MOCK_METHOD1(OnReceiverConnectionAvailable, |
197 void(const blink::mojom::PresentationSessionInfo& session_info)); | 244 void(const blink::mojom::PresentationSessionInfo& session_info)); |
198 | 245 |
199 std::vector<blink::mojom::SessionMessagePtr> messages_received_; | 246 std::vector<blink::mojom::SessionMessagePtr> messages_received_; |
200 }; | 247 }; |
201 | 248 |
202 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 249 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
203 public: | 250 public: |
204 PresentationServiceImplTest() | 251 PresentationServiceImplTest() |
205 : presentation_url1_(GURL(kPresentationUrl1)), | 252 : presentation_url1_(GURL(kPresentationUrl1)), |
206 presentation_url2_(GURL(kPresentationUrl2)), | 253 presentation_url2_(GURL(kPresentationUrl2)), |
207 presentation_url3_(GURL(kPresentationUrl3)) {} | 254 presentation_url3_(GURL(kPresentationUrl3)) {} |
208 | 255 |
209 void SetUp() override { | 256 void SetUp() override { |
210 RenderViewHostImplTestHarness::SetUp(); | 257 RenderViewHostImplTestHarness::SetUp(); |
211 | 258 |
212 auto request = mojo::GetProxy(&service_ptr_); | 259 auto request = mojo::GetProxy(&service_ptr_); |
213 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); | 260 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); |
214 TestRenderFrameHost* render_frame_host = contents()->GetMainFrame(); | 261 TestRenderFrameHost* render_frame_host = contents()->GetMainFrame(); |
215 render_frame_host->InitializeRenderFrameIfNeeded(); | 262 render_frame_host->InitializeRenderFrameIfNeeded(); |
216 service_impl_.reset(new PresentationServiceImpl( | 263 service_impl_.reset(new PresentationServiceImpl( |
217 render_frame_host, contents(), &mock_delegate_)); | 264 render_frame_host, contents(), &mock_delegate_, nullptr)); |
218 service_impl_->Bind(std::move(request)); | 265 service_impl_->Bind(std::move(request)); |
219 | 266 |
220 blink::mojom::PresentationServiceClientPtr client_ptr; | 267 blink::mojom::PresentationServiceClientPtr client_ptr; |
221 client_binding_.reset( | 268 client_binding_.reset( |
222 new mojo::Binding<blink::mojom::PresentationServiceClient>( | 269 new mojo::Binding<blink::mojom::PresentationServiceClient>( |
223 &mock_client_, mojo::GetProxy(&client_ptr))); | 270 &mock_client_, mojo::GetProxy(&client_ptr))); |
224 service_impl_->SetClient(std::move(client_ptr)); | 271 service_impl_->SetClient(std::move(client_ptr)); |
225 | 272 |
226 presentation_urls_.push_back(presentation_url1_); | 273 presentation_urls_.push_back(presentation_url1_); |
227 presentation_urls_.push_back(presentation_url2_); | 274 presentation_urls_.push_back(presentation_url2_); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 base::RunLoop run_loop; | 414 base::RunLoop run_loop; |
368 EXPECT_CALL(mock_client_, MessagesReceived()) | 415 EXPECT_CALL(mock_client_, MessagesReceived()) |
369 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 416 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
370 message_cb.Run(std::move(messages), pass_ownership); | 417 message_cb.Run(std::move(messages), pass_ownership); |
371 run_loop.Run(); | 418 run_loop.Run(); |
372 } | 419 } |
373 ExpectSessionMessages(expected_msgs, mock_client_.messages_received_); | 420 ExpectSessionMessages(expected_msgs, mock_client_.messages_received_); |
374 } | 421 } |
375 | 422 |
376 MockPresentationServiceDelegate mock_delegate_; | 423 MockPresentationServiceDelegate mock_delegate_; |
| 424 MockReceiverPresentationServiceDelegate mock_receiver_delegate_; |
377 | 425 |
378 std::unique_ptr<PresentationServiceImpl> service_impl_; | 426 std::unique_ptr<PresentationServiceImpl> service_impl_; |
379 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_; | 427 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_; |
380 | 428 |
381 MockPresentationServiceClient mock_client_; | 429 MockPresentationServiceClient mock_client_; |
382 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> | 430 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> |
383 client_binding_; | 431 client_binding_; |
384 | 432 |
385 base::Closure run_loop_quit_closure_; | 433 base::Closure run_loop_quit_closure_; |
386 | 434 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 std::vector<uint8_t> binary_data(3, '\1'); | 702 std::vector<uint8_t> binary_data(3, '\1'); |
655 RunListenForSessionMessages(text_msg, binary_data, false); | 703 RunListenForSessionMessages(text_msg, binary_data, false); |
656 } | 704 } |
657 | 705 |
658 TEST_F(PresentationServiceImplTest, ListenForSessionMessagesWithEmptyMsg) { | 706 TEST_F(PresentationServiceImplTest, ListenForSessionMessagesWithEmptyMsg) { |
659 std::string text_msg(""); | 707 std::string text_msg(""); |
660 std::vector<uint8_t> binary_data; | 708 std::vector<uint8_t> binary_data; |
661 RunListenForSessionMessages(text_msg, binary_data, false); | 709 RunListenForSessionMessages(text_msg, binary_data, false); |
662 } | 710 } |
663 | 711 |
| 712 TEST_F(PresentationServiceImplTest, SetPresentationConnection) { |
| 713 blink::mojom::PresentationSessionInfoPtr session( |
| 714 blink::mojom::PresentationSessionInfo::New()); |
| 715 session->url = GURL(kPresentationUrl1); |
| 716 session->id = kPresentationId; |
| 717 |
| 718 blink::mojom::PresentationConnectionPtr connection; |
| 719 |
| 720 MockPresentationConnection mock_presentation_connection; |
| 721 mojo::Binding<blink::mojom::PresentationConnection> binding( |
| 722 &mock_presentation_connection, mojo::GetProxy(&connection)); |
| 723 |
| 724 EXPECT_CALL(mock_delegate_, |
| 725 RegisterOffscreenPresentationConnectionRaw(_, _, _, _)) |
| 726 .Times(1); |
| 727 |
| 728 service_impl_->SetPresentationConnection(std::move(session), |
| 729 std::move(connection)); |
| 730 } |
| 731 |
| 732 TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) { |
| 733 MockReceiverPresentationServiceDelegate mock_receiver_delegate; |
| 734 |
| 735 PresentationServiceImpl service_impl(contents()->GetMainFrame(), contents(), |
| 736 &mock_delegate_, |
| 737 &mock_receiver_delegate); |
| 738 |
| 739 EXPECT_CALL(mock_receiver_delegate, |
| 740 RegisterReceiverConnectionAvailableCallback(_)); |
| 741 |
| 742 blink::mojom::PresentationServiceClientPtr client_ptr; |
| 743 client_binding_.reset( |
| 744 new mojo::Binding<blink::mojom::PresentationServiceClient>( |
| 745 &mock_client_, mojo::GetProxy(&client_ptr))); |
| 746 service_impl.SetClient(std::move(client_ptr)); |
| 747 |
| 748 // NO-OP for ControllerPresentationServiceDelegate API functions |
| 749 EXPECT_CALL(mock_delegate_, ListenForSessionMessages(_, _, _, _)).Times(0); |
| 750 |
| 751 blink::mojom::PresentationSessionInfoPtr session( |
| 752 blink::mojom::PresentationSessionInfo::New()); |
| 753 session->url = GURL(kPresentationUrl1); |
| 754 session->id = kPresentationId; |
| 755 |
| 756 service_impl.ListenForSessionMessages(std::move(session)); |
| 757 } |
| 758 |
664 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { | 759 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { |
665 EXPECT_CALL(mock_delegate_, StartSession(_, _, presentation_urls_, _, _)) | 760 EXPECT_CALL(mock_delegate_, StartSession(_, _, presentation_urls_, _, _)) |
666 .Times(1); | 761 .Times(1); |
667 service_ptr_->StartSession(presentation_urls_, base::Bind(&DoNothing)); | 762 service_ptr_->StartSession(presentation_urls_, base::Bind(&DoNothing)); |
668 | 763 |
669 // This request should fail immediately, since there is already a StartSession | 764 // This request should fail immediately, since there is already a StartSession |
670 // in progress. | 765 // in progress. |
671 service_ptr_->StartSession( | 766 service_ptr_->StartSession( |
672 presentation_urls_, | 767 presentation_urls_, |
673 base::Bind(&PresentationServiceImplTest::ExpectNewSessionCallbackError, | 768 base::Bind(&PresentationServiceImplTest::ExpectNewSessionCallbackError, |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 mock_delegate_.set_screen_availability_listening_supported(false); | 947 mock_delegate_.set_screen_availability_listening_supported(false); |
853 base::RunLoop run_loop; | 948 base::RunLoop run_loop; |
854 EXPECT_CALL(mock_client_, | 949 EXPECT_CALL(mock_client_, |
855 OnScreenAvailabilityNotSupported(presentation_url1_)) | 950 OnScreenAvailabilityNotSupported(presentation_url1_)) |
856 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 951 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
857 ListenForScreenAvailabilityAndWait(presentation_url1_, false); | 952 ListenForScreenAvailabilityAndWait(presentation_url1_, false); |
858 run_loop.Run(); | 953 run_loop.Run(); |
859 } | 954 } |
860 | 955 |
861 } // namespace content | 956 } // namespace content |
OLD | NEW |