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
3 // found in the LICENSE file.
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/media/router/media_source.h"
9 #include "chrome/browser/media/router/media_source_helper.h"
10 #include "chrome/browser/media/router/mock_media_router.h"
11 #include "chrome/browser/media/router/mock_screen_availability_listener.h"
12 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
13 #include "chrome/browser/media/router/test_helper.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "content/public/browser/presentation_screen_availability_listener.h"
17 #include "content/public/browser/presentation_session.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/web_contents_tester.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "ui/base/l10n/l10n_util.h"
25 using ::testing::Mock
;
26 using ::testing::Return
;
27 using ::testing::StrictMock
;
29 namespace media_router
{
31 class MockDelegateObserver
32 : public content::PresentationServiceDelegate::Observer
{
34 MOCK_METHOD0(OnDelegateDestroyed
, void());
35 MOCK_METHOD1(OnDefaultPresentationStarted
,
36 void(const content::PresentationSessionInfo
&));
39 class MockDefaultMediaSourceObserver
40 : public PresentationServiceDelegateImpl::DefaultMediaSourceObserver
{
42 MOCK_METHOD2(OnDefaultMediaSourceChanged
,
43 void(const MediaSource
&, const GURL
&));
46 class PresentationServiceDelegateImplTest
47 : public ChromeRenderViewHostTestHarness
{
49 void SetUp() override
{
50 ChromeRenderViewHostTestHarness::SetUp();
51 content::WebContents
* wc
= web_contents();
53 PresentationServiceDelegateImpl::CreateForWebContents(wc
);
54 delegate_impl_
= PresentationServiceDelegateImpl::FromWebContents(wc
);
55 delegate_impl_
->SetMediaRouterForTest(&router_
);
58 PresentationServiceDelegateImpl
* delegate_impl_
;
59 MockMediaRouter router_
;
62 TEST_F(PresentationServiceDelegateImplTest
, AddScreenAvailabilityListener
) {
63 std::string
presentation_url1("http://url1");
64 std::string
presentation_url2("http://url2");
65 MediaSource source1
= MediaSourceForPresentationUrl(presentation_url1
);
66 MediaSource source2
= MediaSourceForPresentationUrl(presentation_url2
);
67 MockScreenAvailabilityListener
listener1(presentation_url1
);
68 MockScreenAvailabilityListener
listener2(presentation_url2
);
69 int render_process_id
= 10;
70 int render_frame_id1
= 1;
71 int render_frame_id2
= 2;
73 EXPECT_CALL(router_
, RegisterMediaSinksObserver(_
)).Times(2);
74 EXPECT_TRUE(delegate_impl_
->AddScreenAvailabilityListener(
75 render_process_id
, render_frame_id1
, &listener1
));
76 EXPECT_TRUE(delegate_impl_
->AddScreenAvailabilityListener(
77 render_process_id
, render_frame_id2
, &listener2
));
78 EXPECT_TRUE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
79 render_process_id
, render_frame_id1
, source1
.id()))
80 << "Mapping not found for " << source1
.ToString();
81 EXPECT_TRUE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
82 render_process_id
, render_frame_id2
, source2
.id()))
83 << "Mapping not found for " << source2
.ToString();
85 EXPECT_CALL(router_
, UnregisterMediaSinksObserver(_
)).Times(2);
86 delegate_impl_
->RemoveScreenAvailabilityListener(
87 render_process_id
, render_frame_id1
, &listener1
);
88 delegate_impl_
->RemoveScreenAvailabilityListener(
89 render_process_id
, render_frame_id2
, &listener2
);
90 EXPECT_FALSE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
91 render_process_id
, render_frame_id1
, source1
.id()));
92 EXPECT_FALSE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
93 render_process_id
, render_frame_id2
, source2
.id()));
96 TEST_F(PresentationServiceDelegateImplTest
, AddSameListenerTwice
) {
97 std::string
presentation_url1("http://url1");
98 MediaSource
source1(MediaSourceForPresentationUrl(presentation_url1
));
99 MockScreenAvailabilityListener
listener1(presentation_url1
);
100 int render_process_id
= 1;
101 int render_frame_id
= 0;
103 EXPECT_CALL(router_
, RegisterMediaSinksObserver(_
)).Times(1);
104 EXPECT_TRUE(delegate_impl_
->AddScreenAvailabilityListener(
105 render_process_id
, render_frame_id
, &listener1
));
106 EXPECT_FALSE(delegate_impl_
->AddScreenAvailabilityListener(
107 render_process_id
, render_frame_id
, &listener1
));
108 EXPECT_TRUE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
109 render_process_id
, render_frame_id
, source1
.id()));
111 EXPECT_CALL(router_
, UnregisterMediaSinksObserver(_
)).Times(1);
112 delegate_impl_
->RemoveScreenAvailabilityListener(render_process_id
,
113 render_frame_id
, &listener1
);
114 EXPECT_FALSE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
115 render_process_id
, render_frame_id
, source1
.id()));
118 TEST_F(PresentationServiceDelegateImplTest
, SetDefaultPresentationUrl
) {
119 content::WebContentsTester::For(web_contents())
120 ->NavigateAndCommit(GURL("http://www.google.com"));
121 content::RenderFrameHost
* main_frame
= web_contents()->GetMainFrame();
122 ASSERT_TRUE(main_frame
);
123 int render_process_id
= main_frame
->GetProcess()->GetID();
124 int routing_id
= main_frame
->GetRoutingID();
126 std::string
presentation_url1("http://foo");
127 delegate_impl_
->SetDefaultPresentationUrl(render_process_id
, routing_id
,
129 EXPECT_TRUE(delegate_impl_
->default_source().Equals(
130 MediaSourceForPresentationUrl(presentation_url1
)));
132 // Remove default presentation URL.
133 delegate_impl_
->SetDefaultPresentationUrl(render_process_id
, routing_id
, "");
134 EXPECT_TRUE(delegate_impl_
->default_source().Empty());
136 // Set to a new default presentation URL
137 std::string
presentation_url2("https://youtube.com");
138 delegate_impl_
->SetDefaultPresentationUrl(render_process_id
, routing_id
,
140 EXPECT_TRUE(delegate_impl_
->default_source().Equals(
141 MediaSourceForPresentationUrl(presentation_url2
)));
144 TEST_F(PresentationServiceDelegateImplTest
, DefaultMediaSourceObserver
) {
145 content::WebContentsTester::For(web_contents())
146 ->NavigateAndCommit(GURL("http://www.google.com"));
147 content::RenderFrameHost
* main_frame
= web_contents()->GetMainFrame();
148 ASSERT_TRUE(main_frame
);
149 int render_process_id
= main_frame
->GetProcess()->GetID();
150 int routing_id
= main_frame
->GetRoutingID();
151 StrictMock
<MockDefaultMediaSourceObserver
> observer1
;
152 StrictMock
<MockDefaultMediaSourceObserver
> observer2
;
153 delegate_impl_
->AddDefaultMediaSourceObserver(&observer1
);
154 delegate_impl_
->AddDefaultMediaSourceObserver(&observer2
);
155 std::string
url1("http://foo");
156 EXPECT_CALL(observer1
, OnDefaultMediaSourceChanged(
157 Equals(MediaSourceForPresentationUrl(url1
)),
158 GURL("http://www.google.com"))).Times(1);
159 EXPECT_CALL(observer2
, OnDefaultMediaSourceChanged(
160 Equals(MediaSourceForPresentationUrl(url1
)),
161 GURL("http://www.google.com"))).Times(1);
162 delegate_impl_
->SetDefaultPresentationUrl(render_process_id
,
165 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1
));
166 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2
));
168 delegate_impl_
->RemoveDefaultMediaSourceObserver(&observer2
);
169 std::string
url2("http://youtube.com");
170 EXPECT_CALL(observer1
, OnDefaultMediaSourceChanged(
171 Equals(MediaSourceForPresentationUrl(url2
)),
172 GURL("http://www.google.com"))).Times(1);
173 delegate_impl_
->SetDefaultPresentationUrl(render_process_id
,
176 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1
));
177 // Remove default presentation URL.
178 EXPECT_CALL(observer1
, OnDefaultMediaSourceChanged(
179 Equals(MediaSource()),
180 GURL("http://www.google.com"))).Times(1);
181 delegate_impl_
->SetDefaultPresentationUrl(render_process_id
, routing_id
, "");
184 TEST_F(PresentationServiceDelegateImplTest
, Reset
) {
185 std::string
presentation_url1("http://url1");
186 MediaSource source
= MediaSourceForPresentationUrl(presentation_url1
);
187 MockScreenAvailabilityListener
listener1(presentation_url1
);
188 int render_process_id
= 1;
189 int render_frame_id
= 0;
191 EXPECT_TRUE(delegate_impl_
->AddScreenAvailabilityListener(
192 render_process_id
, render_frame_id
, &listener1
));
193 EXPECT_TRUE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
194 render_process_id
, render_frame_id
, source
.id()));
195 EXPECT_CALL(router_
, UnregisterMediaSinksObserver(_
)).Times(1);
196 delegate_impl_
->Reset(render_process_id
, render_frame_id
);
197 EXPECT_FALSE(delegate_impl_
->HasScreenAvailabilityListenerForTest(
198 render_process_id
, render_frame_id
, source
.id()));
201 TEST_F(PresentationServiceDelegateImplTest
, DelegateObservers
) {
202 scoped_ptr
<PresentationServiceDelegateImpl
> manager(
203 new PresentationServiceDelegateImpl(web_contents()));
204 manager
->SetMediaRouterForTest(&router_
);
206 StrictMock
<MockDelegateObserver
> delegate_observer1
;
207 StrictMock
<MockDelegateObserver
> delegate_observer2
;
209 manager
->AddObserver(123, 234, &delegate_observer1
);
210 manager
->AddObserver(345, 456, &delegate_observer2
);
212 // Removes |delegate_observer2|.
213 manager
->RemoveObserver(345, 456);
215 EXPECT_CALL(delegate_observer1
, OnDelegateDestroyed()).Times(1);
219 } // namespace media_router