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