Chromium Code Reviews| Index: media/video/capture/video_capture_device_unittest.cc |
| =================================================================== |
| --- media/video/capture/video_capture_device_unittest.cc (revision 0) |
| +++ media/video/capture/video_capture_device_unittest.cc (revision 0) |
| @@ -0,0 +1,223 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/threading/thread.h" |
| +#include "base/synchronization/waitable_event.h" |
| + |
| + |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
remove extra empty line.
Per K
2011/06/27 11:47:50
Done.
|
| +#include "media/video/capture/fake_video_capture_device.h" |
| +#include "media/video/capture/video_capture_device.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using ::testing::_; |
| +using ::testing::AnyNumber; |
| +using ::testing::Return; |
| +using ::testing::AtLeast; |
| + |
| +namespace media { |
| + |
| +class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
space before :
Can you run gcl lint on the cl and
Per K
2011/06/27 11:47:50
Done.
|
| + public: |
| + MOCK_METHOD0(OnErr, void()); |
| + MOCK_METHOD3(OnFrameInfo, void(int width, int height, int frame_rate)); |
| + |
| + explicit MockFrameObserver(base::WaitableEvent* wait_event) |
| + : frame_received_(false), |
| + wait_event_(wait_event) {} |
| + |
| + virtual void OnError() { |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
for virtual methods Chromium has started to use th
Per K
2011/06/27 11:47:50
Done.
|
| + OnErr(); |
| + } |
| + void OnFrameInfo(const VideoCaptureDevice::Capability& info) { |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
should this be virtual?
also add an empty line bet
Per K
2011/06/27 11:47:50
Done.
|
| + OnFrameInfo(info.width, info.height, info.frame_rate); |
| + } |
| + virtual void OnIncomingCapturedFrame(const uint8* data, int length, |
| + base::Time timestamp) { |
| + wait_event_->Signal(); |
| + } |
| + |
| + bool frame_received_; |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
private/protected?
Per K
2011/06/27 11:47:50
Done.
|
| + base::WaitableEvent* wait_event_; |
| +}; |
| + |
| +class VideoCaptureDeviceTest : public testing::Test { |
| + public: |
| + VideoCaptureDeviceTest(): wait_event_(false, false) { } |
| + |
| + protected: |
| + virtual void SetUp() { |
| + frame_observer_.reset(new MockFrameObserver(&wait_event_)); |
| + |
| + VideoCaptureDevice::GetDeviceNames(&names_); |
| + // Make sure there are more than 0 cameras. |
| + ASSERT_GT(static_cast<int>(names_.size()), 0); |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
instead of using static_cast<int>(), just do:
ASSE
Per K
2011/06/27 11:47:50
Done - kind of.
On 2011/06/23 14:05:56, tommi wro
|
| + } |
| + |
| + virtual void TearDown() { |
| + } |
| + |
| + base::WaitableEvent wait_event_; |
| + scoped_ptr<MockFrameObserver> frame_observer_; |
| + VideoCaptureDevice::Names names_; |
| +}; |
| + |
| +TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { |
| + VideoCaptureDevice::Name device_name; |
| + device_name.device_name = "jibberish"; |
| + device_name.unique_id = "jibberish"; |
| + VideoCaptureDevice* device = VideoCaptureDevice::Create(device_name); |
| + EXPECT_TRUE(device == NULL); |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
fyi - I'm sure lint will tell you to use EXPECT_EQ
Per K
2011/06/27 11:47:50
Lint don't complain about this.
On 2011/06/23 14:
|
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, CaptureVGA) { |
| + scoped_ptr<VideoCaptureDevice> device( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device.get() == NULL); |
| + |
| + // Get info about the new resolution. |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
is there a chance that allocate succeeds but the f
Per K
2011/06/27 11:47:50
Almost all cameras can do VGA 30fps. But whenever
|
| + .Times(1); |
| + |
| + EXPECT_CALL(*frame_observer_, OnErr()) |
| + .Times(0); |
| + |
| + device->Allocate(640, 480, 30, frame_observer_.get()); |
| + device->Start(); |
| + // Wait for 3s or for captured frame. |
| + EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(3000))); |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
add a constant at the top of the file for the wait
Per K
2011/06/27 11:47:50
Done.
|
| + device->Stop(); |
| + device->DeAllocate(); |
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, Capture720p) { |
| + scoped_ptr<VideoCaptureDevice> device( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device.get() == NULL); |
| + |
| + // Get info about the new resolution. |
| + // We don't care about the resulting resolution or frame rate as it might |
| + // be different from one machine to the next. |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(_, _, _)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*frame_observer_, OnErr()) |
| + .Times(0); |
| + |
| + device->Allocate(1280, 720, 30, frame_observer_.get()); |
| + device->Start(); |
| + // Get captured video frames. |
| + EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(3000))); |
| + device->Stop(); |
| + device->DeAllocate(); |
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, AllocateSameCameraTwice) { |
| + scoped_ptr<VideoCaptureDevice> device1( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device1.get() == NULL); |
|
tommi (sloooow) - chröme
2011/06/23 14:05:56
nit: All the ASSERT_FALSE(ptr == NULL) checks read
Per K
2011/06/27 11:47:50
Done.
|
| + |
| + scoped_ptr<VideoCaptureDevice> device2( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device2.get() == NULL); |
| + |
| + // 1. Get info about the new resolution on the first allocated camera |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); |
| + |
| + device1->Allocate(640, 480, 30, frame_observer_.get()); |
| + |
| + // 2. Error when trying to allocate the same camera again. |
| + EXPECT_CALL(*frame_observer_, OnErr()); |
| + device2->Allocate(640, 480, 30, frame_observer_.get()); |
| + |
| + device1->DeAllocate(); |
| + device2->DeAllocate(); |
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, AllocateBadSize) { |
| + scoped_ptr<VideoCaptureDevice> device( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device.get() == NULL); |
| + |
| + EXPECT_CALL(*frame_observer_, OnErr()) |
| + .Times(0); |
| + |
| + // get info about the new resolution |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480 , _)) |
| + .Times(AtLeast(1)); |
| + |
| + device->Allocate(637, 472, 35, frame_observer_.get()); |
| + device->DeAllocate(); |
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, ReAllocateCamera) { |
| + scoped_ptr<VideoCaptureDevice> device( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device.get() == NULL); |
| + EXPECT_CALL(*frame_observer_, OnErr()) |
| + .Times(0); |
| + // get info about the new resolution |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, _)); |
| + |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(320, 240, _)); |
| + |
| + device->Allocate(640, 480, 30, frame_observer_.get()); |
| + device->Start(); |
| + // Nothing shall happen. |
| + device->Allocate(1280, 1024, 30, frame_observer_.get()); |
| + device->DeAllocate(); |
| + // Allocate new size 320, 240 |
| + device->Allocate(320, 240, 30, frame_observer_.get()); |
| + |
| + device->Start(); |
| + // Get captured video frames. |
| + EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000))); |
| + device->Stop(); |
| + device->DeAllocate(); |
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { |
| + scoped_ptr<VideoCaptureDevice> device( |
| + VideoCaptureDevice::Create(names_.front())); |
| + ASSERT_FALSE(device.get() == NULL); |
| + |
| + // 1. Get info about the new resolution. |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); |
| + |
| + device->Allocate(640, 480, 30, frame_observer_.get()); |
| + |
| + device->Start(); |
| + // Get captured video frames. |
| + EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000))); |
| + device->DeAllocate(); |
| +} |
| + |
| +TEST_F(VideoCaptureDeviceTest, TestFakeCapture) { |
| + VideoCaptureDevice::Names names; |
| + |
| + FakeVideoCaptureDevice::GetDeviceNames(&names); |
| + |
| + ASSERT_GT(static_cast<int>(names.size()), 0); |
| + |
| + scoped_ptr<VideoCaptureDevice> device( |
| + FakeVideoCaptureDevice::Create(names.front())); |
| + ASSERT_FALSE(device.get() == NULL); |
| + |
| + // Get info about the new resolution. |
| + EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) |
| + .Times(1); |
| + |
| + EXPECT_CALL(*frame_observer_, OnErr()) |
| + .Times(0); |
| + |
| + device->Allocate(640, 480, 30, frame_observer_.get()); |
| + |
| + device->Start(); |
| + EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000))); |
| + device->Stop(); |
| + device->DeAllocate(); |
| +} |
| + |
| +}; // namespace media |