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