| 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "content/renderer/media/media_stream_extra_data.h" | 7 #include "content/renderer/media/media_stream_extra_data.h" |
| 8 #include "content/renderer/media/media_stream_impl.h" | 8 #include "content/renderer/media/media_stream_impl.h" |
| 9 #include "content/renderer/media/mock_media_stream_dependency_factory.h" | 9 #include "content/renderer/media/mock_media_stream_dependency_factory.h" |
| 10 #include "content/renderer/media/mock_media_stream_dispatcher.h" | 10 #include "content/renderer/media/mock_media_stream_dispatcher.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 last_generated_stream_ = stream; | 46 last_generated_stream_ = stream; |
| 47 state_ = request_succeeded ? REQUEST_SUCCEEDED : REQUEST_FAILED; | 47 state_ = request_succeeded ? REQUEST_SUCCEEDED : REQUEST_FAILED; |
| 48 } | 48 } |
| 49 | 49 |
| 50 virtual blink::WebMediaStream GetMediaStream( | 50 virtual blink::WebMediaStream GetMediaStream( |
| 51 const GURL& url) OVERRIDE { | 51 const GURL& url) OVERRIDE { |
| 52 return last_generated_stream_; | 52 return last_generated_stream_; |
| 53 } | 53 } |
| 54 | 54 |
| 55 using MediaStreamImpl::OnLocalMediaStreamStop; | 55 using MediaStreamImpl::OnLocalMediaStreamStop; |
| 56 using MediaStreamImpl::OnLocalSourceStop; | 56 using MediaStreamImpl::OnLocalSourceStopped; |
| 57 | 57 |
| 58 const blink::WebMediaStream& last_generated_stream() { | 58 const blink::WebMediaStream& last_generated_stream() { |
| 59 return last_generated_stream_; | 59 return last_generated_stream_; |
| 60 } | 60 } |
| 61 | 61 |
| 62 RequestState request_state() const { return state_; } | 62 RequestState request_state() const { return state_; } |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 blink::WebMediaStream last_generated_stream_; | 65 blink::WebMediaStream last_generated_stream_; |
| 66 RequestState state_; | 66 RequestState state_; |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 class MediaStreamImplTest : public ::testing::Test { | 69 class MediaStreamImplTest : public ::testing::Test { |
| 70 public: | 70 public: |
| 71 virtual void SetUp() { | 71 virtual void SetUp() { |
| 72 // Create our test object. | 72 // Create our test object. |
| 73 ms_dispatcher_.reset(new MockMediaStreamDispatcher()); | 73 ms_dispatcher_.reset(new MockMediaStreamDispatcher()); |
| 74 dependency_factory_.reset(new MockMediaStreamDependencyFactory()); | 74 dependency_factory_.reset(new MockMediaStreamDependencyFactory()); |
| 75 ms_impl_.reset(new MediaStreamImplUnderTest(ms_dispatcher_.get(), | 75 ms_impl_.reset(new MediaStreamImplUnderTest(ms_dispatcher_.get(), |
| 76 dependency_factory_.get())); | 76 dependency_factory_.get())); |
| 77 } | 77 } |
| 78 | 78 |
| 79 blink::WebMediaStream RequestLocalMediaStream() { | 79 blink::WebMediaStream RequestLocalMediaStream() { |
| 80 ms_impl_->RequestUserMedia(); | 80 ms_impl_->RequestUserMedia(); |
| 81 FakeMediaStreamDispatcherComplete(); | 81 FakeMediaStreamDispatcherComplete(); |
| 82 ChangeVideoSourceStateToLive(); | 82 ChangeVideoSourceStateToLive(); |
| 83 ChangeAudioSourceStateToLive(); | |
| 84 | 83 |
| 85 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_SUCCEEDED, | 84 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_SUCCEEDED, |
| 86 ms_impl_->request_state()); | 85 ms_impl_->request_state()); |
| 87 | 86 |
| 88 blink::WebMediaStream desc = ms_impl_->last_generated_stream(); | 87 blink::WebMediaStream desc = ms_impl_->last_generated_stream(); |
| 89 content::MediaStreamExtraData* extra_data = | 88 content::MediaStreamExtraData* extra_data = |
| 90 static_cast<content::MediaStreamExtraData*>(desc.extraData()); | 89 static_cast<content::MediaStreamExtraData*>(desc.extraData()); |
| 91 if (!extra_data || !extra_data->stream().get()) { | 90 if (!extra_data || !extra_data->stream().get()) { |
| 92 ADD_FAILURE(); | 91 ADD_FAILURE(); |
| 93 return desc; | 92 return desc; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 106 ms_dispatcher_->audio_array(), | 105 ms_dispatcher_->audio_array(), |
| 107 ms_dispatcher_->video_array()); | 106 ms_dispatcher_->video_array()); |
| 108 } | 107 } |
| 109 | 108 |
| 110 void ChangeVideoSourceStateToLive() { | 109 void ChangeVideoSourceStateToLive() { |
| 111 if (dependency_factory_->last_video_source() != NULL) { | 110 if (dependency_factory_->last_video_source() != NULL) { |
| 112 dependency_factory_->last_video_source()->SetLive(); | 111 dependency_factory_->last_video_source()->SetLive(); |
| 113 } | 112 } |
| 114 } | 113 } |
| 115 | 114 |
| 116 void ChangeAudioSourceStateToLive() { | |
| 117 if (dependency_factory_->last_audio_source() != NULL) { | |
| 118 dependency_factory_->last_audio_source()->SetLive(); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void ChangeVideoSourceStateToEnded() { | 115 void ChangeVideoSourceStateToEnded() { |
| 123 if (dependency_factory_->last_video_source() != NULL) { | 116 if (dependency_factory_->last_video_source() != NULL) { |
| 124 dependency_factory_->last_video_source()->SetEnded(); | 117 dependency_factory_->last_video_source()->SetEnded(); |
| 125 } | 118 } |
| 126 } | 119 } |
| 127 | 120 |
| 128 void ChangeAudioSourceStateToEnded() { | |
| 129 if (dependency_factory_->last_audio_source() != NULL) { | |
| 130 dependency_factory_->last_audio_source()->SetEnded(); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 protected: | 121 protected: |
| 135 scoped_ptr<MockMediaStreamDispatcher> ms_dispatcher_; | 122 scoped_ptr<MockMediaStreamDispatcher> ms_dispatcher_; |
| 136 scoped_ptr<MediaStreamImplUnderTest> ms_impl_; | 123 scoped_ptr<MediaStreamImplUnderTest> ms_impl_; |
| 137 scoped_ptr<MockMediaStreamDependencyFactory> dependency_factory_; | 124 scoped_ptr<MockMediaStreamDependencyFactory> dependency_factory_; |
| 138 }; | 125 }; |
| 139 | 126 |
| 140 TEST_F(MediaStreamImplTest, GenerateMediaStream) { | 127 TEST_F(MediaStreamImplTest, GenerateMediaStream) { |
| 141 // Generate a stream with both audio and video. | 128 // Generate a stream with both audio and video. |
| 142 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); | 129 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); |
| 143 } | 130 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 // Test that the source is stopped even if there are two MediaStreams using | 216 // Test that the source is stopped even if there are two MediaStreams using |
| 230 // the same source. | 217 // the same source. |
| 231 TEST_F(MediaStreamImplTest, StopSource) { | 218 TEST_F(MediaStreamImplTest, StopSource) { |
| 232 // Generate a stream with both audio and video. | 219 // Generate a stream with both audio and video. |
| 233 blink::WebMediaStream desc1 = RequestLocalMediaStream(); | 220 blink::WebMediaStream desc1 = RequestLocalMediaStream(); |
| 234 blink::WebMediaStream desc2 = RequestLocalMediaStream(); | 221 blink::WebMediaStream desc2 = RequestLocalMediaStream(); |
| 235 | 222 |
| 236 // Stop the video source. | 223 // Stop the video source. |
| 237 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; | 224 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; |
| 238 desc1.videoTracks(video_tracks); | 225 desc1.videoTracks(video_tracks); |
| 239 ms_impl_->OnLocalSourceStop(video_tracks[0].source()); | 226 ms_impl_->OnLocalSourceStopped(video_tracks[0].source()); |
| 240 EXPECT_EQ(0, ms_dispatcher_->stop_audio_device_counter()); | 227 EXPECT_EQ(0, ms_dispatcher_->stop_audio_device_counter()); |
| 241 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 228 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 242 | 229 |
| 243 // Stop the audio source. | 230 // Stop the audio source. |
| 244 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; | 231 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; |
| 245 desc1.audioTracks(audio_tracks); | 232 desc1.audioTracks(audio_tracks); |
| 246 ms_impl_->OnLocalSourceStop(audio_tracks[0].source()); | 233 ms_impl_->OnLocalSourceStopped(audio_tracks[0].source()); |
| 247 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 234 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
| 248 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 235 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 249 } | 236 } |
| 250 | 237 |
| 251 // Test that the MediaStreams are deleted if the owning WebFrame is deleted. | 238 // Test that the MediaStreams are deleted if the owning WebFrame is deleted. |
| 252 // In the unit test the owning frame is NULL. | 239 // In the unit test the owning frame is NULL. |
| 253 TEST_F(MediaStreamImplTest, FrameWillClose) { | 240 TEST_F(MediaStreamImplTest, FrameWillClose) { |
| 254 // Test a stream with both audio and video. | 241 // Test a stream with both audio and video. |
| 255 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); | 242 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); |
| 256 | 243 |
| 257 // Test that the MediaStreams are deleted if the owning WebFrame is deleted. | 244 // Test that the MediaStreams are deleted if the owning WebFrame is deleted. |
| 258 // In the unit test the owning frame is NULL. | 245 // In the unit test the owning frame is NULL. |
| 259 ms_impl_->FrameWillClose(NULL); | 246 ms_impl_->FrameWillClose(NULL); |
| 260 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 247 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
| 261 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 248 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 262 } | 249 } |
| 263 | 250 |
| 264 // This test what happens if a source to a MediaSteam fails to start. | 251 // This test what happens if a source to a MediaSteam fails to start. |
| 265 TEST_F(MediaStreamImplTest, MediaSourceFailToStart) { | 252 TEST_F(MediaStreamImplTest, MediaSourceFailToStart) { |
| 266 ms_impl_->RequestUserMedia(); | 253 ms_impl_->RequestUserMedia(); |
| 267 FakeMediaStreamDispatcherComplete(); | 254 FakeMediaStreamDispatcherComplete(); |
| 268 ChangeVideoSourceStateToEnded(); | 255 ChangeVideoSourceStateToEnded(); |
| 269 ChangeAudioSourceStateToEnded(); | |
| 270 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED, | 256 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED, |
| 271 ms_impl_->request_state()); | 257 ms_impl_->request_state()); |
| 272 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 258 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
| 273 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 259 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
| 274 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 260 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 275 } | 261 } |
| 276 | 262 |
| 277 // This test what happens if MediaStreamImpl is deleted while the sources of a | 263 // This test what happens if MediaStreamImpl is deleted while the sources of a |
| 278 // MediaStream is being started. | 264 // MediaStream is being started. |
| 279 TEST_F(MediaStreamImplTest, MediaStreamImplShutDown) { | 265 TEST_F(MediaStreamImplTest, MediaStreamImplShutDown) { |
| 280 ms_impl_->RequestUserMedia(); | 266 ms_impl_->RequestUserMedia(); |
| 281 FakeMediaStreamDispatcherComplete(); | 267 FakeMediaStreamDispatcherComplete(); |
| 282 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 268 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
| 283 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, | 269 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, |
| 284 ms_impl_->request_state()); | 270 ms_impl_->request_state()); |
| 285 ms_impl_.reset(); | 271 ms_impl_.reset(); |
| 286 ChangeAudioSourceStateToLive(); | |
| 287 ChangeVideoSourceStateToLive(); | 272 ChangeVideoSourceStateToLive(); |
| 288 } | 273 } |
| 289 | 274 |
| 290 // This test what happens if the WebFrame is closed while the MediaStream is | 275 // This test what happens if the WebFrame is closed while the MediaStream is |
| 291 // being generated by the MediaStreamDispatcher. | 276 // being generated by the MediaStreamDispatcher. |
| 292 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingStream) { | 277 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingStream) { |
| 293 ms_impl_->RequestUserMedia(); | 278 ms_impl_->RequestUserMedia(); |
| 294 ms_impl_->FrameWillClose(NULL); | 279 ms_impl_->FrameWillClose(NULL); |
| 295 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 280 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
| 296 EXPECT_EQ(0, ms_dispatcher_->stop_audio_device_counter()); | 281 EXPECT_EQ(0, ms_dispatcher_->stop_audio_device_counter()); |
| 297 EXPECT_EQ(0, ms_dispatcher_->stop_video_device_counter()); | 282 EXPECT_EQ(0, ms_dispatcher_->stop_video_device_counter()); |
| 298 ChangeAudioSourceStateToLive(); | |
| 299 ChangeVideoSourceStateToLive(); | 283 ChangeVideoSourceStateToLive(); |
| 300 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, | 284 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, |
| 301 ms_impl_->request_state()); | 285 ms_impl_->request_state()); |
| 302 } | 286 } |
| 303 | 287 |
| 304 // This test what happens if the WebFrame is closed while the sources are being | 288 // This test what happens if the WebFrame is closed while the sources are being |
| 305 // started by MediaStreamDependencyFactory. | 289 // started by MediaStreamDependencyFactory. |
| 306 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingSources) { | 290 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingSources) { |
| 307 ms_impl_->RequestUserMedia(); | 291 ms_impl_->RequestUserMedia(); |
| 308 FakeMediaStreamDispatcherComplete(); | 292 FakeMediaStreamDispatcherComplete(); |
| 309 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 293 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
| 310 ms_impl_->FrameWillClose(NULL); | 294 ms_impl_->FrameWillClose(NULL); |
| 311 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 295 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
| 312 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 296 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 313 ChangeAudioSourceStateToLive(); | |
| 314 ChangeVideoSourceStateToLive(); | 297 ChangeVideoSourceStateToLive(); |
| 315 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, | 298 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, |
| 316 ms_impl_->request_state()); | 299 ms_impl_->request_state()); |
| 317 } | 300 } |
| 318 | 301 |
| 319 // This test what happens if stop is called on a stream after the frame has | 302 // This test what happens if stop is called on a stream after the frame has |
| 320 // been reloaded. | 303 // been reloaded. |
| 321 TEST_F(MediaStreamImplTest, StopStreamAfterReload) { | 304 TEST_F(MediaStreamImplTest, StopStreamAfterReload) { |
| 322 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); | 305 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); |
| 323 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 306 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
| 324 ms_impl_->FrameWillClose(NULL); | 307 ms_impl_->FrameWillClose(NULL); |
| 325 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 308 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
| 326 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 309 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 327 ms_impl_->OnLocalMediaStreamStop(mixed_desc.id().utf8()); | 310 ms_impl_->OnLocalMediaStreamStop(mixed_desc.id().utf8()); |
| 328 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 311 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
| 329 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 312 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
| 330 } | 313 } |
| 331 | 314 |
| 332 } // namespace content | 315 } // namespace content |
| OLD | NEW |