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

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: compile warning 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/video/capture/mock_video_capture_event_handler.h"
9 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
11 13
12 using ::testing::_; 14 using ::testing::_;
13 using ::testing::AtLeast; 15 using ::testing::AtLeast;
14 using ::testing::Return; 16 using ::testing::InvokeWithoutArgs;
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
100 void Quit() {
101 message_loop_.PostTask(FROM_HERE, run_loop_.QuitClosure());
102 }
103
122 protected: 104 protected:
123 scoped_ptr<base::MessageLoop> message_loop_; 105 base::MessageLoop message_loop_;
124 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 106 base::RunLoop run_loop_;
125 scoped_ptr<ChildProcess> child_process_; 107 scoped_ptr<ChildProcess> child_process_;
126 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; 108 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
127 media::VideoCaptureSessionId session_id_; 109 media::VideoCaptureSessionId session_id_;
128 MockVideoCaptureImpl* video_capture_impl_; 110 MockVideoCaptureImpl* video_capture_impl_;
129 media::VideoCaptureParams params_small_; 111 media::VideoCaptureParams params_small_;
130 media::VideoCaptureParams params_large_; 112 media::VideoCaptureParams params_large_;
131 113
132 private: 114 private:
133 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); 115 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
134 }; 116 };
135 117
136 TEST_F(VideoCaptureImplTest, Simple) { 118 TEST_F(VideoCaptureImplTest, Simple) {
137 // Execute SetCapture() and StopCapture() for one client. 119 // Execute SetCapture() and StopCapture() for one client.
138 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); 120 scoped_ptr<MockVideoCaptureEventHandler> client(
121 new MockVideoCaptureEventHandler);
139 122
140 EXPECT_CALL(*client, OnStarted(_)) 123 EXPECT_CALL(*client, OnStarted(_));
141 .WillOnce(Return()); 124 EXPECT_CALL(*client, OnStopped(_));
125 EXPECT_CALL(*client, OnRemoved(_))
126 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit));
142 127
143 video_capture_impl_->StartCapture(client.get(), params_small_); 128 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()); 129 video_capture_impl_->StopCapture(client.get());
152 message_loop_->RunUntilIdle(); 130 run_loop_.Run();
153 } 131 }
154 132
155 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { 133 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
156 // Execute SetCapture() and StopCapture() for 2 clients in sequence. 134 // Execute SetCapture() and StopCapture() for 2 clients in sequence.
157 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); 135 scoped_ptr<MockVideoCaptureEventHandler> client1(
136 new MockVideoCaptureEventHandler);
137 scoped_ptr<MockVideoCaptureEventHandler> client2(
138 new MockVideoCaptureEventHandler);
158 139
159 EXPECT_CALL(*client, OnStarted(_)) 140 EXPECT_CALL(*client1, OnStarted(_));
160 .WillOnce(Return()); 141 EXPECT_CALL(*client1, OnStopped(_));
142 EXPECT_CALL(*client1, OnRemoved(_));
143 EXPECT_CALL(*client2, OnStarted(_));
144 EXPECT_CALL(*client2, OnStopped(_));
145 EXPECT_CALL(*client2, OnRemoved(_))
146 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit));
161 147
162 video_capture_impl_->StartCapture(client.get(), params_small_); 148 video_capture_impl_->StartCapture(client1.get(), params_small_);
163 message_loop_->RunUntilIdle(); 149 video_capture_impl_->StopCapture(client1.get());
164 150 video_capture_impl_->StartCapture(client2.get(), params_small_);
165 EXPECT_CALL(*client, OnStopped(_)) 151 video_capture_impl_->StopCapture(client2.get());
166 .WillOnce(Return()); 152 run_loop_.Run();
167 EXPECT_CALL(*client, OnRemoved(_))
168 .WillOnce(Return());
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 } 153 }
187 154
188 TEST_F(VideoCaptureImplTest, LargeAndSmall) { 155 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
189 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 156 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
190 // The large client starts first and stops first. 157 // The large client starts first and stops first.
191 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); 158 scoped_ptr<MockVideoCaptureEventHandler> client_small(
192 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); 159 new MockVideoCaptureEventHandler);
160 scoped_ptr<MockVideoCaptureEventHandler> client_large(
161 new MockVideoCaptureEventHandler);
193 162
194 EXPECT_CALL(*client_large, OnStarted(_)) 163 EXPECT_CALL(*client_large, OnStarted(_));
195 .WillOnce(Return()); 164 EXPECT_CALL(*client_small, OnStarted(_));
196 EXPECT_CALL(*client_small, OnStarted(_)) 165 EXPECT_CALL(*client_large, OnStopped(_));
197 .WillOnce(Return()); 166 EXPECT_CALL(*client_large, OnRemoved(_));
167 EXPECT_CALL(*client_small, OnStopped(_));
168 EXPECT_CALL(*client_small, OnRemoved(_))
169 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit));
198 170
199 video_capture_impl_->StartCapture(client_large.get(), params_large_); 171 video_capture_impl_->StartCapture(client_large.get(), params_large_);
200 video_capture_impl_->StartCapture(client_small.get(), params_small_); 172 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()); 173 video_capture_impl_->StopCapture(client_large.get());
213 video_capture_impl_->StopCapture(client_small.get()); 174 video_capture_impl_->StopCapture(client_small.get());
214 message_loop_->RunUntilIdle(); 175 run_loop_.Run();
215 } 176 }
216 177
217 TEST_F(VideoCaptureImplTest, SmallAndLarge) { 178 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
218 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. 179 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
219 // The small client starts first and stops first. 180 // The small client starts first and stops first.
220 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); 181 scoped_ptr<MockVideoCaptureEventHandler> client_small(
221 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); 182 new MockVideoCaptureEventHandler);
183 scoped_ptr<MockVideoCaptureEventHandler> client_large(
184 new MockVideoCaptureEventHandler);
222 185
223 EXPECT_CALL(*client_large, OnStarted(_)) 186 EXPECT_CALL(*client_small, OnStarted(_));
224 .WillOnce(Return()); 187 EXPECT_CALL(*client_large, OnStarted(_));
225 EXPECT_CALL(*client_small, OnStarted(_)) 188 EXPECT_CALL(*client_small, OnStopped(_));
226 .WillOnce(Return()); 189 EXPECT_CALL(*client_small, OnRemoved(_));
190 EXPECT_CALL(*client_large, OnStopped(_));
191 EXPECT_CALL(*client_large, OnRemoved(_))
192 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit));
227 193
228 video_capture_impl_->StartCapture(client_small.get(), params_small_); 194 video_capture_impl_->StartCapture(client_small.get(), params_small_);
229 video_capture_impl_->StartCapture(client_large.get(), params_large_); 195 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()); 196 video_capture_impl_->StopCapture(client_small.get());
242 video_capture_impl_->StopCapture(client_large.get()); 197 video_capture_impl_->StopCapture(client_large.get());
243 message_loop_->RunUntilIdle(); 198 run_loop_.Run();
244 }
245
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

Powered by Google App Engine
This is Rietveld 408576698