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/renderer_host/media/media_stream_manager.h" | 15 #include "content/browser/renderer_host/media/media_stream_manager.h" |
16 #include "content/browser/renderer_host/media/video_capture_host.h" | 16 #include "content/browser/renderer_host/media/video_capture_host.h" |
17 #include "content/browser/renderer_host/media/video_capture_manager.h" | 17 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 18 #include "content/browser/resource_context.h" |
18 #include "content/common/media/video_capture_messages.h" | 19 #include "content/common/media/video_capture_messages.h" |
19 #include "media/video/capture/video_capture_types.h" | 20 #include "media/video/capture/video_capture_types.h" |
20 | 21 |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 using ::testing::_; | 25 using ::testing::_; |
25 using ::testing::AtLeast; | 26 using ::testing::AtLeast; |
26 using ::testing::AnyNumber; | 27 using ::testing::AnyNumber; |
27 using ::testing::DoAll; | 28 using ::testing::DoAll; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 if (file_.get() != NULL) { | 61 if (file_.get() != NULL) { |
61 fwrite(buffer, expected_size_, 1, file_.get()); | 62 fwrite(buffer, expected_size_, 1, file_.get()); |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 private: | 66 private: |
66 file_util::ScopedFILE file_; | 67 file_util::ScopedFILE file_; |
67 int expected_size_; | 68 int expected_size_; |
68 }; | 69 }; |
69 | 70 |
| 71 namespace content { |
| 72 // Test version of ResourceContext |
| 73 class MockVideoCaptureResourceContext : public ResourceContext { |
| 74 public: |
| 75 MockVideoCaptureResourceContext() {} |
| 76 virtual ~MockVideoCaptureResourceContext() {} |
| 77 |
| 78 virtual media_stream::MediaStreamManager* media_stream_manger() const { |
| 79 return media_stream_manager_.get(); |
| 80 } |
| 81 |
| 82 void set_media_stream_manager( |
| 83 media_stream::MediaStreamManager* media_stream_manager) { |
| 84 media_stream_manager_ = media_stream_manager; |
| 85 } |
| 86 |
| 87 private: |
| 88 // Pure virtual in ResourceContext. |
| 89 virtual void EnsureInitialized() const {} |
| 90 |
| 91 scoped_refptr<media_stream::MediaStreamManager> media_stream_manager_; |
| 92 }; |
| 93 |
| 94 } // namespace content |
| 95 |
70 class MockVideoCaptureHost : public VideoCaptureHost { | 96 class MockVideoCaptureHost : public VideoCaptureHost { |
71 public: | 97 public: |
72 MockVideoCaptureHost() : return_buffers_(false), dump_video_(false) {} | 98 explicit MockVideoCaptureHost(content::ResourceContext* resource_context) |
| 99 : VideoCaptureHost(resource_context), |
| 100 return_buffers_(false), |
| 101 dump_video_(false) {} |
73 virtual ~MockVideoCaptureHost() { | 102 virtual ~MockVideoCaptureHost() { |
74 STLDeleteContainerPairSecondPointers(filled_dib_.begin(), | 103 STLDeleteContainerPairSecondPointers(filled_dib_.begin(), |
75 filled_dib_.end()); | 104 filled_dib_.end()); |
76 } | 105 } |
77 | 106 |
78 // A list of mock methods. | 107 // A list of mock methods. |
79 MOCK_METHOD4(OnNewBufferCreated, | 108 MOCK_METHOD4(OnNewBufferCreated, |
80 void(int device_id, base::SharedMemoryHandle handle, | 109 void(int device_id, base::SharedMemoryHandle handle, |
81 int length, int buffer_id)); | 110 int length, int buffer_id)); |
82 MOCK_METHOD3(OnBufferFilled, | 111 MOCK_METHOD3(OnBufferFilled, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 } | 212 } |
184 | 213 |
185 class VideoCaptureHostTest : public testing::Test { | 214 class VideoCaptureHostTest : public testing::Test { |
186 public: | 215 public: |
187 VideoCaptureHostTest() {} | 216 VideoCaptureHostTest() {} |
188 | 217 |
189 protected: | 218 protected: |
190 virtual void SetUp() { | 219 virtual void SetUp() { |
191 // Create a message loop so VideoCaptureHostTest can use it. | 220 // Create a message loop so VideoCaptureHostTest can use it. |
192 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 221 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 222 |
| 223 // ResourceContext must be created on the UI thread. |
| 224 ui_thread_.reset(new BrowserThread(BrowserThread::UI, message_loop_.get())); |
| 225 resource_context_.reset(new content::MockVideoCaptureResourceContext()); |
| 226 |
| 227 // MediaStreamManager must be created on the IO thread. |
193 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | 228 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); |
| 229 |
| 230 // Create a MediaStreamManager ad hand over ownership to ResourceContext. |
| 231 media_stream::MediaStreamManager* media_stream_manager = |
| 232 new media_stream::MediaStreamManager(); |
| 233 resource_context_->set_media_stream_manager(media_stream_manager); |
| 234 |
194 // Setup the VideoCaptureManager to use fake video capture device. | 235 // Setup the VideoCaptureManager to use fake video capture device. |
195 #ifndef TEST_REAL_CAPTURE_DEVICE | 236 #ifndef TEST_REAL_CAPTURE_DEVICE |
196 media_stream::VideoCaptureManager* manager = | 237 resource_context_->media_stream_manager()->video_capture_manager()-> |
197 media_stream::MediaStreamManager::Get()->video_capture_manager(); | 238 UseFakeDevice(); |
198 manager->UseFakeDevice(); | |
199 #endif | 239 #endif |
200 host_ = new MockVideoCaptureHost(); | 240 host_ = new MockVideoCaptureHost(resource_context_.get()); |
201 | 241 |
202 // Simulate IPC channel connected. | 242 // Simulate IPC channel connected. |
203 host_->OnChannelConnected(base::GetCurrentProcId()); | 243 host_->OnChannelConnected(base::GetCurrentProcId()); |
204 } | 244 } |
205 | 245 |
206 virtual void TearDown() { | 246 virtual void TearDown() { |
207 // Verifies and removes the expectations on host_ and | 247 // Verifies and removes the expectations on host_ and |
208 // returns true iff successful. | 248 // returns true iff successful. |
209 Mock::VerifyAndClearExpectations(host_); | 249 Mock::VerifyAndClearExpectations(host_); |
210 | 250 |
211 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, | 251 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, |
212 media::VideoCapture::kStopped)) | 252 media::VideoCapture::kStopped)) |
213 .Times(AnyNumber()); | 253 .Times(AnyNumber()); |
214 | 254 |
215 // Simulate closing the IPC channel. | 255 // Simulate closing the IPC channel. |
216 host_->OnChannelClosing(); | 256 host_->OnChannelClosing(); |
217 | 257 |
218 // Release the reference to the mock object. The object will be destructed | 258 // Release the reference to the mock object. The object will be destructed |
219 // on message_loop_. | 259 // on message_loop_. |
220 host_ = NULL; | 260 host_ = NULL; |
221 | 261 |
222 // We need to continue running message_loop_ to complete all destructions. | 262 // We need to continue running message_loop_ to complete all destructions. |
223 SyncWithVideoCaptureManagerThread(); | 263 SyncWithVideoCaptureManagerThread(); |
224 | 264 |
225 io_thread_.reset(); | 265 io_thread_.reset(); |
| 266 ui_thread_.reset(); |
226 } | 267 } |
227 | 268 |
228 // Called on the VideoCaptureManager thread. | 269 // Called on the VideoCaptureManager thread. |
229 static void PostQuitMessageLoop(MessageLoop* message_loop) { | 270 static void PostQuitMessageLoop(MessageLoop* message_loop) { |
230 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 271 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
231 } | 272 } |
232 | 273 |
233 // Called on the main thread. | 274 // Called on the main thread. |
234 static void PostQuitOnVideoCaptureManagerThread(MessageLoop* message_loop) { | 275 static void PostQuitOnVideoCaptureManagerThread( |
235 media_stream::MediaStreamManager::Get()->video_capture_manager()-> | 276 MessageLoop* message_loop, content::ResourceContext* resource_context) { |
| 277 resource_context->media_stream_manager()->video_capture_manager()-> |
236 GetMessageLoop()->PostTask(FROM_HERE, | 278 GetMessageLoop()->PostTask(FROM_HERE, |
237 NewRunnableFunction( | 279 NewRunnableFunction( |
238 &PostQuitMessageLoop, message_loop)); | 280 &PostQuitMessageLoop, message_loop)); |
239 } | 281 } |
240 | 282 |
241 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | 283 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the |
242 // video_capture_manager thread are executed while also processing pending | 284 // video_capture_manager thread are executed while also processing pending |
243 // task in message_loop_ on the current thread. It is used to synchronize | 285 // 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 | 286 // with the video capture manager thread when we are stopping a video |
245 // capture device. | 287 // capture device. |
246 void SyncWithVideoCaptureManagerThread() { | 288 void SyncWithVideoCaptureManagerThread() { |
247 message_loop_->PostTask( | 289 message_loop_->PostTask( |
248 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, | 290 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, |
249 message_loop_.get())); | 291 message_loop_.get(), |
| 292 resource_context_.get())); |
250 message_loop_->Run(); | 293 message_loop_->Run(); |
251 } | 294 } |
252 | 295 |
253 void StartCapture() { | 296 void StartCapture() { |
254 InSequence s; | 297 InSequence s; |
255 // 1. Newly created buffers will arrive. | 298 // 1. Newly created buffers will arrive. |
256 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) | 299 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) |
257 .Times(AnyNumber()) | 300 .Times(AnyNumber()) |
258 .WillRepeatedly(Return()); | 301 .WillRepeatedly(Return()); |
259 | 302 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 media::VideoCapture::kError)) | 378 media::VideoCapture::kError)) |
336 .Times(1); | 379 .Times(1); |
337 VideoCaptureControllerID id(kDeviceId); | 380 VideoCaptureControllerID id(kDeviceId); |
338 host_->OnError(id); | 381 host_->OnError(id); |
339 SyncWithVideoCaptureManagerThread(); | 382 SyncWithVideoCaptureManagerThread(); |
340 } | 383 } |
341 | 384 |
342 scoped_refptr<MockVideoCaptureHost> host_; | 385 scoped_refptr<MockVideoCaptureHost> host_; |
343 private: | 386 private: |
344 scoped_ptr<MessageLoop> message_loop_; | 387 scoped_ptr<MessageLoop> message_loop_; |
| 388 scoped_ptr<BrowserThread> ui_thread_; |
345 scoped_ptr<BrowserThread> io_thread_; | 389 scoped_ptr<BrowserThread> io_thread_; |
| 390 scoped_ptr<content::ResourceContext> resource_context_; |
346 | 391 |
347 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | 392 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); |
348 }; | 393 }; |
349 | 394 |
350 TEST_F(VideoCaptureHostTest, StartCapture) { | 395 TEST_F(VideoCaptureHostTest, StartCapture) { |
351 StartCapture(); | 396 StartCapture(); |
352 } | 397 } |
353 | 398 |
354 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { | 399 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { |
355 StartCapture(); | 400 StartCapture(); |
(...skipping 20 matching lines...) Expand all Loading... |
376 } | 421 } |
377 | 422 |
378 #ifdef DUMP_VIDEO | 423 #ifdef DUMP_VIDEO |
379 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 424 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
380 CaptureAndDumpVideo(640, 480, 30); | 425 CaptureAndDumpVideo(640, 480, 30); |
381 } | 426 } |
382 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 427 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
383 CaptureAndDumpVideo(1280, 720, 30); | 428 CaptureAndDumpVideo(1280, 720, 30); |
384 } | 429 } |
385 #endif | 430 #endif |
OLD | NEW |