OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h" |
| 6 #include "base/synchronization/waitable_event.h" |
| 7 #include "base/threading/thread.h" |
| 8 #include "media/video/capture/fake_video_capture_device.h" |
| 9 #include "media/video/capture/video_capture_device.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using ::testing::_; |
| 14 using ::testing::AnyNumber; |
| 15 using ::testing::Return; |
| 16 using ::testing::AtLeast; |
| 17 |
| 18 namespace media { |
| 19 const int kWaitTime = 3000; |
| 20 |
| 21 class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { |
| 22 public: |
| 23 MOCK_METHOD0(OnErr, void()); |
| 24 MOCK_METHOD3(OnFrameInfo, void(int width, int height, int frame_rate)); |
| 25 |
| 26 explicit MockFrameObserver(base::WaitableEvent* wait_event) |
| 27 : wait_event_(wait_event) {} |
| 28 |
| 29 virtual void OnError() OVERRIDE { |
| 30 OnErr(); |
| 31 } |
| 32 |
| 33 virtual void OnFrameInfo( |
| 34 const VideoCaptureDevice::Capability& info) OVERRIDE { |
| 35 OnFrameInfo(info.width, info.height, info.frame_rate); |
| 36 } |
| 37 |
| 38 virtual void OnIncomingCapturedFrame(const uint8* data, int length, |
| 39 base::Time timestamp) OVERRIDE { |
| 40 wait_event_->Signal(); |
| 41 } |
| 42 |
| 43 private: |
| 44 base::WaitableEvent* wait_event_; |
| 45 }; |
| 46 |
| 47 class VideoCaptureDeviceTest : public testing::Test { |
| 48 public: |
| 49 VideoCaptureDeviceTest(): wait_event_(false, false) { } |
| 50 |
| 51 protected: |
| 52 virtual void SetUp() { |
| 53 frame_observer_.reset(new MockFrameObserver(&wait_event_)); |
| 54 } |
| 55 |
| 56 virtual void TearDown() { |
| 57 } |
| 58 |
| 59 base::WaitableEvent wait_event_; |
| 60 scoped_ptr<MockFrameObserver> frame_observer_; |
| 61 VideoCaptureDevice::Names names_; |
| 62 }; |
| 63 |
| 64 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { |
| 65 VideoCaptureDevice::Name device_name; |
| 66 device_name.device_name = "jibberish"; |
| 67 device_name.unique_id = "jibberish"; |
| 68 VideoCaptureDevice* device = VideoCaptureDevice::Create(device_name); |
| 69 EXPECT_TRUE(device == NULL); |
| 70 } |
| 71 |
| 72 TEST_F(VideoCaptureDeviceTest, CaptureVGA) { |
| 73 VideoCaptureDevice::GetDeviceNames(&names_); |
| 74 // Make sure there are more than 0 cameras. |
| 75 if (!names_.size()) { |
| 76 LOG(WARNING) << "No camera available. Exiting test."; |
| 77 return; |
| 78 } |
| 79 |
| 80 scoped_ptr<VideoCaptureDevice> device( |
| 81 VideoCaptureDevice::Create(names_.front())); |
| 82 ASSERT_FALSE(device.get() == NULL); |
| 83 |
| 84 // Get info about the new resolution. |
| 85 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) |
| 86 .Times(1); |
| 87 |
| 88 EXPECT_CALL(*frame_observer_, OnErr()) |
| 89 .Times(0); |
| 90 |
| 91 device->Allocate(640, 480, 30, frame_observer_.get()); |
| 92 device->Start(); |
| 93 // Wait for 3s or for captured frame. |
| 94 EXPECT_TRUE(wait_event_.TimedWait( |
| 95 base::TimeDelta::FromMilliseconds(kWaitTime))); |
| 96 device->Stop(); |
| 97 device->DeAllocate(); |
| 98 } |
| 99 |
| 100 TEST_F(VideoCaptureDeviceTest, Capture720p) { |
| 101 VideoCaptureDevice::GetDeviceNames(&names_); |
| 102 // Make sure there are more than 0 cameras. |
| 103 if (!names_.size()) { |
| 104 LOG(WARNING) << "No camera available. Exiting test."; |
| 105 return; |
| 106 } |
| 107 |
| 108 scoped_ptr<VideoCaptureDevice> device( |
| 109 VideoCaptureDevice::Create(names_.front())); |
| 110 ASSERT_FALSE(device.get() == NULL); |
| 111 |
| 112 // Get info about the new resolution. |
| 113 // We don't care about the resulting resolution or frame rate as it might |
| 114 // be different from one machine to the next. |
| 115 EXPECT_CALL(*frame_observer_, OnFrameInfo(_, _, _)) |
| 116 .Times(1); |
| 117 |
| 118 EXPECT_CALL(*frame_observer_, OnErr()) |
| 119 .Times(0); |
| 120 |
| 121 device->Allocate(1280, 720, 30, frame_observer_.get()); |
| 122 device->Start(); |
| 123 // Get captured video frames. |
| 124 EXPECT_TRUE(wait_event_.TimedWait( |
| 125 base::TimeDelta::FromMilliseconds(kWaitTime))); |
| 126 device->Stop(); |
| 127 device->DeAllocate(); |
| 128 } |
| 129 |
| 130 TEST_F(VideoCaptureDeviceTest, AllocateSameCameraTwice) { |
| 131 VideoCaptureDevice::GetDeviceNames(&names_); |
| 132 if (!names_.size()) { |
| 133 LOG(WARNING) << "No camera available. Exiting test."; |
| 134 return; |
| 135 } |
| 136 scoped_ptr<VideoCaptureDevice> device1( |
| 137 VideoCaptureDevice::Create(names_.front())); |
| 138 ASSERT_TRUE(device1.get() != NULL); |
| 139 |
| 140 scoped_ptr<VideoCaptureDevice> device2( |
| 141 VideoCaptureDevice::Create(names_.front())); |
| 142 ASSERT_TRUE(device2.get() != NULL); |
| 143 |
| 144 // 1. Get info about the new resolution on the first allocated camera |
| 145 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); |
| 146 |
| 147 device1->Allocate(640, 480, 30, frame_observer_.get()); |
| 148 |
| 149 // 2. Error when trying to allocate the same camera again. |
| 150 EXPECT_CALL(*frame_observer_, OnErr()); |
| 151 device2->Allocate(640, 480, 30, frame_observer_.get()); |
| 152 |
| 153 device1->DeAllocate(); |
| 154 device2->DeAllocate(); |
| 155 } |
| 156 |
| 157 TEST_F(VideoCaptureDeviceTest, AllocateBadSize) { |
| 158 VideoCaptureDevice::GetDeviceNames(&names_); |
| 159 if (!names_.size()) { |
| 160 LOG(WARNING) << "No camera available. Exiting test."; |
| 161 return; |
| 162 } |
| 163 scoped_ptr<VideoCaptureDevice> device( |
| 164 VideoCaptureDevice::Create(names_.front())); |
| 165 ASSERT_TRUE(device.get() != NULL); |
| 166 |
| 167 EXPECT_CALL(*frame_observer_, OnErr()) |
| 168 .Times(0); |
| 169 |
| 170 // get info about the new resolution |
| 171 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480 , _)) |
| 172 .Times(AtLeast(1)); |
| 173 |
| 174 device->Allocate(637, 472, 35, frame_observer_.get()); |
| 175 device->DeAllocate(); |
| 176 } |
| 177 |
| 178 TEST_F(VideoCaptureDeviceTest, ReAllocateCamera) { |
| 179 VideoCaptureDevice::GetDeviceNames(&names_); |
| 180 if (!names_.size()) { |
| 181 LOG(WARNING) << "No camera available. Exiting test."; |
| 182 return; |
| 183 } |
| 184 scoped_ptr<VideoCaptureDevice> device( |
| 185 VideoCaptureDevice::Create(names_.front())); |
| 186 ASSERT_TRUE(device.get() != NULL); |
| 187 EXPECT_CALL(*frame_observer_, OnErr()) |
| 188 .Times(0); |
| 189 // get info about the new resolution |
| 190 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, _)); |
| 191 |
| 192 EXPECT_CALL(*frame_observer_, OnFrameInfo(320, 240, _)); |
| 193 |
| 194 device->Allocate(640, 480, 30, frame_observer_.get()); |
| 195 device->Start(); |
| 196 // Nothing shall happen. |
| 197 device->Allocate(1280, 1024, 30, frame_observer_.get()); |
| 198 device->DeAllocate(); |
| 199 // Allocate new size 320, 240 |
| 200 device->Allocate(320, 240, 30, frame_observer_.get()); |
| 201 |
| 202 device->Start(); |
| 203 // Get captured video frames. |
| 204 EXPECT_TRUE(wait_event_.TimedWait( |
| 205 base::TimeDelta::FromMilliseconds(kWaitTime))); |
| 206 device->Stop(); |
| 207 device->DeAllocate(); |
| 208 } |
| 209 |
| 210 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { |
| 211 VideoCaptureDevice::GetDeviceNames(&names_); |
| 212 if (!names_.size()) { |
| 213 LOG(WARNING) << "No camera available. Exiting test."; |
| 214 return; |
| 215 } |
| 216 scoped_ptr<VideoCaptureDevice> device( |
| 217 VideoCaptureDevice::Create(names_.front())); |
| 218 ASSERT_TRUE(device.get() != NULL); |
| 219 |
| 220 EXPECT_CALL(*frame_observer_, OnErr()) |
| 221 .Times(0); |
| 222 // Get info about the new resolution. |
| 223 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); |
| 224 |
| 225 device->Allocate(640, 480, 30, frame_observer_.get()); |
| 226 |
| 227 device->Start(); |
| 228 // Get captured video frames. |
| 229 EXPECT_TRUE(wait_event_.TimedWait( |
| 230 base::TimeDelta::FromMilliseconds(kWaitTime))); |
| 231 device->DeAllocate(); |
| 232 } |
| 233 |
| 234 TEST_F(VideoCaptureDeviceTest, TestFakeCapture) { |
| 235 VideoCaptureDevice::Names names; |
| 236 |
| 237 FakeVideoCaptureDevice::GetDeviceNames(&names); |
| 238 |
| 239 ASSERT_GT(static_cast<int>(names.size()), 0); |
| 240 |
| 241 scoped_ptr<VideoCaptureDevice> device( |
| 242 FakeVideoCaptureDevice::Create(names.front())); |
| 243 ASSERT_TRUE(device.get() != NULL); |
| 244 |
| 245 // Get info about the new resolution. |
| 246 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) |
| 247 .Times(1); |
| 248 |
| 249 EXPECT_CALL(*frame_observer_, OnErr()) |
| 250 .Times(0); |
| 251 |
| 252 device->Allocate(640, 480, 30, frame_observer_.get()); |
| 253 |
| 254 device->Start(); |
| 255 EXPECT_TRUE(wait_event_.TimedWait( |
| 256 base::TimeDelta::FromMilliseconds(kWaitTime))); |
| 257 device->Stop(); |
| 258 device->DeAllocate(); |
| 259 } |
| 260 |
| 261 }; // namespace media |
OLD | NEW |