| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| 11 #include "content/renderer/media/video_capture_impl.h" | 11 #include "content/renderer/media/video_capture_impl.h" |
| 12 #include "content/renderer/media/video_capture_impl_manager.h" | 12 #include "content/renderer/media/video_capture_impl_manager.h" |
| 13 #include "content/renderer/media/video_capture_message_filter.h" | 13 #include "content/renderer/media/video_capture_message_filter.h" |
| 14 #include "media/base/bind_to_current_loop.h" | 14 #include "media/base/bind_to_current_loop.h" |
| 15 #include "media/video/capture/mock_video_capture_event_handler.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 using ::testing::_; | 18 using ::testing::_; |
| 20 using ::testing::DoAll; | 19 using ::testing::DoAll; |
| 21 using ::testing::SaveArg; | 20 using ::testing::SaveArg; |
| 22 using media::BindToCurrentLoop; | 21 using media::BindToCurrentLoop; |
| 23 using media::MockVideoCaptureEventHandler; | |
| 24 | 22 |
| 25 namespace content { | 23 namespace content { |
| 26 | 24 |
| 27 ACTION_P(RunClosure, closure) { | 25 ACTION_P(RunClosure, closure) { |
| 28 closure.Run(); | 26 closure.Run(); |
| 29 } | 27 } |
| 30 | 28 |
| 31 class MockVideoCaptureImpl : public VideoCaptureImpl { | 29 class MockVideoCaptureImpl : public VideoCaptureImpl { |
| 32 public: | 30 public: |
| 33 MockVideoCaptureImpl(media::VideoCaptureSessionId session_id, | 31 MockVideoCaptureImpl(media::VideoCaptureSessionId session_id, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 48 }; | 46 }; |
| 49 | 47 |
| 50 class MockVideoCaptureImplManager : public VideoCaptureImplManager { | 48 class MockVideoCaptureImplManager : public VideoCaptureImplManager { |
| 51 public: | 49 public: |
| 52 explicit MockVideoCaptureImplManager( | 50 explicit MockVideoCaptureImplManager( |
| 53 base::Closure destruct_video_capture_callback) | 51 base::Closure destruct_video_capture_callback) |
| 54 : destruct_video_capture_callback_( | 52 : destruct_video_capture_callback_( |
| 55 destruct_video_capture_callback) {} | 53 destruct_video_capture_callback) {} |
| 56 | 54 |
| 57 protected: | 55 protected: |
| 56 friend class base::RefCounted<MockVideoCaptureImplManager>; |
| 57 virtual ~MockVideoCaptureImplManager() {} |
| 58 |
| 58 virtual VideoCaptureImpl* CreateVideoCaptureImpl( | 59 virtual VideoCaptureImpl* CreateVideoCaptureImpl( |
| 59 media::VideoCaptureSessionId id, | 60 media::VideoCaptureSessionId id, |
| 60 VideoCaptureMessageFilter* filter) const OVERRIDE { | 61 VideoCaptureMessageFilter* filter) const OVERRIDE { |
| 61 return new MockVideoCaptureImpl(id, | 62 return new MockVideoCaptureImpl(id, |
| 62 filter, | 63 filter, |
| 63 destruct_video_capture_callback_); | 64 destruct_video_capture_callback_); |
| 64 } | 65 } |
| 65 | 66 |
| 66 private: | 67 private: |
| 67 base::Closure destruct_video_capture_callback_; | 68 base::Closure destruct_video_capture_callback_; |
| 68 | 69 |
| 69 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); | 70 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); |
| 70 }; | 71 }; |
| 71 | 72 |
| 72 class VideoCaptureImplManagerTest : public ::testing::Test { | 73 class VideoCaptureImplManagerTest : public ::testing::Test { |
| 73 public: | 74 public: |
| 74 VideoCaptureImplManagerTest() | 75 VideoCaptureImplManagerTest() |
| 75 : manager_(BindToCurrentLoop(cleanup_run_loop_.QuitClosure())) { | 76 : manager_(new MockVideoCaptureImplManager( |
| 77 BindToCurrentLoop(cleanup_run_loop_.QuitClosure()))) { |
| 76 params_.requested_format = media::VideoCaptureFormat( | 78 params_.requested_format = media::VideoCaptureFormat( |
| 77 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | 79 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); |
| 78 child_process_.reset(new ChildProcess()); | 80 child_process_.reset(new ChildProcess()); |
| 79 } | 81 } |
| 80 | 82 |
| 81 void FakeChannelSetup() { | 83 void FakeChannelSetup() { |
| 82 scoped_refptr<base::MessageLoopProxy> loop = | 84 scoped_refptr<base::MessageLoopProxy> loop = |
| 83 child_process_->io_message_loop_proxy(); | 85 child_process_->io_message_loop_proxy(); |
| 84 if (!loop->BelongsToCurrentThread()) { | 86 if (!loop->BelongsToCurrentThread()) { |
| 85 loop->PostTask( | 87 loop->PostTask( |
| 86 FROM_HERE, | 88 FROM_HERE, |
| 87 base::Bind( | 89 base::Bind( |
| 88 &VideoCaptureImplManagerTest::FakeChannelSetup, | 90 &VideoCaptureImplManagerTest::FakeChannelSetup, |
| 89 base::Unretained(this))); | 91 base::Unretained(this))); |
| 90 return; | 92 return; |
| 91 } | 93 } |
| 92 manager_.video_capture_message_filter()->OnFilterAdded(NULL); | 94 manager_->video_capture_message_filter()->OnFilterAdded(NULL); |
| 93 } | 95 } |
| 94 | 96 |
| 95 protected: | 97 protected: |
| 98 MOCK_METHOD3(OnFrameReady, |
| 99 void(const scoped_refptr<media::VideoFrame>&, |
| 100 const media::VideoCaptureFormat&, |
| 101 const base::TimeTicks&)); |
| 102 MOCK_METHOD0(OnStarted, void()); |
| 103 MOCK_METHOD0(OnStopped, void()); |
| 104 |
| 105 void OnStateUpdate(VideoCaptureState state) { |
| 106 switch (state) { |
| 107 case VIDEO_CAPTURE_STATE_STARTED: |
| 108 OnStarted(); |
| 109 break; |
| 110 case VIDEO_CAPTURE_STATE_STOPPED: |
| 111 OnStopped(); |
| 112 break; |
| 113 default: |
| 114 NOTREACHED(); |
| 115 } |
| 116 } |
| 117 |
| 118 base::Closure StartCapture(const media::VideoCaptureParams& params) { |
| 119 return manager_->StartCapture( |
| 120 0, params, |
| 121 base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate, |
| 122 base::Unretained(this)), |
| 123 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady, |
| 124 base::Unretained(this))); |
| 125 } |
| 126 |
| 96 base::MessageLoop message_loop_; | 127 base::MessageLoop message_loop_; |
| 97 scoped_ptr<ChildProcess> child_process_; | 128 scoped_ptr<ChildProcess> child_process_; |
| 98 media::VideoCaptureParams params_; | 129 media::VideoCaptureParams params_; |
| 99 base::RunLoop cleanup_run_loop_; | 130 base::RunLoop cleanup_run_loop_; |
| 100 MockVideoCaptureImplManager manager_; | 131 scoped_refptr<MockVideoCaptureImplManager> manager_; |
| 101 | 132 |
| 102 private: | 133 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); | 134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); |
| 104 }; | 135 }; |
| 105 | 136 |
| 106 // Multiple clients with the same session id. There is only one | 137 // Multiple clients with the same session id. There is only one |
| 107 // media::VideoCapture object. | 138 // media::VideoCapture object. |
| 108 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { | 139 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { |
| 109 scoped_ptr<MockVideoCaptureEventHandler> client1( | 140 base::Closure release_cb1 = manager_->UseDevice(0); |
| 110 new MockVideoCaptureEventHandler); | 141 base::Closure release_cb2 = manager_->UseDevice(0); |
| 111 scoped_ptr<MockVideoCaptureEventHandler> client2( | 142 base::Closure stop_cb1, stop_cb2; |
| 112 new MockVideoCaptureEventHandler); | |
| 113 | |
| 114 media::VideoCapture* device1 = NULL; | |
| 115 media::VideoCapture* device2 = NULL; | |
| 116 | |
| 117 scoped_ptr<VideoCaptureHandle> handle1; | |
| 118 scoped_ptr<VideoCaptureHandle> handle2; | |
| 119 { | 143 { |
| 120 base::RunLoop run_loop; | 144 base::RunLoop run_loop; |
| 121 base::Closure quit_closure = BindToCurrentLoop( | 145 base::Closure quit_closure = BindToCurrentLoop( |
| 122 run_loop.QuitClosure()); | 146 run_loop.QuitClosure()); |
| 123 | 147 EXPECT_CALL(*this, OnStarted()).WillOnce( |
| 124 EXPECT_CALL(*client1, OnStarted(_)).WillOnce(SaveArg<0>(&device1)); | 148 RunClosure(quit_closure)); |
| 125 EXPECT_CALL(*client2, OnStarted(_)).WillOnce( | 149 EXPECT_CALL(*this, OnStarted()).RetiresOnSaturation(); |
| 126 DoAll( | 150 stop_cb1 = StartCapture(params_); |
| 127 SaveArg<0>(&device2), | 151 stop_cb2 = StartCapture(params_); |
| 128 RunClosure(quit_closure))); | |
| 129 handle1 = manager_.UseDevice(1); | |
| 130 handle2 = manager_.UseDevice(1); | |
| 131 handle1->StartCapture(client1.get(), params_); | |
| 132 handle2->StartCapture(client2.get(), params_); | |
| 133 FakeChannelSetup(); | 152 FakeChannelSetup(); |
| 134 run_loop.Run(); | 153 run_loop.Run(); |
| 135 } | 154 } |
| 136 | 155 |
| 137 { | 156 { |
| 138 base::RunLoop run_loop; | 157 base::RunLoop run_loop; |
| 139 base::Closure quit_closure = BindToCurrentLoop( | 158 base::Closure quit_closure = BindToCurrentLoop( |
| 140 run_loop.QuitClosure()); | 159 run_loop.QuitClosure()); |
| 141 | 160 EXPECT_CALL(*this, OnStopped()).WillOnce( |
| 142 EXPECT_CALL(*client1, OnStopped(_)); | |
| 143 EXPECT_CALL(*client1, OnRemoved(_)); | |
| 144 EXPECT_CALL(*client2, OnStopped(_)); | |
| 145 EXPECT_CALL(*client2, OnRemoved(_)).WillOnce( | |
| 146 RunClosure(quit_closure)); | 161 RunClosure(quit_closure)); |
| 147 handle1->StopCapture(client1.get()); | 162 EXPECT_CALL(*this, OnStopped()).RetiresOnSaturation(); |
| 148 handle2->StopCapture(client2.get()); | 163 stop_cb1.Run(); |
| 164 stop_cb2.Run(); |
| 149 run_loop.Run(); | 165 run_loop.Run(); |
| 150 } | 166 } |
| 151 EXPECT_TRUE(device1 == device2); | 167 release_cb1.Run(); |
| 152 | 168 release_cb2.Run(); |
| 153 handle1.reset(); | |
| 154 handle2.reset(); | |
| 155 cleanup_run_loop_.Run(); | 169 cleanup_run_loop_.Run(); |
| 156 } | 170 } |
| 157 | 171 |
| 172 TEST_F(VideoCaptureImplManagerTest, NoLeak) { |
| 173 manager_->UseDevice(0).Reset(); |
| 174 manager_ = NULL; |
| 175 cleanup_run_loop_.Run(); |
| 176 } |
| 177 |
| 158 } // namespace content | 178 } // namespace content |
| OLD | NEW |