| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback_helpers.h" | 6 #include "base/callback_helpers.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| 11 #include "content/public/renderer/media_stream_video_sink.h" | 11 #include "content/public/renderer/media_stream_video_sink.h" |
| 12 #include "content/renderer/media/media_stream_video_capturer_source.h" | 12 #include "content/renderer/media/media_stream_video_capturer_source.h" |
| 13 #include "content/renderer/media/media_stream_video_track.h" | 13 #include "content/renderer/media/media_stream_video_track.h" |
| 14 #include "content/renderer/media/mock_media_constraint_factory.h" | 14 #include "content/renderer/media/mock_media_constraint_factory.h" |
| 15 #include "media/base/bind_to_current_loop.h" | 15 #include "media/base/bind_to_current_loop.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/WebKit/public/web/WebHeap.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 class MockVideoCapturerDelegate : public VideoCapturerDelegate { | 22 class MockVideoCapturerDelegate : public VideoCapturerDelegate { |
| 22 public: | 23 public: |
| 23 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info) | 24 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info) |
| 24 : VideoCapturerDelegate(device_info) {} | 25 : VideoCapturerDelegate(device_info) {} |
| 25 | 26 |
| 26 MOCK_METHOD3(StartCapture, | 27 MOCK_METHOD3(StartCapture, |
| 27 void(const media::VideoCaptureParams& params, | 28 void(const media::VideoCaptureParams& params, |
| 28 const VideoCaptureDeliverFrameCB& new_frame_callback, | 29 const VideoCaptureDeliverFrameCB& new_frame_callback, |
| 29 const RunningCallback& running_callback)); | 30 const RunningCallback& running_callback)); |
| 30 MOCK_METHOD0(StopCapture, void()); | 31 MOCK_METHOD0(StopCapture, void()); |
| 31 | 32 |
| 32 private: | 33 private: |
| 33 virtual ~MockVideoCapturerDelegate() {} | 34 virtual ~MockVideoCapturerDelegate() {} |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 class MediaStreamVideoCapturerSourceTest : public testing::Test { | 37 class MediaStreamVideoCapturerSourceTest : public testing::Test { |
| 37 public: | 38 public: |
| 38 MediaStreamVideoCapturerSourceTest() | 39 MediaStreamVideoCapturerSourceTest() |
| 39 : child_process_(new ChildProcess()), | 40 : child_process_(new ChildProcess()), |
| 40 source_(NULL), | 41 source_(NULL), |
| 41 source_stopped_(false) { | 42 source_stopped_(false) { |
| 42 } | 43 } |
| 43 | 44 |
| 45 virtual void TearDown() OVERRIDE { |
| 46 webkit_source_.reset(); |
| 47 blink::WebHeap::collectAllGarbageForTesting(); |
| 48 } |
| 49 |
| 44 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { | 50 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { |
| 45 delegate_ = new MockVideoCapturerDelegate(device_info); | 51 delegate_ = new MockVideoCapturerDelegate(device_info); |
| 46 source_ = new MediaStreamVideoCapturerSource( | 52 source_ = new MediaStreamVideoCapturerSource( |
| 47 device_info, | 53 device_info, |
| 48 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 54 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
| 49 base::Unretained(this)), | 55 base::Unretained(this)), |
| 50 delegate_); | 56 delegate_); |
| 51 | 57 |
| 52 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 58 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
| 53 blink::WebMediaStreamSource::TypeVideo, | 59 blink::WebMediaStreamSource::TypeVideo, |
| 54 base::UTF8ToUTF16("dummy_source_name")); | 60 base::UTF8ToUTF16("dummy_source_name")); |
| 55 webkit_source_.setExtraData(source_); | 61 webkit_source_.setExtraData(source_); |
| 62 webkit_source_id_ = webkit_source_.id(); |
| 56 } | 63 } |
| 57 | 64 |
| 58 blink::WebMediaStreamTrack StartSource() { | 65 blink::WebMediaStreamTrack StartSource() { |
| 59 MockMediaConstraintFactory factory; | 66 MockMediaConstraintFactory factory; |
| 60 bool enabled = true; | 67 bool enabled = true; |
| 61 // CreateVideoTrack will trigger OnConstraintsApplied. | 68 // CreateVideoTrack will trigger OnConstraintsApplied. |
| 62 return MediaStreamVideoTrack::CreateVideoTrack( | 69 return MediaStreamVideoTrack::CreateVideoTrack( |
| 63 source_, factory.CreateWebMediaConstraints(), | 70 source_, factory.CreateWebMediaConstraints(), |
| 64 base::Bind( | 71 base::Bind( |
| 65 &MediaStreamVideoCapturerSourceTest::OnConstraintsApplied, | 72 &MediaStreamVideoCapturerSourceTest::OnConstraintsApplied, |
| 66 base::Unretained(this)), | 73 base::Unretained(this)), |
| 67 enabled); | 74 enabled); |
| 68 } | 75 } |
| 69 | 76 |
| 70 MockVideoCapturerDelegate& mock_delegate() { | 77 MockVideoCapturerDelegate& mock_delegate() { |
| 71 return *static_cast<MockVideoCapturerDelegate*>(delegate_.get()); | 78 return *static_cast<MockVideoCapturerDelegate*>(delegate_.get()); |
| 72 } | 79 } |
| 73 | 80 |
| 74 void OnSourceStopped(const blink::WebMediaStreamSource& source) { | 81 void OnSourceStopped(const blink::WebMediaStreamSource& source) { |
| 75 source_stopped_ = true; | 82 source_stopped_ = true; |
| 76 EXPECT_EQ(source.id(), webkit_source_.id()); | 83 EXPECT_EQ(source.id(), webkit_source_id_); |
| 77 } | 84 } |
| 78 | 85 |
| 79 protected: | 86 protected: |
| 80 void OnConstraintsApplied(MediaStreamSource* source, | 87 void OnConstraintsApplied(MediaStreamSource* source, |
| 81 MediaStreamRequestResult result, | 88 MediaStreamRequestResult result, |
| 82 const blink::WebString& result_name) { | 89 const blink::WebString& result_name) { |
| 83 } | 90 } |
| 84 | 91 |
| 85 base::MessageLoopForUI message_loop_; | 92 base::MessageLoopForUI message_loop_; |
| 86 scoped_ptr<ChildProcess> child_process_; | 93 scoped_ptr<ChildProcess> child_process_; |
| 87 blink::WebMediaStreamSource webkit_source_; | 94 blink::WebMediaStreamSource webkit_source_; |
| 88 MediaStreamVideoCapturerSource* source_; // owned by webkit_source. | 95 MediaStreamVideoCapturerSource* source_; // owned by webkit_source. |
| 89 scoped_refptr<VideoCapturerDelegate> delegate_; | 96 scoped_refptr<VideoCapturerDelegate> delegate_; |
| 97 blink::WebString webkit_source_id_; |
| 90 bool source_stopped_; | 98 bool source_stopped_; |
| 91 }; | 99 }; |
| 92 | 100 |
| 93 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { | 101 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { |
| 94 StreamDeviceInfo device_info; | 102 StreamDeviceInfo device_info; |
| 95 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 103 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
| 96 InitWithDeviceInfo(device_info); | 104 InitWithDeviceInfo(device_info); |
| 97 | 105 |
| 98 EXPECT_CALL(mock_delegate(), StartCapture( | 106 EXPECT_CALL(mock_delegate(), StartCapture( |
| 99 testing::Field(&media::VideoCaptureParams::allow_resolution_change, true), | 107 testing::Field(&media::VideoCaptureParams::allow_resolution_change, true), |
| (...skipping 25 matching lines...) Expand all Loading... |
| 125 delegate_ = new VideoCapturerDelegate(device_info); | 133 delegate_ = new VideoCapturerDelegate(device_info); |
| 126 source_ = new MediaStreamVideoCapturerSource( | 134 source_ = new MediaStreamVideoCapturerSource( |
| 127 device_info, | 135 device_info, |
| 128 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 136 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
| 129 base::Unretained(this)), | 137 base::Unretained(this)), |
| 130 delegate_); | 138 delegate_); |
| 131 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 139 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
| 132 blink::WebMediaStreamSource::TypeVideo, | 140 blink::WebMediaStreamSource::TypeVideo, |
| 133 base::UTF8ToUTF16("dummy_source_name")); | 141 base::UTF8ToUTF16("dummy_source_name")); |
| 134 webkit_source_.setExtraData(source_); | 142 webkit_source_.setExtraData(source_); |
| 143 webkit_source_id_ = webkit_source_.id(); |
| 135 blink::WebMediaStreamTrack track = StartSource(); | 144 blink::WebMediaStreamTrack track = StartSource(); |
| 136 message_loop_.RunUntilIdle(); | 145 message_loop_.RunUntilIdle(); |
| 137 | 146 |
| 138 delegate_->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); | 147 delegate_->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); |
| 139 message_loop_.RunUntilIdle(); | 148 message_loop_.RunUntilIdle(); |
| 140 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive, | 149 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive, |
| 141 webkit_source_.readyState()); | 150 webkit_source_.readyState()); |
| 142 | 151 |
| 143 EXPECT_FALSE(source_stopped_); | 152 EXPECT_FALSE(source_stopped_); |
| 144 delegate_->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_ERROR); | 153 delegate_->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_ERROR); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 base::Bind(deliver_frame_cb, | 214 base::Bind(deliver_frame_cb, |
| 206 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), | 215 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), |
| 207 media::VideoCaptureFormat(), | 216 media::VideoCaptureFormat(), |
| 208 reference_capture_time)); | 217 reference_capture_time)); |
| 209 run_loop.Run(); | 218 run_loop.Run(); |
| 210 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); | 219 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); |
| 211 EXPECT_EQ(reference_capture_time, capture_time); | 220 EXPECT_EQ(reference_capture_time, capture_time); |
| 212 } | 221 } |
| 213 | 222 |
| 214 } // namespace content | 223 } // namespace content |
| OLD | NEW |