| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/process_util.h" | 11 #include "base/process_util.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
| 15 #include "content/browser/mock_resource_context.h" |
| 15 #include "content/browser/renderer_host/media/media_stream_manager.h" | 16 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 16 #include "content/browser/renderer_host/media/video_capture_host.h" | 17 #include "content/browser/renderer_host/media/video_capture_host.h" |
| 17 #include "content/browser/renderer_host/media/video_capture_manager.h" | 18 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 19 #include "content/browser/resource_context.h" |
| 18 #include "content/common/media/video_capture_messages.h" | 20 #include "content/common/media/video_capture_messages.h" |
| 19 #include "media/video/capture/video_capture_types.h" | 21 #include "media/video/capture/video_capture_types.h" |
| 20 | 22 |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 25 |
| 24 using ::testing::_; | 26 using ::testing::_; |
| 25 using ::testing::AtLeast; | 27 using ::testing::AtLeast; |
| 26 using ::testing::AnyNumber; | 28 using ::testing::AnyNumber; |
| 27 using ::testing::DoAll; | 29 using ::testing::DoAll; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 64 } |
| 63 } | 65 } |
| 64 | 66 |
| 65 private: | 67 private: |
| 66 file_util::ScopedFILE file_; | 68 file_util::ScopedFILE file_; |
| 67 int expected_size_; | 69 int expected_size_; |
| 68 }; | 70 }; |
| 69 | 71 |
| 70 class MockVideoCaptureHost : public VideoCaptureHost { | 72 class MockVideoCaptureHost : public VideoCaptureHost { |
| 71 public: | 73 public: |
| 72 MockVideoCaptureHost() : return_buffers_(false), dump_video_(false) {} | 74 explicit MockVideoCaptureHost(content::ResourceContext* resource_context) |
| 75 : VideoCaptureHost(resource_context), |
| 76 return_buffers_(false), |
| 77 dump_video_(false) {} |
| 73 virtual ~MockVideoCaptureHost() { | 78 virtual ~MockVideoCaptureHost() { |
| 74 STLDeleteContainerPairSecondPointers(filled_dib_.begin(), | 79 STLDeleteContainerPairSecondPointers(filled_dib_.begin(), |
| 75 filled_dib_.end()); | 80 filled_dib_.end()); |
| 76 } | 81 } |
| 77 | 82 |
| 78 // A list of mock methods. | 83 // A list of mock methods. |
| 79 MOCK_METHOD4(OnNewBufferCreated, | 84 MOCK_METHOD4(OnNewBufferCreated, |
| 80 void(int device_id, base::SharedMemoryHandle handle, | 85 void(int device_id, base::SharedMemoryHandle handle, |
| 81 int length, int buffer_id)); | 86 int length, int buffer_id)); |
| 82 MOCK_METHOD3(OnBufferFilled, | 87 MOCK_METHOD3(OnBufferFilled, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 } | 188 } |
| 184 | 189 |
| 185 class VideoCaptureHostTest : public testing::Test { | 190 class VideoCaptureHostTest : public testing::Test { |
| 186 public: | 191 public: |
| 187 VideoCaptureHostTest() {} | 192 VideoCaptureHostTest() {} |
| 188 | 193 |
| 189 protected: | 194 protected: |
| 190 virtual void SetUp() { | 195 virtual void SetUp() { |
| 191 // Create a message loop so VideoCaptureHostTest can use it. | 196 // Create a message loop so VideoCaptureHostTest can use it. |
| 192 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 197 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 198 |
| 199 // ResourceContext must be created on the UI thread. |
| 200 ui_thread_.reset(new BrowserThread(BrowserThread::UI, message_loop_.get())); |
| 201 |
| 202 // MediaStreamManager must be created on the IO thread. |
| 193 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | 203 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); |
| 194 // Setup the VideoCaptureManager to use fake video capture device. | 204 |
| 205 // Create a MediaStreamManager instance and hand over pointer to |
| 206 // ResourceContext. |
| 207 media_stream_manager_.reset(new media_stream::MediaStreamManager()); |
| 208 |
| 195 #ifndef TEST_REAL_CAPTURE_DEVICE | 209 #ifndef TEST_REAL_CAPTURE_DEVICE |
| 196 media_stream::VideoCaptureManager* manager = | 210 media_stream_manager_->UseFakeDevice(); |
| 197 media_stream::MediaStreamManager::Get()->video_capture_manager(); | |
| 198 manager->UseFakeDevice(); | |
| 199 #endif | 211 #endif |
| 200 host_ = new MockVideoCaptureHost(); | 212 |
| 213 content::MockResourceContext::GetInstance()->set_media_stream_manager( |
| 214 media_stream_manager_.get()); |
| 215 host_ = new MockVideoCaptureHost( |
| 216 content::MockResourceContext::GetInstance()); |
| 201 | 217 |
| 202 // Simulate IPC channel connected. | 218 // Simulate IPC channel connected. |
| 203 host_->OnChannelConnected(base::GetCurrentProcId()); | 219 host_->OnChannelConnected(base::GetCurrentProcId()); |
| 204 } | 220 } |
| 205 | 221 |
| 206 virtual void TearDown() { | 222 virtual void TearDown() { |
| 207 // Verifies and removes the expectations on host_ and | 223 // Verifies and removes the expectations on host_ and |
| 208 // returns true iff successful. | 224 // returns true iff successful. |
| 209 Mock::VerifyAndClearExpectations(host_); | 225 Mock::VerifyAndClearExpectations(host_); |
| 210 | 226 |
| 211 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, | 227 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, |
| 212 media::VideoCapture::kStopped)) | 228 media::VideoCapture::kStopped)) |
| 213 .Times(AnyNumber()); | 229 .Times(AnyNumber()); |
| 214 | 230 |
| 215 // Simulate closing the IPC channel. | 231 // Simulate closing the IPC channel. |
| 216 host_->OnChannelClosing(); | 232 host_->OnChannelClosing(); |
| 217 | 233 |
| 218 // Release the reference to the mock object. The object will be destructed | 234 // Release the reference to the mock object. The object will be destructed |
| 219 // on message_loop_. | 235 // on message_loop_. |
| 220 host_ = NULL; | 236 host_ = NULL; |
| 221 | 237 |
| 222 // We need to continue running message_loop_ to complete all destructions. | 238 // We need to continue running message_loop_ to complete all destructions. |
| 223 SyncWithVideoCaptureManagerThread(); | 239 SyncWithVideoCaptureManagerThread(); |
| 224 | 240 |
| 225 io_thread_.reset(); | 241 content::MockResourceContext::GetInstance()->set_media_stream_manager(NULL); |
| 226 } | 242 } |
| 227 | 243 |
| 228 // Called on the VideoCaptureManager thread. | 244 // Called on the VideoCaptureManager thread. |
| 229 static void PostQuitMessageLoop(MessageLoop* message_loop) { | 245 static void PostQuitMessageLoop(MessageLoop* message_loop) { |
| 230 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 246 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 231 } | 247 } |
| 232 | 248 |
| 233 // Called on the main thread. | 249 // Called on the main thread. |
| 234 static void PostQuitOnVideoCaptureManagerThread(MessageLoop* message_loop) { | 250 static void PostQuitOnVideoCaptureManagerThread( |
| 235 media_stream::MediaStreamManager::Get()->video_capture_manager()-> | 251 MessageLoop* message_loop, content::ResourceContext* resource_context) { |
| 252 resource_context->media_stream_manager()->video_capture_manager()-> |
| 236 GetMessageLoop()->PostTask(FROM_HERE, | 253 GetMessageLoop()->PostTask(FROM_HERE, |
| 237 NewRunnableFunction( | 254 NewRunnableFunction( |
| 238 &PostQuitMessageLoop, message_loop)); | 255 &PostQuitMessageLoop, message_loop)); |
| 239 } | 256 } |
| 240 | 257 |
| 241 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | 258 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the |
| 242 // video_capture_manager thread are executed while also processing pending | 259 // video_capture_manager thread are executed while also processing pending |
| 243 // task in message_loop_ on the current thread. It is used to synchronize | 260 // task in message_loop_ on the current thread. It is used to synchronize |
| 244 // with the video capture manager thread when we are stopping a video | 261 // with the video capture manager thread when we are stopping a video |
| 245 // capture device. | 262 // capture device. |
| 246 void SyncWithVideoCaptureManagerThread() { | 263 void SyncWithVideoCaptureManagerThread() { |
| 247 message_loop_->PostTask( | 264 message_loop_->PostTask( |
| 248 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, | 265 FROM_HERE, |
| 249 message_loop_.get())); | 266 NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, |
| 267 message_loop_.get(), |
| 268 content::MockResourceContext::GetInstance())); |
| 250 message_loop_->Run(); | 269 message_loop_->Run(); |
| 251 } | 270 } |
| 252 | 271 |
| 253 void StartCapture() { | 272 void StartCapture() { |
| 254 InSequence s; | 273 InSequence s; |
| 255 // 1. Newly created buffers will arrive. | 274 // 1. Newly created buffers will arrive. |
| 256 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) | 275 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) |
| 257 .Times(AnyNumber()) | 276 .Times(AnyNumber()) |
| 258 .WillRepeatedly(Return()); | 277 .WillRepeatedly(Return()); |
| 259 | 278 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 media::VideoCapture::kError)) | 354 media::VideoCapture::kError)) |
| 336 .Times(1); | 355 .Times(1); |
| 337 VideoCaptureControllerID id(kDeviceId); | 356 VideoCaptureControllerID id(kDeviceId); |
| 338 host_->OnError(id); | 357 host_->OnError(id); |
| 339 SyncWithVideoCaptureManagerThread(); | 358 SyncWithVideoCaptureManagerThread(); |
| 340 } | 359 } |
| 341 | 360 |
| 342 scoped_refptr<MockVideoCaptureHost> host_; | 361 scoped_refptr<MockVideoCaptureHost> host_; |
| 343 private: | 362 private: |
| 344 scoped_ptr<MessageLoop> message_loop_; | 363 scoped_ptr<MessageLoop> message_loop_; |
| 364 scoped_ptr<BrowserThread> ui_thread_; |
| 345 scoped_ptr<BrowserThread> io_thread_; | 365 scoped_ptr<BrowserThread> io_thread_; |
| 366 scoped_ptr<media_stream::MediaStreamManager> media_stream_manager_; |
| 346 | 367 |
| 347 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | 368 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); |
| 348 }; | 369 }; |
| 349 | 370 |
| 350 TEST_F(VideoCaptureHostTest, StartCapture) { | 371 TEST_F(VideoCaptureHostTest, StartCapture) { |
| 351 StartCapture(); | 372 StartCapture(); |
| 352 } | 373 } |
| 353 | 374 |
| 354 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { | 375 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { |
| 355 StartCapture(); | 376 StartCapture(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 376 } | 397 } |
| 377 | 398 |
| 378 #ifdef DUMP_VIDEO | 399 #ifdef DUMP_VIDEO |
| 379 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 400 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
| 380 CaptureAndDumpVideo(640, 480, 30); | 401 CaptureAndDumpVideo(640, 480, 30); |
| 381 } | 402 } |
| 382 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 403 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
| 383 CaptureAndDumpVideo(1280, 720, 30); | 404 CaptureAndDumpVideo(1280, 720, 30); |
| 384 } | 405 } |
| 385 #endif | 406 #endif |
| OLD | NEW |