Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: content/renderer/media/video_capture_impl_unittest.cc

Issue 120893002: Eliminate video capture thread in renderer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: upload again Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "base/run_loop.h"
6 #include "content/child/child_process.h" 7 #include "content/child/child_process.h"
7 #include "content/common/media/video_capture_messages.h" 8 #include "content/common/media/video_capture_messages.h"
8 #include "content/renderer/media/video_capture_impl.h" 9 #include "content/renderer/media/video_capture_impl.h"
10 #include "media/base/bind_to_current_loop.h"
11 #include "media/video/capture/mock_video_capture_event_handler.h"
9 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
11 14
12 using ::testing::_; 15 using ::testing::_;
13 using ::testing::AtLeast; 16 using ::testing::AtLeast;
14 using ::testing::Return; 17 using media::MockVideoCaptureEventHandler;
15 18
16 namespace content { 19 namespace content {
17 20
18 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { 21 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
19 public: 22 public:
20 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} 23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
21 24
22 // Filter implementation. 25 // Filter implementation.
23 MOCK_METHOD1(Send, bool(IPC::Message* message)); 26 MOCK_METHOD1(Send, bool(IPC::Message* message));
24 27
25 protected: 28 protected:
26 virtual ~MockVideoCaptureMessageFilter() {} 29 virtual ~MockVideoCaptureMessageFilter() {}
27 30
28 private: 31 private:
29 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); 32 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
30 }; 33 };
31 34
32 class MockVideoCaptureClient : public media::VideoCapture::EventHandler {
33 public:
34 MockVideoCaptureClient() {}
35 virtual ~MockVideoCaptureClient() {}
36
37 // EventHandler implementation.
38 MOCK_METHOD1(OnStarted, void(media::VideoCapture* capture));
39 MOCK_METHOD1(OnStopped, void(media::VideoCapture* capture));
40 MOCK_METHOD1(OnPaused, void(media::VideoCapture* capture));
41 MOCK_METHOD2(OnError, void(media::VideoCapture* capture, int error_code));
42 MOCK_METHOD1(OnRemoved, void(media::VideoCapture* capture));
43 MOCK_METHOD2(OnFrameReady,
44 void(media::VideoCapture* capture,
45 const scoped_refptr<media::VideoFrame>& frame));
46 MOCK_METHOD2(OnDeviceInfoReceived,
47 void(media::VideoCapture* capture,
48 const media::VideoCaptureFormat& device_info));
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient);
52 };
53
54 class VideoCaptureImplTest : public ::testing::Test { 35 class VideoCaptureImplTest : public ::testing::Test {
55 public: 36 public:
56 class MockVideoCaptureImpl : public VideoCaptureImpl { 37 class MockVideoCaptureImpl : public VideoCaptureImpl {
57 public: 38 public:
58 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, 39 MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
59 scoped_refptr<base::MessageLoopProxy> ml_proxy,
60 VideoCaptureMessageFilter* filter) 40 VideoCaptureMessageFilter* filter)
61 : VideoCaptureImpl(id, ml_proxy.get(), filter) {} 41 : VideoCaptureImpl(id, filter) {}
62 virtual ~MockVideoCaptureImpl() {} 42 virtual ~MockVideoCaptureImpl() {}
63 43
64 // Override Send() to mimic device to send events. 44 // Override Send() to mimic device to send events.
65 virtual void Send(IPC::Message* message) OVERRIDE { 45 virtual void Send(IPC::Message* message) OVERRIDE {
66 CHECK(message); 46 CHECK(message);
67 47
68 // In this method, messages are sent to the according handlers as if 48 // In this method, messages are sent to the according handlers as if
69 // we are the device. 49 // we are the device.
70 bool handled = true; 50 bool handled = true;
71 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) 51 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message)
(...skipping 23 matching lines...) Expand all
95 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} 75 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {}
96 }; 76 };
97 77
98 VideoCaptureImplTest() { 78 VideoCaptureImplTest() {
99 params_small_.requested_format = media::VideoCaptureFormat( 79 params_small_.requested_format = media::VideoCaptureFormat(
100 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); 80 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
101 81
102 params_large_.requested_format = media::VideoCaptureFormat( 82 params_large_.requested_format = media::VideoCaptureFormat(
103 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); 83 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
104 84
105 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO));
106 message_loop_proxy_ = base::MessageLoopProxy::current().get();
107 child_process_.reset(new ChildProcess()); 85 child_process_.reset(new ChildProcess());
108 86
109 message_filter_ = new MockVideoCaptureMessageFilter; 87 message_filter_ = new MockVideoCaptureMessageFilter;
110 session_id_ = 1; 88 session_id_ = 1;
111 89
112 video_capture_impl_ = new MockVideoCaptureImpl( 90 video_capture_impl_ = new MockVideoCaptureImpl(
113 session_id_, message_loop_proxy_, message_filter_.get()); 91 session_id_, message_filter_.get());
114 92
115 video_capture_impl_->device_id_ = 2; 93 video_capture_impl_->device_id_ = 2;
116 } 94 }
117 95
118 virtual ~VideoCaptureImplTest() { 96 virtual ~VideoCaptureImplTest() {
119 delete video_capture_impl_; 97 delete video_capture_impl_;
120 } 98 }
121 99
122 protected: 100 protected:
123 scoped_ptr<base::MessageLoop> message_loop_; 101 base::MessageLoop message_loop_;
124 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 102 base::RunLoop run_loop_;
125 scoped_ptr<ChildProcess> child_process_; 103 scoped_ptr<ChildProcess> child_process_;
126 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; 104 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
127 media::VideoCaptureSessionId session_id_; 105 media::VideoCaptureSessionId session_id_;
128 MockVideoCaptureImpl* video_capture_impl_; 106 MockVideoCaptureImpl* video_capture_impl_;
129 media::VideoCaptureParams params_small_; 107 media::VideoCaptureParams params_small_;
130 media::VideoCaptureParams params_large_; 108 media::VideoCaptureParams params_large_;
131 109
132 private: 110 private:
133 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); 111 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
134 }; 112 };
135 113
136 TEST_F(VideoCaptureImplTest, Simple) { 114 TEST_F(VideoCaptureImplTest, Simple) {
137 // Execute SetCapture() and StopCapture() for one client. 115 // Execute SetCapture() and StopCapture() for one client.
138 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); 116 scoped_ptr<MockVideoCaptureEventHandler> client(
117 new MockVideoCaptureEventHandler);
139 118
140 EXPECT_CALL(*client, OnStarted(_)) 119 EXPECT_CALL(*client, OnStarted(_));
141 .WillOnce(Return()); 120 EXPECT_CALL(*client, OnStopped(_));
121 EXPECT_CALL(*client, OnRemoved(_));
142 122
143 video_capture_impl_->StartCapture(client.get(), params_small_); 123 video_capture_impl_->StartCapture(client.get(), params_small_);
144 message_loop_->RunUntilIdle();
145
146 EXPECT_CALL(*client, OnStopped(_))
147 .WillOnce(Return());
148 EXPECT_CALL(*client, OnRemoved(_))
149 .WillOnce(Return());
150
151 video_capture_impl_->StopCapture(client.get()); 124 video_capture_impl_->StopCapture(client.get());
152 message_loop_->RunUntilIdle(); 125 video_capture_impl_->DeInit(
126 media::BindToCurrentLoop(run_loop_.QuitClosure()));
127 run_loop_.Run();
153 } 128 }
154 129
155 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { 130 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
156 // Execute SetCapture() and StopCapture() for 2 clients in sequence. 131 // Execute SetCapture() and StopCapture() for 2 clients in sequence.
157 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); 132 scoped_ptr<MockVideoCaptureEventHandler> client1(
133 new MockVideoCaptureEventHandler);
134 scoped_ptr<MockVideoCaptureEventHandler> client2(
135 new MockVideoCaptureEventHandler);
158 136
159 EXPECT_CALL(*client, OnStarted(_)) 137 EXPECT_CALL(*client1, OnStarted(_));
160 .WillOnce(Return()); 138 EXPECT_CALL(*client1, OnStopped(_));
139 EXPECT_CALL(*client1, OnRemoved(_));
140 EXPECT_CALL(*client2, OnStarted(_));
141 EXPECT_CALL(*client2, OnStopped(_));
142 EXPECT_CALL(*client2, OnRemoved(_));
161 143
162 video_capture_impl_->StartCapture(client.get(), params_small_); 144 video_capture_impl_->StartCapture(client1.get(), params_small_);
163 message_loop_->RunUntilIdle(); 145 video_capture_impl_->StopCapture(client1.get());
164 146 video_capture_impl_->StartCapture(client2.get(), params_small_);
165 EXPECT_CALL(*client, OnStopped(_)) 147 video_capture_impl_->StopCapture(client2.get());
166 .WillOnce(Return()); 148 video_capture_impl_->DeInit(
167 EXPECT_CALL(*client, OnRemoved(_)) 149 media::BindToCurrentLoop(run_loop_.QuitClosure()));
168 .WillOnce(Return()); 150 run_loop_.Run();
169
170 video_capture_impl_->StopCapture(client.get());
171 message_loop_->RunUntilIdle();
172
173 EXPECT_CALL(*client, OnStarted(_))
174 .WillOnce(Return());
175
176 video_capture_impl_->StartCapture(client.get(), params_small_);
177 message_loop_->RunUntilIdle();
178
179 EXPECT_CALL(*client, OnStopped(_))
180 .WillOnce(Return());
181 EXPECT_CALL(*client, OnRemoved(_))
182 .WillOnce(Return());
183
184 video_capture_impl_->StopCapture(client.get());
185 message_loop_->RunUntilIdle();
186 } 151 }
187 152
188 TEST_F(VideoCaptureImplTest, LargeAndSmall) { 153 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
189 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 154 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
190 // The large client starts first and stops first. 155 // The large client starts first and stops first.
191 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); 156 scoped_ptr<MockVideoCaptureEventHandler> client_small(
192 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); 157 new MockVideoCaptureEventHandler);
158 scoped_ptr<MockVideoCaptureEventHandler> client_large(
159 new MockVideoCaptureEventHandler);
193 160
194 EXPECT_CALL(*client_large, OnStarted(_)) 161 EXPECT_CALL(*client_large, OnStarted(_));
195 .WillOnce(Return()); 162 EXPECT_CALL(*client_small, OnStarted(_));
196 EXPECT_CALL(*client_small, OnStarted(_)) 163 EXPECT_CALL(*client_large, OnStopped(_));
197 .WillOnce(Return()); 164 EXPECT_CALL(*client_large, OnRemoved(_));
165 EXPECT_CALL(*client_small, OnStopped(_));
166 EXPECT_CALL(*client_small, OnRemoved(_));
198 167
199 video_capture_impl_->StartCapture(client_large.get(), params_large_); 168 video_capture_impl_->StartCapture(client_large.get(), params_large_);
200 video_capture_impl_->StartCapture(client_small.get(), params_small_); 169 video_capture_impl_->StartCapture(client_small.get(), params_small_);
201 message_loop_->RunUntilIdle();
202
203 EXPECT_CALL(*client_large, OnStopped(_))
204 .WillOnce(Return());
205 EXPECT_CALL(*client_large, OnRemoved(_))
206 .WillOnce(Return());
207 EXPECT_CALL(*client_small, OnStopped(_))
208 .WillOnce(Return());
209 EXPECT_CALL(*client_small, OnRemoved(_))
210 .WillOnce(Return());
211
212 video_capture_impl_->StopCapture(client_large.get()); 170 video_capture_impl_->StopCapture(client_large.get());
213 video_capture_impl_->StopCapture(client_small.get()); 171 video_capture_impl_->StopCapture(client_small.get());
214 message_loop_->RunUntilIdle(); 172 video_capture_impl_->DeInit(
173 media::BindToCurrentLoop(run_loop_.QuitClosure()));
174 run_loop_.Run();
215 } 175 }
216 176
217 TEST_F(VideoCaptureImplTest, SmallAndLarge) { 177 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
218 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 178 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
219 // The small client starts first and stops first. 179 // The small client starts first and stops first.
220 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); 180 scoped_ptr<MockVideoCaptureEventHandler> client_small(
221 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); 181 new MockVideoCaptureEventHandler);
182 scoped_ptr<MockVideoCaptureEventHandler> client_large(
183 new MockVideoCaptureEventHandler);
222 184
223 EXPECT_CALL(*client_large, OnStarted(_)) 185 EXPECT_CALL(*client_small, OnStarted(_));
224 .WillOnce(Return()); 186 EXPECT_CALL(*client_large, OnStarted(_));
225 EXPECT_CALL(*client_small, OnStarted(_)) 187 EXPECT_CALL(*client_small, OnStopped(_));
226 .WillOnce(Return()); 188 EXPECT_CALL(*client_small, OnRemoved(_));
189 EXPECT_CALL(*client_large, OnStopped(_));
190 EXPECT_CALL(*client_large, OnRemoved(_));
227 191
228 video_capture_impl_->StartCapture(client_small.get(), params_small_); 192 video_capture_impl_->StartCapture(client_small.get(), params_small_);
229 video_capture_impl_->StartCapture(client_large.get(), params_large_); 193 video_capture_impl_->StartCapture(client_large.get(), params_large_);
230 message_loop_->RunUntilIdle();
231
232 EXPECT_CALL(*client_large, OnStopped(_))
233 .WillOnce(Return());
234 EXPECT_CALL(*client_large, OnRemoved(_))
235 .WillOnce(Return());
236 EXPECT_CALL(*client_small, OnStopped(_))
237 .WillOnce(Return());
238 EXPECT_CALL(*client_small, OnRemoved(_))
239 .WillOnce(Return());
240
241 video_capture_impl_->StopCapture(client_small.get()); 194 video_capture_impl_->StopCapture(client_small.get());
242 video_capture_impl_->StopCapture(client_large.get()); 195 video_capture_impl_->StopCapture(client_large.get());
243 message_loop_->RunUntilIdle(); 196 video_capture_impl_->DeInit(
244 } 197 media::BindToCurrentLoop(run_loop_.QuitClosure()));
245 198 run_loop_.Run();
246 TEST_F(VideoCaptureImplTest, TwoClientsWithSameSize) {
247 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
248 // The client1 starts first and stops first.
249 scoped_ptr<MockVideoCaptureClient> client1(new MockVideoCaptureClient);
250 scoped_ptr<MockVideoCaptureClient> client2(new MockVideoCaptureClient);
251
252 EXPECT_CALL(*client1, OnStarted(_))
253 .WillOnce(Return());
254 EXPECT_CALL(*client2, OnStarted(_))
255 .WillOnce(Return());
256
257 video_capture_impl_->StartCapture(client1.get(), params_small_);
258 video_capture_impl_->StartCapture(client2.get(), params_small_);
259 message_loop_->RunUntilIdle();
260
261 EXPECT_CALL(*client1, OnStopped(_))
262 .WillOnce(Return());
263 EXPECT_CALL(*client1, OnRemoved(_))
264 .WillOnce(Return());
265 EXPECT_CALL(*client2, OnStopped(_))
266 .WillOnce(Return());
267 EXPECT_CALL(*client2, OnRemoved(_))
268 .WillOnce(Return());
269
270 video_capture_impl_->StopCapture(client1.get());
271 video_capture_impl_->StopCapture(client2.get());
272 message_loop_->RunUntilIdle();
273 } 199 }
274 200
275 } // namespace content 201 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/video_capture_impl_manager_unittest.cc ('k') | content/renderer/pepper/pepper_platform_video_capture.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698