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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 namespace { | 34 namespace { |
35 | 35 |
36 // Matches mojo structs. | 36 // Matches mojo structs. |
37 MATCHER_P(Equals, expected, "") { | 37 MATCHER_P(Equals, expected, "") { |
38 return expected.Equals(arg); | 38 return expected.Equals(arg); |
39 } | 39 } |
40 | 40 |
41 const char *const kPresentationId = "presentationId"; | 41 const char *const kPresentationId = "presentationId"; |
42 const char *const kPresentationUrl = "http://foo.com/index.html"; | 42 const char *const kPresentationUrl = "http://foo.com/index.html"; |
43 | 43 |
44 bool ArePresentationSessionsEqual( | |
45 const presentation::PresentationSessionInfo& expected, | |
46 const presentation::PresentationSessionInfo& actual) { | |
47 return expected.url == actual.url && expected.id == actual.id; | |
48 } | |
49 | |
50 bool ArePresentationSessionMessagesEqual( | 44 bool ArePresentationSessionMessagesEqual( |
51 const presentation::SessionMessage* expected, | 45 const presentation::SessionMessage* expected, |
52 const presentation::SessionMessage* actual) { | 46 const presentation::SessionMessage* actual) { |
53 return expected->type == actual->type && | 47 return expected->type == actual->type && |
54 expected->message == actual->message && | 48 expected->message == actual->message && |
55 expected->data.Equals(actual->data); | 49 expected->data.Equals(actual->data); |
56 } | 50 } |
57 | 51 |
58 void DoNothing( | 52 void DoNothing( |
59 presentation::PresentationSessionInfoPtr info, | 53 presentation::PresentationSessionInfoPtr info, |
(...skipping 24 matching lines...) Expand all Loading... |
84 | 78 |
85 MOCK_METHOD3(RemoveScreenAvailabilityListener, | 79 MOCK_METHOD3(RemoveScreenAvailabilityListener, |
86 void( | 80 void( |
87 int render_process_id, | 81 int render_process_id, |
88 int routing_id, | 82 int routing_id, |
89 PresentationScreenAvailabilityListener* listener)); | 83 PresentationScreenAvailabilityListener* listener)); |
90 MOCK_METHOD2(Reset, | 84 MOCK_METHOD2(Reset, |
91 void( | 85 void( |
92 int render_process_id, | 86 int render_process_id, |
93 int routing_id)); | 87 int routing_id)); |
94 MOCK_METHOD3(SetDefaultPresentationUrl, | 88 MOCK_METHOD4(SetDefaultPresentationUrl, |
95 void( | 89 void(int render_process_id, |
96 int render_process_id, | 90 int routing_id, |
97 int routing_id, | 91 const std::string& default_presentation_url, |
98 const std::string& default_presentation_url)); | 92 const PresentationSessionStartedCallback& callback)); |
99 MOCK_METHOD5(StartSession, | 93 MOCK_METHOD5(StartSession, |
100 void( | 94 void(int render_process_id, |
101 int render_process_id, | 95 int render_frame_id, |
102 int render_frame_id, | 96 const std::string& presentation_url, |
103 const std::string& presentation_url, | 97 const PresentationSessionStartedCallback& success_cb, |
104 const PresentationSessionSuccessCallback& success_cb, | 98 const PresentationSessionErrorCallback& error_cb)); |
105 const PresentationSessionErrorCallback& error_cb)); | |
106 MOCK_METHOD6(JoinSession, | 99 MOCK_METHOD6(JoinSession, |
107 void( | 100 void(int render_process_id, |
108 int render_process_id, | 101 int render_frame_id, |
109 int render_frame_id, | 102 const std::string& presentation_url, |
110 const std::string& presentation_url, | 103 const std::string& presentation_id, |
111 const std::string& presentation_id, | 104 const PresentationSessionStartedCallback& success_cb, |
112 const PresentationSessionSuccessCallback& success_cb, | 105 const PresentationSessionErrorCallback& error_cb)); |
113 const PresentationSessionErrorCallback& error_cb)); | |
114 MOCK_METHOD3(CloseSession, | 106 MOCK_METHOD3(CloseSession, |
115 void(int render_process_id, | 107 void(int render_process_id, |
116 int render_frame_id, | 108 int render_frame_id, |
117 const std::string& presentation_id)); | 109 const std::string& presentation_id)); |
118 MOCK_METHOD4(ListenForSessionMessages, | 110 MOCK_METHOD4(ListenForSessionMessages, |
119 void(int render_process_id, | 111 void(int render_process_id, |
120 int render_frame_id, | 112 int render_frame_id, |
121 const content::PresentationSessionInfo& session, | 113 const content::PresentationSessionInfo& session, |
122 const PresentationSessionMessageCallback& message_cb)); | 114 const PresentationSessionMessageCallback& message_cb)); |
123 MOCK_METHOD5(SendMessageRawPtr, | 115 MOCK_METHOD5(SendMessageRawPtr, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 MOCK_METHOD1(OnScreenAvailabilityNotSupported, void(const mojo::String& url)); | 157 MOCK_METHOD1(OnScreenAvailabilityNotSupported, void(const mojo::String& url)); |
166 | 158 |
167 void OnSessionMessagesReceived( | 159 void OnSessionMessagesReceived( |
168 presentation::PresentationSessionInfoPtr session_info, | 160 presentation::PresentationSessionInfoPtr session_info, |
169 mojo::Array<presentation::SessionMessagePtr> messages) override { | 161 mojo::Array<presentation::SessionMessagePtr> messages) override { |
170 messages_received_ = messages.Pass(); | 162 messages_received_ = messages.Pass(); |
171 MessagesReceived(); | 163 MessagesReceived(); |
172 } | 164 } |
173 MOCK_METHOD0(MessagesReceived, void()); | 165 MOCK_METHOD0(MessagesReceived, void()); |
174 | 166 |
| 167 void OnDefaultSessionStarted( |
| 168 presentation::PresentationSessionInfoPtr session_info) override { |
| 169 OnDefaultSessionStarted(*session_info); |
| 170 } |
| 171 MOCK_METHOD1(OnDefaultSessionStarted, |
| 172 void(const presentation::PresentationSessionInfo& session_info)); |
| 173 |
175 mojo::Array<presentation::SessionMessagePtr> messages_received_; | 174 mojo::Array<presentation::SessionMessagePtr> messages_received_; |
176 }; | 175 }; |
177 | 176 |
178 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { | 177 class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
179 public: | 178 public: |
180 PresentationServiceImplTest() : default_session_started_count_(0) {} | 179 PresentationServiceImplTest() : default_session_started_count_(0) {} |
181 | 180 |
182 void SetUp() override { | 181 void SetUp() override { |
183 RenderViewHostImplTestHarness::SetUp(); | 182 RenderViewHostImplTestHarness::SetUp(); |
184 | 183 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 249 |
251 void ExpectReset() { | 250 void ExpectReset() { |
252 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); | 251 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); |
253 } | 252 } |
254 | 253 |
255 void ExpectCleanState() { | 254 void ExpectCleanState() { |
256 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); | 255 EXPECT_TRUE(service_impl_->default_presentation_url_.empty()); |
257 EXPECT_EQ( | 256 EXPECT_EQ( |
258 service_impl_->screen_availability_listeners_.find(kPresentationUrl), | 257 service_impl_->screen_availability_listeners_.find(kPresentationUrl), |
259 service_impl_->screen_availability_listeners_.end()); | 258 service_impl_->screen_availability_listeners_.end()); |
260 EXPECT_FALSE(service_impl_->default_session_start_context_.get()); | |
261 EXPECT_FALSE(service_impl_->on_session_messages_callback_.get()); | 259 EXPECT_FALSE(service_impl_->on_session_messages_callback_.get()); |
262 } | 260 } |
263 | 261 |
264 void ExpectNewSessionMojoCallbackSuccess( | 262 void ExpectNewSessionMojoCallbackSuccess( |
265 presentation::PresentationSessionInfoPtr info, | 263 presentation::PresentationSessionInfoPtr info, |
266 presentation::PresentationErrorPtr error) { | 264 presentation::PresentationErrorPtr error) { |
267 EXPECT_FALSE(info.is_null()); | 265 EXPECT_FALSE(info.is_null()); |
268 EXPECT_TRUE(error.is_null()); | 266 EXPECT_TRUE(error.is_null()); |
269 if (!run_loop_quit_closure_.is_null()) | 267 if (!run_loop_quit_closure_.is_null()) |
270 run_loop_quit_closure_.Run(); | 268 run_loop_quit_closure_.Run(); |
271 } | 269 } |
272 | 270 |
273 void ExpectNewSessionMojoCallbackError( | 271 void ExpectNewSessionMojoCallbackError( |
274 presentation::PresentationSessionInfoPtr info, | 272 presentation::PresentationSessionInfoPtr info, |
275 presentation::PresentationErrorPtr error) { | 273 presentation::PresentationErrorPtr error) { |
276 EXPECT_TRUE(info.is_null()); | 274 EXPECT_TRUE(info.is_null()); |
277 EXPECT_FALSE(error.is_null()); | 275 EXPECT_FALSE(error.is_null()); |
278 if (!run_loop_quit_closure_.is_null()) | 276 if (!run_loop_quit_closure_.is_null()) |
279 run_loop_quit_closure_.Run(); | 277 run_loop_quit_closure_.Run(); |
280 } | 278 } |
281 | 279 |
282 void ExpectDefaultSessionStarted( | |
283 const presentation::PresentationSessionInfo& expected_session, | |
284 presentation::PresentationSessionInfoPtr actual_session) { | |
285 ASSERT_TRUE(!actual_session.is_null()); | |
286 EXPECT_TRUE(ArePresentationSessionsEqual( | |
287 expected_session, *actual_session)); | |
288 ++default_session_started_count_; | |
289 if (!run_loop_quit_closure_.is_null()) | |
290 run_loop_quit_closure_.Run(); | |
291 } | |
292 | |
293 void ExpectDefaultSessionNull( | |
294 presentation::PresentationSessionInfoPtr actual_session) { | |
295 EXPECT_TRUE(actual_session.is_null()); | |
296 ++default_session_started_count_; | |
297 if (!run_loop_quit_closure_.is_null()) | |
298 run_loop_quit_closure_.Run(); | |
299 } | |
300 | |
301 void ExpectSessionMessages( | 280 void ExpectSessionMessages( |
302 const mojo::Array<presentation::SessionMessagePtr>& expected_msgs, | 281 const mojo::Array<presentation::SessionMessagePtr>& expected_msgs, |
303 const mojo::Array<presentation::SessionMessagePtr>& actual_msgs) { | 282 const mojo::Array<presentation::SessionMessagePtr>& actual_msgs) { |
304 EXPECT_EQ(expected_msgs.size(), actual_msgs.size()); | 283 EXPECT_EQ(expected_msgs.size(), actual_msgs.size()); |
305 for (size_t i = 0; i < actual_msgs.size(); ++i) { | 284 for (size_t i = 0; i < actual_msgs.size(); ++i) { |
306 EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_msgs[i].get(), | 285 EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_msgs[i].get(), |
307 actual_msgs[i].get())); | 286 actual_msgs[i].get())); |
308 } | 287 } |
309 } | 288 } |
310 | 289 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 | 422 |
444 TEST_F(PresentationServiceImplTest, DelegateFails) { | 423 TEST_F(PresentationServiceImplTest, DelegateFails) { |
445 ListenForScreenAvailabilityAndWait(kPresentationUrl, false); | 424 ListenForScreenAvailabilityAndWait(kPresentationUrl, false); |
446 ASSERT_EQ( | 425 ASSERT_EQ( |
447 service_impl_->screen_availability_listeners_.find(kPresentationUrl), | 426 service_impl_->screen_availability_listeners_.find(kPresentationUrl), |
448 service_impl_->screen_availability_listeners_.end()); | 427 service_impl_->screen_availability_listeners_.end()); |
449 } | 428 } |
450 | 429 |
451 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { | 430 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { |
452 std::string url1("http://fooUrl"); | 431 std::string url1("http://fooUrl"); |
453 EXPECT_CALL(mock_delegate_, | 432 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(url1), _)) |
454 SetDefaultPresentationUrl(_, _, Eq(url1))) | |
455 .Times(1); | 433 .Times(1); |
456 service_impl_->SetDefaultPresentationURL(url1); | 434 service_impl_->SetDefaultPresentationURL(url1); |
457 EXPECT_EQ(url1, service_impl_->default_presentation_url_); | 435 EXPECT_EQ(url1, service_impl_->default_presentation_url_); |
458 | 436 |
459 std::string url2("http://barUrl"); | 437 std::string url2("http://barUrl"); |
460 // Sets different DPU. | 438 // Sets different DPU. |
461 EXPECT_CALL(mock_delegate_, | 439 content::PresentationSessionStartedCallback callback; |
462 SetDefaultPresentationUrl(_, _, Eq(url2))) | 440 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrl(_, _, Eq(url2), _)) |
463 .Times(1); | 441 .WillOnce(SaveArg<3>(&callback)); |
464 service_impl_->SetDefaultPresentationURL(url2); | 442 service_impl_->SetDefaultPresentationURL(url2); |
465 EXPECT_EQ(url2, service_impl_->default_presentation_url_); | 443 EXPECT_EQ(url2, service_impl_->default_presentation_url_); |
| 444 |
| 445 presentation::PresentationSessionInfo session_info; |
| 446 session_info.url = url2; |
| 447 session_info.id = kPresentationId; |
| 448 EXPECT_CALL(mock_client_, OnDefaultSessionStarted(Equals(session_info))) |
| 449 .Times(1); |
| 450 callback.Run(content::PresentationSessionInfo(url2, kPresentationId)); |
466 } | 451 } |
467 | 452 |
468 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) { | 453 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrl) { |
469 EXPECT_CALL(mock_delegate_, | 454 EXPECT_CALL(mock_delegate_, |
470 SetDefaultPresentationUrl(_, _, Eq(kPresentationUrl))) | 455 SetDefaultPresentationUrl(_, _, Eq(kPresentationUrl), _)) |
471 .Times(1); | 456 .Times(1); |
472 service_impl_->SetDefaultPresentationURL(kPresentationUrl); | 457 service_impl_->SetDefaultPresentationURL(kPresentationUrl); |
473 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); | 458 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
474 EXPECT_EQ(kPresentationUrl, service_impl_->default_presentation_url_); | 459 EXPECT_EQ(kPresentationUrl, service_impl_->default_presentation_url_); |
475 | 460 |
476 // Same URL as before; no-ops. | 461 // Same URL as before; no-ops. |
477 service_impl_->SetDefaultPresentationURL(kPresentationUrl); | 462 service_impl_->SetDefaultPresentationURL(kPresentationUrl); |
478 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); | 463 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
479 EXPECT_EQ(kPresentationUrl, service_impl_->default_presentation_url_); | 464 EXPECT_EQ(kPresentationUrl, service_impl_->default_presentation_url_); |
480 } | 465 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 // This request should fail immediately, since there is already a StartSession | 570 // This request should fail immediately, since there is already a StartSession |
586 // in progress. | 571 // in progress. |
587 service_ptr_->StartSession( | 572 service_ptr_->StartSession( |
588 presentation_url2, | 573 presentation_url2, |
589 base::Bind( | 574 base::Bind( |
590 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError, | 575 &PresentationServiceImplTest::ExpectNewSessionMojoCallbackError, |
591 base::Unretained(this))); | 576 base::Unretained(this))); |
592 SaveQuitClosureAndRunLoop(); | 577 SaveQuitClosureAndRunLoop(); |
593 } | 578 } |
594 | 579 |
595 TEST_F(PresentationServiceImplTest, ListenForDefaultSessionStart) { | |
596 presentation::PresentationSessionInfo expected_session; | |
597 expected_session.url = kPresentationUrl; | |
598 expected_session.id = kPresentationId; | |
599 service_ptr_->ListenForDefaultSessionStart( | |
600 base::Bind(&PresentationServiceImplTest::ExpectDefaultSessionStarted, | |
601 base::Unretained(this), | |
602 expected_session)); | |
603 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); | |
604 service_impl_->OnDefaultPresentationStarted( | |
605 content::PresentationSessionInfo(kPresentationUrl, kPresentationId)); | |
606 SaveQuitClosureAndRunLoop(); | |
607 EXPECT_EQ(1, default_session_started_count_); | |
608 } | |
609 | |
610 TEST_F(PresentationServiceImplTest, ListenForDefaultSessionStartAfterSet) { | |
611 // Note that the callback will only pick up presentation_url2/id2 since | |
612 // ListenForDefaultSessionStart wasn't called yet when the DPU was still | |
613 // presentation_url1. | |
614 std::string presentation_url1("http://fooUrl1"); | |
615 std::string presentation_id1("presentationId1"); | |
616 std::string presentation_url2("http://fooUrl2"); | |
617 std::string presentation_id2("presentationId2"); | |
618 service_impl_->OnDefaultPresentationStarted( | |
619 content::PresentationSessionInfo(presentation_url1, presentation_id1)); | |
620 | |
621 presentation::PresentationSessionInfo expected_session; | |
622 expected_session.url = presentation_url2; | |
623 expected_session.id = presentation_id2; | |
624 service_ptr_->ListenForDefaultSessionStart( | |
625 base::Bind(&PresentationServiceImplTest::ExpectDefaultSessionStarted, | |
626 base::Unretained(this), | |
627 expected_session)); | |
628 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); | |
629 service_impl_->OnDefaultPresentationStarted( | |
630 content::PresentationSessionInfo(presentation_url2, presentation_id2)); | |
631 SaveQuitClosureAndRunLoop(); | |
632 EXPECT_EQ(1, default_session_started_count_); | |
633 } | |
634 | |
635 TEST_F(PresentationServiceImplTest, DefaultSessionStartReset) { | |
636 service_ptr_->ListenForDefaultSessionStart( | |
637 base::Bind(&PresentationServiceImplTest::ExpectDefaultSessionNull, | |
638 base::Unretained(this))); | |
639 RunLoopFor(TestTimeouts::tiny_timeout()); | |
640 | |
641 ExpectReset(); | |
642 service_impl_->Reset(); | |
643 ExpectCleanState(); | |
644 SaveQuitClosureAndRunLoop(); | |
645 EXPECT_EQ(1, default_session_started_count_); | |
646 } | |
647 | |
648 TEST_F(PresentationServiceImplTest, SendStringMessage) { | 580 TEST_F(PresentationServiceImplTest, SendStringMessage) { |
649 std::string message("Test presentation session message"); | 581 std::string message("Test presentation session message"); |
650 | 582 |
651 presentation::PresentationSessionInfoPtr session( | 583 presentation::PresentationSessionInfoPtr session( |
652 presentation::PresentationSessionInfo::New()); | 584 presentation::PresentationSessionInfo::New()); |
653 session->url = kPresentationUrl; | 585 session->url = kPresentationUrl; |
654 session->id = kPresentationId; | 586 session->id = kPresentationId; |
655 presentation::SessionMessagePtr message_request( | 587 presentation::SessionMessagePtr message_request( |
656 presentation::SessionMessage::New()); | 588 presentation::SessionMessage::New()); |
657 message_request->type = presentation::PresentationMessageType:: | 589 message_request->type = presentation::PresentationMessageType:: |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 | 778 |
847 TEST_F(PresentationServiceImplTest, ScreenAvailabilityNotSupported) { | 779 TEST_F(PresentationServiceImplTest, ScreenAvailabilityNotSupported) { |
848 mock_delegate_.set_screen_availability_listening_supported(false); | 780 mock_delegate_.set_screen_availability_listening_supported(false); |
849 EXPECT_CALL(mock_client_, | 781 EXPECT_CALL(mock_client_, |
850 OnScreenAvailabilityNotSupported(Eq(kPresentationUrl))); | 782 OnScreenAvailabilityNotSupported(Eq(kPresentationUrl))); |
851 | 783 |
852 ListenForScreenAvailabilityAndWait(kPresentationUrl, false); | 784 ListenForScreenAvailabilityAndWait(kPresentationUrl, false); |
853 } | 785 } |
854 | 786 |
855 } // namespace content | 787 } // namespace content |
OLD | NEW |