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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/threading/thread.h"
10 #include "media/video/capture/fake_video_capture_device.h"
11 #include "media/video/capture/video_capture_device.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 #if defined(OS_MACOSX)
16 // Mac/QTKit will always give you the size you ask for and this case will fail.
17 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize
19 #define MAYBE_AllocateBadSize AllocateBadSize
23 using ::testing::AnyNumber
;
24 using ::testing::Return
;
25 using ::testing::AtLeast
;
29 class MockFrameObserver
: public media::VideoCaptureDevice::EventHandler
{
31 MOCK_METHOD0(OnErr
, void());
32 MOCK_METHOD3(OnFrameInfo
, void(int width
, int height
, int frame_rate
));
34 explicit MockFrameObserver(base::WaitableEvent
* wait_event
)
35 : wait_event_(wait_event
) {}
37 virtual void OnError() OVERRIDE
{
41 virtual void OnFrameInfo(
42 const VideoCaptureDevice::Capability
& info
) OVERRIDE
{
43 OnFrameInfo(info
.width
, info
.height
, info
.frame_rate
);
46 virtual void OnIncomingCapturedFrame(const uint8
* data
, int length
,
47 base::Time timestamp
) OVERRIDE
{
48 wait_event_
->Signal();
52 base::WaitableEvent
* wait_event_
;
55 class VideoCaptureDeviceTest
: public testing::Test
{
57 VideoCaptureDeviceTest(): wait_event_(false, false) { }
60 loop_
->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
65 virtual void SetUp() {
66 frame_observer_
.reset(new MockFrameObserver(&wait_event_
));
67 loop_
.reset(new MessageLoopForUI());
70 virtual void TearDown() {
73 base::WaitableEvent wait_event_
;
74 scoped_ptr
<MockFrameObserver
> frame_observer_
;
75 VideoCaptureDevice::Names names_
;
76 scoped_ptr
<MessageLoop
> loop_
;
79 TEST_F(VideoCaptureDeviceTest
, OpenInvalidDevice
) {
80 VideoCaptureDevice::Name device_name
;
81 device_name
.device_name
= "jibberish";
82 device_name
.unique_id
= "jibberish";
83 VideoCaptureDevice
* device
= VideoCaptureDevice::Create(device_name
);
84 EXPECT_TRUE(device
== NULL
);
87 TEST_F(VideoCaptureDeviceTest
, CaptureVGA
) {
88 VideoCaptureDevice::GetDeviceNames(&names_
);
90 LOG(WARNING
) << "No camera available. Exiting test.";
94 scoped_ptr
<VideoCaptureDevice
> device(
95 VideoCaptureDevice::Create(names_
.front()));
96 ASSERT_FALSE(device
.get() == NULL
);
98 // Get info about the new resolution.
99 EXPECT_CALL(*frame_observer_
, OnFrameInfo(640, 480, 30))
102 EXPECT_CALL(*frame_observer_
, OnErr())
105 device
->Allocate(640, 480, 30, frame_observer_
.get());
107 // Get captured video frames.
109 EXPECT_TRUE(wait_event_
.TimedWait(base::TimeDelta::FromMilliseconds(
110 TestTimeouts::action_max_timeout_ms())));
112 device
->DeAllocate();
115 TEST_F(VideoCaptureDeviceTest
, Capture720p
) {
116 VideoCaptureDevice::GetDeviceNames(&names_
);
117 if (!names_
.size()) {
118 LOG(WARNING
) << "No camera available. Exiting test.";
122 scoped_ptr
<VideoCaptureDevice
> device(
123 VideoCaptureDevice::Create(names_
.front()));
124 ASSERT_FALSE(device
.get() == NULL
);
126 // Get info about the new resolution.
127 // We don't care about the resulting resolution or frame rate as it might
128 // be different from one machine to the next.
129 EXPECT_CALL(*frame_observer_
, OnFrameInfo(_
, _
, _
))
132 EXPECT_CALL(*frame_observer_
, OnErr())
135 device
->Allocate(1280, 720, 30, frame_observer_
.get());
137 // Get captured video frames.
139 EXPECT_TRUE(wait_event_
.TimedWait(base::TimeDelta::FromMilliseconds(
140 TestTimeouts::action_max_timeout_ms())));
142 device
->DeAllocate();
145 TEST_F(VideoCaptureDeviceTest
, MAYBE_AllocateBadSize
) {
146 VideoCaptureDevice::GetDeviceNames(&names_
);
147 if (!names_
.size()) {
148 LOG(WARNING
) << "No camera available. Exiting test.";
151 scoped_ptr
<VideoCaptureDevice
> device(
152 VideoCaptureDevice::Create(names_
.front()));
153 ASSERT_TRUE(device
.get() != NULL
);
155 EXPECT_CALL(*frame_observer_
, OnErr())
158 // get info about the new resolution
159 EXPECT_CALL(*frame_observer_
, OnFrameInfo(640, 480 , _
))
162 device
->Allocate(637, 472, 35, frame_observer_
.get());
163 device
->DeAllocate();
166 TEST_F(VideoCaptureDeviceTest
, ReAllocateCamera
) {
167 VideoCaptureDevice::GetDeviceNames(&names_
);
168 if (!names_
.size()) {
169 LOG(WARNING
) << "No camera available. Exiting test.";
172 scoped_ptr
<VideoCaptureDevice
> device(
173 VideoCaptureDevice::Create(names_
.front()));
174 ASSERT_TRUE(device
.get() != NULL
);
175 EXPECT_CALL(*frame_observer_
, OnErr())
177 // get info about the new resolution
178 EXPECT_CALL(*frame_observer_
, OnFrameInfo(640, 480, _
));
180 EXPECT_CALL(*frame_observer_
, OnFrameInfo(320, 240, _
));
182 device
->Allocate(640, 480, 30, frame_observer_
.get());
184 // Nothing shall happen.
185 device
->Allocate(1280, 1024, 30, frame_observer_
.get());
186 device
->DeAllocate();
187 // Allocate new size 320, 240
188 device
->Allocate(320, 240, 30, frame_observer_
.get());
191 // Get captured video frames.
193 EXPECT_TRUE(wait_event_
.TimedWait(base::TimeDelta::FromMilliseconds(
194 TestTimeouts::action_max_timeout_ms())));
196 device
->DeAllocate();
199 TEST_F(VideoCaptureDeviceTest
, DeAllocateCameraWhileRunning
) {
200 VideoCaptureDevice::GetDeviceNames(&names_
);
201 if (!names_
.size()) {
202 LOG(WARNING
) << "No camera available. Exiting test.";
205 scoped_ptr
<VideoCaptureDevice
> device(
206 VideoCaptureDevice::Create(names_
.front()));
207 ASSERT_TRUE(device
.get() != NULL
);
209 EXPECT_CALL(*frame_observer_
, OnErr())
211 // Get info about the new resolution.
212 EXPECT_CALL(*frame_observer_
, OnFrameInfo(640, 480, 30));
214 device
->Allocate(640, 480, 30, frame_observer_
.get());
217 // Get captured video frames.
219 EXPECT_TRUE(wait_event_
.TimedWait(base::TimeDelta::FromMilliseconds(
220 TestTimeouts::action_max_timeout_ms())));
221 device
->DeAllocate();
224 TEST_F(VideoCaptureDeviceTest
, TestFakeCapture
) {
225 VideoCaptureDevice::Names names
;
227 FakeVideoCaptureDevice::GetDeviceNames(&names
);
229 ASSERT_GT(static_cast<int>(names
.size()), 0);
231 scoped_ptr
<VideoCaptureDevice
> device(
232 FakeVideoCaptureDevice::Create(names
.front()));
233 ASSERT_TRUE(device
.get() != NULL
);
235 // Get info about the new resolution.
236 EXPECT_CALL(*frame_observer_
, OnFrameInfo(640, 480, 30))
239 EXPECT_CALL(*frame_observer_
, OnErr())
242 device
->Allocate(640, 480, 30, frame_observer_
.get());
245 EXPECT_TRUE(wait_event_
.TimedWait(base::TimeDelta::FromMilliseconds(
246 TestTimeouts::action_max_timeout_ms())));
248 device
->DeAllocate();
251 }; // namespace media