1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/media/capture/desktop_capture_device_aura.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "content/browser/browser_thread_impl.h"
9 #include "content/public/browser/desktop_media_id.h"
10 #include "media/base/video_capture_types.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/aura/client/window_tree_client.h"
14 #include "ui/aura/test/aura_test_helper.h"
15 #include "ui/aura/test/test_window_delegate.h"
16 #include "ui/aura/window.h"
17 #include "ui/compositor/test/context_factories_for_test.h"
18 #include "ui/wm/core/default_activation_client.h"
21 using ::testing::AnyNumber
;
22 using ::testing::DoAll
;
23 using ::testing::Expectation
;
24 using ::testing::InvokeWithoutArgs
;
25 using ::testing::SaveArg
;
34 const int kFrameRate
= 30;
36 class MockDeviceClient
: public media::VideoCaptureDevice::Client
{
38 MOCK_METHOD5(OnIncomingCapturedData
,
39 void(const uint8
* data
,
41 const media::VideoCaptureFormat
& frame_format
,
43 const base::TimeTicks
& timestamp
));
44 MOCK_METHOD9(OnIncomingCapturedYuvData
,
45 void (const uint8
* y_data
,
51 const media::VideoCaptureFormat
& frame_format
,
52 int clockwise_rotation
,
53 const base::TimeTicks
& timestamp
));
54 MOCK_METHOD0(DoReserveOutputBuffer
, void(void));
55 MOCK_METHOD0(DoOnIncomingCapturedBuffer
, void(void));
56 MOCK_METHOD0(DoOnIncomingCapturedVideoFrame
, void(void));
57 MOCK_METHOD1(OnError
, void(const std::string
& reason
));
59 // Trampoline methods to workaround GMOCK problems with scoped_ptr<>.
60 scoped_ptr
<Buffer
> ReserveOutputBuffer(
61 const gfx::Size
& dimensions
,
62 media::VideoCapturePixelFormat format
,
63 media::VideoPixelStorage storage
) override
{
64 EXPECT_TRUE((format
== media::VIDEO_CAPTURE_PIXEL_FORMAT_I420
&&
65 storage
== media::PIXEL_STORAGE_CPU
) ||
66 (format
== media::VIDEO_CAPTURE_PIXEL_FORMAT_ARGB
&&
67 storage
== media::PIXEL_STORAGE_TEXTURE
));
68 DoReserveOutputBuffer();
69 return scoped_ptr
<Buffer
>();
71 void OnIncomingCapturedBuffer(scoped_ptr
<Buffer
> buffer
,
72 const media::VideoCaptureFormat
& frame_format
,
73 const base::TimeTicks
& timestamp
) override
{
74 DoOnIncomingCapturedBuffer();
76 void OnIncomingCapturedVideoFrame(
77 scoped_ptr
<Buffer
> buffer
,
78 const scoped_refptr
<media::VideoFrame
>& frame
,
79 const base::TimeTicks
& timestamp
) override
{
80 DoOnIncomingCapturedVideoFrame();
83 double GetBufferPoolUtilization() const override
{ return 0.0; }
86 // Test harness that sets up a minimal environment with necessary stubs.
87 class DesktopCaptureDeviceAuraTest
: public testing::Test
{
89 DesktopCaptureDeviceAuraTest()
90 : browser_thread_for_ui_(BrowserThread::UI
, &message_loop_
) {}
91 ~DesktopCaptureDeviceAuraTest() override
{}
94 void SetUp() override
{
95 // The ContextFactory must exist before any Compositors are created.
96 bool enable_pixel_output
= false;
97 ui::ContextFactory
* context_factory
=
98 ui::InitializeContextFactoryForTests(enable_pixel_output
);
99 helper_
.reset(new aura::test::AuraTestHelper(&message_loop_
));
100 helper_
->SetUp(context_factory
);
101 new wm::DefaultActivationClient(helper_
->root_window());
103 // We need a window to cover desktop area so that DesktopCaptureDeviceAura
104 // can use gfx::NativeWindow::GetWindowAtScreenPoint() to locate the
105 // root window associated with the primary display.
106 gfx::Rect desktop_bounds
= root_window()->bounds();
107 window_delegate_
.reset(new aura::test::TestWindowDelegate());
108 desktop_window_
.reset(new aura::Window(window_delegate_
.get()));
109 desktop_window_
->Init(ui::LAYER_TEXTURED
);
110 desktop_window_
->SetBounds(desktop_bounds
);
111 aura::client::ParentWindowWithContext(
112 desktop_window_
.get(), root_window(), desktop_bounds
);
113 desktop_window_
->Show();
116 void TearDown() override
{
117 helper_
->RunAllPendingInMessageLoop();
118 root_window()->RemoveChild(desktop_window_
.get());
119 desktop_window_
.reset();
120 window_delegate_
.reset();
122 ui::TerminateContextFactoryForTests();
125 aura::Window
* root_window() { return helper_
->root_window(); }
128 base::MessageLoopForUI message_loop_
;
129 BrowserThreadImpl browser_thread_for_ui_
;
130 scoped_ptr
<aura::test::AuraTestHelper
> helper_
;
131 scoped_ptr
<aura::Window
> desktop_window_
;
132 scoped_ptr
<aura::test::TestWindowDelegate
> window_delegate_
;
134 DISALLOW_COPY_AND_ASSIGN(DesktopCaptureDeviceAuraTest
);
137 TEST_F(DesktopCaptureDeviceAuraTest
, StartAndStop
) {
138 scoped_ptr
<media::VideoCaptureDevice
> capture_device(
139 DesktopCaptureDeviceAura::Create(
140 content::DesktopMediaID::RegisterAuraWindow(root_window())));
142 scoped_ptr
<MockDeviceClient
> client(new MockDeviceClient());
143 EXPECT_CALL(*client
, OnError(_
)).Times(0);
145 media::VideoCaptureParams capture_params
;
146 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
147 capture_params
.requested_format
.frame_rate
= kFrameRate
;
148 capture_params
.requested_format
.pixel_format
=
149 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420
;
150 capture_device
->AllocateAndStart(capture_params
, client
.Pass());
151 capture_device
->StopAndDeAllocate();
155 } // namespace content