Chromium Code Reviews| 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 // Unit test for VideoCaptureManager | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/process_util.h" | |
| 12 #include "content/browser/browser_thread.h" | |
| 13 #include "content/browser/media_stream/media_stream_provider.h" | |
| 14 #include "content/browser/media_stream/video_capture_manager.h" | |
| 15 #include "media/video/capture/video_capture_device.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using ::testing::_; | |
| 20 using ::testing::AnyNumber; | |
| 21 using ::testing::InSequence; | |
| 22 using ::testing::Return; | |
| 23 | |
| 24 namespace media_stream { | |
| 25 | |
| 26 // Listener class used to track progress of VideoCaptureManager test | |
| 27 class MockMediaStreamProviderListener : public MediaStreamProviderListener { | |
| 28 public: | |
| 29 MockMediaStreamProviderListener() | |
| 30 : devices_() { | |
| 31 } | |
| 32 ~MockMediaStreamProviderListener() {} | |
| 33 | |
| 34 MOCK_METHOD1(Opened, void(const MediaCaptureSessionId)); | |
| 35 MOCK_METHOD1(Closed, void(const MediaCaptureSessionId)); | |
| 36 MOCK_METHOD1(DevicesEnumerated, void(const MediaCaptureDevices&)); | |
| 37 MOCK_METHOD2(Error, void(const MediaCaptureSessionId, | |
| 38 MediaStreamProviderError)); | |
| 39 | |
| 40 virtual void Opened(MediaStreamType stream_type, | |
| 41 MediaCaptureSessionId capture_session_id) { | |
|
wjia(left Chromium)
2011/05/18 22:45:25
the virtual method can be mocked directly:
MOCK_ME
mflodman1
2011/05/19 07:37:28
Done.
| |
| 42 Opened(capture_session_id); | |
| 43 } | |
| 44 | |
| 45 virtual void Closed(MediaStreamType stream_type, | |
| 46 MediaCaptureSessionId capture_session_id) { | |
| 47 Closed(capture_session_id); | |
| 48 } | |
| 49 | |
| 50 virtual void DevicesEnumerated(MediaStreamType stream_type, | |
| 51 const MediaCaptureDevices& devices) { | |
| 52 devices_.clear(); | |
| 53 for (MediaCaptureDevices::const_iterator it = devices.begin(); | |
| 54 it != devices.end(); | |
| 55 ++it) { | |
| 56 devices_.push_back(*it); | |
| 57 } | |
| 58 DevicesEnumerated(devices); | |
| 59 } | |
| 60 | |
| 61 virtual void Error(MediaStreamType stream_type, | |
| 62 MediaCaptureSessionId capture_session_id, | |
| 63 MediaStreamProviderError error) { | |
| 64 Error(capture_session_id, error); | |
| 65 } | |
| 66 | |
| 67 media_stream::MediaCaptureDevices devices_; | |
| 68 }; // class MockMediaStreamProviderListener | |
| 69 | |
| 70 } // namespace media_stream | |
| 71 | |
| 72 namespace { | |
| 73 | |
| 74 // Needed as an input argument to Start() | |
| 75 class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { | |
| 76 public: | |
| 77 MOCK_METHOD2(OnIncomingCapturedFrame, void(const uint8* data, | |
| 78 int length)); | |
| 79 | |
| 80 virtual void OnError() { | |
| 81 } | |
| 82 void OnFrameInfo( | |
| 83 const media::VideoCaptureDevice::Capability& info) { | |
| 84 } | |
| 85 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | |
| 86 base::Time timestamp) { | |
| 87 OnIncomingCapturedFrame(data, length); | |
| 88 } | |
| 89 }; | |
| 90 | |
| 91 // Test class | |
| 92 class VideoCaptureManagerTest : public testing::Test { | |
| 93 public: | |
| 94 VideoCaptureManagerTest() | |
| 95 : listener_(), | |
| 96 message_loop_(), | |
| 97 io_thread_(), | |
| 98 frame_observer_() { | |
| 99 } | |
| 100 virtual ~VideoCaptureManagerTest() {} | |
| 101 | |
| 102 protected: | |
| 103 virtual void SetUp() { | |
| 104 listener_.reset(new media_stream::MockMediaStreamProviderListener()); | |
| 105 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | |
| 106 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | |
| 107 frame_observer_.reset(new MockFrameObserver()); | |
| 108 } | |
| 109 | |
| 110 virtual void TearDown() { | |
| 111 io_thread_.reset(); | |
| 112 } | |
| 113 | |
| 114 // Called on the VideoCaptureManager thread. | |
| 115 static void PostQuitMessageLoop(MessageLoop* message_loop) { | |
| 116 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 117 } | |
| 118 | |
| 119 // Called on the main thread. | |
| 120 static void PostQuitOnVideoCaptureManagerThread(MessageLoop* message_loop) { | |
| 121 media_stream::VideoCaptureManager::Get()->GetMessageLoop()->PostTask( | |
| 122 FROM_HERE, NewRunnableFunction(&PostQuitMessageLoop, message_loop)); | |
| 123 } | |
| 124 | |
| 125 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | |
| 126 // video_capture_manager internal thread are executed while also processing | |
| 127 // pending task in message_loop_ on the current thread. It is used to | |
| 128 // synchronize with the video capture manager thread when we are stopping a | |
| 129 // video capture device. | |
| 130 void SyncWithVideoCaptureManagerThread() { | |
| 131 message_loop_->PostTask( | |
| 132 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, | |
| 133 message_loop_.get())); | |
| 134 message_loop_->Run(); | |
| 135 } | |
| 136 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; | |
| 137 scoped_ptr<MessageLoop> message_loop_; | |
| 138 scoped_ptr<BrowserThread> io_thread_; | |
| 139 scoped_ptr<MockFrameObserver> frame_observer_; | |
| 140 | |
| 141 private: | |
| 142 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); | |
| 143 }; | |
| 144 | |
| 145 // Test cases | |
| 146 | |
| 147 // Try to open, start, stop and close a device. | |
| 148 TEST_F(VideoCaptureManagerTest, CreateAndClose) { | |
| 149 InSequence s; | |
| 150 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | |
| 151 .Times(1); | |
| 152 EXPECT_CALL(*listener_, Opened(_)) | |
| 153 .Times(1); | |
| 154 EXPECT_CALL(*frame_observer_, OnIncomingCapturedFrame(_, _)) | |
| 155 .Times(AnyNumber()); | |
| 156 EXPECT_CALL(*listener_, Closed(_)) | |
| 157 .Times(1); | |
| 158 | |
| 159 media_stream::VideoCaptureManager* vcm = | |
| 160 media_stream::VideoCaptureManager::Get(); | |
| 161 // Make sure fake devices are used | |
| 162 vcm->UseFakeDevice(); | |
| 163 vcm->Register(listener_.get()); | |
| 164 vcm->EnumerateDevices(); | |
| 165 | |
| 166 // Wait to get device callback... | |
| 167 SyncWithVideoCaptureManagerThread(); | |
| 168 | |
| 169 int video_session_id = vcm->Open(listener_->devices_.front()); | |
| 170 | |
| 171 media::VideoCaptureParams capture_params; | |
| 172 capture_params.session_id = video_session_id; | |
| 173 capture_params.width = 320; | |
| 174 capture_params.height = 240; | |
| 175 capture_params.frame_per_second = 30; | |
| 176 vcm->Start(capture_params, frame_observer_.get()); | |
| 177 | |
| 178 vcm->Stop(video_session_id, NULL); | |
| 179 vcm->Close(video_session_id); | |
| 180 | |
| 181 // Wait to check callbacks before removing the listener | |
| 182 SyncWithVideoCaptureManagerThread(); | |
| 183 vcm->Unregister(); | |
| 184 } | |
| 185 | |
| 186 // Open the same device twice, should fail. | |
| 187 TEST_F(VideoCaptureManagerTest, OpenTwice) { | |
| 188 InSequence s; | |
| 189 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | |
| 190 .Times(1); | |
| 191 EXPECT_CALL(*listener_, Opened(_)) | |
| 192 .Times(1); | |
| 193 EXPECT_CALL(*listener_, Error(_, media_stream::kDeviceAlreadyInUse)) | |
| 194 .Times(1); | |
| 195 EXPECT_CALL(*listener_, Closed(_)) | |
| 196 .Times(1); | |
| 197 | |
| 198 media_stream::VideoCaptureManager* vcm = | |
| 199 media_stream::VideoCaptureManager::Get(); | |
| 200 // Make sure fake devices are used | |
| 201 vcm->UseFakeDevice(); | |
| 202 vcm->Register(listener_.get()); | |
| 203 vcm->EnumerateDevices(); | |
| 204 | |
| 205 // Wait to get device callback... | |
| 206 SyncWithVideoCaptureManagerThread(); | |
| 207 | |
| 208 int video_session_id = vcm->Open(listener_->devices_.front()); | |
| 209 | |
| 210 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' | |
| 211 vcm->Open(listener_->devices_.front()); | |
| 212 | |
| 213 vcm->Close(video_session_id); | |
| 214 | |
| 215 // Wait to check callbacks before removing the listener | |
| 216 SyncWithVideoCaptureManagerThread(); | |
| 217 vcm->Unregister(); | |
| 218 } | |
| 219 | |
| 220 // Open two different devices. | |
| 221 TEST_F(VideoCaptureManagerTest, OpenTwo) { | |
| 222 InSequence s; | |
| 223 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | |
| 224 .Times(1); | |
| 225 EXPECT_CALL(*listener_, Opened(_)) | |
| 226 .Times(1); | |
| 227 EXPECT_CALL(*listener_, Opened(_)) | |
| 228 .Times(1); | |
|
wjia(left Chromium)
2011/05/18 22:45:25
these 2 can be consolidated to .Times(2)
mflodman1
2011/05/19 07:37:28
Done.
| |
| 229 EXPECT_CALL(*listener_, Closed(_)) | |
| 230 .Times(1); | |
| 231 EXPECT_CALL(*listener_, Closed(_)) | |
| 232 .Times(1); | |
| 233 | |
| 234 media_stream::VideoCaptureManager* vcm = | |
| 235 media_stream::VideoCaptureManager::Get(); | |
| 236 // Make sure fake devices are used | |
| 237 vcm->UseFakeDevice(); | |
| 238 vcm->Register(listener_.get()); | |
| 239 vcm->EnumerateDevices(); | |
| 240 | |
| 241 // Wait to get device callback... | |
| 242 SyncWithVideoCaptureManagerThread(); | |
| 243 | |
| 244 media_stream::MediaCaptureDevices::iterator it = | |
| 245 listener_->devices_.begin(); | |
| 246 | |
| 247 int video_session_id_first = vcm->Open(*it); | |
| 248 | |
| 249 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' | |
| 250 ++it; | |
| 251 int video_session_id_second = vcm->Open(*it); | |
| 252 | |
| 253 vcm->Close(video_session_id_first); | |
| 254 vcm->Close(video_session_id_second); | |
| 255 | |
| 256 // Wait to check callbacks before removing the listener | |
| 257 SyncWithVideoCaptureManagerThread(); | |
| 258 vcm->Unregister(); | |
| 259 } | |
| 260 | |
| 261 // Try open a non-existing device. | |
| 262 TEST_F(VideoCaptureManagerTest, OpenNotExisting) { | |
| 263 InSequence s; | |
| 264 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | |
| 265 .Times(1); | |
| 266 EXPECT_CALL(*listener_, Error(_, media_stream::kDeviceNotAvailable)) | |
| 267 .Times(1); | |
| 268 | |
| 269 media_stream::VideoCaptureManager* vcm = | |
| 270 media_stream::VideoCaptureManager::Get(); | |
| 271 // Make sure fake devices are used | |
| 272 vcm->UseFakeDevice(); | |
| 273 vcm->Register(listener_.get()); | |
| 274 vcm->EnumerateDevices(); | |
| 275 | |
| 276 // Wait to get device callback... | |
| 277 SyncWithVideoCaptureManagerThread(); | |
| 278 | |
| 279 media_stream::MediaStreamType stream_type = media_stream::kVideoCapture; | |
| 280 std::string device_name("device_doesnt_exist"); | |
| 281 std::string device_id("id_doesnt_exist"); | |
| 282 media_stream::MediaCaptureDeviceInfo dummy_device(stream_type, device_name, | |
| 283 device_id, false); | |
| 284 | |
| 285 // This should fail with error code 'kDeviceNotAvailable' | |
| 286 vcm->Open(dummy_device); | |
| 287 | |
| 288 // Wait to check callbacks before removing the listener | |
| 289 SyncWithVideoCaptureManagerThread(); | |
| 290 vcm->Unregister(); | |
| 291 } | |
| 292 | |
| 293 // Start a device using "magic" id, i.e. call Start without calling Open. | |
| 294 TEST_F(VideoCaptureManagerTest, StartUsingId) { | |
| 295 InSequence s; | |
| 296 EXPECT_CALL(*listener_, Opened(_)) | |
| 297 .Times(1); | |
| 298 EXPECT_CALL(*frame_observer_, OnIncomingCapturedFrame(_, _)) | |
| 299 .Times(AnyNumber()); | |
| 300 EXPECT_CALL(*listener_, Closed(_)) | |
| 301 .Times(1); | |
| 302 | |
| 303 media_stream::VideoCaptureManager* vcm = | |
| 304 media_stream::VideoCaptureManager::Get(); | |
| 305 // Make sure fake devices are used | |
| 306 vcm->UseFakeDevice(); | |
| 307 vcm->Register(listener_.get()); | |
| 308 | |
| 309 media::VideoCaptureParams capture_params; | |
| 310 capture_params.session_id = | |
| 311 media_stream::VideoCaptureManager::kStartOpenSessionId; | |
| 312 capture_params.width = 320; | |
| 313 capture_params.height = 240; | |
| 314 capture_params.frame_per_second = 30; | |
| 315 // Start shall trigger the Open callback | |
| 316 vcm->Start(capture_params, frame_observer_.get()); | |
| 317 | |
| 318 // Stop shall trigger the Close callback | |
| 319 vcm->Stop(media_stream::VideoCaptureManager::kStartOpenSessionId, NULL); | |
| 320 | |
| 321 // Wait to check callbacks before removing the listener | |
| 322 SyncWithVideoCaptureManagerThread(); | |
| 323 vcm->Unregister(); | |
| 324 } | |
| 325 | |
| 326 } // namespace | |
| OLD | NEW |