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 12 matching lines...) Expand all Loading... |
46 | 44 |
47 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl); | 45 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl); |
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) {} |
| 54 virtual ~MockVideoCaptureImplManager() {} |
56 | 55 |
57 protected: | 56 protected: |
58 virtual VideoCaptureImpl* CreateVideoCaptureImpl( | 57 virtual VideoCaptureImpl* CreateVideoCaptureImplForTesting( |
59 media::VideoCaptureSessionId id, | 58 media::VideoCaptureSessionId id, |
60 VideoCaptureMessageFilter* filter) const OVERRIDE { | 59 VideoCaptureMessageFilter* filter) const OVERRIDE { |
61 return new MockVideoCaptureImpl(id, | 60 return new MockVideoCaptureImpl(id, |
62 filter, | 61 filter, |
63 destruct_video_capture_callback_); | 62 destruct_video_capture_callback_); |
64 } | 63 } |
65 | 64 |
66 private: | 65 private: |
67 base::Closure destruct_video_capture_callback_; | 66 base::Closure destruct_video_capture_callback_; |
68 | 67 |
69 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); | 68 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); |
70 }; | 69 }; |
71 | 70 |
72 class VideoCaptureImplManagerTest : public ::testing::Test { | 71 class VideoCaptureImplManagerTest : public ::testing::Test { |
73 public: | 72 public: |
74 VideoCaptureImplManagerTest() | 73 VideoCaptureImplManagerTest() |
75 : manager_(BindToCurrentLoop(cleanup_run_loop_.QuitClosure())) { | 74 : manager_(new MockVideoCaptureImplManager( |
| 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 |
96 base::MessageLoop message_loop_; | 124 base::MessageLoop message_loop_; |
97 scoped_ptr<ChildProcess> child_process_; | 125 scoped_ptr<ChildProcess> child_process_; |
98 media::VideoCaptureParams params_; | 126 media::VideoCaptureParams params_; |
99 base::RunLoop cleanup_run_loop_; | 127 base::RunLoop cleanup_run_loop_; |
100 MockVideoCaptureImplManager manager_; | 128 scoped_ptr<MockVideoCaptureImplManager> manager_; |
101 | 129 |
102 private: | 130 private: |
103 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); | 131 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); |
104 }; | 132 }; |
105 | 133 |
106 // Multiple clients with the same session id. There is only one | 134 // Multiple clients with the same session id. There is only one |
107 // media::VideoCapture object. | 135 // media::VideoCapture object. |
108 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { | 136 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { |
109 scoped_ptr<MockVideoCaptureEventHandler> client1( | 137 base::Closure release_cb1 = manager_->UseDevice(0); |
110 new MockVideoCaptureEventHandler); | 138 base::Closure release_cb2 = manager_->UseDevice(0); |
111 scoped_ptr<MockVideoCaptureEventHandler> client2( | 139 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 { | 140 { |
120 base::RunLoop run_loop; | 141 base::RunLoop run_loop; |
121 base::Closure quit_closure = BindToCurrentLoop( | 142 base::Closure quit_closure = BindToCurrentLoop( |
122 run_loop.QuitClosure()); | 143 run_loop.QuitClosure()); |
123 | 144 EXPECT_CALL(*this, OnStarted()).WillOnce( |
124 EXPECT_CALL(*client1, OnStarted(_)).WillOnce(SaveArg<0>(&device1)); | 145 RunClosure(quit_closure)); |
125 EXPECT_CALL(*client2, OnStarted(_)).WillOnce( | 146 EXPECT_CALL(*this, OnStarted()).RetiresOnSaturation(); |
126 DoAll( | 147 stop_cb1 = StartCapture(params_); |
127 SaveArg<0>(&device2), | 148 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(); | 149 FakeChannelSetup(); |
134 run_loop.Run(); | 150 run_loop.Run(); |
135 } | 151 } |
136 | 152 |
137 { | 153 { |
138 base::RunLoop run_loop; | 154 base::RunLoop run_loop; |
139 base::Closure quit_closure = BindToCurrentLoop( | 155 base::Closure quit_closure = BindToCurrentLoop( |
140 run_loop.QuitClosure()); | 156 run_loop.QuitClosure()); |
141 | 157 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)); | 158 RunClosure(quit_closure)); |
147 handle1->StopCapture(client1.get()); | 159 EXPECT_CALL(*this, OnStopped()).RetiresOnSaturation(); |
148 handle2->StopCapture(client2.get()); | 160 stop_cb1.Run(); |
| 161 stop_cb2.Run(); |
149 run_loop.Run(); | 162 run_loop.Run(); |
150 } | 163 } |
151 EXPECT_TRUE(device1 == device2); | |
152 | 164 |
153 handle1.reset(); | 165 release_cb1.Run(); |
154 handle2.reset(); | 166 release_cb2.Run(); |
155 cleanup_run_loop_.Run(); | 167 cleanup_run_loop_.Run(); |
156 } | 168 } |
157 | 169 |
| 170 TEST_F(VideoCaptureImplManagerTest, NoLeak) { |
| 171 manager_->UseDevice(0).Reset(); |
| 172 manager_.reset(); |
| 173 cleanup_run_loop_.Run(); |
| 174 } |
| 175 |
158 } // namespace content | 176 } // namespace content |
OLD | NEW |