OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <vector> | |
6 | |
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/grit/generated_resources.h" | |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
15 #include "content/public/browser/presentation_screen_availability_listener.h" | |
16 #include "content/public/browser/presentation_session.h" | |
17 #include "content/public/browser/render_process_host.h" | |
18 #include "content/public/browser/web_contents.h" | |
19 #include "content/public/test/web_contents_tester.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 | |
23 using ::testing::_; | |
24 using ::testing::Mock; | |
25 using ::testing::Return; | |
26 using ::testing::StrictMock; | |
27 | |
28 namespace media_router { | |
29 | |
30 class MockDelegateObserver | |
31 : public content::PresentationServiceDelegate::Observer { | |
32 public: | |
33 MOCK_METHOD0(OnDelegateDestroyed, void()); | |
34 MOCK_METHOD1(OnDefaultPresentationStarted, | |
35 void(const content::PresentationSessionInfo&)); | |
36 }; | |
37 | |
38 class PresentationServiceDelegateImplTest | |
39 : public ChromeRenderViewHostTestHarness { | |
40 public: | |
41 void SetUp() override { | |
42 ChromeRenderViewHostTestHarness::SetUp(); | |
43 content::WebContents* wc = web_contents(); | |
44 ASSERT_TRUE(wc); | |
45 PresentationServiceDelegateImpl::CreateForWebContents(wc); | |
46 delegate_impl_ = PresentationServiceDelegateImpl::FromWebContents(wc); | |
47 delegate_impl_->SetMediaRouterForTest(&router_); | |
48 } | |
49 | |
50 PresentationServiceDelegateImpl* delegate_impl_; | |
51 MockMediaRouter router_; | |
52 }; | |
53 | |
54 TEST_F(PresentationServiceDelegateImplTest, AddScreenAvailabilityListener) { | |
55 std::string presentation_url1("http://url1"); | |
56 std::string presentation_url2("http://url2"); | |
57 std::string presentation_url3("http://url3"); | |
58 | |
59 std::vector<MediaSource> sources; | |
60 sources.push_back(ForPresentationUrl(presentation_url1)); | |
61 sources.push_back(ForPresentationUrl(presentation_url2)); | |
62 sources.push_back(ForPresentationUrl(presentation_url3)); | |
63 | |
64 MockScreenAvailabilityListener listener1(presentation_url1); | |
65 MockScreenAvailabilityListener listener2(presentation_url2); | |
66 MockScreenAvailabilityListener listener3(presentation_url3); | |
67 | |
68 RenderFrameHostId rfh_id(0, 0); | |
Wez
2015/05/27 22:37:53
render_view_host_id :)
haibinlu
2015/05/28 21:44:05
removed.
| |
69 | |
70 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) | |
71 .Times(3) | |
72 .WillRepeatedly(Return(true)); | |
73 | |
74 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
75 rfh_id.first, rfh_id.second, &listener1)); | |
76 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
77 rfh_id.first, rfh_id.second, &listener2)); | |
78 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
79 rfh_id.first, rfh_id.second, &listener3)); | |
80 | |
81 // Should be able to find observers for MediaSources corresponding | |
82 // to presentation URLs. | |
83 for (const auto& source : sources) { | |
84 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( | |
85 rfh_id, source.id())) | |
86 << "Mapping not found for " << source.ToString(); | |
87 } | |
88 | |
89 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(3); | |
90 | |
91 delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, | |
92 &listener1); | |
93 delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, | |
94 &listener2); | |
95 delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, | |
96 &listener3); | |
97 | |
98 // 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
| |
99 // been removed. | |
100 for (const auto& source : sources) { | |
101 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( | |
102 rfh_id, source.id())); | |
103 } | |
104 } | |
105 | |
106 TEST_F(PresentationServiceDelegateImplTest, AddListenerSameUrlTwice) { | |
107 std::string presentation_url1("http://url1"); | |
108 MediaSource source1(ForPresentationUrl(presentation_url1)); | |
109 MockScreenAvailabilityListener listener1(presentation_url1); | |
110 | |
111 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) | |
112 .Times(1) | |
113 .WillRepeatedly(Return(true)); | |
114 RenderFrameHostId rfh_id(0, 0); | |
115 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
116 rfh_id.first, rfh_id.second, &listener1)); | |
117 | |
118 // Register same source for same frame again shouldn't add an observer. | |
119 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( | |
120 rfh_id.first, rfh_id.second, &listener1)); | |
121 | |
122 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( | |
123 rfh_id, source1.id())); | |
124 | |
125 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(1); | |
126 delegate_impl_->RemoveScreenAvailabilityListener(rfh_id.first, rfh_id.second, | |
127 &listener1); | |
128 | |
129 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( | |
130 rfh_id, source1.id())); | |
131 } | |
132 | |
133 TEST_F(PresentationServiceDelegateImplTest, MaxNumObservers) { | |
134 std::vector<MediaSource> sources; | |
135 ScopedVector<content::PresentationScreenAvailabilityListener> listeners; | |
136 for (size_t i = 0; i < PresentationServiceDelegateImpl::kMaxNumSources; i++) { | |
137 std::string presentation_url(base::StringPrintf("http://url%zu", i)); | |
138 sources.push_back(ForPresentationUrl(presentation_url)); | |
139 listeners.push_back(new MockScreenAvailabilityListener(presentation_url)); | |
140 } | |
141 | |
142 RenderFrameHostId rfh_id(0, 0); | |
143 | |
144 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) | |
145 .Times(PresentationServiceDelegateImpl::kMaxNumSources) | |
146 .WillRepeatedly(Return(true)); | |
147 for (const auto& listener_ptr : listeners) { | |
148 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
149 rfh_id.first, rfh_id.second, listener_ptr)); | |
150 } | |
151 | |
152 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&router_)); | |
153 | |
154 // Already reached maximum number of observers for that frame. | |
155 // No new listener will be added. | |
156 MockScreenAvailabilityListener extra_listener("http://extraUrl"); | |
157 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener( | |
158 rfh_id.first, rfh_id.second, &extra_listener)); | |
159 | |
160 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)) | |
161 .Times(PresentationServiceDelegateImpl::kMaxNumSources); | |
162 for (const auto& listener_ptr : listeners) { | |
163 delegate_impl_->RemoveScreenAvailabilityListener( | |
164 rfh_id.first, rfh_id.second, listener_ptr); | |
165 } | |
166 } | |
167 | |
168 TEST_F(PresentationServiceDelegateImplTest, SetDefaultPresentationUrl) { | |
169 content::WebContentsTester::For(web_contents()) | |
170 ->NavigateAndCommit(GURL("http://www.google.com")); | |
171 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame(); | |
172 ASSERT_TRUE(main_frame); | |
173 | |
174 int render_process_id = main_frame->GetProcess()->GetID(); | |
175 int routing_id = main_frame->GetRoutingID(); | |
176 std::string presentation_url("http://foo"); | |
177 delegate_impl_->SetDefaultPresentationUrl( | |
178 render_process_id, routing_id, presentation_url, "defaultPresentationId"); | |
179 | |
180 EXPECT_TRUE(delegate_impl_->default_source().Equals( | |
181 ForPresentationUrl(presentation_url))); | |
182 } | |
183 | |
184 TEST_F(PresentationServiceDelegateImplTest, Reset) { | |
185 std::string presentation_url1("http://url1"); | |
186 std::string presentation_url2("http://url2"); | |
187 std::string presentation_url3("http://url3"); | |
188 | |
189 std::vector<MediaSource> sources; | |
190 sources.push_back(ForPresentationUrl(presentation_url1)); | |
191 sources.push_back(ForPresentationUrl(presentation_url2)); | |
192 sources.push_back(ForPresentationUrl(presentation_url3)); | |
193 | |
194 MockScreenAvailabilityListener listener1(presentation_url1); | |
195 MockScreenAvailabilityListener listener2(presentation_url2); | |
196 MockScreenAvailabilityListener listener3(presentation_url3); | |
197 | |
198 RenderFrameHostId rfh_id(0, 0); | |
199 | |
200 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)) | |
201 .Times(3) | |
202 .WillRepeatedly(Return(true)); | |
203 | |
204 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
205 rfh_id.first, rfh_id.second, &listener1)); | |
206 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
207 rfh_id.first, rfh_id.second, &listener2)); | |
208 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener( | |
209 rfh_id.first, rfh_id.second, &listener3)); | |
210 | |
211 // Should be able to find observers for MediaSources corresponding | |
212 // to presentation URLs. | |
213 for (const auto& source : sources) { | |
214 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest( | |
215 rfh_id, source.id())); | |
216 } | |
217 | |
218 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(3); | |
219 | |
220 delegate_impl_->Reset(rfh_id.first, rfh_id.second); | |
221 | |
222 for (const auto& source : sources) { | |
223 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest( | |
224 rfh_id, source.id())); | |
225 } | |
226 } | |
227 | |
228 TEST_F(PresentationServiceDelegateImplTest, DelegateObservers) { | |
229 scoped_ptr<PresentationServiceDelegateImpl> manager( | |
230 new PresentationServiceDelegateImpl(web_contents())); | |
231 manager->SetMediaRouterForTest(&router_); | |
232 | |
233 StrictMock<MockDelegateObserver> delegate_observer1; | |
234 StrictMock<MockDelegateObserver> delegate_observer2; | |
235 | |
236 manager->AddObserver(123, 234, &delegate_observer1); | |
237 manager->AddObserver(345, 456, &delegate_observer2); | |
238 | |
239 // Removes |delegate_observer2|. | |
240 manager->RemoveObserver(345, 456); | |
241 | |
242 EXPECT_CALL(delegate_observer1, OnDelegateDestroyed()).Times(1); | |
243 manager.reset(); | |
244 } | |
245 | |
246 } // namespace media_router | |
OLD | NEW |