Disable force-compositing-mode on background pages
[chromium-blink-merge.git] / media / video / capture / video_capture_device_unittest.cc
blob15242044ad6f63be11b9c75de009046af8fd3efa
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
18 #else
19 #define MAYBE_AllocateBadSize AllocateBadSize
20 #endif
22 using ::testing::_;
23 using ::testing::AnyNumber;
24 using ::testing::Return;
25 using ::testing::AtLeast;
27 namespace media {
29 class MockFrameObserver : public media::VideoCaptureDevice::EventHandler {
30 public:
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 {
38 OnErr();
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();
51 private:
52 base::WaitableEvent* wait_event_;
55 class VideoCaptureDeviceTest : public testing::Test {
56 public:
57 VideoCaptureDeviceTest(): wait_event_(false, false) { }
59 void PostQuitTask() {
60 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
61 loop_->Run();
64 protected:
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_);
89 if (!names_.size()) {
90 LOG(WARNING) << "No camera available. Exiting test.";
91 return;
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))
100 .Times(1);
102 EXPECT_CALL(*frame_observer_, OnErr())
103 .Times(0);
105 device->Allocate(640, 480, 30, frame_observer_.get());
106 device->Start();
107 // Get captured video frames.
108 PostQuitTask();
109 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(
110 TestTimeouts::action_max_timeout_ms())));
111 device->Stop();
112 device->DeAllocate();
115 TEST_F(VideoCaptureDeviceTest, Capture720p) {
116 VideoCaptureDevice::GetDeviceNames(&names_);
117 if (!names_.size()) {
118 LOG(WARNING) << "No camera available. Exiting test.";
119 return;
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(_, _, _))
130 .Times(1);
132 EXPECT_CALL(*frame_observer_, OnErr())
133 .Times(0);
135 device->Allocate(1280, 720, 30, frame_observer_.get());
136 device->Start();
137 // Get captured video frames.
138 PostQuitTask();
139 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(
140 TestTimeouts::action_max_timeout_ms())));
141 device->Stop();
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.";
149 return;
151 scoped_ptr<VideoCaptureDevice> device(
152 VideoCaptureDevice::Create(names_.front()));
153 ASSERT_TRUE(device.get() != NULL);
155 EXPECT_CALL(*frame_observer_, OnErr())
156 .Times(0);
158 // get info about the new resolution
159 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480 , _))
160 .Times(AtLeast(1));
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.";
170 return;
172 scoped_ptr<VideoCaptureDevice> device(
173 VideoCaptureDevice::Create(names_.front()));
174 ASSERT_TRUE(device.get() != NULL);
175 EXPECT_CALL(*frame_observer_, OnErr())
176 .Times(0);
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());
183 device->Start();
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());
190 device->Start();
191 // Get captured video frames.
192 PostQuitTask();
193 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(
194 TestTimeouts::action_max_timeout_ms())));
195 device->Stop();
196 device->DeAllocate();
199 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) {
200 VideoCaptureDevice::GetDeviceNames(&names_);
201 if (!names_.size()) {
202 LOG(WARNING) << "No camera available. Exiting test.";
203 return;
205 scoped_ptr<VideoCaptureDevice> device(
206 VideoCaptureDevice::Create(names_.front()));
207 ASSERT_TRUE(device.get() != NULL);
209 EXPECT_CALL(*frame_observer_, OnErr())
210 .Times(0);
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());
216 device->Start();
217 // Get captured video frames.
218 PostQuitTask();
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))
237 .Times(1);
239 EXPECT_CALL(*frame_observer_, OnErr())
240 .Times(0);
242 device->Allocate(640, 480, 30, frame_observer_.get());
244 device->Start();
245 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(
246 TestTimeouts::action_max_timeout_ms())));
247 device->Stop();
248 device->DeAllocate();
251 }; // namespace media