Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: content/browser/presentation/presentation_service_impl_unittest.cc

Issue 2938023002: [PresentationService] Improve PresentationServiceImplTest. (Closed)
Patch Set: . Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/presentation/presentation_service_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "content/browser/presentation/presentation_service_impl.h" 5 #include "content/browser/presentation/presentation_service_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <iterator> 10 #include <iterator>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/location.h"
17 #include "base/run_loop.h" 16 #include "base/run_loop.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/test/test_timeouts.h"
20 #include "base/threading/thread_task_runner_handle.h"
21 #include "content/public/browser/navigation_handle.h" 17 #include "content/public/browser/navigation_handle.h"
22 #include "content/public/browser/presentation_service_delegate.h" 18 #include "content/public/browser/presentation_service_delegate.h"
23 #include "content/public/common/presentation_connection_message.h" 19 #include "content/public/common/presentation_connection_message.h"
24 #include "content/public/common/presentation_info.h" 20 #include "content/public/common/presentation_info.h"
25 #include "content/test/test_render_frame_host.h" 21 #include "content/test/test_render_frame_host.h"
26 #include "content/test/test_render_view_host.h" 22 #include "content/test/test_render_view_host.h"
27 #include "content/test/test_web_contents.h" 23 #include "content/test/test_web_contents.h"
28 #include "mojo/public/cpp/bindings/interface_ptr.h" 24 #include "mojo/public/cpp/bindings/interface_ptr.h"
29 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
30 26
31 using ::testing::_; 27 using ::testing::_;
32 using ::testing::ByRef;
33 using ::testing::Eq; 28 using ::testing::Eq;
34 using ::testing::Invoke;
35 using ::testing::InvokeWithoutArgs;
36 using ::testing::Mock; 29 using ::testing::Mock;
37 using ::testing::Return; 30 using ::testing::Return;
38 using ::testing::SaveArg; 31 using ::testing::SaveArg;
39 using ::testing::WithArgs;
40 32
41 namespace content { 33 namespace content {
42 34
43 namespace { 35 namespace {
44 36
45 // Matches content::PresentationInfo. 37 // Matches content::PresentationInfo.
46 MATCHER_P(InfoEquals, expected, "") { 38 MATCHER_P(InfoEquals, expected, "") {
47 return expected.presentation_url == arg.presentation_url && 39 return expected.presentation_url == arg.presentation_url &&
48 expected.presentation_id == arg.presentation_id; 40 expected.presentation_id == arg.presentation_id;
49 } 41 }
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 : presentation_url1_(GURL(kPresentationUrl1)), 228 : presentation_url1_(GURL(kPresentationUrl1)),
237 presentation_url2_(GURL(kPresentationUrl2)), 229 presentation_url2_(GURL(kPresentationUrl2)),
238 presentation_url3_(GURL(kPresentationUrl3)) {} 230 presentation_url3_(GURL(kPresentationUrl3)) {}
239 231
240 void SetUp() override { 232 void SetUp() override {
241 RenderViewHostImplTestHarness::SetUp(); 233 RenderViewHostImplTestHarness::SetUp();
242 // This needed to keep the WebContentsObserverSanityChecker checks happy for 234 // This needed to keep the WebContentsObserverSanityChecker checks happy for
243 // when AppendChild is called. 235 // when AppendChild is called.
244 NavigateAndCommit(GURL("about:blank")); 236 NavigateAndCommit(GURL("about:blank"));
245 237
246 auto request = mojo::MakeRequest(&service_ptr_);
247 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); 238 EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1);
248 TestRenderFrameHost* render_frame_host = contents()->GetMainFrame(); 239 TestRenderFrameHost* render_frame_host = contents()->GetMainFrame();
249 render_frame_host->InitializeRenderFrameIfNeeded(); 240 render_frame_host->InitializeRenderFrameIfNeeded();
250 service_impl_.reset(new PresentationServiceImpl( 241 service_impl_.reset(new PresentationServiceImpl(
251 render_frame_host, contents(), &mock_delegate_, nullptr)); 242 render_frame_host, contents(), &mock_delegate_, nullptr));
252 service_impl_->Bind(std::move(request));
253 243
254 blink::mojom::PresentationServiceClientPtr client_ptr; 244 blink::mojom::PresentationServiceClientPtr client_ptr;
255 client_binding_.reset( 245 client_binding_.reset(
256 new mojo::Binding<blink::mojom::PresentationServiceClient>( 246 new mojo::Binding<blink::mojom::PresentationServiceClient>(
257 &mock_client_, mojo::MakeRequest(&client_ptr))); 247 &mock_client_, mojo::MakeRequest(&client_ptr)));
258 service_impl_->SetClient(std::move(client_ptr)); 248 service_impl_->SetClient(std::move(client_ptr));
259 249
260 presentation_urls_.push_back(presentation_url1_); 250 presentation_urls_.push_back(presentation_url1_);
261 presentation_urls_.push_back(presentation_url2_); 251 presentation_urls_.push_back(presentation_url2_);
262 } 252 }
263 253
264 void TearDown() override { 254 void TearDown() override {
265 service_ptr_.reset();
266 if (service_impl_.get()) { 255 if (service_impl_.get()) {
267 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); 256 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1);
268 service_impl_.reset(); 257 service_impl_.reset();
269 } 258 }
270 RenderViewHostImplTestHarness::TearDown(); 259 RenderViewHostImplTestHarness::TearDown();
271 } 260 }
272 261
273 void Navigate(bool main_frame) { 262 void Navigate(bool main_frame) {
274 RenderFrameHost* rfh = main_rfh(); 263 RenderFrameHost* rfh = main_rfh();
275 RenderFrameHostTester* rfh_tester = RenderFrameHostTester::For(rfh); 264 RenderFrameHostTester* rfh_tester = RenderFrameHostTester::For(rfh);
276 if (!main_frame) 265 if (!main_frame)
277 rfh = rfh_tester->AppendChild("subframe"); 266 rfh = rfh_tester->AppendChild("subframe");
278 std::unique_ptr<NavigationHandle> navigation_handle = 267 std::unique_ptr<NavigationHandle> navigation_handle =
279 NavigationHandle::CreateNavigationHandleForTesting( 268 NavigationHandle::CreateNavigationHandleForTesting(
280 GURL(), rfh, true); 269 GURL(), rfh, true);
281 // Destructor calls DidFinishNavigation. 270 // Destructor calls DidFinishNavigation.
282 } 271 }
283 272
284 void ListenForScreenAvailabilityAndWait(const GURL& url, 273 void ListenForScreenAvailabilityAndWait(const GURL& url,
285 bool delegate_success) { 274 bool delegate_success) {
286 base::RunLoop run_loop;
287 // This will call to |service_impl_| via mojo. Process the message
288 // using RunLoop.
289 // The callback shouldn't be invoked since there is no availability
290 // result yet.
291 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener()) 275 EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener())
292 .WillOnce(DoAll( 276 .WillOnce(Return(delegate_success));
293 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), 277 service_impl_->ListenForScreenAvailability(url);
294 Return(delegate_success)));
295 service_ptr_->ListenForScreenAvailability(url);
296 run_loop.Run();
297 278
298 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); 279 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
299 } 280 }
300 281
301 void RunLoopFor(base::TimeDelta duration) {
302 base::RunLoop run_loop;
303 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
304 FROM_HERE, run_loop.QuitClosure(), duration);
305 run_loop.Run();
306 }
307
308 void SaveQuitClosureAndRunLoop() {
309 base::RunLoop run_loop;
310 run_loop_quit_closure_ = run_loop.QuitClosure();
311 run_loop.Run();
312 run_loop_quit_closure_.Reset();
313 }
314
315 void SimulateScreenAvailabilityChangeAndWait(const GURL& url, 282 void SimulateScreenAvailabilityChangeAndWait(const GURL& url,
316 bool available) { 283 bool available) {
317 auto listener_it = service_impl_->screen_availability_listeners_.find(url); 284 auto listener_it = service_impl_->screen_availability_listeners_.find(url);
318 ASSERT_TRUE(listener_it->second); 285 ASSERT_TRUE(listener_it->second);
319 286
320 base::RunLoop run_loop;
321 blink::mojom::ScreenAvailability expected_availability = 287 blink::mojom::ScreenAvailability expected_availability =
322 available ? blink::mojom::ScreenAvailability::AVAILABLE 288 available ? blink::mojom::ScreenAvailability::AVAILABLE
323 : blink::mojom::ScreenAvailability::UNAVAILABLE; 289 : blink::mojom::ScreenAvailability::UNAVAILABLE;
324 EXPECT_CALL(mock_client_, 290 EXPECT_CALL(mock_client_,
325 OnScreenAvailabilityUpdated(url, expected_availability)) 291 OnScreenAvailabilityUpdated(url, expected_availability));
326 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
327 listener_it->second->OnScreenAvailabilityChanged(available); 292 listener_it->second->OnScreenAvailabilityChanged(available);
328 run_loop.Run(); 293 base::RunLoop().RunUntilIdle();
329 } 294 }
330 295
331 void ExpectReset() { 296 void ExpectReset() {
332 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); 297 EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1);
333 } 298 }
334 299
335 void ExpectCleanState() { 300 void ExpectCleanState() {
336 EXPECT_TRUE(service_impl_->default_presentation_urls_.empty()); 301 EXPECT_TRUE(service_impl_->default_presentation_urls_.empty());
337 EXPECT_EQ( 302 EXPECT_EQ(
338 service_impl_->screen_availability_listeners_.find(presentation_url1_), 303 service_impl_->screen_availability_listeners_.find(presentation_url1_),
339 service_impl_->screen_availability_listeners_.end()); 304 service_impl_->screen_availability_listeners_.end());
340 } 305 }
341 306
342 void ExpectNewPresentationCallbackSuccess( 307 void ExpectNewPresentationCallbackSuccess(
zhaobin 2017/06/15 02:40:25 Would it be simpler if we use MockCallback instead
imcheng 2017/06/15 05:39:23 It would certainly help us get rid of the 2 helper
343 const base::Optional<PresentationInfo>& info, 308 const base::Optional<PresentationInfo>& info,
344 const base::Optional<PresentationError>& error) { 309 const base::Optional<PresentationError>& error) {
345 EXPECT_TRUE(info); 310 EXPECT_TRUE(info);
346 EXPECT_FALSE(error); 311 EXPECT_FALSE(error);
347 if (!run_loop_quit_closure_.is_null()) 312 presentation_success_count_++;
348 run_loop_quit_closure_.Run();
349 } 313 }
350 314
351 void ExpectNewPresentationCallbackError( 315 void ExpectNewPresentationCallbackError(
352 const base::Optional<PresentationInfo>& info, 316 const base::Optional<PresentationInfo>& info,
353 const base::Optional<PresentationError>& error) { 317 const base::Optional<PresentationError>& error) {
354 EXPECT_FALSE(info); 318 EXPECT_FALSE(info);
355 EXPECT_TRUE(error); 319 EXPECT_TRUE(error);
356 if (!run_loop_quit_closure_.is_null()) 320 presentation_error_count_++;
357 run_loop_quit_closure_.Run();
358 }
359
360 void ExpectConnectionMessages(
361 const std::vector<PresentationConnectionMessage>& expected_msgs,
362 const std::vector<PresentationConnectionMessage>& actual_msgs) {
363 EXPECT_EQ(expected_msgs.size(), actual_msgs.size());
364 for (size_t i = 0; i < actual_msgs.size(); ++i)
365 EXPECT_EQ(expected_msgs[i], actual_msgs[i]);
366 } 321 }
367 322
368 MockPresentationServiceDelegate mock_delegate_; 323 MockPresentationServiceDelegate mock_delegate_;
369 MockReceiverPresentationServiceDelegate mock_receiver_delegate_; 324 MockReceiverPresentationServiceDelegate mock_receiver_delegate_;
370 325
371 std::unique_ptr<PresentationServiceImpl> service_impl_; 326 std::unique_ptr<PresentationServiceImpl> service_impl_;
372 mojo::InterfacePtr<blink::mojom::PresentationService> service_ptr_;
373 327
374 MockPresentationServiceClient mock_client_; 328 MockPresentationServiceClient mock_client_;
375 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>> 329 std::unique_ptr<mojo::Binding<blink::mojom::PresentationServiceClient>>
376 client_binding_; 330 client_binding_;
377 331
378 base::Closure run_loop_quit_closure_;
379
380 GURL presentation_url1_; 332 GURL presentation_url1_;
381 GURL presentation_url2_; 333 GURL presentation_url2_;
382 GURL presentation_url3_; 334 GURL presentation_url3_;
383 std::vector<GURL> presentation_urls_; 335 std::vector<GURL> presentation_urls_;
336
337 int presentation_success_count_ = 0;
338 int presentation_error_count_ = 0;
384 }; 339 };
385 340
386 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { 341 TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) {
387 ListenForScreenAvailabilityAndWait(presentation_url1_, true); 342 ListenForScreenAvailabilityAndWait(presentation_url1_, true);
388 343
389 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, true); 344 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, true);
390 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, false); 345 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, false);
391 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, true); 346 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, true);
392 } 347 }
393 348
349 TEST_F(PresentationServiceImplTest, ScreenAvailabilityNotSupported) {
350 mock_delegate_.set_screen_availability_listening_supported(false);
351 EXPECT_CALL(mock_client_,
352 OnScreenAvailabilityNotSupported(presentation_url1_));
353 ListenForScreenAvailabilityAndWait(presentation_url1_, false);
354 base::RunLoop().RunUntilIdle();
355 }
356
394 TEST_F(PresentationServiceImplTest, Reset) { 357 TEST_F(PresentationServiceImplTest, Reset) {
395 ListenForScreenAvailabilityAndWait(presentation_url1_, true); 358 ListenForScreenAvailabilityAndWait(presentation_url1_, true);
396 359
397 ExpectReset(); 360 ExpectReset();
398 service_impl_->Reset(); 361 service_impl_->Reset();
399 ExpectCleanState(); 362 ExpectCleanState();
400 } 363 }
401 364
402 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { 365 TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) {
403 ListenForScreenAvailabilityAndWait(presentation_url1_, true); 366 ListenForScreenAvailabilityAndWait(presentation_url1_, true);
(...skipping 14 matching lines...) Expand all
418 } 381 }
419 382
420 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) { 383 TEST_F(PresentationServiceImplTest, ThisRenderFrameDeleted) {
421 ListenForScreenAvailabilityAndWait(presentation_url1_, true); 384 ListenForScreenAvailabilityAndWait(presentation_url1_, true);
422 385
423 ExpectReset(); 386 ExpectReset();
424 387
425 // Since the frame matched the service, |service_impl_| will be deleted. 388 // Since the frame matched the service, |service_impl_| will be deleted.
426 PresentationServiceImpl* service = service_impl_.release(); 389 PresentationServiceImpl* service = service_impl_.release();
427 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); 390 EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1);
428 service->RenderFrameDeleted(contents()->GetMainFrame()); 391 service->RenderFrameDeleted(main_rfh());
429 } 392 }
430 393
431 TEST_F(PresentationServiceImplTest, OtherRenderFrameDeleted) { 394 TEST_F(PresentationServiceImplTest, OtherRenderFrameDeleted) {
432 ListenForScreenAvailabilityAndWait(presentation_url1_, true); 395 ListenForScreenAvailabilityAndWait(presentation_url1_, true);
433 396
434 // TODO(imcheng): How to get a different RenderFrameHost? 397 // Create a new frame and RFH.
435 service_impl_->RenderFrameDeleted(nullptr); 398 RenderFrameHost* rfh = main_rfh();
399 RenderFrameHostTester* rfh_tester = RenderFrameHostTester::For(rfh);
400 rfh = rfh_tester->AppendChild("subframe");
401 service_impl_->RenderFrameDeleted(rfh);
436 402
437 // Availability is reported and callback should be invoked since listener 403 // Availability is reported and callback should be invoked since listener
438 // has not been deleted. 404 // has not been deleted.
439 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, true); 405 SimulateScreenAvailabilityChangeAndWait(presentation_url1_, true);
440 } 406 }
441 407
442 TEST_F(PresentationServiceImplTest, DelegateFails) { 408 TEST_F(PresentationServiceImplTest, DelegateFails) {
443 ListenForScreenAvailabilityAndWait(presentation_url1_, false); 409 ListenForScreenAvailabilityAndWait(presentation_url1_, false);
444 ASSERT_EQ( 410 ASSERT_EQ(
445 service_impl_->screen_availability_listeners_.find(presentation_url1_), 411 service_impl_->screen_availability_listeners_.end(),
446 service_impl_->screen_availability_listeners_.end()); 412 service_impl_->screen_availability_listeners_.find(presentation_url1_));
447 } 413 }
448 414
449 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrls) { 415 TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrls) {
450 EXPECT_CALL(mock_delegate_, 416 EXPECT_CALL(mock_delegate_,
451 SetDefaultPresentationUrls(_, _, presentation_urls_, _)) 417 SetDefaultPresentationUrls(_, _, presentation_urls_, _))
452 .Times(1); 418 .Times(1);
453 419
454 service_impl_->SetDefaultPresentationUrls(presentation_urls_); 420 service_impl_->SetDefaultPresentationUrls(presentation_urls_);
455 421
456 // Sets different DPUs. 422 // Sets different DPUs.
457 std::vector<GURL> more_urls = presentation_urls_; 423 std::vector<GURL> more_urls = presentation_urls_;
458 more_urls.push_back(presentation_url3_); 424 more_urls.push_back(presentation_url3_);
459 425
460 PresentationConnectionCallback callback; 426 PresentationConnectionCallback callback;
461 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls(_, _, more_urls, _)) 427 EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls(_, _, more_urls, _))
462 .WillOnce(SaveArg<3>(&callback)); 428 .WillOnce(SaveArg<3>(&callback));
463 service_impl_->SetDefaultPresentationUrls(more_urls); 429 service_impl_->SetDefaultPresentationUrls(more_urls);
464 430
465 PresentationInfo presentation_info(presentation_url2_, kPresentationId); 431 PresentationInfo presentation_info(presentation_url2_, kPresentationId);
466 432
467 base::RunLoop run_loop;
468 EXPECT_CALL(mock_client_, 433 EXPECT_CALL(mock_client_,
469 OnDefaultPresentationStarted(InfoEquals(presentation_info))) 434 OnDefaultPresentationStarted(InfoEquals(presentation_info)));
470 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
471 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)); 435 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _));
472 callback.Run(PresentationInfo(presentation_url2_, kPresentationId)); 436 callback.Run(PresentationInfo(presentation_url2_, kPresentationId));
473 run_loop.Run(); 437 base::RunLoop().RunUntilIdle();
474 } 438 }
475 439
476 TEST_F(PresentationServiceImplTest, ListenForConnectionStateChange) { 440 TEST_F(PresentationServiceImplTest, ListenForConnectionStateChange) {
477 PresentationInfo connection(presentation_url1_, kPresentationId); 441 PresentationInfo connection(presentation_url1_, kPresentationId);
478 PresentationConnectionStateChangedCallback state_changed_cb; 442 PresentationConnectionStateChangedCallback state_changed_cb;
479 // Trigger state change. It should be propagated back up to |mock_client_|. 443 // Trigger state change. It should be propagated back up to |mock_client_|.
480 PresentationInfo presentation_connection(presentation_url1_, kPresentationId); 444 PresentationInfo presentation_connection(presentation_url1_, kPresentationId);
481 445
482 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) 446 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _))
483 .WillOnce(SaveArg<3>(&state_changed_cb)); 447 .WillOnce(SaveArg<3>(&state_changed_cb));
484 service_impl_->ListenForConnectionStateChange(connection); 448 service_impl_->ListenForConnectionStateChange(connection);
485 449
486 { 450 EXPECT_CALL(mock_client_, OnConnectionStateChanged(
487 base::RunLoop run_loop; 451 InfoEquals(presentation_connection),
488 EXPECT_CALL(mock_client_, OnConnectionStateChanged( 452 PRESENTATION_CONNECTION_STATE_TERMINATED));
489 InfoEquals(presentation_connection), 453 state_changed_cb.Run(PresentationConnectionStateChangeInfo(
490 PRESENTATION_CONNECTION_STATE_TERMINATED)) 454 PRESENTATION_CONNECTION_STATE_TERMINATED));
491 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); 455 base::RunLoop().RunUntilIdle();
492 state_changed_cb.Run(PresentationConnectionStateChangeInfo(
493 PRESENTATION_CONNECTION_STATE_TERMINATED));
494 run_loop.Run();
495 }
496 } 456 }
497 457
498 TEST_F(PresentationServiceImplTest, ListenForConnectionClose) { 458 TEST_F(PresentationServiceImplTest, ListenForConnectionClose) {
499 PresentationInfo connection(presentation_url1_, kPresentationId); 459 PresentationInfo connection(presentation_url1_, kPresentationId);
500 PresentationConnectionStateChangedCallback state_changed_cb; 460 PresentationConnectionStateChangedCallback state_changed_cb;
501 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) 461 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _))
502 .WillOnce(SaveArg<3>(&state_changed_cb)); 462 .WillOnce(SaveArg<3>(&state_changed_cb));
503 service_impl_->ListenForConnectionStateChange(connection); 463 service_impl_->ListenForConnectionStateChange(connection);
504 464
505 // Trigger connection close. It should be propagated back up to 465 // Trigger connection close. It should be propagated back up to
506 // |mock_client_|. 466 // |mock_client_|.
507 PresentationInfo presentation_connection(presentation_url1_, kPresentationId); 467 PresentationInfo presentation_connection(presentation_url1_, kPresentationId);
508 { 468 PresentationConnectionStateChangeInfo closed_info(
509 base::RunLoop run_loop; 469 PRESENTATION_CONNECTION_STATE_CLOSED);
510 PresentationConnectionStateChangeInfo closed_info( 470 closed_info.close_reason = PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY;
511 PRESENTATION_CONNECTION_STATE_CLOSED); 471 closed_info.message = "Foo";
512 closed_info.close_reason = PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY;
513 closed_info.message = "Foo";
514 472
515 EXPECT_CALL(mock_client_, 473 EXPECT_CALL(mock_client_,
516 OnConnectionClosed( 474 OnConnectionClosed(InfoEquals(presentation_connection),
517 InfoEquals(presentation_connection), 475 PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY,
518 PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY, "Foo")) 476 "Foo"));
519 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); 477 state_changed_cb.Run(closed_info);
520 state_changed_cb.Run(closed_info); 478 base::RunLoop().RunUntilIdle();
521 run_loop.Run();
522 }
523 } 479 }
524 480
525 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrls) { 481 TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrls) {
526 EXPECT_CALL(mock_delegate_, 482 EXPECT_CALL(mock_delegate_,
527 SetDefaultPresentationUrls(_, _, presentation_urls_, _)) 483 SetDefaultPresentationUrls(_, _, presentation_urls_, _))
528 .Times(1); 484 .Times(1);
529 service_impl_->SetDefaultPresentationUrls(presentation_urls_); 485 service_impl_->SetDefaultPresentationUrls(presentation_urls_);
530 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); 486 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
531 487
532 // Same URLs as before; no-ops. 488 // Same URLs as before; no-ops.
533 service_impl_->SetDefaultPresentationUrls(presentation_urls_); 489 service_impl_->SetDefaultPresentationUrls(presentation_urls_);
534 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); 490 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_));
535 } 491 }
536 492
537 TEST_F(PresentationServiceImplTest, StartPresentationSuccess) { 493 TEST_F(PresentationServiceImplTest, StartPresentationSuccess) {
538 service_ptr_->StartPresentation( 494 base::Callback<void(const PresentationInfo&)> success_cb;
495 EXPECT_CALL(mock_delegate_, StartPresentation(_, _, presentation_urls_, _, _))
496 .WillOnce(SaveArg<3>(&success_cb));
497 service_impl_->StartPresentation(
539 presentation_urls_, 498 presentation_urls_,
540 base::Bind( 499 base::Bind(
541 &PresentationServiceImplTest::ExpectNewPresentationCallbackSuccess, 500 &PresentationServiceImplTest::ExpectNewPresentationCallbackSuccess,
542 base::Unretained(this))); 501 base::Unretained(this)));
543 base::RunLoop run_loop; 502 EXPECT_FALSE(success_cb.is_null());
544 base::Callback<void(const PresentationInfo&)> success_cb;
545 EXPECT_CALL(mock_delegate_, StartPresentation(_, _, presentation_urls_, _, _))
546 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
547 SaveArg<3>(&success_cb)));
548 run_loop.Run();
549
550 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) 503 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _))
551 .Times(1); 504 .Times(1);
552 success_cb.Run(PresentationInfo(presentation_url1_, kPresentationId)); 505 success_cb.Run(PresentationInfo(presentation_url1_, kPresentationId));
553 SaveQuitClosureAndRunLoop(); 506 EXPECT_EQ(1, presentation_success_count_);
554 } 507 }
555 508
556 TEST_F(PresentationServiceImplTest, StartPresentationError) { 509 TEST_F(PresentationServiceImplTest, StartPresentationError) {
557 service_ptr_->StartPresentation( 510 base::Callback<void(const PresentationError&)> error_cb;
511 EXPECT_CALL(mock_delegate_, StartPresentation(_, _, presentation_urls_, _, _))
512 .WillOnce(SaveArg<4>(&error_cb));
513 service_impl_->StartPresentation(
558 presentation_urls_, 514 presentation_urls_,
559 base::Bind( 515 base::Bind(
560 &PresentationServiceImplTest::ExpectNewPresentationCallbackError, 516 &PresentationServiceImplTest::ExpectNewPresentationCallbackError,
561 base::Unretained(this))); 517 base::Unretained(this)));
562 base::RunLoop run_loop; 518 EXPECT_FALSE(error_cb.is_null());
563 base::Callback<void(const PresentationError&)> error_cb; 519 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
520 EXPECT_EQ(1, presentation_error_count_);
521 }
522
523 TEST_F(PresentationServiceImplTest, StartPresentationInProgress) {
564 EXPECT_CALL(mock_delegate_, StartPresentation(_, _, presentation_urls_, _, _)) 524 EXPECT_CALL(mock_delegate_, StartPresentation(_, _, presentation_urls_, _, _))
565 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit), 525 .Times(1);
566 SaveArg<4>(&error_cb))); 526 service_impl_->StartPresentation(presentation_urls_, base::Bind(&DoNothing));
567 run_loop.Run(); 527 EXPECT_EQ(0, presentation_error_count_);
568 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message")); 528
569 SaveQuitClosureAndRunLoop(); 529 // This request should fail immediately, since there is already a
530 // StartPresentation in progress.
531 service_impl_->StartPresentation(
532 presentation_urls_,
533 base::Bind(
534 &PresentationServiceImplTest::ExpectNewPresentationCallbackError,
535 base::Unretained(this)));
536 EXPECT_EQ(1, presentation_error_count_);
570 } 537 }
571 538
572 TEST_F(PresentationServiceImplTest, ReconnectPresentationSuccess) { 539 TEST_F(PresentationServiceImplTest, ReconnectPresentationSuccess) {
573 service_ptr_->ReconnectPresentation( 540 base::Callback<void(const PresentationInfo&)> success_cb;
541 EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, presentation_urls_,
542 kPresentationId, _, _))
543 .WillOnce(SaveArg<4>(&success_cb));
544 service_impl_->ReconnectPresentation(
574 presentation_urls_, base::Optional<std::string>(kPresentationId), 545 presentation_urls_, base::Optional<std::string>(kPresentationId),
575 base::Bind( 546 base::Bind(
576 &PresentationServiceImplTest::ExpectNewPresentationCallbackSuccess, 547 &PresentationServiceImplTest::ExpectNewPresentationCallbackSuccess,
577 base::Unretained(this))); 548 base::Unretained(this)));
578 base::RunLoop run_loop; 549 EXPECT_FALSE(success_cb.is_null());
579 base::Callback<void(const PresentationInfo&)> success_cb;
580 EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, presentation_urls_,
581 kPresentationId, _, _))
582 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
583 SaveArg<4>(&success_cb)));
584 run_loop.Run();
585
586 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) 550 EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _))
587 .Times(1); 551 .Times(1);
588 success_cb.Run(PresentationInfo(presentation_url1_, kPresentationId)); 552 success_cb.Run(PresentationInfo(presentation_url1_, kPresentationId));
589 SaveQuitClosureAndRunLoop(); 553 EXPECT_EQ(1, presentation_success_count_);
590 } 554 }
591 555
592 TEST_F(PresentationServiceImplTest, ReconnectPresentationError) { 556 TEST_F(PresentationServiceImplTest, ReconnectPresentationError) {
593 service_ptr_->ReconnectPresentation( 557 base::Callback<void(const PresentationError&)> error_cb;
558 EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, presentation_urls_,
559 kPresentationId, _, _))
560 .WillOnce(SaveArg<5>(&error_cb));
561 service_impl_->ReconnectPresentation(
594 presentation_urls_, base::Optional<std::string>(kPresentationId), 562 presentation_urls_, base::Optional<std::string>(kPresentationId),
595 base::Bind( 563 base::Bind(
596 &PresentationServiceImplTest::ExpectNewPresentationCallbackError, 564 &PresentationServiceImplTest::ExpectNewPresentationCallbackError,
597 base::Unretained(this))); 565 base::Unretained(this)));
598 base::RunLoop run_loop; 566 EXPECT_FALSE(error_cb.is_null());
599 base::Callback<void(const PresentationError&)> error_cb;
600 EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, presentation_urls_,
601 kPresentationId, _, _))
602 .WillOnce(DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
603 SaveArg<5>(&error_cb)));
604 run_loop.Run();
605 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message")); 567 error_cb.Run(PresentationError(PRESENTATION_ERROR_UNKNOWN, "Error message"));
606 SaveQuitClosureAndRunLoop(); 568 EXPECT_EQ(1, presentation_error_count_);
569 }
570
571 TEST_F(PresentationServiceImplTest, MaxPendingReconnectPresentationRequests) {
572 const char* presentation_url = "http://fooUrl%d";
573 const char* presentation_id = "presentationId%d";
574 int num_requests = PresentationServiceImpl::kMaxQueuedRequests;
575 int i = 0;
576 EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, _, _, _, _))
577 .Times(num_requests);
578 for (; i < num_requests; ++i) {
579 std::vector<GURL> urls = {GURL(base::StringPrintf(presentation_url, i))};
580 service_impl_->ReconnectPresentation(
581 urls, base::StringPrintf(presentation_id, i), base::Bind(&DoNothing));
582 }
583
584 EXPECT_EQ(0, presentation_error_count_);
585 std::vector<GURL> urls = {GURL(base::StringPrintf(presentation_url, i))};
586 // Exceeded maximum queue size, should invoke mojo callback with error.
587 service_impl_->ReconnectPresentation(
588 urls, base::StringPrintf(presentation_id, i),
589 base::Bind(
590 &PresentationServiceImplTest::ExpectNewPresentationCallbackError,
591 base::Unretained(this)));
592 EXPECT_EQ(1, presentation_error_count_);
607 } 593 }
608 594
609 TEST_F(PresentationServiceImplTest, CloseConnection) { 595 TEST_F(PresentationServiceImplTest, CloseConnection) {
610 service_ptr_->CloseConnection(presentation_url1_, kPresentationId); 596 EXPECT_CALL(mock_delegate_, CloseConnection(_, _, Eq(kPresentationId)));
611 597 service_impl_->CloseConnection(presentation_url1_, kPresentationId);
612 base::RunLoop run_loop;
613 EXPECT_CALL(mock_delegate_, CloseConnection(_, _, Eq(kPresentationId)))
614 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
615 run_loop.Run();
616 } 598 }
617 599
618 TEST_F(PresentationServiceImplTest, Terminate) { 600 TEST_F(PresentationServiceImplTest, Terminate) {
619 service_ptr_->Terminate(presentation_url1_, kPresentationId); 601 EXPECT_CALL(mock_delegate_, Terminate(_, _, Eq(kPresentationId)));
620 base::RunLoop run_loop; 602 service_impl_->Terminate(presentation_url1_, kPresentationId);
621 EXPECT_CALL(mock_delegate_, Terminate(_, _, Eq(kPresentationId)))
622 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
623 run_loop.Run();
624 } 603 }
625 604
626 TEST_F(PresentationServiceImplTest, SetPresentationConnection) { 605 TEST_F(PresentationServiceImplTest, SetPresentationConnection) {
627 PresentationInfo presentation_info(presentation_url1_, kPresentationId); 606 PresentationInfo presentation_info(presentation_url1_, kPresentationId);
628 607
629 blink::mojom::PresentationConnectionPtr connection; 608 blink::mojom::PresentationConnectionPtr connection;
630 MockPresentationConnection mock_presentation_connection; 609 MockPresentationConnection mock_presentation_connection;
631 mojo::Binding<blink::mojom::PresentationConnection> connection_binding( 610 mojo::Binding<blink::mojom::PresentationConnection> connection_binding(
632 &mock_presentation_connection, mojo::MakeRequest(&connection)); 611 &mock_presentation_connection, mojo::MakeRequest(&connection));
633 blink::mojom::PresentationConnectionPtr receiver_connection; 612 blink::mojom::PresentationConnectionPtr receiver_connection;
634 auto request = mojo::MakeRequest(&receiver_connection); 613 auto request = mojo::MakeRequest(&receiver_connection);
635 614
636 PresentationInfo expected(presentation_url1_, kPresentationId); 615 PresentationInfo expected(presentation_url1_, kPresentationId);
637 EXPECT_CALL(mock_delegate_, RegisterOffscreenPresentationConnectionRaw( 616 EXPECT_CALL(mock_delegate_, RegisterOffscreenPresentationConnectionRaw(
638 _, _, InfoEquals(expected), _)); 617 _, _, InfoEquals(expected), _));
639 618
640 service_impl_->SetPresentationConnection( 619 service_impl_->SetPresentationConnection(
641 presentation_info, std::move(connection), std::move(request)); 620 presentation_info, std::move(connection), std::move(request));
642 } 621 }
643 622
644 TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) { 623 TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) {
645 MockReceiverPresentationServiceDelegate mock_receiver_delegate; 624 MockReceiverPresentationServiceDelegate mock_receiver_delegate;
625 EXPECT_CALL(mock_receiver_delegate, AddObserver(_, _, _)).Times(1);
646 626
647 PresentationServiceImpl service_impl(contents()->GetMainFrame(), contents(), 627 PresentationServiceImpl service_impl(main_rfh(), contents(), nullptr,
648 nullptr, &mock_receiver_delegate); 628 &mock_receiver_delegate);
649 629
650 ReceiverConnectionAvailableCallback callback; 630 ReceiverConnectionAvailableCallback callback;
651 EXPECT_CALL(mock_receiver_delegate, 631 EXPECT_CALL(mock_receiver_delegate,
652 RegisterReceiverConnectionAvailableCallback(_)) 632 RegisterReceiverConnectionAvailableCallback(_))
653 .WillOnce(SaveArg<0>(&callback)); 633 .WillOnce(SaveArg<0>(&callback));
654 634
655 blink::mojom::PresentationServiceClientPtr client_ptr; 635 blink::mojom::PresentationServiceClientPtr client_ptr;
656 client_binding_.reset( 636 client_binding_.reset(
657 new mojo::Binding<blink::mojom::PresentationServiceClient>( 637 new mojo::Binding<blink::mojom::PresentationServiceClient>(
658 &mock_client_, mojo::MakeRequest(&client_ptr))); 638 &mock_client_, mojo::MakeRequest(&client_ptr)));
659 service_impl.controller_delegate_ = nullptr; 639 service_impl.controller_delegate_ = nullptr;
660 service_impl.SetClient(std::move(client_ptr)); 640 service_impl.SetClient(std::move(client_ptr));
661 EXPECT_FALSE(callback.is_null()); 641 EXPECT_FALSE(callback.is_null());
662 642
663 // NO-OP for ControllerPresentationServiceDelegate API functions 643 // NO-OP for ControllerPresentationServiceDelegate API functions
644 PresentationInfo presentation_info(presentation_url1_, kPresentationId);
664 EXPECT_CALL(mock_delegate_, ListenForConnectionMessages(_, _, _, _)).Times(0); 645 EXPECT_CALL(mock_delegate_, ListenForConnectionMessages(_, _, _, _)).Times(0);
646 service_impl.ListenForConnectionMessages(presentation_info);
665 647
666 PresentationInfo presentation_info(presentation_url1_, kPresentationId); 648 // Client gets notified of receiver connections.
667 service_impl.ListenForConnectionMessages(presentation_info); 649 blink::mojom::PresentationConnectionPtr controller_connection;
668 } 650 MockPresentationConnection mock_presentation_connection;
651 mojo::Binding<blink::mojom::PresentationConnection> connection_binding(
652 &mock_presentation_connection, mojo::MakeRequest(&controller_connection));
653 blink::mojom::PresentationConnectionPtr receiver_connection;
654 EXPECT_CALL(mock_client_,
655 OnReceiverConnectionAvailable(InfoEquals(presentation_info)));
656 callback.Run(presentation_info, std::move(controller_connection),
657 mojo::MakeRequest(&receiver_connection));
658 base::RunLoop().RunUntilIdle();
669 659
670 TEST_F(PresentationServiceImplTest, StartPresentationInProgress) { 660 EXPECT_CALL(mock_receiver_delegate, RemoveObserver(_, _)).Times(1);
671 EXPECT_CALL(mock_delegate_, StartPresentation(_, _, presentation_urls_, _, _))
672 .Times(1);
673 service_ptr_->StartPresentation(presentation_urls_, base::Bind(&DoNothing));
674
675 // This request should fail immediately, since there is already a
676 // StartPresentation in progress.
677 service_ptr_->StartPresentation(
678 presentation_urls_,
679 base::Bind(
680 &PresentationServiceImplTest::ExpectNewPresentationCallbackError,
681 base::Unretained(this)));
682 SaveQuitClosureAndRunLoop();
683 }
684
685 TEST_F(PresentationServiceImplTest, MaxPendingReconnectPresentationRequests) {
686 const char* presentation_url = "http://fooUrl%d";
687 const char* presentation_id = "presentationId%d";
688 int num_requests = PresentationServiceImpl::kMaxQueuedRequests;
689 int i = 0;
690 EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, _, _, _, _))
691 .Times(num_requests);
692 for (; i < num_requests; ++i) {
693 std::vector<GURL> urls = {GURL(base::StringPrintf(presentation_url, i))};
694 service_ptr_->ReconnectPresentation(
695 urls, base::StringPrintf(presentation_id, i), base::Bind(&DoNothing));
696 }
697
698 std::vector<GURL> urls = {GURL(base::StringPrintf(presentation_url, i))};
699 // Exceeded maximum queue size, should invoke mojo callback with error.
700 service_ptr_->ReconnectPresentation(
701 urls, base::StringPrintf(presentation_id, i),
702 base::Bind(
703 &PresentationServiceImplTest::ExpectNewPresentationCallbackError,
704 base::Unretained(this)));
705 SaveQuitClosureAndRunLoop();
706 }
707
708 TEST_F(PresentationServiceImplTest, ScreenAvailabilityNotSupported) {
709 mock_delegate_.set_screen_availability_listening_supported(false);
710 base::RunLoop run_loop;
711 EXPECT_CALL(mock_client_,
712 OnScreenAvailabilityNotSupported(presentation_url1_))
713 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
714 ListenForScreenAvailabilityAndWait(presentation_url1_, false);
715 run_loop.Run();
716 } 661 }
717 662
718 } // namespace content 663 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/presentation/presentation_service_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698