Chromium Code Reviews| Index: chrome/browser/media/router/presentation_service_delegate_impl_unittest.cc |
| diff --git a/chrome/browser/media/router/presentation_service_delegate_impl_unittest.cc b/chrome/browser/media/router/presentation_service_delegate_impl_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84b024ee519d744b1425e647b6fc7588469db813 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/presentation_service_delegate_impl_unittest.cc |
| @@ -0,0 +1,246 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <vector> |
| + |
| +#include "base/strings/stringprintf.h" |
| +#include "chrome/browser/media/router/media_source.h" |
| +#include "chrome/browser/media/router/media_source_helper.h" |
| +#include "chrome/browser/media/router/mock_media_router.h" |
| +#include "chrome/browser/media/router/mock_screen_availability_listener.h" |
| +#include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| +#include "content/public/browser/presentation_screen_availability_listener.h" |
| +#include "content/public/browser/presentation_session.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/test/web_contents_tester.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +using ::testing::_; |
| +using ::testing::Mock; |
| +using ::testing::Return; |
| +using ::testing::StrictMock; |
| + |
| +namespace media_router { |
| + |
| +class MockDelegateObserver |
| + : public content::PresentationServiceDelegate::Observer { |
| + public: |
| + MOCK_METHOD0(OnDelegateDestroyed, void()); |
| + MOCK_METHOD1(OnDefaultPresentationStarted, |
| + void(const content::PresentationSessionInfo&)); |
| +}; |
| + |
| +class PresentationServiceDelegateImplTest |
| + : public ChromeRenderViewHostTestHarness { |
| + public: |
| + void SetUp() override { |
| + ChromeRenderViewHostTestHarness::SetUp(); |
| + content::WebContents* wc = web_contents(); |
| + ASSERT_TRUE(wc); |
| + PresentationServiceDelegateImpl::CreateForWebContents(wc); |
| + delegate_impl_ = PresentationServiceDelegateImpl::FromWebContents(wc); |
| + delegate_impl_->SetMediaRouterForTest(&router_); |
| + } |
| + |
| + PresentationServiceDelegateImpl* delegate_impl_; |
| + MockMediaRouter router_; |
| +}; |
| + |
| +TEST_F(PresentationServiceDelegateImplTest, AddScreenAvailabilityListener) { |
| + std::string presentation_url1("http://url1"); |
| + std::string presentation_url2("http://url2"); |
| + std::string presentation_url3("http://url3"); |
| + |
| + std::vector<MediaSource> sources; |
| + sources.push_back(ForPresentationUrl(presentation_url1)); |
| + sources.push_back(ForPresentationUrl(presentation_url2)); |
| + sources.push_back(ForPresentationUrl(presentation_url3)); |
| + |
| + MockScreenAvailabilityListener listener1(presentation_url1); |
| + MockScreenAvailabilityListener listener2(presentation_url2); |
| + MockScreenAvailabilityListener listener3(presentation_url3); |
| + |
| + RenderFrameHostId rfh_id(0, 0); |
|
Wez
2015/05/27 22:37:53
render_view_host_id :)
haibinlu
2015/05/28 21:44:05
removed.
|
| + |
| + EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) |
| + .Times(3) |
| + .WillRepeatedly(Return(true)); |
| + |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener1)); |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener2)); |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener3)); |
| + |
| + // Should be able to find observers for MediaSources corresponding |
| + // to presentation URLs. |
| + for (const auto& source : sources) { |
| + EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
| + rfh_id, source.id())) |
| + << "Mapping not found for " << source.ToString(); |
| + } |
| + |
| + EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(3); |
| + |
| + delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, |
| + &listener1); |
| + delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, |
| + &listener2); |
| + delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, |
| + &listener3); |
| + |
| + // The RFH entry should have been removed since all of its listeners have |
|
Wez
2015/05/27 22:37:53
RenderViewHost!
haibinlu
2015/05/28 21:44:05
removed
|
| + // been removed. |
| + for (const auto& source : sources) { |
| + EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
| + rfh_id, source.id())); |
| + } |
| +} |
| + |
| +TEST_F(PresentationServiceDelegateImplTest, AddListenerSameUrlTwice) { |
| + std::string presentation_url1("http://url1"); |
| + MediaSource source1(ForPresentationUrl(presentation_url1)); |
| + MockScreenAvailabilityListener listener1(presentation_url1); |
| + |
| + EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) |
| + .Times(1) |
| + .WillRepeatedly(Return(true)); |
| + RenderFrameHostId rfh_id(0, 0); |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener1)); |
| + |
| + // Register same source for same frame again shouldn't add an observer. |
| + EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener1)); |
| + |
| + EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
| + rfh_id, source1.id())); |
| + |
| + EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(1); |
| + delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, |
| + &listener1); |
| + |
| + EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
| + rfh_id, source1.id())); |
| +} |
| + |
| +TEST_F(PresentationServiceDelegateImplTest, MaxNumObservers) { |
| + std::vector<MediaSource> sources; |
| + ScopedVector<content::PresentationScreenAvailabilityListener> listeners; |
| + for (size_t i = 0; i < PresentationServiceDelegateImpl::kMaxNumSources; i++) { |
| + std::string presentation_url(base::StringPrintf("http://url%zu", i)); |
| + sources.push_back(ForPresentationUrl(presentation_url)); |
| + listeners.push_back(new MockScreenAvailabilityListener(presentation_url)); |
| + } |
| + |
| + RenderFrameHostId rfh_id(0, 0); |
| + |
| + EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) |
| + .Times(PresentationServiceDelegateImpl::kMaxNumSources) |
| + .WillRepeatedly(Return(true)); |
| + for (const auto& listener_ptr : listeners) { |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, listener_ptr)); |
| + } |
| + |
| + EXPECT_TRUE(Mock::VerifyAndClearExpectations(&router_)); |
| + |
| + // Already reached maximum number of observers for that frame. |
| + // No new listener will be added. |
| + MockScreenAvailabilityListener extra_listener("http://extraUrl"); |
| + EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &extra_listener)); |
| + |
| + EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)) |
| + .Times(PresentationServiceDelegateImpl::kMaxNumSources); |
| + for (const auto& listener_ptr : listeners) { |
| + delegate_impl_->RemoveScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, listener_ptr); |
| + } |
| +} |
| + |
| +TEST_F(PresentationServiceDelegateImplTest, SetDefaultPresentationUrl) { |
| + content::WebContentsTester::For(web_contents()) |
| + ->NavigateAndCommit(GURL("http://www.google.com")); |
| + content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); |
| + ASSERT_TRUE(main_frame); |
| + |
| + int render_process_id = main_frame->GetProcess()->GetID(); |
| + int routing_id = main_frame->GetRoutingID(); |
| + std::string presentation_url("http://foo"); |
| + delegate_impl_->SetDefaultPresentationUrl( |
| + render_process_id, routing_id, presentation_url, "defaultPresentationId"); |
| + |
| + EXPECT_TRUE(delegate_impl_->default_source().Equals( |
| + ForPresentationUrl(presentation_url))); |
| +} |
| + |
| +TEST_F(PresentationServiceDelegateImplTest, Reset) { |
| + std::string presentation_url1("http://url1"); |
| + std::string presentation_url2("http://url2"); |
| + std::string presentation_url3("http://url3"); |
| + |
| + std::vector<MediaSource> sources; |
| + sources.push_back(ForPresentationUrl(presentation_url1)); |
| + sources.push_back(ForPresentationUrl(presentation_url2)); |
| + sources.push_back(ForPresentationUrl(presentation_url3)); |
| + |
| + MockScreenAvailabilityListener listener1(presentation_url1); |
| + MockScreenAvailabilityListener listener2(presentation_url2); |
| + MockScreenAvailabilityListener listener3(presentation_url3); |
| + |
| + RenderFrameHostId rfh_id(0, 0); |
| + |
| + EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) |
| + .Times(3) |
| + .WillRepeatedly(Return(true)); |
| + |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener1)); |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener2)); |
| + EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( |
| + rfh_id.first, rfh_id.second, &listener3)); |
| + |
| + // Should be able to find observers for MediaSources corresponding |
| + // to presentation URLs. |
| + for (const auto& source : sources) { |
| + EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
| + rfh_id, source.id())); |
| + } |
| + |
| + EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(3); |
| + |
| + delegate_impl_->Reset(rfh_id.first, rfh_id.second); |
| + |
| + for (const auto& source : sources) { |
| + EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( |
| + rfh_id, source.id())); |
| + } |
| +} |
| + |
| +TEST_F(PresentationServiceDelegateImplTest, DelegateObservers) { |
| + scoped_ptr<PresentationServiceDelegateImpl> manager( |
| + new PresentationServiceDelegateImpl(web_contents())); |
| + manager->SetMediaRouterForTest(&router_); |
| + |
| + StrictMock<MockDelegateObserver> delegate_observer1; |
| + StrictMock<MockDelegateObserver> delegate_observer2; |
| + |
| + manager->AddObserver(123, 234, &delegate_observer1); |
| + manager->AddObserver(345, 456, &delegate_observer2); |
| + |
| + // Removes |delegate_observer2|. |
| + manager->RemoveObserver(345, 456); |
| + |
| + EXPECT_CALL(delegate_observer1, OnDelegateDestroyed()).Times(1); |
| + manager.reset(); |
| +} |
| + |
| +} // namespace media_router |