Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Unit test for VideoCaptureManager. | 5 // Unit test for VideoCaptureManager. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 public: | 66 public: |
| 67 virtual void OnError() {} | 67 virtual void OnError() {} |
| 68 void OnFrameInfo(const media::VideoCaptureCapability& info) {} | 68 void OnFrameInfo(const media::VideoCaptureCapability& info) {} |
| 69 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | 69 virtual void OnIncomingCapturedFrame(const uint8* data, int length, |
| 70 base::Time timestamp) {} | 70 base::Time timestamp) {} |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 // Test class | 73 // Test class |
| 74 class VideoCaptureManagerTest : public testing::Test { | 74 class VideoCaptureManagerTest : public testing::Test { |
| 75 public: | 75 public: |
| 76 VideoCaptureManagerTest() | 76 VideoCaptureManagerTest() {} |
| 77 : vcm_(), | |
| 78 listener_(), | |
| 79 message_loop_(), | |
| 80 io_thread_(), | |
| 81 frame_observer_() { | |
| 82 } | |
| 83 virtual ~VideoCaptureManagerTest() {} | 77 virtual ~VideoCaptureManagerTest() {} |
| 84 | 78 |
| 85 protected: | 79 protected: |
| 86 virtual void SetUp() { | 80 virtual void SetUp() { |
| 87 listener_.reset(new media_stream::MockMediaStreamProviderListener()); | 81 listener_.reset(new media_stream::MockMediaStreamProviderListener()); |
| 88 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 82 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 89 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 83 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
| 90 message_loop_.get())); | 84 message_loop_.get())); |
| 91 vcm_ = new media_stream::VideoCaptureManager(); | 85 device_thread_.reset(new base::Thread("VideoCaptureManagerTestThread")); |
| 86 CHECK(device_thread_->Start()); | |
| 87 vcm_ = new media_stream::VideoCaptureManager( | |
| 88 device_thread_->message_loop_proxy()); | |
| 92 vcm_->UseFakeDevice(); | 89 vcm_->UseFakeDevice(); |
| 93 vcm_->Register(listener_.get()); | 90 vcm_->Register(listener_.get()); |
| 94 frame_observer_.reset(new MockFrameObserver()); | 91 frame_observer_.reset(new MockFrameObserver()); |
| 95 } | 92 } |
| 96 | 93 |
| 97 virtual void TearDown() { | 94 virtual void TearDown() { |
| 98 io_thread_.reset(); | 95 io_thread_.reset(); |
| 96 device_thread_->Stop(); | |
|
scherkus (not reviewing)
2012/06/27 00:52:31
nit: should we be stopping/resetting the threads i
no longer working on chromium
2012/06/27 14:07:16
I will remove the device_thread_ here and inject t
| |
| 99 } | 97 } |
| 100 | 98 |
| 101 // Called on the VideoCaptureManager thread. | 99 // Called on the VideoCaptureManager thread. |
| 102 static void PostQuitMessageLoop(MessageLoop* message_loop) { | 100 static void PostQuitMessageLoop(MessageLoop* message_loop) { |
| 103 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 101 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 104 } | 102 } |
| 105 | 103 |
| 106 // Called on the main thread. | 104 // Called on the main thread. |
| 107 static void PostQuitOnVideoCaptureManagerThread( | 105 static void PostQuitOnVideoCaptureManagerThread( |
| 108 MessageLoop* message_loop, media_stream::VideoCaptureManager* vcm) { | 106 MessageLoop* message_loop, MessageLoop* device_message_loop) { |
| 109 vcm->GetMessageLoop()->PostTask( | 107 device_message_loop->PostTask( |
| 110 FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop)); | 108 FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop)); |
| 111 } | 109 } |
| 112 | 110 |
| 113 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | 111 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the |
| 114 // video_capture_manager internal thread are executed while also processing | 112 // video_capture_manager internal thread are executed while also processing |
| 115 // pending task in message_loop_ on the current thread. It is used to | 113 // pending task in message_loop_ on the current thread. It is used to |
| 116 // synchronize with the video capture manager thread when we are stopping a | 114 // synchronize with the video capture manager thread when we are stopping a |
| 117 // video capture device. | 115 // video capture device. |
| 118 void SyncWithVideoCaptureManagerThread() { | 116 void SyncWithVideoCaptureManagerThread() { |
| 119 message_loop_->PostTask( | 117 message_loop_->PostTask( |
| 120 FROM_HERE, base::Bind(&PostQuitOnVideoCaptureManagerThread, | 118 FROM_HERE, base::Bind(&PostQuitOnVideoCaptureManagerThread, |
| 121 message_loop_.get(), | 119 message_loop_.get(), |
| 122 vcm_)); | 120 device_thread_->message_loop())); |
| 123 message_loop_->Run(); | 121 message_loop_->Run(); |
| 124 } | 122 } |
| 125 scoped_refptr<media_stream::VideoCaptureManager> vcm_; | 123 scoped_refptr<media_stream::VideoCaptureManager> vcm_; |
| 126 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; | 124 scoped_ptr<media_stream::MockMediaStreamProviderListener> listener_; |
| 127 scoped_ptr<MessageLoop> message_loop_; | 125 scoped_ptr<MessageLoop> message_loop_; |
| 128 scoped_ptr<BrowserThreadImpl> io_thread_; | 126 scoped_ptr<BrowserThreadImpl> io_thread_; |
| 127 scoped_ptr<base::Thread> device_thread_; | |
| 129 scoped_ptr<MockFrameObserver> frame_observer_; | 128 scoped_ptr<MockFrameObserver> frame_observer_; |
| 130 | 129 |
| 131 private: | 130 private: |
| 132 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); | 131 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManagerTest); |
| 133 }; | 132 }; |
| 134 | 133 |
| 135 // Test cases | 134 // Test cases |
| 136 | 135 |
| 137 // Try to open, start, stop and close a device. | 136 // Try to open, start, stop and close a device. |
| 138 TEST_F(VideoCaptureManagerTest, CreateAndClose) { | 137 TEST_F(VideoCaptureManagerTest, CreateAndClose) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 // VideoCaptureManager destructor otherwise. | 319 // VideoCaptureManager destructor otherwise. |
| 321 vcm_->Close(video_session_id); | 320 vcm_->Close(video_session_id); |
| 322 vcm_->Stop(video_session_id, base::Closure()); | 321 vcm_->Stop(video_session_id, base::Closure()); |
| 323 | 322 |
| 324 // Wait to check callbacks before removing the listener | 323 // Wait to check callbacks before removing the listener |
| 325 SyncWithVideoCaptureManagerThread(); | 324 SyncWithVideoCaptureManagerThread(); |
| 326 vcm_->Unregister(); | 325 vcm_->Unregister(); |
| 327 } | 326 } |
| 328 | 327 |
| 329 } // namespace | 328 } // namespace |
| OLD | NEW |