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(media::VideoPixelFormat format
,
61 const gfx::Size
& dimensions
) override
{
62 DoReserveOutputBuffer();
63 return scoped_ptr
<Buffer
>();
65 void OnIncomingCapturedBuffer(scoped_ptr
<Buffer
> buffer
,
66 const media::VideoCaptureFormat
& frame_format
,
67 const base::TimeTicks
& timestamp
) override
{
68 DoOnIncomingCapturedBuffer();
70 void OnIncomingCapturedVideoFrame(
71 scoped_ptr
<Buffer
> buffer
,
72 const scoped_refptr
<media::VideoFrame
>& frame
,
73 const base::TimeTicks
& timestamp
) override
{
74 DoOnIncomingCapturedVideoFrame();
78 // Test harness that sets up a minimal environment with necessary stubs.
79 class DesktopCaptureDeviceAuraTest
: public testing::Test
{
81 DesktopCaptureDeviceAuraTest()
82 : browser_thread_for_ui_(BrowserThread::UI
, &message_loop_
) {}
83 ~DesktopCaptureDeviceAuraTest() override
{}
86 void SetUp() override
{
87 // The ContextFactory must exist before any Compositors are created.
88 bool enable_pixel_output
= false;
89 ui::ContextFactory
* context_factory
=
90 ui::InitializeContextFactoryForTests(enable_pixel_output
);
91 helper_
.reset(new aura::test::AuraTestHelper(&message_loop_
));
92 helper_
->SetUp(context_factory
);
93 new wm::DefaultActivationClient(helper_
->root_window());
95 // We need a window to cover desktop area so that DesktopCaptureDeviceAura
96 // can use gfx::NativeWindow::GetWindowAtScreenPoint() to locate the
97 // root window associated with the primary display.
98 gfx::Rect desktop_bounds
= root_window()->bounds();
99 window_delegate_
.reset(new aura::test::TestWindowDelegate());
100 desktop_window_
.reset(new aura::Window(window_delegate_
.get()));
101 desktop_window_
->Init(ui::LAYER_TEXTURED
);
102 desktop_window_
->SetBounds(desktop_bounds
);
103 aura::client::ParentWindowWithContext(
104 desktop_window_
.get(), root_window(), desktop_bounds
);
105 desktop_window_
->Show();
108 void TearDown() override
{
109 helper_
->RunAllPendingInMessageLoop();
110 root_window()->RemoveChild(desktop_window_
.get());
111 desktop_window_
.reset();
112 window_delegate_
.reset();
114 ui::TerminateContextFactoryForTests();
117 aura::Window
* root_window() { return helper_
->root_window(); }
120 base::MessageLoopForUI message_loop_
;
121 BrowserThreadImpl browser_thread_for_ui_
;
122 scoped_ptr
<aura::test::AuraTestHelper
> helper_
;
123 scoped_ptr
<aura::Window
> desktop_window_
;
124 scoped_ptr
<aura::test::TestWindowDelegate
> window_delegate_
;
126 DISALLOW_COPY_AND_ASSIGN(DesktopCaptureDeviceAuraTest
);
129 TEST_F(DesktopCaptureDeviceAuraTest
, StartAndStop
) {
130 scoped_ptr
<media::VideoCaptureDevice
> capture_device(
131 DesktopCaptureDeviceAura::Create(
132 content::DesktopMediaID::RegisterAuraWindow(root_window())));
134 scoped_ptr
<MockDeviceClient
> client(new MockDeviceClient());
135 EXPECT_CALL(*client
, OnError(_
)).Times(0);
137 media::VideoCaptureParams capture_params
;
138 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
139 capture_params
.requested_format
.frame_rate
= kFrameRate
;
140 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
141 capture_device
->AllocateAndStart(capture_params
, client
.Pass());
142 capture_device
->StopAndDeAllocate();
146 } // namespace content