Fix build break
[chromium-blink-merge.git] / content / renderer / media / media_stream_impl_unittest.cc
blob3cf0cc4a59b7c812025706118900ca919b75dc03
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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/utf_string_conversions.h"
7 #include "content/renderer/media/media_stream_extra_data.h"
8 #include "content/renderer/media/media_stream_impl.h"
9 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
10 #include "content/renderer/media/mock_media_stream_dispatcher.h"
11 #include "content/renderer/media/video_capture_impl_manager.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStream.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamSource.h"
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamTrack.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
19 namespace content {
21 class MediaStreamImplUnderTest : public MediaStreamImpl {
22 public:
23 enum RequestState {
24 REQUEST_NOT_STARTED,
25 REQUEST_NOT_COMPLETE,
26 REQUEST_SUCCEEDED,
27 REQUEST_FAILED,
30 MediaStreamImplUnderTest(MediaStreamDispatcher* media_stream_dispatcher,
31 VideoCaptureImplManager* vc_manager,
32 MediaStreamDependencyFactory* dependency_factory)
33 : MediaStreamImpl(NULL, media_stream_dispatcher, vc_manager,
34 dependency_factory),
35 state_(REQUEST_NOT_STARTED) {
38 void RequestUserMedia(bool audio, bool video) {
39 WebKit::WebUserMediaRequest user_media_request;
40 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_sources(
41 audio ? static_cast<size_t>(1) : 0);
42 WebKit::WebVector<WebKit::WebMediaStreamSource> video_sources(
43 video ? static_cast<size_t>(1) : 0);
44 state_ = REQUEST_NOT_COMPLETE;
45 requestUserMedia(user_media_request, audio_sources,
46 video_sources);
49 virtual void CompleteGetUserMediaRequest(
50 const WebKit::WebMediaStream& stream,
51 WebKit::WebUserMediaRequest* request_info,
52 bool request_succeeded) OVERRIDE {
53 last_generated_stream_ = stream;
54 state_ = request_succeeded ? REQUEST_SUCCEEDED : REQUEST_FAILED;
57 virtual WebKit::WebMediaStream GetMediaStream(
58 const GURL& url) OVERRIDE {
59 return last_generated_stream_;
62 using MediaStreamImpl::OnLocalMediaStreamStop;
64 const WebKit::WebMediaStream& last_generated_stream() {
65 return last_generated_stream_;
68 RequestState request_state() const { return state_; }
70 private:
71 WebKit::WebMediaStream last_generated_stream_;
72 RequestState state_;
75 class MediaStreamImplTest : public ::testing::Test {
76 public:
77 virtual void SetUp() {
78 // Create our test object.
79 ms_dispatcher_.reset(new MockMediaStreamDispatcher());
80 scoped_refptr<VideoCaptureImplManager> vc_manager(
81 new VideoCaptureImplManager());
82 dependency_factory_.reset(new MockMediaStreamDependencyFactory());
83 ms_impl_.reset(new MediaStreamImplUnderTest(ms_dispatcher_.get(),
84 vc_manager.get(),
85 dependency_factory_.get()));
88 WebKit::WebMediaStream RequestLocalMediaStream(bool audio,
89 bool video) {
90 ms_impl_->RequestUserMedia(audio, video);
91 FakeMediaStreamDispatcherComplete();
92 if (video)
93 ChangeVideoSourceStateToLive();
94 if (audio)
95 ChangeAudioSourceStateToLive();
97 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_SUCCEEDED,
98 ms_impl_->request_state());
100 WebKit::WebMediaStream desc = ms_impl_->last_generated_stream();
101 content::MediaStreamExtraData* extra_data =
102 static_cast<content::MediaStreamExtraData*>(desc.extraData());
103 if (!extra_data || !extra_data->stream()) {
104 ADD_FAILURE();
105 return desc;
108 if (audio)
109 EXPECT_EQ(1u, extra_data->stream()->GetAudioTracks().size());
110 if (video)
111 EXPECT_EQ(1u, extra_data->stream()->GetVideoTracks().size());
112 if (audio && video) {
113 EXPECT_NE(extra_data->stream()->GetAudioTracks()[0]->id(),
114 extra_data->stream()->GetVideoTracks()[0]->id());
116 return desc;
120 void FakeMediaStreamDispatcherComplete() {
121 ms_impl_->OnStreamGenerated(ms_dispatcher_->request_id(),
122 ms_dispatcher_->stream_label(),
123 ms_dispatcher_->audio_array(),
124 ms_dispatcher_->video_array());
127 void ChangeVideoSourceStateToLive() {
128 if (dependency_factory_->last_video_source() != NULL) {
129 dependency_factory_->last_video_source()->SetLive();
133 void ChangeAudioSourceStateToLive() {
134 if (dependency_factory_->last_audio_source() != NULL) {
135 dependency_factory_->last_audio_source()->SetLive();
139 void ChangeVideoSourceStateToEnded() {
140 if (dependency_factory_->last_video_source() != NULL) {
141 dependency_factory_->last_video_source()->SetEnded();
145 void ChangeAudioSourceStateToEnded() {
146 if (dependency_factory_->last_audio_source() != NULL) {
147 dependency_factory_->last_audio_source()->SetEnded();
151 protected:
152 scoped_ptr<MockMediaStreamDispatcher> ms_dispatcher_;
153 scoped_ptr<MediaStreamImplUnderTest> ms_impl_;
154 scoped_ptr<MockMediaStreamDependencyFactory> dependency_factory_;
157 TEST_F(MediaStreamImplTest, LocalMediaStream) {
158 // Test a stream with both audio and video.
159 WebKit::WebMediaStream mixed_desc = RequestLocalMediaStream(true, true);
161 // Test a stream with audio only.
162 WebKit::WebMediaStream audio_desc = RequestLocalMediaStream(true, false);
164 // Test a stream with video only.
165 WebKit::WebMediaStream video_desc = RequestLocalMediaStream(false, true);
167 // Stop generated local streams.
168 ms_impl_->OnLocalMediaStreamStop(mixed_desc.label().utf8());
169 EXPECT_EQ(1, ms_dispatcher_->stop_stream_counter());
170 ms_impl_->OnLocalMediaStreamStop(audio_desc.label().utf8());
171 EXPECT_EQ(2, ms_dispatcher_->stop_stream_counter());
173 // Test that the MediaStreams are deleted if the owning WebFrame is deleted.
174 // In the unit test the owning frame is NULL.
175 ms_impl_->FrameWillClose(NULL);
176 EXPECT_EQ(3, ms_dispatcher_->stop_stream_counter());
179 // This test what happens if a source to a MediaSteam fails to start.
180 TEST_F(MediaStreamImplTest, MediaSourceFailToStart) {
181 ms_impl_->RequestUserMedia(true, true);
182 FakeMediaStreamDispatcherComplete();
183 ChangeVideoSourceStateToEnded();
184 ChangeAudioSourceStateToEnded();
185 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED,
186 ms_impl_->request_state());
187 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
188 EXPECT_EQ(1, ms_dispatcher_->stop_stream_counter());
191 // This test what happens if MediaStreamImpl is deleted while the sources of a
192 // MediaStream is being started.
193 TEST_F(MediaStreamImplTest, MediaStreamImplShutDown) {
194 ms_impl_->RequestUserMedia(true, true);
195 FakeMediaStreamDispatcherComplete();
196 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
197 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE,
198 ms_impl_->request_state());
199 ms_impl_.reset();
200 ChangeAudioSourceStateToLive();
201 ChangeVideoSourceStateToLive();
204 // This test what happens if the WebFrame is closed while the MediaStream is
205 // being generated by the MediaStreamDispatcher.
206 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingStream) {
207 ms_impl_->RequestUserMedia(true, true);
208 ms_impl_->FrameWillClose(NULL);
209 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
210 EXPECT_EQ(0, ms_dispatcher_->stop_stream_counter());
211 ChangeAudioSourceStateToLive();
212 ChangeVideoSourceStateToLive();
213 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE,
214 ms_impl_->request_state());
217 // This test what happens if the WebFrame is closed while the sources are being
218 // started by MediaStreamDependencyFactory.
219 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingSources) {
220 ms_impl_->RequestUserMedia(true, true);
221 FakeMediaStreamDispatcherComplete();
222 EXPECT_EQ(0, ms_dispatcher_->stop_stream_counter());
223 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
224 ms_impl_->FrameWillClose(NULL);
225 EXPECT_EQ(1, ms_dispatcher_->stop_stream_counter());
226 ChangeAudioSourceStateToLive();
227 ChangeVideoSourceStateToLive();
228 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE,
229 ms_impl_->request_state());
232 // This test what happens if stop is called on a stream after the frame has
233 // been reloaded.
234 TEST_F(MediaStreamImplTest, StopStreamAfterReload) {
235 WebKit::WebMediaStream mixed_desc = RequestLocalMediaStream(true, true);
236 EXPECT_EQ(0, ms_dispatcher_->stop_stream_counter());
237 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
238 ms_impl_->FrameWillClose(NULL);
239 EXPECT_EQ(1, ms_dispatcher_->stop_stream_counter());
240 ms_impl_->OnLocalMediaStreamStop(mixed_desc.label().utf8());
241 EXPECT_EQ(1, ms_dispatcher_->stop_stream_counter());
244 } // namespace content