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/video/capture/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_METHOD2(ReserveOutputBuffer
,
39 scoped_refptr
<Buffer
>(media::VideoFrame::Format format
,
40 const gfx::Size
& dimensions
));
41 MOCK_METHOD1(OnError
, void(const std::string
& reason
));
42 MOCK_METHOD5(OnIncomingCapturedData
,
43 void(const uint8
* data
,
45 const media::VideoCaptureFormat
& frame_format
,
47 base::TimeTicks timestamp
));
48 MOCK_METHOD4(OnIncomingCapturedVideoFrame
,
49 void(const scoped_refptr
<Buffer
>& buffer
,
50 const media::VideoCaptureFormat
& buffer_format
,
51 const scoped_refptr
<media::VideoFrame
>& frame
,
52 base::TimeTicks timestamp
));
55 // Test harness that sets up a minimal environment with necessary stubs.
56 class DesktopCaptureDeviceAuraTest
: public testing::Test
{
58 DesktopCaptureDeviceAuraTest()
59 : browser_thread_for_ui_(BrowserThread::UI
, &message_loop_
) {}
60 virtual ~DesktopCaptureDeviceAuraTest() {}
63 virtual void SetUp() OVERRIDE
{
64 // The ContextFactory must exist before any Compositors are created.
65 bool enable_pixel_output
= false;
66 ui::ContextFactory
* context_factory
=
67 ui::InitializeContextFactoryForTests(enable_pixel_output
);
68 helper_
.reset(new aura::test::AuraTestHelper(&message_loop_
));
69 helper_
->SetUp(context_factory
);
70 new wm::DefaultActivationClient(helper_
->root_window());
72 // We need a window to cover desktop area so that DesktopCaptureDeviceAura
73 // can use gfx::NativeWindow::GetWindowAtScreenPoint() to locate the
74 // root window associated with the primary display.
75 gfx::Rect desktop_bounds
= root_window()->bounds();
76 window_delegate_
.reset(new aura::test::TestWindowDelegate());
77 desktop_window_
.reset(new aura::Window(window_delegate_
.get()));
78 desktop_window_
->Init(aura::WINDOW_LAYER_TEXTURED
);
79 desktop_window_
->SetBounds(desktop_bounds
);
80 aura::client::ParentWindowWithContext(
81 desktop_window_
.get(), root_window(), desktop_bounds
);
82 desktop_window_
->Show();
85 virtual void TearDown() OVERRIDE
{
86 helper_
->RunAllPendingInMessageLoop();
87 root_window()->RemoveChild(desktop_window_
.get());
88 desktop_window_
.reset();
89 window_delegate_
.reset();
91 ui::TerminateContextFactoryForTests();
94 aura::Window
* root_window() { return helper_
->root_window(); }
97 base::MessageLoopForUI message_loop_
;
98 BrowserThreadImpl browser_thread_for_ui_
;
99 scoped_ptr
<aura::test::AuraTestHelper
> helper_
;
100 scoped_ptr
<aura::Window
> desktop_window_
;
101 scoped_ptr
<aura::test::TestWindowDelegate
> window_delegate_
;
103 DISALLOW_COPY_AND_ASSIGN(DesktopCaptureDeviceAuraTest
);
106 TEST_F(DesktopCaptureDeviceAuraTest
, StartAndStop
) {
107 scoped_ptr
<media::VideoCaptureDevice
> capture_device(
108 DesktopCaptureDeviceAura::Create(
109 content::DesktopMediaID::RegisterAuraWindow(root_window())));
111 scoped_ptr
<MockDeviceClient
> client(new MockDeviceClient());
112 EXPECT_CALL(*client
, OnError(_
)).Times(0);
114 media::VideoCaptureParams capture_params
;
115 capture_params
.requested_format
.frame_size
.SetSize(640, 480);
116 capture_params
.requested_format
.frame_rate
= kFrameRate
;
117 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
118 capture_params
.allow_resolution_change
= false;
119 capture_device
->AllocateAndStart(
120 capture_params
, client
.PassAs
<media::VideoCaptureDevice::Client
>());
121 capture_device
->StopAndDeAllocate();
125 } // namespace content