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.
4
5#include "content/browser/media/capture/desktop_capture_device_aura.h"
6
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"
19
20using ::testing::_;
21using ::testing::AnyNumber;
22using ::testing::DoAll;
23using ::testing::Expectation;
24using ::testing::InvokeWithoutArgs;
25using ::testing::SaveArg;
26
27namespace media {
28class VideoFrame;
29}  // namespace media
30
31namespace content {
32namespace {
33
34const int kFrameRate = 30;
35
36class MockDeviceClient : public media::VideoCaptureDevice::Client {
37 public:
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,
44                    int length,
45                    const media::VideoCaptureFormat& frame_format,
46                    int rotation,
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));
53};
54
55// Test harness that sets up a minimal environment with necessary stubs.
56class DesktopCaptureDeviceAuraTest : public testing::Test {
57 public:
58  DesktopCaptureDeviceAuraTest()
59      : browser_thread_for_ui_(BrowserThread::UI, &message_loop_) {}
60  virtual ~DesktopCaptureDeviceAuraTest() {}
61
62 protected:
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());
71
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();
83  }
84
85  virtual void TearDown() OVERRIDE {
86    helper_->RunAllPendingInMessageLoop();
87    root_window()->RemoveChild(desktop_window_.get());
88    desktop_window_.reset();
89    window_delegate_.reset();
90    helper_->TearDown();
91    ui::TerminateContextFactoryForTests();
92  }
93
94  aura::Window* root_window() { return helper_->root_window(); }
95
96 private:
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_;
102
103  DISALLOW_COPY_AND_ASSIGN(DesktopCaptureDeviceAuraTest);
104};
105
106TEST_F(DesktopCaptureDeviceAuraTest, StartAndStop) {
107  scoped_ptr<media::VideoCaptureDevice> capture_device(
108      DesktopCaptureDeviceAura::Create(
109          content::DesktopMediaID::RegisterAuraWindow(root_window())));
110
111  scoped_ptr<MockDeviceClient> client(new MockDeviceClient());
112  EXPECT_CALL(*client, OnError(_)).Times(0);
113
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_device->AllocateAndStart(
119      capture_params, client.PassAs<media::VideoCaptureDevice::Client>());
120  capture_device->StopAndDeAllocate();
121}
122
123}  // namespace
124}  // namespace content
125