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/route_request_result.h" | 12 #include "chrome/browser/media/router/route_request_result.h" |
13 #include "chrome/browser/media/router/test_helper.h" | 13 #include "chrome/browser/media/router/test_helper.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/prefs/scoped_user_pref_update.h" |
17 #include "content/public/browser/presentation_screen_availability_listener.h" | 19 #include "content/public/browser/presentation_screen_availability_listener.h" |
18 #include "content/public/browser/presentation_session.h" | 20 #include "content/public/browser/presentation_session.h" |
19 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
21 #include "content/public/test/web_contents_tester.h" | 23 #include "content/public/test/web_contents_tester.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "url/origin.h" |
23 | 26 |
24 using ::testing::_; | 27 using ::testing::_; |
25 using ::testing::Mock; | 28 using ::testing::Mock; |
26 using ::testing::Return; | 29 using ::testing::Return; |
27 using ::testing::SaveArg; | 30 using ::testing::SaveArg; |
28 using ::testing::StrictMock; | 31 using ::testing::StrictMock; |
29 | 32 |
30 namespace { | 33 namespace { |
31 | 34 |
32 const char kPresentationUrl1[] = "http://foo.fakeurl.com/"; | 35 const char kPresentationUrl1[] = "http://foo.fakeurl.com/"; |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 ASSERT_TRUE(main_frame); | 469 ASSERT_TRUE(main_frame); |
467 int render_process_id = main_frame->GetProcess()->GetID(); | 470 int render_process_id = main_frame->GetProcess()->GetID(); |
468 int render_frame_id = main_frame->GetRoutingID(); | 471 int render_frame_id = main_frame->GetRoutingID(); |
469 | 472 |
470 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).WillOnce(Return(false)); | 473 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).WillOnce(Return(false)); |
471 EXPECT_CALL(listener, OnScreenAvailabilityNotSupported()); | 474 EXPECT_CALL(listener, OnScreenAvailabilityNotSupported()); |
472 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( | 475 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( |
473 render_process_id, render_frame_id, &listener)); | 476 render_process_id, render_frame_id, &listener)); |
474 } | 477 } |
475 | 478 |
| 479 #if !defined(OS_ANDROID) |
| 480 TEST_F(PresentationServiceDelegateImplTest, AutoJoinRequest) { |
| 481 GURL frame_url(kFrameUrl); |
| 482 std::string origin(url::Origin(frame_url).Serialize()); |
| 483 content::WebContentsTester::For(GetWebContents()) |
| 484 ->NavigateAndCommit(frame_url); |
| 485 content::RenderFrameHost* main_frame = GetWebContents()->GetMainFrame(); |
| 486 ASSERT_TRUE(main_frame); |
| 487 int render_process_id = main_frame->GetProcess()->GetID(); |
| 488 int routing_id = main_frame->GetRoutingID(); |
| 489 |
| 490 MockCreatePresentationConnnectionCallbacks mock_create_connection_callbacks; |
| 491 const std::string kPresentationId("auto-join"); |
| 492 ASSERT_TRUE(IsAutoJoinPresentationId(kPresentationId)); |
| 493 |
| 494 // Set the user preference for |origin| to prefer tab mirroring. |
| 495 { |
| 496 ListPrefUpdate update(profile()->GetPrefs(), |
| 497 prefs::kMediaRouterTabMirroringSources); |
| 498 update->AppendIfNotPresent(base::MakeUnique<base::StringValue>(origin)); |
| 499 } |
| 500 |
| 501 // Auto-join requests should be rejected. |
| 502 EXPECT_CALL(mock_create_connection_callbacks, OnCreateConnectionError(_)); |
| 503 EXPECT_CALL(router_, JoinRoute(_, kPresentationId, _, _, _, _, _)).Times(0); |
| 504 delegate_impl_->JoinSession( |
| 505 render_process_id, routing_id, presentation_urls_, kPresentationId, |
| 506 base::Bind(&MockCreatePresentationConnnectionCallbacks:: |
| 507 OnCreateConnectionSuccess, |
| 508 base::Unretained(&mock_create_connection_callbacks)), |
| 509 base::Bind( |
| 510 &MockCreatePresentationConnnectionCallbacks::OnCreateConnectionError, |
| 511 base::Unretained(&mock_create_connection_callbacks))); |
| 512 |
| 513 // Remove the user preference for |origin|. |
| 514 { |
| 515 ListPrefUpdate update(profile()->GetPrefs(), |
| 516 prefs::kMediaRouterTabMirroringSources); |
| 517 update->Remove(base::StringValue(origin), nullptr); |
| 518 } |
| 519 |
| 520 // Auto-join requests should now go through. |
| 521 EXPECT_CALL(router_, JoinRoute(_, kPresentationId, _, _, _, _, _)).Times(1); |
| 522 delegate_impl_->JoinSession( |
| 523 render_process_id, routing_id, presentation_urls_, kPresentationId, |
| 524 base::Bind(&MockCreatePresentationConnnectionCallbacks:: |
| 525 OnCreateConnectionSuccess, |
| 526 base::Unretained(&mock_create_connection_callbacks)), |
| 527 base::Bind( |
| 528 &MockCreatePresentationConnnectionCallbacks::OnCreateConnectionError, |
| 529 base::Unretained(&mock_create_connection_callbacks))); |
| 530 } |
| 531 |
| 532 TEST_F(PresentationServiceDelegateImplIncognitoTest, AutoJoinRequest) { |
| 533 GURL frame_url(kFrameUrl); |
| 534 std::string origin(url::Origin(frame_url).Serialize()); |
| 535 content::WebContentsTester::For(GetWebContents()) |
| 536 ->NavigateAndCommit(frame_url); |
| 537 content::RenderFrameHost* main_frame = GetWebContents()->GetMainFrame(); |
| 538 ASSERT_TRUE(main_frame); |
| 539 int render_process_id = main_frame->GetProcess()->GetID(); |
| 540 int routing_id = main_frame->GetRoutingID(); |
| 541 |
| 542 MockCreatePresentationConnnectionCallbacks mock_create_connection_callbacks; |
| 543 const std::string kPresentationId("auto-join"); |
| 544 ASSERT_TRUE(IsAutoJoinPresentationId(kPresentationId)); |
| 545 |
| 546 // Set the user preference for |origin| to prefer tab mirroring. |
| 547 { |
| 548 ListPrefUpdate update(profile()->GetOffTheRecordProfile()->GetPrefs(), |
| 549 prefs::kMediaRouterTabMirroringSources); |
| 550 update->AppendIfNotPresent(base::MakeUnique<base::StringValue>(origin)); |
| 551 } |
| 552 |
| 553 // Setting the pref in incognito shouldn't set it for the non-incognito |
| 554 // profile. |
| 555 const base::ListValue* non_incognito_origins = |
| 556 profile()->GetPrefs()->GetList(prefs::kMediaRouterTabMirroringSources); |
| 557 EXPECT_EQ(non_incognito_origins->Find(base::StringValue(origin)), |
| 558 non_incognito_origins->end()); |
| 559 |
| 560 // Auto-join requests should be rejected. |
| 561 EXPECT_CALL(mock_create_connection_callbacks, OnCreateConnectionError(_)); |
| 562 EXPECT_CALL(router_, JoinRoute(_, kPresentationId, _, _, _, _, _)).Times(0); |
| 563 delegate_impl_->JoinSession( |
| 564 render_process_id, routing_id, presentation_urls_, kPresentationId, |
| 565 base::Bind(&MockCreatePresentationConnnectionCallbacks:: |
| 566 OnCreateConnectionSuccess, |
| 567 base::Unretained(&mock_create_connection_callbacks)), |
| 568 base::Bind( |
| 569 &MockCreatePresentationConnnectionCallbacks::OnCreateConnectionError, |
| 570 base::Unretained(&mock_create_connection_callbacks))); |
| 571 |
| 572 // Remove the user preference for |origin| in incognito. |
| 573 { |
| 574 ListPrefUpdate update(profile()->GetOffTheRecordProfile()->GetPrefs(), |
| 575 prefs::kMediaRouterTabMirroringSources); |
| 576 update->Remove(base::StringValue(origin), nullptr); |
| 577 } |
| 578 |
| 579 // Auto-join requests should now go through. |
| 580 EXPECT_CALL(router_, JoinRoute(_, kPresentationId, _, _, _, _, _)).Times(1); |
| 581 delegate_impl_->JoinSession( |
| 582 render_process_id, routing_id, presentation_urls_, kPresentationId, |
| 583 base::Bind(&MockCreatePresentationConnnectionCallbacks:: |
| 584 OnCreateConnectionSuccess, |
| 585 base::Unretained(&mock_create_connection_callbacks)), |
| 586 base::Bind( |
| 587 &MockCreatePresentationConnnectionCallbacks::OnCreateConnectionError, |
| 588 base::Unretained(&mock_create_connection_callbacks))); |
| 589 } |
| 590 #endif // !defined(OS_ANDROID) |
| 591 |
476 } // namespace media_router | 592 } // namespace media_router |
OLD | NEW |