| 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 "chrome/browser/media/router/presentation_service_delegate_impl.h" | 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/media/router/media_source.h" | 8 #include "chrome/browser/media/router/media_source.h" |
| 9 #include "chrome/browser/media/router/media_source_helper.h" | 9 #include "chrome/browser/media/router/media_source_helper.h" |
| 10 #include "chrome/browser/media/router/mock_media_router.h" | 10 #include "chrome/browser/media/router/mock_media_router.h" |
| 11 #include "chrome/browser/media/router/mock_screen_availability_listener.h" | 11 #include "chrome/browser/media/router/mock_screen_availability_listener.h" |
| 12 #include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| 13 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h" |
| 12 #include "chrome/browser/media/router/route_request_result.h" | 14 #include "chrome/browser/media/router/route_request_result.h" |
| 13 #include "chrome/browser/media/router/test_helper.h" | 15 #include "chrome/browser/media/router/test_helper.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/prefs/scoped_user_pref_update.h" | 20 #include "components/prefs/scoped_user_pref_update.h" |
| 19 #include "content/public/browser/presentation_screen_availability_listener.h" | 21 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 20 #include "content/public/browser/presentation_session.h" | 22 #include "content/public/browser/presentation_session.h" |
| 21 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/test/web_contents_tester.h" | 25 #include "content/public/test/web_contents_tester.h" |
| 26 #include "mojo/public/cpp/bindings/binding.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "url/origin.h" | 28 #include "url/origin.h" |
| 26 | 29 |
| 27 using ::testing::_; | 30 using ::testing::_; |
| 28 using ::testing::Mock; | 31 using ::testing::Mock; |
| 29 using ::testing::Return; | 32 using ::testing::Return; |
| 30 using ::testing::SaveArg; | 33 using ::testing::SaveArg; |
| 31 using ::testing::StrictMock; | 34 using ::testing::StrictMock; |
| 32 | 35 |
| 33 namespace { | 36 namespace { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 59 }; | 62 }; |
| 60 | 63 |
| 61 class MockCreatePresentationConnnectionCallbacks { | 64 class MockCreatePresentationConnnectionCallbacks { |
| 62 public: | 65 public: |
| 63 MOCK_METHOD1(OnCreateConnectionSuccess, | 66 MOCK_METHOD1(OnCreateConnectionSuccess, |
| 64 void(const content::PresentationSessionInfo& connection)); | 67 void(const content::PresentationSessionInfo& connection)); |
| 65 MOCK_METHOD1(OnCreateConnectionError, | 68 MOCK_METHOD1(OnCreateConnectionError, |
| 66 void(const content::PresentationError& error)); | 69 void(const content::PresentationError& error)); |
| 67 }; | 70 }; |
| 68 | 71 |
| 72 class MockOffscreenPresentationManager : public OffscreenPresentationManager { |
| 73 public: |
| 74 void RegisterOffscreenPresentationController( |
| 75 const std::string& presentation_id, |
| 76 const GURL& presentation_url, |
| 77 const RenderFrameHostId& render_frame_id, |
| 78 content::PresentationConnectionPtr controller, |
| 79 content::PresentationConnectionRequest) override { |
| 80 RegisterOffscreenPresentationController(presentation_id, presentation_url, |
| 81 render_frame_id); |
| 82 } |
| 83 |
| 84 MOCK_METHOD3(RegisterOffscreenPresentationController, |
| 85 void(const std::string& presentation_id, |
| 86 const GURL& presentation_url, |
| 87 const RenderFrameHostId& render_frame_id)); |
| 88 MOCK_METHOD2(UnregisterOffscreenPresentationController, |
| 89 void(const std::string& presentation_id, |
| 90 const RenderFrameHostId& render_frame_id)); |
| 91 MOCK_METHOD3(OnOffscreenPresentationReceiverCreated, |
| 92 void(const std::string& presentation_id, |
| 93 const GURL& presentation_url, |
| 94 const content::ReceiverConnectionAvailableCallback& |
| 95 receiver_callback)); |
| 96 MOCK_METHOD1(OnOffscreenPresentationReceiverTerminated, |
| 97 void(const std::string& presentation_id)); |
| 98 }; |
| 99 |
| 100 std::unique_ptr<KeyedService> BuildMockOffscreenPresentationManager( |
| 101 content::BrowserContext* context) { |
| 102 return base::MakeUnique<MockOffscreenPresentationManager>(); |
| 103 } |
| 104 |
| 69 class PresentationServiceDelegateImplTest | 105 class PresentationServiceDelegateImplTest |
| 70 : public ChromeRenderViewHostTestHarness { | 106 : public ChromeRenderViewHostTestHarness { |
| 71 public: | 107 public: |
| 72 PresentationServiceDelegateImplTest() | 108 PresentationServiceDelegateImplTest() |
| 73 : delegate_impl_(nullptr), | 109 : delegate_impl_(nullptr), |
| 74 presentation_url1_(kPresentationUrl1), | 110 presentation_url1_(kPresentationUrl1), |
| 75 presentation_url2_(kPresentationUrl2), | 111 presentation_url2_(kPresentationUrl2), |
| 76 source1_(MediaSourceForPresentationUrl(presentation_url1_)), | 112 source1_(MediaSourceForPresentationUrl(presentation_url1_)), |
| 77 source2_(MediaSourceForPresentationUrl(presentation_url2_)), | 113 source2_(MediaSourceForPresentationUrl(presentation_url2_)), |
| 78 listener1_(presentation_url1_), | 114 listener1_(presentation_url1_), |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 manager.reset(); | 478 manager.reset(); |
| 443 } | 479 } |
| 444 | 480 |
| 445 TEST_F(PresentationServiceDelegateImplTest, SinksObserverCantRegister) { | 481 TEST_F(PresentationServiceDelegateImplTest, SinksObserverCantRegister) { |
| 446 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).WillOnce(Return(false)); | 482 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).WillOnce(Return(false)); |
| 447 EXPECT_CALL(listener1_, OnScreenAvailabilityNotSupported()); | 483 EXPECT_CALL(listener1_, OnScreenAvailabilityNotSupported()); |
| 448 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( | 484 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( |
| 449 main_frame_process_id_, main_frame_routing_id_, &listener1_)); | 485 main_frame_process_id_, main_frame_routing_id_, &listener1_)); |
| 450 } | 486 } |
| 451 | 487 |
| 488 TEST_F(PresentationServiceDelegateImplTest, ConnectToOffscreenPresentation) { |
| 489 content::RenderFrameHost* main_frame = GetWebContents()->GetMainFrame(); |
| 490 ASSERT_TRUE(main_frame); |
| 491 int render_process_id = main_frame->GetProcess()->GetID(); |
| 492 int render_frame_id = main_frame->GetRoutingID(); |
| 493 std::string presentation_id = "presentation_id"; |
| 494 GURL presentation_url = GURL("http://www.example.com/presentation.html"); |
| 495 content::PresentationSessionInfo session_info(presentation_url, |
| 496 presentation_id); |
| 497 |
| 498 OffscreenPresentationManagerFactory::GetInstanceForTest()->SetTestingFactory( |
| 499 profile(), &BuildMockOffscreenPresentationManager); |
| 500 MockOffscreenPresentationManager* mock_offscreen_manager = |
| 501 static_cast<MockOffscreenPresentationManager*>( |
| 502 OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext( |
| 503 profile())); |
| 504 EXPECT_CALL(*mock_offscreen_manager, |
| 505 RegisterOffscreenPresentationController( |
| 506 presentation_id, presentation_url, |
| 507 RenderFrameHostId(render_process_id, render_frame_id))); |
| 508 |
| 509 content::PresentationConnectionPtr connection_ptr; |
| 510 content::PresentationConnectionRequest connection_request; |
| 511 delegate_impl_->ConnectToOffscreenPresentation( |
| 512 render_process_id, render_frame_id, session_info, |
| 513 std::move(connection_ptr), std::move(connection_request)); |
| 514 } |
| 515 |
| 452 #if !defined(OS_ANDROID) | 516 #if !defined(OS_ANDROID) |
| 453 TEST_F(PresentationServiceDelegateImplTest, AutoJoinRequest) { | 517 TEST_F(PresentationServiceDelegateImplTest, AutoJoinRequest) { |
| 454 GURL frame_url(kFrameUrl); | 518 GURL frame_url(kFrameUrl); |
| 455 std::string origin(url::Origin(frame_url).Serialize()); | 519 std::string origin(url::Origin(frame_url).Serialize()); |
| 456 content::WebContentsTester::For(GetWebContents()) | 520 content::WebContentsTester::For(GetWebContents()) |
| 457 ->NavigateAndCommit(frame_url); | 521 ->NavigateAndCommit(frame_url); |
| 458 | 522 |
| 459 MockCreatePresentationConnnectionCallbacks mock_create_connection_callbacks; | 523 MockCreatePresentationConnnectionCallbacks mock_create_connection_callbacks; |
| 460 const std::string kPresentationId("auto-join"); | 524 const std::string kPresentationId("auto-join"); |
| 461 ASSERT_TRUE(IsAutoJoinPresentationId(kPresentationId)); | 525 ASSERT_TRUE(IsAutoJoinPresentationId(kPresentationId)); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 base::Bind(&MockCreatePresentationConnnectionCallbacks:: | 616 base::Bind(&MockCreatePresentationConnnectionCallbacks:: |
| 553 OnCreateConnectionSuccess, | 617 OnCreateConnectionSuccess, |
| 554 base::Unretained(&mock_create_connection_callbacks)), | 618 base::Unretained(&mock_create_connection_callbacks)), |
| 555 base::Bind( | 619 base::Bind( |
| 556 &MockCreatePresentationConnnectionCallbacks::OnCreateConnectionError, | 620 &MockCreatePresentationConnnectionCallbacks::OnCreateConnectionError, |
| 557 base::Unretained(&mock_create_connection_callbacks))); | 621 base::Unretained(&mock_create_connection_callbacks))); |
| 558 } | 622 } |
| 559 #endif // !defined(OS_ANDROID) | 623 #endif // !defined(OS_ANDROID) |
| 560 | 624 |
| 561 } // namespace media_router | 625 } // namespace media_router |
| OLD | NEW |