| OLD | NEW |
| 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/media/capture/web_contents_video_capture_device.h" | 5 #include "content/browser/media/capture/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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "media/base/video_util.h" | 31 #include "media/base/video_util.h" |
| 32 #include "media/base/yuv_convert.h" | 32 #include "media/base/yuv_convert.h" |
| 33 #include "skia/ext/platform_canvas.h" | 33 #include "skia/ext/platform_canvas.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "third_party/skia/include/core/SkColor.h" | 36 #include "third_party/skia/include/core/SkColor.h" |
| 37 #include "ui/base/layout.h" | 37 #include "ui/base/layout.h" |
| 38 #include "ui/gfx/display.h" | 38 #include "ui/gfx/display.h" |
| 39 #include "ui/gfx/geometry/dip_util.h" | 39 #include "ui/gfx/geometry/dip_util.h" |
| 40 #include "ui/gfx/screen.h" | 40 #include "ui/gfx/screen.h" |
| 41 #include "ui/gfx/test/test_screen.h" |
| 41 | 42 |
| 42 namespace content { | 43 namespace content { |
| 43 namespace { | 44 namespace { |
| 44 | 45 |
| 45 const int kTestWidth = 320; | 46 const int kTestWidth = 320; |
| 46 const int kTestHeight = 240; | 47 const int kTestHeight = 240; |
| 47 const int kTestFramesPerSecond = 20; | 48 const int kTestFramesPerSecond = 20; |
| 48 const float kTestDeviceScaleFactor = 2.0f; | 49 const float kTestDeviceScaleFactor = 2.0f; |
| 49 const SkColor kNothingYet = 0xdeadbeef; | 50 const SkColor kNothingYet = 0xdeadbeef; |
| 50 const SkColor kNotInterested = ~kNothingYet; | 51 const SkColor kNotInterested = ~kNothingYet; |
| (...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 private: | 534 private: |
| 534 base::Lock lock_; | 535 base::Lock lock_; |
| 535 bool error_encountered_; | 536 bool error_encountered_; |
| 536 SkColor wait_color_yuv_; | 537 SkColor wait_color_yuv_; |
| 537 gfx::Size wait_size_; | 538 gfx::Size wait_size_; |
| 538 scoped_ptr<StubClient> client_; | 539 scoped_ptr<StubClient> client_; |
| 539 | 540 |
| 540 DISALLOW_COPY_AND_ASSIGN(StubClientObserver); | 541 DISALLOW_COPY_AND_ASSIGN(StubClientObserver); |
| 541 }; | 542 }; |
| 542 | 543 |
| 543 // A dummy implementation of gfx::Screen, since WebContentsVideoCaptureDevice | |
| 544 // needs access to a gfx::Display's device scale factor. | |
| 545 class FakeScreen : public gfx::Screen { | |
| 546 public: | |
| 547 FakeScreen() : the_one_display_(0x1337, gfx::Rect(0, 0, 2560, 1440)) { | |
| 548 the_one_display_.set_device_scale_factor(kTestDeviceScaleFactor); | |
| 549 } | |
| 550 ~FakeScreen() override {} | |
| 551 | |
| 552 // gfx::Screen implementation (only what's needed for testing). | |
| 553 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); } | |
| 554 gfx::NativeWindow GetWindowUnderCursor() override { return NULL; } | |
| 555 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override { | |
| 556 return NULL; | |
| 557 } | |
| 558 int GetNumDisplays() const override { return 1; } | |
| 559 std::vector<gfx::Display> GetAllDisplays() const override { | |
| 560 return std::vector<gfx::Display>(1, the_one_display_); | |
| 561 } | |
| 562 gfx::Display GetDisplayNearestWindow(gfx::NativeView view) const override { | |
| 563 return the_one_display_; | |
| 564 } | |
| 565 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override { | |
| 566 return the_one_display_; | |
| 567 } | |
| 568 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override { | |
| 569 return the_one_display_; | |
| 570 } | |
| 571 gfx::Display GetPrimaryDisplay() const override { return the_one_display_; } | |
| 572 void AddObserver(gfx::DisplayObserver* observer) override {} | |
| 573 void RemoveObserver(gfx::DisplayObserver* observer) override {} | |
| 574 | |
| 575 private: | |
| 576 gfx::Display the_one_display_; | |
| 577 | |
| 578 DISALLOW_COPY_AND_ASSIGN(FakeScreen); | |
| 579 }; | |
| 580 | |
| 581 // Test harness that sets up a minimal environment with necessary stubs. | 544 // Test harness that sets up a minimal environment with necessary stubs. |
| 582 class WebContentsVideoCaptureDeviceTest : public testing::Test { | 545 class WebContentsVideoCaptureDeviceTest : public testing::Test { |
| 583 public: | 546 public: |
| 584 // This is public because C++ method pointer scoping rules are silly and make | 547 // This is public because C++ method pointer scoping rules are silly and make |
| 585 // this hard to use with Bind(). | 548 // this hard to use with Bind(). |
| 586 void ResetWebContents() { | 549 void ResetWebContents() { |
| 587 web_contents_.reset(); | 550 web_contents_.reset(); |
| 588 } | 551 } |
| 589 | 552 |
| 590 protected: | 553 protected: |
| 591 void SetUp() override { | 554 void SetUp() override { |
| 592 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, &fake_screen_); | 555 test_screen_.display()->set_id(0x1337); |
| 593 ASSERT_EQ(&fake_screen_, gfx::Screen::GetNativeScreen()); | 556 test_screen_.display()->set_bounds(gfx::Rect(0, 0, 2560, 1440)); |
| 557 test_screen_.display()->set_device_scale_factor(kTestDeviceScaleFactor); |
| 558 |
| 559 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, &test_screen_); |
| 560 ASSERT_EQ(&test_screen_, gfx::Screen::GetNativeScreen()); |
| 594 | 561 |
| 595 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be | 562 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be |
| 596 // eliminated here, if only we could use RenderViewHostTestHarness. The | 563 // eliminated here, if only we could use RenderViewHostTestHarness. The |
| 597 // catch is that we need our TestRenderViewHost to support a | 564 // catch is that we need our TestRenderViewHost to support a |
| 598 // CopyFromBackingStore operation that we control. To accomplish that, | 565 // CopyFromBackingStore operation that we control. To accomplish that, |
| 599 // either RenderViewHostTestHarness would have to support installing a | 566 // either RenderViewHostTestHarness would have to support installing a |
| 600 // custom RenderViewHostFactory, or else we implant some kind of delegated | 567 // custom RenderViewHostFactory, or else we implant some kind of delegated |
| 601 // CopyFromBackingStore functionality into TestRenderViewHost itself. | 568 // CopyFromBackingStore functionality into TestRenderViewHost itself. |
| 602 | 569 |
| 603 render_process_host_factory_.reset(new MockRenderProcessHostFactory()); | 570 render_process_host_factory_.reset(new MockRenderProcessHostFactory()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 | 615 |
| 649 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); | 616 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); |
| 650 } | 617 } |
| 651 | 618 |
| 652 // Accessors. | 619 // Accessors. |
| 653 CaptureTestSourceController* source() { return &controller_; } | 620 CaptureTestSourceController* source() { return &controller_; } |
| 654 WebContents* web_contents() const { return web_contents_.get(); } | 621 WebContents* web_contents() const { return web_contents_.get(); } |
| 655 media::VideoCaptureDevice* device() { return device_.get(); } | 622 media::VideoCaptureDevice* device() { return device_.get(); } |
| 656 | 623 |
| 657 // Returns the device scale factor of the capture target's native view. This | 624 // Returns the device scale factor of the capture target's native view. This |
| 658 // is necessary because, architecturally, the FakeScreen implementation is | 625 // is necessary because, architecturally, the TestScreen implementation is |
| 659 // ignored on Mac platforms (when determining the device scale factor for a | 626 // ignored on Mac platforms (when determining the device scale factor for a |
| 660 // particular window). | 627 // particular window). |
| 661 float GetDeviceScaleFactor() const { | 628 float GetDeviceScaleFactor() const { |
| 662 RenderWidgetHostView* const view = | 629 RenderWidgetHostView* const view = |
| 663 web_contents_->GetRenderViewHost()->GetView(); | 630 web_contents_->GetRenderViewHost()->GetView(); |
| 664 CHECK(view); | 631 CHECK(view); |
| 665 return ui::GetScaleFactorForNativeView(view->GetNativeView()); | 632 return ui::GetScaleFactorForNativeView(view->GetNativeView()); |
| 666 } | 633 } |
| 667 | 634 |
| 668 void SimulateDrawEvent() { | 635 void SimulateDrawEvent() { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 696 as_web_contents_impl->GetMainFrame()->GetRenderWidgetHost(), true); | 663 as_web_contents_impl->GetMainFrame()->GetRenderWidgetHost(), true); |
| 697 } | 664 } |
| 698 | 665 |
| 699 void DestroyVideoCaptureDevice() { device_.reset(); } | 666 void DestroyVideoCaptureDevice() { device_.reset(); } |
| 700 | 667 |
| 701 StubClientObserver* client_observer() { | 668 StubClientObserver* client_observer() { |
| 702 return &client_observer_; | 669 return &client_observer_; |
| 703 } | 670 } |
| 704 | 671 |
| 705 private: | 672 private: |
| 706 FakeScreen fake_screen_; | 673 gfx::test::TestScreen test_screen_; |
| 707 | 674 |
| 708 StubClientObserver client_observer_; | 675 StubClientObserver client_observer_; |
| 709 | 676 |
| 710 // The controller controls which pixel patterns to produce. | 677 // The controller controls which pixel patterns to produce. |
| 711 CaptureTestSourceController controller_; | 678 CaptureTestSourceController controller_; |
| 712 | 679 |
| 713 // Self-registering RenderProcessHostFactory. | 680 // Self-registering RenderProcessHostFactory. |
| 714 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_; | 681 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_; |
| 715 | 682 |
| 716 // Creates capture-capable RenderViewHosts whose pixel content production is | 683 // Creates capture-capable RenderViewHosts whose pixel content production is |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1066 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize( | 1033 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize( |
| 1067 SK_ColorBLACK, gfx::Size(kTestWidth, | 1034 SK_ColorBLACK, gfx::Size(kTestWidth, |
| 1068 kTestWidth * arbitrary_source_size.height() / | 1035 kTestWidth * arbitrary_source_size.height() / |
| 1069 arbitrary_source_size.width()))); | 1036 arbitrary_source_size.width()))); |
| 1070 | 1037 |
| 1071 device()->StopAndDeAllocate(); | 1038 device()->StopAndDeAllocate(); |
| 1072 } | 1039 } |
| 1073 | 1040 |
| 1074 } // namespace | 1041 } // namespace |
| 1075 } // namespace content | 1042 } // namespace content |
| OLD | NEW |