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