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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "content/browser/presentation/presentation_service_impl.h" | 8 #include "content/browser/presentation/presentation_service_impl.h" |
9 #include "content/public/browser/presentation_service_delegate.h" | 9 #include "content/public/browser/presentation_service_delegate.h" |
10 #include "content/public/browser/presentation_session.h" | 10 #include "content/public/browser/presentation_session.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 void TearDown() override { | 84 void TearDown() override { |
85 service_ptr_.reset(); | 85 service_ptr_.reset(); |
86 | 86 |
87 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service_impl_.get()))) | 87 EXPECT_CALL(mock_delegate_, RemoveObserver(Eq(service_impl_.get()))) |
88 .Times(1); | 88 .Times(1); |
89 service_impl_.reset(); | 89 service_impl_.reset(); |
90 | 90 |
91 RenderViewHostImplTestHarness::TearDown(); | 91 RenderViewHostImplTestHarness::TearDown(); |
92 } | 92 } |
93 | 93 |
94 void GetScreenAvailabilityAndWait( | 94 void ListenForScreenAvailabilityAndWait( |
95 const std::string& presentation_url, | 95 const std::string& presentation_url, |
96 const base::Callback<void(const std::string&, bool)>& callback, | 96 const base::Callback<void(const std::string&, bool)>& callback, |
97 bool delegate_success) { | 97 bool delegate_success) { |
98 base::RunLoop run_loop; | 98 base::RunLoop run_loop; |
99 // This will call to |service_impl_| via mojo. Process the message | 99 // This will call to |service_impl_| via mojo. Process the message |
100 // using RunLoop. | 100 // using RunLoop. |
101 // The callback shouldn't be invoked since there is no availability | 101 // The callback shouldn't be invoked since there is no availability |
102 // result yet. | 102 // result yet. |
103 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener(_, _, _)) | 103 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener(_, _, _)) |
104 .WillOnce(DoAll( | 104 .WillOnce(DoAll( |
105 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), | 105 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), |
106 Return(delegate_success))); | 106 Return(delegate_success))); |
107 service_ptr_->GetScreenAvailability(presentation_url, callback); | 107 service_ptr_->ListenForScreenAvailability(presentation_url, callback); |
108 run_loop.Run(); | 108 run_loop.Run(); |
109 | 109 |
110 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); | 110 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
111 } | 111 } |
112 | 112 |
113 void ExpectListenerDoesNotExist(const std::string& presentation_url) { | 113 void ExpectListenerDoesNotExist(const std::string& presentation_url) { |
114 const auto& contexts = service_impl_->availability_contexts_; | 114 const auto& contexts = service_impl_->availability_contexts_; |
115 auto it = contexts.find(presentation_url); | 115 auto it = contexts.find(presentation_url); |
116 EXPECT_TRUE(it == contexts.end()); | 116 EXPECT_TRUE(it == contexts.end()); |
117 } | 117 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 run_loop_quit_closure_.Run(); | 183 run_loop_quit_closure_.Run(); |
184 } | 184 } |
185 | 185 |
186 MockPresentationServiceDelegate mock_delegate_; | 186 MockPresentationServiceDelegate mock_delegate_; |
187 scoped_ptr<PresentationServiceImpl> service_impl_; | 187 scoped_ptr<PresentationServiceImpl> service_impl_; |
188 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; | 188 mojo::InterfacePtr<presentation::PresentationService> service_ptr_; |
189 base::Closure run_loop_quit_closure_; | 189 base::Closure run_loop_quit_closure_; |
190 int callback_count_; | 190 int callback_count_; |
191 }; | 191 }; |
192 | 192 |
193 TEST_F(PresentationServiceImplTest, GetScreenAvailability) { | 193 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { |
194 std::string presentation_url("http://fooUrl"); | 194 std::string presentation_url("http://fooUrl"); |
195 GetScreenAvailabilityAndWait( | 195 ListenForScreenAvailabilityAndWait( |
196 presentation_url, | 196 presentation_url, |
197 base::Bind( | 197 base::Bind( |
198 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 198 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
199 base::Unretained(this), true), | 199 base::Unretained(this), true), |
200 true); | 200 true); |
201 | 201 |
202 // Different presentation URL. | 202 // Different presentation URL. |
203 GetScreenAvailabilityAndWait( | 203 ListenForScreenAvailabilityAndWait( |
204 "http://barUrl", | 204 "http://barUrl", |
205 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 205 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
206 base::Unretained(this)), | 206 base::Unretained(this)), |
207 true); | 207 true); |
208 | 208 |
209 // Result now available; callback will be invoked with availability result. | 209 // Result now available; callback will be invoked with availability result. |
210 SimulateScreenAvailabilityChange(presentation_url, true); | 210 SimulateScreenAvailabilityChange(presentation_url, true); |
211 SaveQuitClosureAndRunLoop(); | 211 SaveQuitClosureAndRunLoop(); |
212 | 212 |
213 EXPECT_EQ(1, callback_count_); | 213 EXPECT_EQ(1, callback_count_); |
214 | 214 |
215 // Result updated but callback not invoked since it's been erased. | 215 // Result updated but callback not invoked since it's been erased. |
216 SimulateScreenAvailabilityChange(presentation_url, false); | 216 SimulateScreenAvailabilityChange(presentation_url, false); |
217 | 217 |
218 // Register another callback which should immediately invoke callback | 218 // Register another callback which should immediately invoke callback |
219 // since updated result is available. | 219 // since updated result is available. |
220 service_ptr_->GetScreenAvailability( | 220 service_ptr_->ListenForScreenAvailability( |
221 presentation_url, | 221 presentation_url, |
222 base::Bind( | 222 base::Bind( |
223 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 223 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
224 base::Unretained(this), | 224 base::Unretained(this), |
225 false)); | 225 false)); |
226 SaveQuitClosureAndRunLoop(); | 226 SaveQuitClosureAndRunLoop(); |
227 EXPECT_EQ(2, callback_count_); | 227 EXPECT_EQ(2, callback_count_); |
228 } | 228 } |
229 | 229 |
230 TEST_F(PresentationServiceImplTest, Reset) { | 230 TEST_F(PresentationServiceImplTest, Reset) { |
231 std::string presentation_url("http://fooUrl"); | 231 std::string presentation_url("http://fooUrl"); |
232 GetScreenAvailabilityAndWait( | 232 ListenForScreenAvailabilityAndWait( |
233 presentation_url, | 233 presentation_url, |
234 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 234 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
235 base::Unretained(this)), | 235 base::Unretained(this)), |
236 true); | 236 true); |
237 | 237 |
238 ExpectReset(); | 238 ExpectReset(); |
239 service_impl_->Reset(); | 239 service_impl_->Reset(); |
240 ExpectCleanState(); | 240 ExpectCleanState(); |
241 | 241 |
242 EXPECT_EQ(0, callback_count_); | 242 EXPECT_EQ(0, callback_count_); |
243 } | 243 } |
244 | 244 |
245 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { | 245 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { |
246 std::string presentation_url("http://fooUrl"); | 246 std::string presentation_url("http://fooUrl"); |
247 GetScreenAvailabilityAndWait( | 247 ListenForScreenAvailabilityAndWait( |
248 presentation_url, | 248 presentation_url, |
249 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 249 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
250 base::Unretained(this)), | 250 base::Unretained(this)), |
251 true); | 251 true); |
252 | 252 |
253 ExpectReset(); | 253 ExpectReset(); |
254 service_impl_->DidNavigateAnyFrame( | 254 service_impl_->DidNavigateAnyFrame( |
255 contents()->GetMainFrame(), | 255 contents()->GetMainFrame(), |
256 content::LoadCommittedDetails(), | 256 content::LoadCommittedDetails(), |
257 content::FrameNavigateParams()); | 257 content::FrameNavigateParams()); |
258 ExpectCleanState(); | 258 ExpectCleanState(); |
259 } | 259 } |
260 | 260 |
261 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { | 261 TEST_F(PresentationServiceImplTest, DidNavigateNotThisFrame) { |
262 std::string presentation_url("http://fooUrl"); | 262 std::string presentation_url("http://fooUrl"); |
263 GetScreenAvailabilityAndWait( | 263 ListenForScreenAvailabilityAndWait( |
264 presentation_url, | 264 presentation_url, |
265 base::Bind( | 265 base::Bind( |
266 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 266 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
267 base::Unretained(this), | 267 base::Unretained(this), |
268 true), | 268 true), |
269 true); | 269 true); |
270 | 270 |
271 // TODO(imcheng): How to get a different RenderFrameHost? | 271 // TODO(imcheng): How to get a different RenderFrameHost? |
272 service_impl_->DidNavigateAnyFrame( | 272 service_impl_->DidNavigateAnyFrame( |
273 nullptr, | 273 nullptr, |
274 content::LoadCommittedDetails(), | 274 content::LoadCommittedDetails(), |
275 content::FrameNavigateParams()); | 275 content::FrameNavigateParams()); |
276 | 276 |
277 // Availability is reported and callback is invoked since it was not | 277 // Availability is reported and callback is invoked since it was not |
278 // removed. | 278 // removed. |
279 SimulateScreenAvailabilityChange(presentation_url, true); | 279 SimulateScreenAvailabilityChange(presentation_url, true); |
280 SaveQuitClosureAndRunLoop(); | 280 SaveQuitClosureAndRunLoop(); |
281 EXPECT_EQ(1, callback_count_); | 281 EXPECT_EQ(1, callback_count_); |
282 } | 282 } |
283 | 283 |
284 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { | 284 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { |
285 std::string presentation_url("http://fooUrl"); | 285 std::string presentation_url("http://fooUrl"); |
286 GetScreenAvailabilityAndWait( | 286 ListenForScreenAvailabilityAndWait( |
287 presentation_url, | 287 presentation_url, |
288 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 288 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
289 base::Unretained(this)), | 289 base::Unretained(this)), |
290 true); | 290 true); |
291 | 291 |
292 ExpectReset(); | 292 ExpectReset(); |
293 service_impl_->RenderFrameDeleted(contents()->GetMainFrame()); | 293 service_impl_->RenderFrameDeleted(contents()->GetMainFrame()); |
294 ExpectCleanState(); | 294 ExpectCleanState(); |
295 } | 295 } |
296 | 296 |
297 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { | 297 TEST_F(PresentationServiceImplTest, NotThisRenderFrameDeleted) { |
298 std::string presentation_url("http://fooUrl"); | 298 std::string presentation_url("http://fooUrl"); |
299 GetScreenAvailabilityAndWait( | 299 ListenForScreenAvailabilityAndWait( |
300 presentation_url, | 300 presentation_url, |
301 base::Bind( | 301 base::Bind( |
302 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 302 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
303 base::Unretained(this), | 303 base::Unretained(this), |
304 true), | 304 true), |
305 true); | 305 true); |
306 | 306 |
307 // TODO(imcheng): How to get a different RenderFrameHost? | 307 // TODO(imcheng): How to get a different RenderFrameHost? |
308 service_impl_->RenderFrameDeleted(nullptr); | 308 service_impl_->RenderFrameDeleted(nullptr); |
309 | 309 |
310 // Availability is reported and callback should be invoked since listener | 310 // Availability is reported and callback should be invoked since listener |
311 // has not been deleted. | 311 // has not been deleted. |
312 SimulateScreenAvailabilityChange(presentation_url, true); | 312 SimulateScreenAvailabilityChange(presentation_url, true); |
313 SaveQuitClosureAndRunLoop(); | 313 SaveQuitClosureAndRunLoop(); |
314 EXPECT_EQ(1, callback_count_); | 314 EXPECT_EQ(1, callback_count_); |
315 } | 315 } |
316 | 316 |
317 TEST_F(PresentationServiceImplTest, GetScreenAvailabilityTwice) { | 317 TEST_F(PresentationServiceImplTest, ListenForScreenAvailabilityTwice) { |
318 std::string presentation_url("http://fooUrl"); | 318 std::string presentation_url("http://fooUrl"); |
319 GetScreenAvailabilityAndWait( | 319 ListenForScreenAvailabilityAndWait( |
320 presentation_url, | 320 presentation_url, |
321 base::Bind( | 321 base::Bind( |
322 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 322 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
323 base::Unretained(this), | 323 base::Unretained(this), |
324 false), | 324 false), |
325 true); | 325 true); |
326 | 326 |
327 // Second call should overwrite the callback from first call. | 327 // Second call should overwrite the callback from first call. |
328 // It shouldn't result in an extra call to delegate. | 328 // It shouldn't result in an extra call to delegate. |
329 service_ptr_->GetScreenAvailability( | 329 service_ptr_->ListenForScreenAvailability( |
330 presentation_url, | 330 presentation_url, |
331 base::Bind( | 331 base::Bind( |
332 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 332 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
333 base::Unretained(this), | 333 base::Unretained(this), |
334 false)); | 334 false)); |
335 | 335 |
336 // Cannot use GetScreenAvailabilityAndWait here since the mock delegate | 336 // Cannot use ListenForScreenAvailabilityAndWait here since the mock delegate |
337 // won't be triggered again to quit the RunLoop. | 337 // won't be triggered again to quit the RunLoop. |
338 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); | 338 RunLoopFor(base::TimeDelta::FromMilliseconds(50)); |
339 | 339 |
340 // Result now available; callback will be invoked with availability result. | 340 // Result now available; callback will be invoked with availability result. |
341 SimulateScreenAvailabilityChange(presentation_url, false); | 341 SimulateScreenAvailabilityChange(presentation_url, false); |
342 SaveQuitClosureAndRunLoop(); | 342 SaveQuitClosureAndRunLoop(); |
343 | 343 |
344 EXPECT_EQ(2, callback_count_); | 344 EXPECT_EQ(2, callback_count_); |
345 } | 345 } |
346 | 346 |
347 TEST_F(PresentationServiceImplTest, DelegateFails) { | 347 TEST_F(PresentationServiceImplTest, DelegateFails) { |
348 std::string presentation_url("http://fooUrl"); | 348 std::string presentation_url("http://fooUrl"); |
349 GetScreenAvailabilityAndWait( | 349 ListenForScreenAvailabilityAndWait( |
350 presentation_url, | 350 presentation_url, |
351 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, | 351 base::Bind(&PresentationServiceImplTest::ShouldNotBeCalled, |
352 base::Unretained(this)), | 352 base::Unretained(this)), |
353 false); | 353 false); |
354 | 354 |
355 ExpectListenerDoesNotExist(presentation_url); | 355 ExpectListenerDoesNotExist(presentation_url); |
356 } | 356 } |
357 | 357 |
358 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { | 358 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrl) { |
359 std::string url1("http://fooUrl"); | 359 std::string url1("http://fooUrl"); |
360 std::string dpu_id("dpuId"); | 360 std::string dpu_id("dpuId"); |
361 EXPECT_CALL(mock_delegate_, | 361 EXPECT_CALL(mock_delegate_, |
362 SetDefaultPresentationUrl(_, _, Eq(url1), Eq(dpu_id))) | 362 SetDefaultPresentationUrl(_, _, Eq(url1), Eq(dpu_id))) |
363 .Times(1); | 363 .Times(1); |
364 service_impl_->SetDefaultPresentationURL(url1, dpu_id); | 364 service_impl_->SetDefaultPresentationURL(url1, dpu_id); |
365 EXPECT_EQ(url1, service_impl_->default_presentation_url_); | 365 EXPECT_EQ(url1, service_impl_->default_presentation_url_); |
366 | 366 |
367 // Now there should be a callback registered with the DPU. | 367 // Now there should be a callback registered with the DPU. |
368 GetScreenAvailabilityAndWait( | 368 ListenForScreenAvailabilityAndWait( |
369 url1, | 369 url1, |
370 base::Bind( | 370 base::Bind( |
371 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 371 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
372 base::Unretained(this), true), | 372 base::Unretained(this), true), |
373 true); | 373 true); |
374 const auto& contexts = service_impl_->availability_contexts_; | 374 const auto& contexts = service_impl_->availability_contexts_; |
375 auto it = contexts.find(url1); | 375 auto it = contexts.find(url1); |
376 ASSERT_TRUE(it != contexts.end()); | 376 ASSERT_TRUE(it != contexts.end()); |
377 EXPECT_TRUE(it->second->HasPendingCallbacks()); | 377 EXPECT_TRUE(it->second->HasPendingCallbacks()); |
378 | 378 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 TEST_F(PresentationServiceImplTest, ClearDefaultPresentationUrl) { | 418 TEST_F(PresentationServiceImplTest, ClearDefaultPresentationUrl) { |
419 std::string url("http://fooUrl"); | 419 std::string url("http://fooUrl"); |
420 std::string dpu_id("dpuId"); | 420 std::string dpu_id("dpuId"); |
421 EXPECT_CALL(mock_delegate_, | 421 EXPECT_CALL(mock_delegate_, |
422 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) | 422 SetDefaultPresentationUrl(_, _, Eq(url), Eq(dpu_id))) |
423 .Times(1); | 423 .Times(1); |
424 service_impl_->SetDefaultPresentationURL(url, dpu_id); | 424 service_impl_->SetDefaultPresentationURL(url, dpu_id); |
425 EXPECT_EQ(url, service_impl_->default_presentation_url_); | 425 EXPECT_EQ(url, service_impl_->default_presentation_url_); |
426 | 426 |
427 // Now there should be a callback registered with the DPU. | 427 // Now there should be a callback registered with the DPU. |
428 GetScreenAvailabilityAndWait( | 428 ListenForScreenAvailabilityAndWait( |
429 url, | 429 url, |
430 base::Bind( | 430 base::Bind( |
431 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, | 431 &PresentationServiceImplTest::ScreenAvailabilityChangedCallback, |
432 base::Unretained(this), true), | 432 base::Unretained(this), true), |
433 true); | 433 true); |
434 | 434 |
435 const auto& contexts = service_impl_->availability_contexts_; | 435 const auto& contexts = service_impl_->availability_contexts_; |
436 auto it = contexts.find(url); | 436 auto it = contexts.find(url); |
437 ASSERT_TRUE(it != contexts.end()); | 437 ASSERT_TRUE(it != contexts.end()); |
438 EXPECT_TRUE(it->second->HasPendingCallbacks()); | 438 EXPECT_TRUE(it->second->HasPendingCallbacks()); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 // Running the callback means the first request is done. It should now | 571 // Running the callback means the first request is done. It should now |
572 // move on to the queued request. | 572 // move on to the queued request. |
573 EXPECT_CALL(mock_delegate_, StartSession( | 573 EXPECT_CALL(mock_delegate_, StartSession( |
574 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) | 574 _, _, Eq(presentation_url2), Eq(presentation_id2), _, _)) |
575 .Times(1); | 575 .Times(1); |
576 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); | 576 success_cb.Run(PresentationSessionInfo(presentation_url1, presentation_id1)); |
577 SaveQuitClosureAndRunLoop(); | 577 SaveQuitClosureAndRunLoop(); |
578 } | 578 } |
579 | 579 |
580 } // namespace content | 580 } // namespace content |
OLD | NEW |