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 26 matching lines...) Expand all Loading... |
37 | 37 |
38 namespace content { | 38 namespace content { |
39 | 39 |
40 namespace { | 40 namespace { |
41 | 41 |
42 // Matches Mojo structs. | 42 // Matches Mojo structs. |
43 MATCHER_P(Equals, expected, "") { | 43 MATCHER_P(Equals, expected, "") { |
44 return expected.Equals(arg); | 44 return expected.Equals(arg); |
45 } | 45 } |
46 | 46 |
47 // Matches PresentationSessionInfo passed by reference. | 47 // Matches blink::mojom::PresentationSessionInfo passed by reference. |
48 MATCHER_P(SessionInfoEquals, expected, "") { | 48 MATCHER_P(SessionInfoEquals, expected, "") { |
49 blink::mojom::PresentationSessionInfo& expected_value = expected; | 49 blink::mojom::PresentationSessionInfo& expected_value = expected; |
50 return expected_value.Equals(arg); | 50 return expected_value.Equals(arg); |
51 } | 51 } |
52 | 52 |
| 53 // Matches content::PresentationSessionInfo passed by reference. |
| 54 MATCHER_P(ContentSessionInfoEquals, expected, "") { |
| 55 const content::PresentationSessionInfo& expected_value = expected; |
| 56 return expected_value.presentation_url == arg.presentation_url && |
| 57 expected_value.presentation_id == arg.presentation_id; |
| 58 } |
| 59 |
53 const char kPresentationId[] = "presentationId"; | 60 const char kPresentationId[] = "presentationId"; |
54 const char kPresentationUrl1[] = "http://foo.com/index.html"; | 61 const char kPresentationUrl1[] = "http://foo.com/index.html"; |
55 const char kPresentationUrl2[] = "http://example.com/index.html"; | 62 const char kPresentationUrl2[] = "http://example.com/index.html"; |
56 const char kPresentationUrl3[] = "http://example.net/index.html"; | 63 const char kPresentationUrl3[] = "http://example.net/index.html"; |
57 | 64 |
58 void DoNothing(blink::mojom::PresentationSessionInfoPtr info, | 65 void DoNothing(blink::mojom::PresentationSessionInfoPtr info, |
59 blink::mojom::PresentationErrorPtr error) {} | 66 blink::mojom::PresentationErrorPtr error) {} |
60 | 67 |
61 } // namespace | 68 } // namespace |
62 | 69 |
63 class MockPresentationServiceDelegate : public PresentationServiceDelegate { | 70 class MockPresentationServiceDelegate |
| 71 : public ControllerPresentationServiceDelegate { |
64 public: | 72 public: |
65 MOCK_METHOD3(AddObserver, | 73 MOCK_METHOD3(AddObserver, |
66 void(int render_process_id, | 74 void(int render_process_id, |
67 int render_frame_id, | 75 int render_frame_id, |
68 PresentationServiceDelegate::Observer* observer)); | 76 PresentationServiceDelegate::Observer* observer)); |
69 MOCK_METHOD2(RemoveObserver, | 77 MOCK_METHOD2(RemoveObserver, |
70 void(int render_process_id, int render_frame_id)); | 78 void(int render_process_id, int render_frame_id)); |
71 | 79 |
72 bool AddScreenAvailabilityListener( | 80 bool AddScreenAvailabilityListener( |
73 int render_process_id, | 81 int render_process_id, |
74 int routing_id, | 82 int routing_id, |
75 PresentationScreenAvailabilityListener* listener) override { | 83 PresentationScreenAvailabilityListener* listener) override { |
76 if (!screen_availability_listening_supported_) | 84 if (!screen_availability_listening_supported_) |
77 listener->OnScreenAvailabilityNotSupported(); | 85 listener->OnScreenAvailabilityNotSupported(); |
78 | 86 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 SendMessageRawPtr(render_process_id, render_frame_id, session, | 140 SendMessageRawPtr(render_process_id, render_frame_id, session, |
133 message_request.release(), send_message_cb); | 141 message_request.release(), send_message_cb); |
134 } | 142 } |
135 MOCK_METHOD4(ListenForConnectionStateChange, | 143 MOCK_METHOD4(ListenForConnectionStateChange, |
136 void(int render_process_id, | 144 void(int render_process_id, |
137 int render_frame_id, | 145 int render_frame_id, |
138 const content::PresentationSessionInfo& connection, | 146 const content::PresentationSessionInfo& connection, |
139 const content::PresentationConnectionStateChangedCallback& | 147 const content::PresentationConnectionStateChangedCallback& |
140 state_changed_cb)); | 148 state_changed_cb)); |
141 | 149 |
| 150 void ConnectToOffscreenPresentation( |
| 151 int render_process_id, |
| 152 int render_frame_id, |
| 153 const content::PresentationSessionInfo& session, |
| 154 PresentationConnectionPtr connection) override { |
| 155 RegisterOffscreenPresentationConnectionRaw( |
| 156 render_process_id, render_frame_id, session, connection.get()); |
| 157 } |
| 158 |
| 159 MOCK_METHOD4(RegisterOffscreenPresentationConnectionRaw, |
| 160 void(int render_process_id, |
| 161 int render_frame_id, |
| 162 const content::PresentationSessionInfo& session, |
| 163 blink::mojom::PresentationConnection* connection)); |
| 164 |
142 void set_screen_availability_listening_supported(bool value) { | 165 void set_screen_availability_listening_supported(bool value) { |
143 screen_availability_listening_supported_ = value; | 166 screen_availability_listening_supported_ = value; |
144 } | 167 } |
145 | 168 |
146 private: | 169 private: |
147 bool screen_availability_listening_supported_ = true; | 170 bool screen_availability_listening_supported_ = true; |
148 }; | 171 }; |
149 | 172 |
| 173 class MockReceiverPresentationServiceDelegate |
| 174 : public ReceiverPresentationServiceDelegate { |
| 175 public: |
| 176 MOCK_METHOD3(AddObserver, |
| 177 void(int render_process_id, |
| 178 int render_frame_id, |
| 179 PresentationServiceDelegate::Observer* observer)); |
| 180 MOCK_METHOD2(RemoveObserver, |
| 181 void(int render_process_id, int render_frame_id)); |
| 182 MOCK_METHOD2(Reset, void(int render_process_id, int routing_id)); |
| 183 MOCK_METHOD1(RegisterReceiverConnectionAvailableCallback, |
| 184 void(const content::ReceiverConnectionAvailableCallback&)); |
| 185 }; |
| 186 |
| 187 class MockPresentationConnection : public blink::mojom::PresentationConnection { |
| 188 public: |
| 189 void SetTargetConnection( |
| 190 blink::mojom::PresentationConnectionPtr connection) override { |
| 191 SetTargetConnection(*connection); |
| 192 } |
| 193 MOCK_METHOD1(SetTargetConnection, |
| 194 void(blink::mojom::PresentationConnection& connection)); |
| 195 |
| 196 void OnMessage(blink::mojom::SessionMessagePtr message) override { |
| 197 OnConnectionMessageReceived(*message); |
| 198 } |
| 199 MOCK_METHOD1(OnConnectionMessageReceived, |
| 200 void(const blink::mojom::SessionMessage& message)); |
| 201 }; |
| 202 |
150 class MockPresentationServiceClient | 203 class MockPresentationServiceClient |
151 : public blink::mojom::PresentationServiceClient { | 204 : public blink::mojom::PresentationServiceClient { |
152 public: | 205 public: |
153 MOCK_METHOD2(OnScreenAvailabilityUpdated, | 206 MOCK_METHOD2(OnScreenAvailabilityUpdated, |
154 void(const GURL& url, bool available)); | 207 void(const GURL& url, bool available)); |
155 void OnConnectionStateChanged( | 208 void OnConnectionStateChanged( |
156 blink::mojom::PresentationSessionInfoPtr connection, | 209 blink::mojom::PresentationSessionInfoPtr connection, |
157 blink::mojom::PresentationConnectionState new_state) override { | 210 blink::mojom::PresentationConnectionState new_state) override { |
158 OnConnectionStateChanged(*connection, new_state); | 211 OnConnectionStateChanged(*connection, new_state); |
159 } | 212 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 presentation_url3_(GURL(kPresentationUrl3)) {} | 260 presentation_url3_(GURL(kPresentationUrl3)) {} |
208 | 261 |
209 void SetUp() override { | 262 void SetUp() override { |
210 RenderViewHostImplTestHarness::SetUp(); | 263 RenderViewHostImplTestHarness::SetUp(); |
211 | 264 |
212 auto request = mojo::GetProxy(&service_ptr_); | 265 auto request = mojo::GetProxy(&service_ptr_); |
213 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); | 266 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); |
214 TestRenderFrameHost* render_frame_host = contents()->GetMainFrame(); | 267 TestRenderFrameHost* render_frame_host = contents()->GetMainFrame(); |
215 render_frame_host->InitializeRenderFrameIfNeeded(); | 268 render_frame_host->InitializeRenderFrameIfNeeded(); |
216 service_impl_.reset(new PresentationServiceImpl( | 269 service_impl_.reset(new PresentationServiceImpl( |
217 render_frame_host, contents(), &mock_delegate_)); | 270 render_frame_host, contents(), &mock_delegate_, nullptr)); |
218 service_impl_->Bind(std::move(request)); | 271 service_impl_->Bind(std::move(request)); |
219 | 272 |
220 blink::mojom::PresentationServiceClientPtr client_ptr; | 273 blink::mojom::PresentationServiceClientPtr client_ptr; |
221 client_binding_.reset( | 274 client_binding_.reset( |
222 new mojo::Binding<blink::mojom::PresentationServiceClient>( | 275 new mojo::Binding<blink::mojom::PresentationServiceClient>( |
223 &mock_client_, mojo::GetProxy(&client_ptr))); | 276 &mock_client_, mojo::GetProxy(&client_ptr))); |
224 service_impl_->SetClient(std::move(client_ptr)); | 277 service_impl_->SetClient(std::move(client_ptr)); |
225 | 278 |
226 presentation_urls_.push_back(presentation_url1_); | 279 presentation_urls_.push_back(presentation_url1_); |
227 presentation_urls_.push_back(presentation_url2_); | 280 presentation_urls_.push_back(presentation_url2_); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 base::RunLoop run_loop; | 420 base::RunLoop run_loop; |
368 EXPECT_CALL(mock_client_, MessagesReceived()) | 421 EXPECT_CALL(mock_client_, MessagesReceived()) |
369 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 422 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
370 message_cb.Run(std::move(messages), pass_ownership); | 423 message_cb.Run(std::move(messages), pass_ownership); |
371 run_loop.Run(); | 424 run_loop.Run(); |
372 } | 425 } |
373 ExpectSessionMessages(expected_msgs, mock_client_.messages_received_); | 426 ExpectSessionMessages(expected_msgs, mock_client_.messages_received_); |
374 } | 427 } |
375 | 428 |
376 MockPresentationServiceDelegate mock_delegate_; | 429 MockPresentationServiceDelegate mock_delegate_; |
| 430 MockReceiverPresentationServiceDelegate mock_receiver_delegate_; |
377 | 431 |
378 std::unique_ptr<PresentationServiceImpl> service_impl_; | 432 std::unique_ptr<PresentationServiceImpl> service_impl_; |
379 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_; | 433 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_; |
380 | 434 |
381 MockPresentationServiceClient mock_client_; | 435 MockPresentationServiceClient mock_client_; |
382 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> | 436 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> |
383 client_binding_; | 437 client_binding_; |
384 | 438 |
385 base::Closure run_loop_quit_closure_; | 439 base::Closure run_loop_quit_closure_; |
386 | 440 |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 std::vector<uint8_t> binary_data(3, '\1'); | 714 std::vector<uint8_t> binary_data(3, '\1'); |
661 RunListenForSessionMessages(text_msg, binary_data, false); | 715 RunListenForSessionMessages(text_msg, binary_data, false); |
662 } | 716 } |
663 | 717 |
664 TEST_F(PresentationServiceImplTest, ListenForSessionMessagesWithEmptyMsg) { | 718 TEST_F(PresentationServiceImplTest, ListenForSessionMessagesWithEmptyMsg) { |
665 std::string text_msg(""); | 719 std::string text_msg(""); |
666 std::vector<uint8_t> binary_data; | 720 std::vector<uint8_t> binary_data; |
667 RunListenForSessionMessages(text_msg, binary_data, false); | 721 RunListenForSessionMessages(text_msg, binary_data, false); |
668 } | 722 } |
669 | 723 |
| 724 TEST_F(PresentationServiceImplTest, SetPresentationConnection) { |
| 725 blink::mojom::PresentationSessionInfoPtr session( |
| 726 blink::mojom::PresentationSessionInfo::New()); |
| 727 session->url = presentation_url1_; |
| 728 session->id = kPresentationId; |
| 729 |
| 730 blink::mojom::PresentationConnectionPtr connection; |
| 731 MockPresentationConnection mock_presentation_connection; |
| 732 mojo::Binding<blink::mojom::PresentationConnection> connection_binding( |
| 733 &mock_presentation_connection, mojo::GetProxy(&connection)); |
| 734 |
| 735 content::PresentationSessionInfo expected(presentation_url1_, |
| 736 kPresentationId); |
| 737 EXPECT_CALL(mock_delegate_, |
| 738 RegisterOffscreenPresentationConnectionRaw( |
| 739 _, _, ContentSessionInfoEquals(ByRef(expected)), _)); |
| 740 |
| 741 service_impl_->SetPresentationConnection(std::move(session), |
| 742 std::move(connection)); |
| 743 } |
| 744 |
| 745 TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) { |
| 746 MockReceiverPresentationServiceDelegate mock_receiver_delegate; |
| 747 |
| 748 PresentationServiceImpl service_impl(contents()->GetMainFrame(), contents(), |
| 749 nullptr, &mock_receiver_delegate); |
| 750 |
| 751 ReceiverConnectionAvailableCallback callback; |
| 752 EXPECT_CALL(mock_receiver_delegate, |
| 753 RegisterReceiverConnectionAvailableCallback(_)) |
| 754 .WillOnce(SaveArg<0>(&callback)); |
| 755 |
| 756 blink::mojom::PresentationServiceClientPtr client_ptr; |
| 757 client_binding_.reset( |
| 758 new mojo::Binding<blink::mojom::PresentationServiceClient>( |
| 759 &mock_client_, mojo::GetProxy(&client_ptr))); |
| 760 service_impl.controller_delegate_ = nullptr; |
| 761 service_impl.SetClient(std::move(client_ptr)); |
| 762 EXPECT_FALSE(callback.is_null()); |
| 763 |
| 764 // NO-OP for ControllerPresentationServiceDelegate API functions |
| 765 EXPECT_CALL(mock_delegate_, ListenForSessionMessages(_, _, _, _)).Times(0); |
| 766 |
| 767 blink::mojom::PresentationSessionInfoPtr session( |
| 768 blink::mojom::PresentationSessionInfo::New()); |
| 769 session->url = GURL(kPresentationUrl1); |
| 770 session->id = kPresentationId; |
| 771 |
| 772 service_impl.ListenForSessionMessages(std::move(session)); |
| 773 } |
| 774 |
670 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { | 775 TEST_F(PresentationServiceImplTest, StartSessionInProgress) { |
671 EXPECT_CALL(mock_delegate_, StartSession(_, _, presentation_urls_, _, _)) | 776 EXPECT_CALL(mock_delegate_, StartSession(_, _, presentation_urls_, _, _)) |
672 .Times(1); | 777 .Times(1); |
673 service_ptr_->StartSession(presentation_urls_, base::Bind(&DoNothing)); | 778 service_ptr_->StartSession(presentation_urls_, base::Bind(&DoNothing)); |
674 | 779 |
675 // This request should fail immediately, since there is already a StartSession | 780 // This request should fail immediately, since there is already a StartSession |
676 // in progress. | 781 // in progress. |
677 service_ptr_->StartSession( | 782 service_ptr_->StartSession( |
678 presentation_urls_, | 783 presentation_urls_, |
679 base::Bind(&PresentationServiceImplTest::ExpectNewSessionCallbackError, | 784 base::Bind(&PresentationServiceImplTest::ExpectNewSessionCallbackError, |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 mock_delegate_.set_screen_availability_listening_supported(false); | 963 mock_delegate_.set_screen_availability_listening_supported(false); |
859 base::RunLoop run_loop; | 964 base::RunLoop run_loop; |
860 EXPECT_CALL(mock_client_, | 965 EXPECT_CALL(mock_client_, |
861 OnScreenAvailabilityNotSupported(presentation_url1_)) | 966 OnScreenAvailabilityNotSupported(presentation_url1_)) |
862 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 967 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
863 ListenForScreenAvailabilityAndWait(presentation_url1_, false); | 968 ListenForScreenAvailabilityAndWait(presentation_url1_, false); |
864 run_loop.Run(); | 969 run_loop.Run(); |
865 } | 970 } |
866 | 971 |
867 } // namespace content | 972 } // namespace content |
OLD | NEW |