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 const MediaCaptureSessionId capture_session_id) { | |
| 42 Opened(capture_session_id); | |
| 43 } | |
| 44 | |
| 45 virtual void Closed(MediaStreamType stream_type, | |
| 46 const 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 const 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 // Needed as an input argument to Start() | |
| 73 class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { | |
|
John Knottenbelt
2011/05/11 11:36:35
Do you intend any of these classes to be reused ou
mflodman1
2011/05/15 20:24:00
Done.
| |
| 74 public: | |
| 75 MOCK_METHOD2(OnIncomingCapturedFrame, void(const uint8* data, | |
| 76 int length)); | |
| 77 | |
| 78 virtual void OnError() { | |
| 79 } | |
| 80 void OnFrameInfo( | |
| 81 const media::VideoCaptureDevice::Capability& info) { | |
| 82 } | |
| 83 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | |
| 84 base::Time timestamp) { | |
| 85 OnIncomingCapturedFrame(data, length); | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 // Test class | |
| 90 class VideoCaptureManagerTest : public testing::Test { | |
| 91 public: | |
| 92 VideoCaptureManagerTest() | |
| 93 : listener_(), | |
| 94 message_loop_(), | |
| 95 io_thread_(), | |
| 96 frame_observer_() { | |
| 97 } | |
| 98 virtual ~VideoCaptureManagerTest() {} | |
| 99 | |
| 100 protected: | |
| 101 virtual void SetUp() { | |
| 102 // Call this function to use fake devices instead of real devices. | |
| 103 media_stream::VideoCaptureManager::CreateTestManager(); | |
| 104 | |
| 105 listener_.reset(new media_stream::MockMediaStreamProviderListener()); | |
| 106 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | |
| 107 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | |
| 108 frame_observer_.reset(new MockFrameObserver()); | |
| 109 } | |
| 110 | |
| 111 virtual void TearDown() { | |
| 112 io_thread_.reset(); | |
| 113 } | |
| 114 | |
| 115 // Called on the VideoCaptureManager thread. | |
| 116 static void PostQuitMessageLoop(MessageLoop* message_loop) { | |
| 117 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 118 } | |
| 119 | |
| 120 // Called on the main thread. | |
| 121 static void PostQuitOnVideoCaptureManagerThread(MessageLoop* message_loop) { | |
| 122 media_stream::VideoCaptureManager::Get()->GetMessageLoop()->PostTask( | |
| 123 FROM_HERE, NewRunnableFunction(&PostQuitMessageLoop, message_loop)); | |
| 124 } | |
| 125 | |
| 126 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | |
| 127 // video_capture_manager internal thread are executed while also processing | |
| 128 // pending task in message_loop_ on the current thread. It is used to | |
| 129 // synchronize with the video capture manager thread when we are stopping a | |
| 130 // video capture device. | |
| 131 void SyncWithVideoCaptureManagerThread() { | |
| 132 message_loop_->PostTask( | |
| 133 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, | |
| 134 message_loop_.get())); | |
| 135 message_loop_->Run(); | |
| 136 } | |
| 137 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; | |
| 138 scoped_ptr<MessageLoop> message_loop_; | |
| 139 scoped_ptr<BrowserThread> io_thread_; | |
| 140 scoped_ptr<MockFrameObserver> frame_observer_; | |
| 141 | |
| 142 private: | |
| 143 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); | |
| 144 }; | |
| 145 | |
| 146 // Test cases | |
| 147 | |
| 148 // Try to open, start, stop and close a device. | |
| 149 TEST_F(VideoCaptureManagerTest, CreateAndClose) { | |
| 150 InSequence s; | |
| 151 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | |
| 152 .Times(1); | |
| 153 EXPECT_CALL(*listener_, Opened(_)) | |
| 154 .Times(1); | |
| 155 EXPECT_CALL(*frame_observer_, OnIncomingCapturedFrame(_, _)) | |
| 156 .Times(AnyNumber()); | |
| 157 EXPECT_CALL(*listener_, Closed(_)) | |
| 158 .Times(1); | |
| 159 | |
| 160 media_stream::VideoCaptureManager* vcm = | |
| 161 media_stream::VideoCaptureManager::Get(); | |
| 162 vcm->Register(media_stream::kVideoCapture, listener_.get()); | |
| 163 vcm->EnumerateDevices(media_stream::kVideoCapture); | |
| 164 | |
| 165 // Wait to get device callback... | |
| 166 SyncWithVideoCaptureManagerThread(); | |
| 167 | |
| 168 int video_session_id = vcm->Open(media_stream::kVideoCapture, | |
| 169 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(media_stream::kVideoCapture, video_session_id); | |
| 180 | |
| 181 // Wait to check callbacks before removing the listener | |
| 182 SyncWithVideoCaptureManagerThread(); | |
| 183 vcm->Unregister(media_stream::kVideoCapture, listener_.get()); | |
| 184 } | |
| 185 | |
| 186 // Open the same device twice, should faild. | |
|
John Knottenbelt
2011/05/11 11:36:35
Nit: faild => fail
mflodman1
2011/05/15 20:24:00
Done.
| |
| 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 vcm->Register(media_stream::kVideoCapture, listener_.get()); | |
| 201 vcm->EnumerateDevices(media_stream::kVideoCapture); | |
| 202 | |
| 203 // Wait to get device callback... | |
| 204 SyncWithVideoCaptureManagerThread(); | |
| 205 | |
| 206 int video_session_id = vcm->Open(media_stream::kVideoCapture, | |
| 207 listener_->devices_.front()); | |
| 208 | |
| 209 // This should trigger an error callback with error code 'kDeviceAlreadyInUse' | |
| 210 vcm->Open(media_stream::kVideoCapture, listener_->devices_.front()); | |
| 211 | |
| 212 vcm->Close(media_stream::kVideoCapture, video_session_id); | |
| 213 | |
| 214 // Wait to check callbacks before removing the listener | |
| 215 SyncWithVideoCaptureManagerThread(); | |
| 216 vcm->Unregister(media_stream::kVideoCapture, listener_.get()); | |
| 217 } | |
| 218 | |
| 219 // Try open a non-existing device. | |
| 220 TEST_F(VideoCaptureManagerTest, OpenNotExisting) { | |
| 221 InSequence s; | |
| 222 EXPECT_CALL(*listener_, DevicesEnumerated(_)) | |
| 223 .Times(1); | |
| 224 EXPECT_CALL(*listener_, Error(_, media_stream::kDeviceNotAvailable)) | |
| 225 .Times(1); | |
| 226 | |
| 227 media_stream::VideoCaptureManager* vcm = | |
| 228 media_stream::VideoCaptureManager::Get(); | |
| 229 vcm->Register(media_stream::kVideoCapture, listener_.get()); | |
| 230 vcm->EnumerateDevices(media_stream::kVideoCapture); | |
| 231 | |
| 232 // Wait to get device callback... | |
| 233 SyncWithVideoCaptureManagerThread(); | |
| 234 | |
| 235 media_stream::MediaStreamType stream_type = media_stream::kVideoCapture; | |
| 236 std::string device_name("device_doesnt_exist"); | |
| 237 std::string device_id("id_doesnt_exist"); | |
| 238 media_stream::MediaCaptureDeviceInfo dummy_device(stream_type, device_name, | |
| 239 device_id, false); | |
| 240 | |
| 241 // This should fail with error code 'kDeviceNotAvailable' | |
| 242 vcm->Open(media_stream::kVideoCapture, dummy_device); | |
| 243 | |
| 244 // Wait to check callbacks before removing the listener | |
| 245 SyncWithVideoCaptureManagerThread(); | |
| 246 vcm->Unregister(media_stream::kVideoCapture, listener_.get()); | |
| 247 } | |
| 248 | |
| 249 // Start a device using "magic" id. | |
| 250 TEST_F(VideoCaptureManagerTest, StartUsingId) { | |
| 251 InSequence s; | |
| 252 EXPECT_CALL(*listener_, Opened(_)) | |
| 253 .Times(1); | |
| 254 EXPECT_CALL(*frame_observer_, OnIncomingCapturedFrame(_, _)) | |
| 255 .Times(AnyNumber()); | |
| 256 EXPECT_CALL(*listener_, Closed(_)) | |
| 257 .Times(1); | |
| 258 | |
| 259 media_stream::VideoCaptureManager* vcm = | |
| 260 media_stream::VideoCaptureManager::Get(); | |
| 261 vcm->Register(media_stream::kVideoCapture, listener_.get()); | |
| 262 | |
| 263 media::VideoCaptureParams capture_params; | |
| 264 capture_params.session_id = | |
| 265 media_stream::VideoCaptureManager::kStartOpenSessionId; | |
| 266 capture_params.width = 320; | |
| 267 capture_params.height = 240; | |
| 268 capture_params.frame_per_second = 30; | |
| 269 // Start shall trigger the Open callback | |
| 270 vcm->Start(capture_params, frame_observer_.get()); | |
| 271 | |
| 272 // Stop shall trigger the Close callback | |
| 273 vcm->Stop(media_stream::VideoCaptureManager::kStartOpenSessionId, NULL); | |
| 274 | |
| 275 // Wait to check callbacks before removing the listener | |
| 276 SyncWithVideoCaptureManagerThread(); | |
| 277 vcm->Unregister(media_stream::kVideoCapture, listener_.get()); | |
| 278 } | |
| OLD | NEW |