OLD | NEW |
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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
10 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" | 10 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | 153 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
154 // Make sure we use fake devices to avoid long delays. | 154 // Make sure we use fake devices to avoid long delays. |
155 media_stream_manager_->UseFakeDevice(); | 155 media_stream_manager_->UseFakeDevice(); |
156 | 156 |
157 host_ = new MockMediaStreamDispatcherHost(message_loop_.get(), | 157 host_ = new MockMediaStreamDispatcherHost(message_loop_.get(), |
158 media_stream_manager_.get()); | 158 media_stream_manager_.get()); |
159 | 159 |
160 // Use the fake content client and browser. | 160 // Use the fake content client and browser. |
161 content_client_.reset(new TestContentClient); | 161 content_client_.reset(new TestContentClient); |
162 SetContentClient(content_client_.get()); | 162 SetContentClient(content_client_.get()); |
163 old_browser_client_ = SetBrowserClientForTesting(host_); | 163 old_browser_client_ = SetBrowserClientForTesting(host_.get()); |
164 } | 164 } |
165 | 165 |
166 virtual void SetupFakeUI(bool expect_started) { | 166 virtual void SetupFakeUI(bool expect_started) { |
167 scoped_ptr<MockMediaStreamUI> stream_ui(new MockMediaStreamUI()); | 167 scoped_ptr<MockMediaStreamUI> stream_ui(new MockMediaStreamUI()); |
168 if (expect_started) { | 168 if (expect_started) { |
169 EXPECT_CALL(*stream_ui, OnStarted(_)); | 169 EXPECT_CALL(*stream_ui, OnStarted(_)); |
170 } | 170 } |
171 media_stream_manager_->UseFakeUI(stream_ui.PassAs<MediaStreamUI>()); | 171 media_stream_manager_->UseFakeUI(stream_ui.PassAs<MediaStreamUI>()); |
172 } | 172 } |
173 | 173 |
(...skipping 16 matching lines...) Expand all Loading... |
190 scoped_ptr<media::AudioManager> audio_manager_; | 190 scoped_ptr<media::AudioManager> audio_manager_; |
191 scoped_ptr<MediaStreamManager> media_stream_manager_; | 191 scoped_ptr<MediaStreamManager> media_stream_manager_; |
192 ContentBrowserClient* old_browser_client_; | 192 ContentBrowserClient* old_browser_client_; |
193 scoped_ptr<ContentClient> content_client_; | 193 scoped_ptr<ContentClient> content_client_; |
194 }; | 194 }; |
195 | 195 |
196 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { | 196 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { |
197 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); | 197 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
198 | 198 |
199 SetupFakeUI(true); | 199 SetupFakeUI(true); |
200 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); | 200 EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); |
201 host_->OnGenerateStream(kPageRequestId, options); | 201 host_->OnGenerateStream(kPageRequestId, options); |
202 | 202 |
203 WaitForResult(); | 203 WaitForResult(); |
204 | 204 |
205 std::string label = host_->label_; | 205 std::string label = host_->label_; |
206 | 206 |
207 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 207 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
208 EXPECT_EQ(host_->video_devices_.size(), 1u); | 208 EXPECT_EQ(host_->video_devices_.size(), 1u); |
209 EXPECT_EQ(host_->NumberOfStreams(), 1u); | 209 EXPECT_EQ(host_->NumberOfStreams(), 1u); |
210 | 210 |
211 host_->OnStopGeneratedStream(label); | 211 host_->OnStopGeneratedStream(label); |
212 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 212 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
213 } | 213 } |
214 | 214 |
215 TEST_F(MediaStreamDispatcherHostTest, GenerateThreeStreams) { | 215 TEST_F(MediaStreamDispatcherHostTest, GenerateThreeStreams) { |
216 // This test opens three video capture devices. Two fake devices exists and it | 216 // This test opens three video capture devices. Two fake devices exists and it |
217 // is expected the last call to |Open()| will open the first device again, but | 217 // is expected the last call to |Open()| will open the first device again, but |
218 // with a different label. | 218 // with a different label. |
219 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); | 219 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
220 | 220 |
221 // Generate first stream. | 221 // Generate first stream. |
222 SetupFakeUI(true); | 222 SetupFakeUI(true); |
223 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); | 223 EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); |
224 host_->OnGenerateStream(kPageRequestId, options); | 224 host_->OnGenerateStream(kPageRequestId, options); |
225 | 225 |
226 WaitForResult(); | 226 WaitForResult(); |
227 | 227 |
228 // Check the latest generated stream. | 228 // Check the latest generated stream. |
229 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 229 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
230 EXPECT_EQ(host_->video_devices_.size(), 1u); | 230 EXPECT_EQ(host_->video_devices_.size(), 1u); |
231 std::string label1 = host_->label_; | 231 std::string label1 = host_->label_; |
232 std::string device_id1 = host_->video_devices_.front().device.id; | 232 std::string device_id1 = host_->video_devices_.front().device.id; |
233 | 233 |
234 // Check that we now have one opened streams. | 234 // Check that we now have one opened streams. |
235 EXPECT_EQ(host_->NumberOfStreams(), 1u); | 235 EXPECT_EQ(host_->NumberOfStreams(), 1u); |
236 | 236 |
237 // Generate second stream. | 237 // Generate second stream. |
238 SetupFakeUI(true); | 238 SetupFakeUI(true); |
239 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId + 1, 0, 1)); | 239 EXPECT_CALL(*host_.get(), |
240 host_->OnGenerateStream(kPageRequestId+1, options); | 240 OnStreamGenerated(kRenderId, kPageRequestId + 1, 0, 1)); |
241 | 241 host_->OnGenerateStream(kPageRequestId + 1, options); |
242 | 242 |
243 WaitForResult(); | 243 WaitForResult(); |
244 | 244 |
245 // Check the latest generated stream. | 245 // Check the latest generated stream. |
246 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 246 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
247 EXPECT_EQ(host_->video_devices_.size(), 1u); | 247 EXPECT_EQ(host_->video_devices_.size(), 1u); |
248 std::string label2 = host_->label_; | 248 std::string label2 = host_->label_; |
249 std::string device_id2 = host_->video_devices_.front().device.id; | 249 std::string device_id2 = host_->video_devices_.front().device.id; |
250 EXPECT_EQ(device_id1, device_id2); | 250 EXPECT_EQ(device_id1, device_id2); |
251 EXPECT_NE(label1, label2); | 251 EXPECT_NE(label1, label2); |
252 | 252 |
253 // Check that we now have two opened streams. | 253 // Check that we now have two opened streams. |
254 EXPECT_EQ(2u, host_->NumberOfStreams()); | 254 EXPECT_EQ(2u, host_->NumberOfStreams()); |
255 | 255 |
256 // Generate third stream. | 256 // Generate third stream. |
257 SetupFakeUI(true); | 257 SetupFakeUI(true); |
258 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId + 2, 0, 1)); | 258 EXPECT_CALL(*host_.get(), |
259 host_->OnGenerateStream(kPageRequestId+2, options); | 259 OnStreamGenerated(kRenderId, kPageRequestId + 2, 0, 1)); |
| 260 host_->OnGenerateStream(kPageRequestId + 2, options); |
260 | 261 |
261 WaitForResult(); | 262 WaitForResult(); |
262 | 263 |
263 // Check the latest generated stream. | 264 // Check the latest generated stream. |
264 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 265 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
265 EXPECT_EQ(host_->video_devices_.size(), 1u); | 266 EXPECT_EQ(host_->video_devices_.size(), 1u); |
266 std::string label3 = host_->label_; | 267 std::string label3 = host_->label_; |
267 std::string device_id3 = host_->video_devices_.front().device.id; | 268 std::string device_id3 = host_->video_devices_.front().device.id; |
268 EXPECT_EQ(device_id1, device_id3); | 269 EXPECT_EQ(device_id1, device_id3); |
269 EXPECT_NE(label1, label3); | 270 EXPECT_NE(label1, label3); |
270 EXPECT_NE(label2, label3); | 271 EXPECT_NE(label2, label3); |
271 | 272 |
272 // Check that we now have three opened streams. | 273 // Check that we now have three opened streams. |
273 EXPECT_EQ(host_->NumberOfStreams(), 3u); | 274 EXPECT_EQ(host_->NumberOfStreams(), 3u); |
274 | 275 |
275 host_->OnStopGeneratedStream(label1); | 276 host_->OnStopGeneratedStream(label1); |
276 host_->OnStopGeneratedStream(label2); | 277 host_->OnStopGeneratedStream(label2); |
277 host_->OnStopGeneratedStream(label3); | 278 host_->OnStopGeneratedStream(label3); |
278 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 279 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
279 } | 280 } |
280 | 281 |
281 TEST_F(MediaStreamDispatcherHostTest, FailOpenVideoDevice) { | 282 TEST_F(MediaStreamDispatcherHostTest, FailOpenVideoDevice) { |
282 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); | 283 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
283 | 284 |
284 media::FakeVideoCaptureDevice::SetFailNextCreate(); | 285 media::FakeVideoCaptureDevice::SetFailNextCreate(); |
285 SetupFakeUI(false); | 286 SetupFakeUI(false); |
286 host_->OnGenerateStream(kPageRequestId, options); | 287 host_->OnGenerateStream(kPageRequestId, options); |
287 EXPECT_CALL(*host_, OnStreamGenerationFailed(kRenderId, kPageRequestId)); | 288 EXPECT_CALL(*host_.get(), |
| 289 OnStreamGenerationFailed(kRenderId, kPageRequestId)); |
288 WaitForResult(); | 290 WaitForResult(); |
289 } | 291 } |
290 | 292 |
291 TEST_F(MediaStreamDispatcherHostTest, CancelPendingStreamsOnChannelClosing) { | 293 TEST_F(MediaStreamDispatcherHostTest, CancelPendingStreamsOnChannelClosing) { |
292 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); | 294 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
293 | 295 |
294 // Create multiple GenerateStream requests. | 296 // Create multiple GenerateStream requests. |
295 size_t streams = 5; | 297 size_t streams = 5; |
296 for (size_t i = 1; i <= streams; ++i) { | 298 for (size_t i = 1; i <= streams; ++i) { |
297 host_->OnGenerateStream(kPageRequestId + i, options); | 299 host_->OnGenerateStream(kPageRequestId + i, options); |
298 EXPECT_EQ(host_->NumberOfStreams(), i); | 300 EXPECT_EQ(host_->NumberOfStreams(), i); |
299 } | 301 } |
300 | 302 |
301 // Calling OnChannelClosing() to cancel all the pending requests. | 303 // Calling OnChannelClosing() to cancel all the pending requests. |
302 host_->OnChannelClosing(); | 304 host_->OnChannelClosing(); |
303 | 305 |
304 // Streams should have been cleaned up. | 306 // Streams should have been cleaned up. |
305 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 307 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
306 } | 308 } |
307 | 309 |
308 TEST_F(MediaStreamDispatcherHostTest, StopGeneratedStreamsOnChannelClosing) { | 310 TEST_F(MediaStreamDispatcherHostTest, StopGeneratedStreamsOnChannelClosing) { |
309 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); | 311 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
310 | 312 |
311 // Create first group of streams. | 313 // Create first group of streams. |
312 size_t generated_streams = 3; | 314 size_t generated_streams = 3; |
313 for (size_t i = 0; i < generated_streams; ++i) { | 315 for (size_t i = 0; i < generated_streams; ++i) { |
314 SetupFakeUI(true); | 316 SetupFakeUI(true); |
315 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId + i, 0, 1)); | 317 EXPECT_CALL(*host_.get(), |
| 318 OnStreamGenerated(kRenderId, kPageRequestId + i, 0, 1)); |
316 host_->OnGenerateStream(kPageRequestId + i, options); | 319 host_->OnGenerateStream(kPageRequestId + i, options); |
317 | 320 |
318 // Wait until the stream is generated. | 321 // Wait until the stream is generated. |
319 WaitForResult(); | 322 WaitForResult(); |
320 } | 323 } |
321 EXPECT_EQ(host_->NumberOfStreams(), generated_streams); | 324 EXPECT_EQ(host_->NumberOfStreams(), generated_streams); |
322 | 325 |
323 // Calling OnChannelClosing() to cancel all the pending/generated streams. | 326 // Calling OnChannelClosing() to cancel all the pending/generated streams. |
324 host_->OnChannelClosing(); | 327 host_->OnChannelClosing(); |
325 | 328 |
326 message_loop_->RunUntilIdle(); | 329 message_loop_->RunUntilIdle(); |
327 | 330 |
328 // Streams should have been cleaned up. | 331 // Streams should have been cleaned up. |
329 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 332 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
330 } | 333 } |
331 | 334 |
332 TEST_F(MediaStreamDispatcherHostTest, CloseFromUI) { | 335 TEST_F(MediaStreamDispatcherHostTest, CloseFromUI) { |
333 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); | 336 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
334 | 337 |
335 base::Closure close_callback; | 338 base::Closure close_callback; |
336 scoped_ptr<MockMediaStreamUI> stream_ui(new MockMediaStreamUI()); | 339 scoped_ptr<MockMediaStreamUI> stream_ui(new MockMediaStreamUI()); |
337 EXPECT_CALL(*stream_ui, OnStarted(_)) | 340 EXPECT_CALL(*stream_ui, OnStarted(_)) |
338 .WillOnce(SaveArg<0>(&close_callback)); | 341 .WillOnce(SaveArg<0>(&close_callback)); |
339 media_stream_manager_->UseFakeUI(stream_ui.PassAs<MediaStreamUI>()); | 342 media_stream_manager_->UseFakeUI(stream_ui.PassAs<MediaStreamUI>()); |
340 | 343 |
341 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); | 344 EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); |
342 EXPECT_CALL(*host_, OnStreamGenerationFailed(kRenderId, kPageRequestId)); | 345 EXPECT_CALL(*host_.get(), |
| 346 OnStreamGenerationFailed(kRenderId, kPageRequestId)); |
343 host_->OnGenerateStream(kPageRequestId, options); | 347 host_->OnGenerateStream(kPageRequestId, options); |
344 | 348 |
345 WaitForResult(); | 349 WaitForResult(); |
346 | 350 |
347 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 351 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
348 EXPECT_EQ(host_->video_devices_.size(), 1u); | 352 EXPECT_EQ(host_->video_devices_.size(), 1u); |
349 EXPECT_EQ(host_->NumberOfStreams(), 1u); | 353 EXPECT_EQ(host_->NumberOfStreams(), 1u); |
350 | 354 |
351 ASSERT_FALSE(close_callback.is_null()); | 355 ASSERT_FALSE(close_callback.is_null()); |
352 close_callback.Run(); | 356 close_callback.Run(); |
353 message_loop_->RunUntilIdle(); | 357 message_loop_->RunUntilIdle(); |
354 | 358 |
355 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 359 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
356 } | 360 } |
357 | 361 |
358 }; // namespace content | 362 }; // namespace content |
OLD | NEW |