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

Side by Side Diff: content/browser/renderer_host/media/web_contents_video_capture_device_unittest.cc

Issue 14197014: Add TestBrowserThreadBundle into RenderViewHostTestHarness. Kill some unnecessary real threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: once more unto the try serves. Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/renderer_host/media/web_contents_video_capture_device. h" 5 #include "content/browser/renderer_host/media/web_contents_video_capture_device. h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/debug/debugger.h" 8 #include "base/debug/debugger.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_timeouts.h" 10 #include "base/test/test_timeouts.h"
11 #include "base/time.h" 11 #include "base/time.h"
12 #include "base/timer.h" 12 #include "base/timer.h"
13 #include "content/browser/browser_thread_impl.h" 13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" 14 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
15 #include "content/browser/renderer_host/media/video_capture_oracle.h" 15 #include "content/browser/renderer_host/media/video_capture_oracle.h"
16 #include "content/browser/renderer_host/media/web_contents_capture_util.h" 16 #include "content/browser/renderer_host/media/web_contents_capture_util.h"
17 #include "content/browser/renderer_host/render_view_host_factory.h" 17 #include "content/browser/renderer_host/render_view_host_factory.h"
18 #include "content/browser/renderer_host/render_widget_host_impl.h" 18 #include "content/browser/renderer_host/render_widget_host_impl.h"
19 #include "content/browser/renderer_host/test_render_view_host.h" 19 #include "content/browser/renderer_host/test_render_view_host.h"
20 #include "content/port/browser/render_widget_host_view_frame_subscriber.h" 20 #include "content/port/browser/render_widget_host_view_frame_subscriber.h"
21 #include "content/public/browser/notification_service.h" 21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h" 22 #include "content/public/browser/notification_types.h"
23 #include "content/public/test/mock_render_process_host.h" 23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h" 24 #include "content/public/test/test_browser_context.h"
25 #include "content/public/test/test_browser_thread.h" 25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h" 26 #include "content/public/test/test_utils.h"
27 #include "content/test/test_web_contents.h" 27 #include "content/test/test_web_contents.h"
28 #include "media/base/video_util.h" 28 #include "media/base/video_util.h"
29 #include "media/base/yuv_convert.h" 29 #include "media/base/yuv_convert.h"
30 #include "media/video/capture/video_capture_types.h" 30 #include "media/video/capture/video_capture_types.h"
31 #include "skia/ext/platform_canvas.h" 31 #include "skia/ext/platform_canvas.h"
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/skia/include/core/SkColor.h" 33 #include "third_party/skia/include/core/SkColor.h"
34 34
35 namespace content { 35 namespace content {
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 bool error_encountered_; 410 bool error_encountered_;
411 SkColor wait_color_yuv_; 411 SkColor wait_color_yuv_;
412 scoped_refptr<VideoCaptureBufferPool> buffer_pool_; 412 scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
413 413
414 DISALLOW_COPY_AND_ASSIGN(StubConsumer); 414 DISALLOW_COPY_AND_ASSIGN(StubConsumer);
415 }; 415 };
416 416
417 // Test harness that sets up a minimal environment with necessary stubs. 417 // Test harness that sets up a minimal environment with necessary stubs.
418 class WebContentsVideoCaptureDeviceTest : public testing::Test { 418 class WebContentsVideoCaptureDeviceTest : public testing::Test {
419 public: 419 public:
420 WebContentsVideoCaptureDeviceTest() {} 420 // This is public because C++ method pointer scoping rules are silly and make
421 421 // this hard to use with Bind().
422 void ResetWebContents() { 422 void ResetWebContents() {
423 web_contents_.reset(); 423 web_contents_.reset();
424 } 424 }
425 425
426 protected: 426 protected:
427 virtual void SetUp() { 427 virtual void SetUp() {
428 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be 428 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
429 // eliminated here, if only we could use RenderViewHostTestHarness. The 429 // eliminated here, if only we could use RenderViewHostTestHarness. The
430 // catch is that we need our TestRenderViewHost to support a 430 // catch is that we need our TestRenderViewHost to support a
431 // CopyFromBackingStore operation that we control. To accomplish that, 431 // CopyFromBackingStore operation that we control. To accomplish that,
432 // either RenderViewHostTestHarness would have to support installing a 432 // either RenderViewHostTestHarness would have to support installing a
433 // custom RenderViewHostFactory, or else we implant some kind of delegated 433 // custom RenderViewHostFactory, or else we implant some kind of delegated
434 // CopyFromBackingStore functionality into TestRenderViewHost itself. 434 // CopyFromBackingStore functionality into TestRenderViewHost itself.
435 435
436 // The main thread will serve as the UI thread as well as the test thread.
437 // We'll manually pump the run loop at appropriate times in the test.
438 ui_thread_.reset(new TestBrowserThread(BrowserThread::UI, &message_loop_));
439
440 render_process_host_factory_.reset(new MockRenderProcessHostFactory()); 436 render_process_host_factory_.reset(new MockRenderProcessHostFactory());
441 // Create our (self-registering) RVH factory, so that when we create a 437 // Create our (self-registering) RVH factory, so that when we create a
442 // WebContents, it in turn creates CaptureTestRenderViewHosts. 438 // WebContents, it in turn creates CaptureTestRenderViewHosts.
443 render_view_host_factory_.reset( 439 render_view_host_factory_.reset(
444 new CaptureTestRenderViewHostFactory(&controller_)); 440 new CaptureTestRenderViewHostFactory(&controller_));
445 441
446 browser_context_.reset(new TestBrowserContext()); 442 browser_context_.reset(new TestBrowserContext());
447 443
448 scoped_refptr<SiteInstance> site_instance = 444 scoped_refptr<SiteInstance> site_instance =
449 SiteInstance::Create(browser_context_.get()); 445 SiteInstance::Create(browser_context_.get());
450 static_cast<SiteInstanceImpl*>(site_instance.get())-> 446 static_cast<SiteInstanceImpl*>(site_instance.get())->
451 set_render_process_host_factory(render_process_host_factory_.get()); 447 set_render_process_host_factory(render_process_host_factory_.get());
452 web_contents_.reset( 448 web_contents_.reset(
453 TestWebContents::Create(browser_context_.get(), site_instance.get())); 449 TestWebContents::Create(browser_context_.get(), site_instance.get()));
454 450
455 // This is actually a CaptureTestRenderViewHost. 451 // This is actually a CaptureTestRenderViewHost.
456 RenderWidgetHostImpl* rwh = 452 RenderWidgetHostImpl* rwh =
457 RenderWidgetHostImpl::From(web_contents_->GetRenderViewHost()); 453 RenderWidgetHostImpl::From(web_contents_->GetRenderViewHost());
458 454
459 std::string device_id = 455 std::string device_id =
460 WebContentsCaptureUtil::AppendWebContentsDeviceScheme( 456 WebContentsCaptureUtil::AppendWebContentsDeviceScheme(
461 base::StringPrintf("%d:%d", rwh->GetProcess()->GetID(), 457 base::StringPrintf("%d:%d", rwh->GetProcess()->GetID(),
462 rwh->GetRoutingID())); 458 rwh->GetRoutingID()));
463 459
464 device_.reset(WebContentsVideoCaptureDevice::Create(device_id)); 460 device_.reset(WebContentsVideoCaptureDevice::Create(device_id));
465 461
466 content::RunAllPendingInMessageLoop(); 462 base::RunLoop().RunUntilIdle();
467 } 463 }
468 464
469 virtual void TearDown() { 465 virtual void TearDown() {
470 // Tear down in opposite order of set-up. 466 // Tear down in opposite order of set-up.
471 467
472 // The device is destroyed asynchronously, and will notify the 468 // The device is destroyed asynchronously, and will notify the
473 // CaptureTestSourceController when it finishes destruction. 469 // CaptureTestSourceController when it finishes destruction.
474 // Trigger this, and wait. 470 // Trigger this, and wait.
475 if (device_) { 471 if (device_) {
476 device_->DeAllocate(); 472 device_->DeAllocate();
477 device_.reset(); 473 device_.reset();
478 } 474 }
479 475
480 content::RunAllPendingInMessageLoop(); 476 base::RunLoop().RunUntilIdle();
481 477
482 // Destroy the browser objects. 478 // Destroy the browser objects.
483 web_contents_.reset(); 479 web_contents_.reset();
484 browser_context_.reset(); 480 browser_context_.reset();
485 481
486 content::RunAllPendingInMessageLoop(); 482 base::RunLoop().RunUntilIdle();
487 483
488 render_view_host_factory_.reset(); 484 render_view_host_factory_.reset();
489 render_process_host_factory_.reset(); 485 render_process_host_factory_.reset();
490 } 486 }
491 487
492 // Accessors. 488 // Accessors.
493 CaptureTestSourceController* source() { return &controller_; } 489 CaptureTestSourceController* source() { return &controller_; }
494 media::VideoCaptureDevice* device() { return device_.get(); } 490 media::VideoCaptureDevice* device() { return device_.get(); }
495 StubConsumer* consumer() { return &consumer_; } 491 StubConsumer* consumer() { return &consumer_; }
496 492
(...skipping 14 matching lines...) Expand all
511 507
512 void DestroyVideoCaptureDevice() { device_.reset(); } 508 void DestroyVideoCaptureDevice() { device_.reset(); }
513 509
514 private: 510 private:
515 // The consumer is the ultimate recipient of captured pixel data. 511 // The consumer is the ultimate recipient of captured pixel data.
516 StubConsumer consumer_; 512 StubConsumer consumer_;
517 513
518 // The controller controls which pixel patterns to produce. 514 // The controller controls which pixel patterns to produce.
519 CaptureTestSourceController controller_; 515 CaptureTestSourceController controller_;
520 516
521 // We run the UI message loop on the main thread. The capture device
522 // will also spin up its own threads.
523 base::MessageLoopForUI message_loop_;
524 scoped_ptr<TestBrowserThread> ui_thread_;
525
526 // Self-registering RenderProcessHostFactory. 517 // Self-registering RenderProcessHostFactory.
527 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_; 518 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_;
528 519
529 // Creates capture-capable RenderViewHosts whose pixel content production is 520 // Creates capture-capable RenderViewHosts whose pixel content production is
530 // under the control of |controller_|. 521 // under the control of |controller_|.
531 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_; 522 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_;
532 523
533 // A mocked-out browser and tab. 524 // A mocked-out browser and tab.
534 scoped_ptr<TestBrowserContext> browser_context_; 525 scoped_ptr<TestBrowserContext> browser_context_;
535 scoped_ptr<WebContents> web_contents_; 526 scoped_ptr<WebContents> web_contents_;
536 527
537 // Finally, the WebContentsVideoCaptureDevice under test. 528 // Finally, the WebContentsVideoCaptureDevice under test.
538 scoped_ptr<media::VideoCaptureDevice> device_; 529 scoped_ptr<media::VideoCaptureDevice> device_;
539 530
540 DISALLOW_COPY_AND_ASSIGN(WebContentsVideoCaptureDeviceTest); 531 TestBrowserThreadBundle thread_bundle_;
541 }; 532 };
542 533
543 TEST_F(WebContentsVideoCaptureDeviceTest, InvalidInitialWebContentsError) { 534 TEST_F(WebContentsVideoCaptureDeviceTest, InvalidInitialWebContentsError) {
544 // Before the installs itself on the UI thread up to start capturing, we'll 535 // Before the installs itself on the UI thread up to start capturing, we'll
545 // delete the web contents. This should trigger an error which can happen in 536 // delete the web contents. This should trigger an error which can happen in
546 // practice; we should be able to recover gracefully. 537 // practice; we should be able to recover gracefully.
547 ResetWebContents(); 538 ResetWebContents();
548 539
549 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer()); 540 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
550 device()->Start(); 541 device()->Start();
551 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError()); 542 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError());
552 device()->DeAllocate(); 543 device()->DeAllocate();
553 } 544 }
554 545
555 TEST_F(WebContentsVideoCaptureDeviceTest, WebContentsDestroyed) { 546 TEST_F(WebContentsVideoCaptureDeviceTest, WebContentsDestroyed) {
556 // We'll simulate the tab being closed after the capture pipeline is up and 547 // We'll simulate the tab being closed after the capture pipeline is up and
557 // running. 548 // running.
558 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer()); 549 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
559 device()->Start(); 550 device()->Start();
560 551
561 // Do one capture to prove 552 // Do one capture to prove
562 source()->SetSolidColor(SK_ColorRED); 553 source()->SetSolidColor(SK_ColorRED);
563 SimulateDrawEvent(); 554 SimulateDrawEvent();
564 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED)); 555 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
565 556
566 content::RunAllPendingInMessageLoop(); 557 base::RunLoop().RunUntilIdle();
567 558
568 // Post a task to close the tab. We should see an error reported to the 559 // Post a task to close the tab. We should see an error reported to the
569 // consumer. 560 // consumer.
570 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 561 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
571 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents, 562 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents,
572 base::Unretained(this))); 563 base::Unretained(this)));
573 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError()); 564 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError());
574 device()->DeAllocate(); 565 device()->DeAllocate();
575 } 566 }
576 567
577 TEST_F(WebContentsVideoCaptureDeviceTest, 568 TEST_F(WebContentsVideoCaptureDeviceTest,
578 StopDeviceBeforeCaptureMachineCreation) { 569 StopDeviceBeforeCaptureMachineCreation) {
579 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer()); 570 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
580 device()->Start(); 571 device()->Start();
581 // Make a point of not running the UI messageloop here. 572 // Make a point of not running the UI messageloop here.
582 device()->Stop(); 573 device()->Stop();
583 device()->DeAllocate(); 574 device()->DeAllocate();
584 DestroyVideoCaptureDevice(); 575 DestroyVideoCaptureDevice();
585 576
586 // Currently, there should be CreateCaptureMachineOnUIThread() and 577 // Currently, there should be CreateCaptureMachineOnUIThread() and
587 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message 578 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
588 // loop. These should both succeed without crashing, and the machine should 579 // loop. These should both succeed without crashing, and the machine should
589 // wind up in the idle state. 580 // wind up in the idle state.
590 content::RunAllPendingInMessageLoop(); 581 base::RunLoop().RunUntilIdle();
591 } 582 }
592 583
593 TEST_F(WebContentsVideoCaptureDeviceTest, StopWithRendererWorkToDo) { 584 TEST_F(WebContentsVideoCaptureDeviceTest, StopWithRendererWorkToDo) {
594 // Set up the test to use RGB copies and an normal 585 // Set up the test to use RGB copies and an normal
595 source()->SetCanCopyToVideoFrame(false); 586 source()->SetCanCopyToVideoFrame(false);
596 source()->SetUseFrameSubscriber(false); 587 source()->SetUseFrameSubscriber(false);
597 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, 588 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond,
598 consumer()); 589 consumer());
599 device()->Start(); 590 device()->Start();
600 // Make a point of not running the UI messageloop here. 591 // Make a point of not running the UI messageloop here.
601 content::RunAllPendingInMessageLoop(); 592 // TODO(ajwong): Why do we care?
593 base::RunLoop().RunUntilIdle();
602 594
603 for (int i = 0; i < 10; ++i) 595 for (int i = 0; i < 10; ++i)
604 SimulateDrawEvent(); 596 SimulateDrawEvent();
605 597
606 device()->Stop(); 598 device()->Stop();
607 device()->DeAllocate(); 599 device()->DeAllocate();
608 // Currently, there should be CreateCaptureMachineOnUIThread() and 600 // Currently, there should be CreateCaptureMachineOnUIThread() and
609 // DestroyCaptureMachineOnUIThread() tasks pending on the current message 601 // DestroyCaptureMachineOnUIThread() tasks pending on the current message
610 // loop. These should both succeed without crashing, and the machine should 602 // loop. These should both succeed without crashing, and the machine should
611 // wind up in the idle state. 603 // wind up in the idle state.
612 ASSERT_FALSE(consumer()->HasError()); 604 ASSERT_FALSE(consumer()->HasError());
613 content::RunAllPendingInMessageLoop(); 605 base::RunLoop().RunUntilIdle();
614 ASSERT_FALSE(consumer()->HasError()); 606 ASSERT_FALSE(consumer()->HasError());
615 } 607 }
616 608
617 TEST_F(WebContentsVideoCaptureDeviceTest, DeviceRestart) { 609 TEST_F(WebContentsVideoCaptureDeviceTest, DeviceRestart) {
618 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer()); 610 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
619 device()->Start(); 611 device()->Start();
620 content::RunAllPendingInMessageLoop(); 612 base::RunLoop().RunUntilIdle();
621 source()->SetSolidColor(SK_ColorRED); 613 source()->SetSolidColor(SK_ColorRED);
622 SimulateDrawEvent(); 614 SimulateDrawEvent();
623 SimulateDrawEvent(); 615 SimulateDrawEvent();
624 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED)); 616 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
625 SimulateDrawEvent(); 617 SimulateDrawEvent();
626 SimulateDrawEvent(); 618 SimulateDrawEvent();
627 source()->SetSolidColor(SK_ColorGREEN); 619 source()->SetSolidColor(SK_ColorGREEN);
628 SimulateDrawEvent(); 620 SimulateDrawEvent();
629 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN)); 621 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN));
630 device()->Stop(); 622 device()->Stop();
631 623
632 // Device is stopped, but content can still be animating. 624 // Device is stopped, but content can still be animating.
633 SimulateDrawEvent(); 625 SimulateDrawEvent();
634 SimulateDrawEvent(); 626 SimulateDrawEvent();
635 content::RunAllPendingInMessageLoop(); 627 base::RunLoop().RunUntilIdle();
636 628
637 device()->Start(); 629 device()->Start();
638 source()->SetSolidColor(SK_ColorBLUE); 630 source()->SetSolidColor(SK_ColorBLUE);
639 SimulateDrawEvent(); 631 SimulateDrawEvent();
640 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorBLUE)); 632 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorBLUE));
641 source()->SetSolidColor(SK_ColorYELLOW); 633 source()->SetSolidColor(SK_ColorYELLOW);
642 SimulateDrawEvent(); 634 SimulateDrawEvent();
643 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorYELLOW)); 635 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorYELLOW));
644 device()->DeAllocate(); 636 device()->DeAllocate();
645 } 637 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN)); 717 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN));
726 source()->SetSolidColor(SK_ColorRED); 718 source()->SetSolidColor(SK_ColorRED);
727 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED)); 719 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
728 720
729 device()->Stop(); 721 device()->Stop();
730 device()->DeAllocate(); 722 device()->DeAllocate();
731 } 723 }
732 724
733 } // namespace 725 } // namespace
734 } // namespace content 726 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698