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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 public: | 23 public: |
24 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info) | 24 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info) |
25 : VideoCapturerDelegate(device_info) {} | 25 : VideoCapturerDelegate(device_info) {} |
26 | 26 |
27 MOCK_METHOD3(StartCapture, | 27 MOCK_METHOD3(StartCapture, |
28 void(const media::VideoCaptureParams& params, | 28 void(const media::VideoCaptureParams& params, |
29 const VideoCaptureDeliverFrameCB& new_frame_callback, | 29 const VideoCaptureDeliverFrameCB& new_frame_callback, |
30 const RunningCallback& running_callback)); | 30 const RunningCallback& running_callback)); |
31 MOCK_METHOD0(StopCapture, void()); | 31 MOCK_METHOD0(StopCapture, void()); |
32 | 32 |
33 private: | |
34 virtual ~MockVideoCapturerDelegate() {} | 33 virtual ~MockVideoCapturerDelegate() {} |
35 }; | 34 }; |
36 | 35 |
37 class MediaStreamVideoCapturerSourceTest : public testing::Test { | 36 class MediaStreamVideoCapturerSourceTest : public testing::Test { |
38 public: | 37 public: |
39 MediaStreamVideoCapturerSourceTest() | 38 MediaStreamVideoCapturerSourceTest() |
40 : child_process_(new ChildProcess()), | 39 : child_process_(new ChildProcess()), |
41 source_(NULL), | 40 source_(NULL), |
| 41 delegate_(NULL), |
42 source_stopped_(false) { | 42 source_stopped_(false) { |
43 } | 43 } |
44 | 44 |
45 void TearDown() override { | 45 void TearDown() override { |
46 webkit_source_.reset(); | 46 webkit_source_.reset(); |
47 blink::WebHeap::collectAllGarbageForTesting(); | 47 blink::WebHeap::collectAllGarbageForTesting(); |
48 } | 48 } |
49 | 49 |
50 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { | 50 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { |
51 delegate_ = new MockVideoCapturerDelegate(device_info); | 51 scoped_ptr<MockVideoCapturerDelegate> delegate( |
| 52 new MockVideoCapturerDelegate(device_info)); |
| 53 delegate_ = delegate.get(); |
52 source_ = new MediaStreamVideoCapturerSource( | 54 source_ = new MediaStreamVideoCapturerSource( |
53 device_info, | 55 device_info, |
54 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 56 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
55 base::Unretained(this)), | 57 base::Unretained(this)), |
56 delegate_); | 58 delegate.Pass()); |
57 | 59 |
58 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 60 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
59 blink::WebMediaStreamSource::TypeVideo, | 61 blink::WebMediaStreamSource::TypeVideo, |
60 base::UTF8ToUTF16("dummy_source_name"), | 62 base::UTF8ToUTF16("dummy_source_name"), |
61 false /* remote */ , true /* readonly */); | 63 false /* remote */ , true /* readonly */); |
62 webkit_source_.setExtraData(source_); | 64 webkit_source_.setExtraData(source_); |
63 webkit_source_id_ = webkit_source_.id(); | 65 webkit_source_id_ = webkit_source_.id(); |
64 } | 66 } |
65 | 67 |
66 blink::WebMediaStreamTrack StartSource() { | 68 blink::WebMediaStreamTrack StartSource() { |
67 MockMediaConstraintFactory factory; | 69 MockMediaConstraintFactory factory; |
68 bool enabled = true; | 70 bool enabled = true; |
69 // CreateVideoTrack will trigger OnConstraintsApplied. | 71 // CreateVideoTrack will trigger OnConstraintsApplied. |
70 return MediaStreamVideoTrack::CreateVideoTrack( | 72 return MediaStreamVideoTrack::CreateVideoTrack( |
71 source_, factory.CreateWebMediaConstraints(), | 73 source_, factory.CreateWebMediaConstraints(), |
72 base::Bind( | 74 base::Bind( |
73 &MediaStreamVideoCapturerSourceTest::OnConstraintsApplied, | 75 &MediaStreamVideoCapturerSourceTest::OnConstraintsApplied, |
74 base::Unretained(this)), | 76 base::Unretained(this)), |
75 enabled); | 77 enabled); |
76 } | 78 } |
77 | 79 |
78 MockVideoCapturerDelegate& mock_delegate() { | 80 MockVideoCapturerDelegate& mock_delegate() { |
79 return *static_cast<MockVideoCapturerDelegate*>(delegate_.get()); | 81 return *delegate_; |
80 } | 82 } |
81 | 83 |
82 void OnSourceStopped(const blink::WebMediaStreamSource& source) { | 84 void OnSourceStopped(const blink::WebMediaStreamSource& source) { |
83 source_stopped_ = true; | 85 source_stopped_ = true; |
84 EXPECT_EQ(source.id(), webkit_source_id_); | 86 EXPECT_EQ(source.id(), webkit_source_id_); |
85 } | 87 } |
86 | 88 |
87 protected: | 89 protected: |
88 void OnConstraintsApplied(MediaStreamSource* source, | 90 void OnConstraintsApplied(MediaStreamSource* source, |
89 MediaStreamRequestResult result, | 91 MediaStreamRequestResult result, |
90 const blink::WebString& result_name) { | 92 const blink::WebString& result_name) { |
91 } | 93 } |
92 | 94 |
93 base::MessageLoopForUI message_loop_; | 95 base::MessageLoopForUI message_loop_; |
94 scoped_ptr<ChildProcess> child_process_; | 96 scoped_ptr<ChildProcess> child_process_; |
95 blink::WebMediaStreamSource webkit_source_; | 97 blink::WebMediaStreamSource webkit_source_; |
96 MediaStreamVideoCapturerSource* source_; // owned by webkit_source. | 98 MediaStreamVideoCapturerSource* source_; // owned by |webkit_source_|. |
97 scoped_refptr<VideoCapturerDelegate> delegate_; | 99 MockVideoCapturerDelegate* delegate_; // owned by |source|. |
98 blink::WebString webkit_source_id_; | 100 blink::WebString webkit_source_id_; |
99 bool source_stopped_; | 101 bool source_stopped_; |
100 }; | 102 }; |
101 | 103 |
102 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { | 104 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { |
103 StreamDeviceInfo device_info; | 105 StreamDeviceInfo device_info; |
104 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 106 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
105 InitWithDeviceInfo(device_info); | 107 InitWithDeviceInfo(device_info); |
106 | 108 |
107 EXPECT_CALL(mock_delegate(), StartCapture( | 109 EXPECT_CALL(mock_delegate(), StartCapture( |
(...skipping 18 matching lines...) Expand all Loading... |
126 testing::_, | 128 testing::_, |
127 testing::_)).Times(1); | 129 testing::_)).Times(1); |
128 blink::WebMediaStreamTrack track = StartSource(); | 130 blink::WebMediaStreamTrack track = StartSource(); |
129 // When the track goes out of scope, the source will be stopped. | 131 // When the track goes out of scope, the source will be stopped. |
130 EXPECT_CALL(mock_delegate(), StopCapture()); | 132 EXPECT_CALL(mock_delegate(), StopCapture()); |
131 } | 133 } |
132 | 134 |
133 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { | 135 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { |
134 StreamDeviceInfo device_info; | 136 StreamDeviceInfo device_info; |
135 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 137 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
136 delegate_ = new VideoCapturerDelegate(device_info); | 138 scoped_ptr<VideoCapturerDelegate> delegate( |
| 139 new VideoCapturerDelegate(device_info)); |
| 140 VideoCapturerDelegate* delegate_ptr = delegate.get(); |
137 source_ = new MediaStreamVideoCapturerSource( | 141 source_ = new MediaStreamVideoCapturerSource( |
138 device_info, | 142 device_info, |
139 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 143 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
140 base::Unretained(this)), | 144 base::Unretained(this)), |
141 delegate_); | 145 delegate.Pass()); |
142 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 146 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
143 blink::WebMediaStreamSource::TypeVideo, | 147 blink::WebMediaStreamSource::TypeVideo, |
144 base::UTF8ToUTF16("dummy_source_name"), | 148 base::UTF8ToUTF16("dummy_source_name"), |
145 false /* remote */ , true /* readonly */); | 149 false /* remote */ , true /* readonly */); |
146 webkit_source_.setExtraData(source_); | 150 webkit_source_.setExtraData(source_); |
147 webkit_source_id_ = webkit_source_.id(); | 151 webkit_source_id_ = webkit_source_.id(); |
148 blink::WebMediaStreamTrack track = StartSource(); | 152 blink::WebMediaStreamTrack track = StartSource(); |
149 message_loop_.RunUntilIdle(); | 153 message_loop_.RunUntilIdle(); |
150 | 154 |
151 delegate_->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); | 155 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); |
152 message_loop_.RunUntilIdle(); | 156 message_loop_.RunUntilIdle(); |
153 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive, | 157 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive, |
154 webkit_source_.readyState()); | 158 webkit_source_.readyState()); |
155 | 159 |
156 EXPECT_FALSE(source_stopped_); | 160 EXPECT_FALSE(source_stopped_); |
157 delegate_->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_ERROR); | 161 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_ERROR); |
158 message_loop_.RunUntilIdle(); | 162 message_loop_.RunUntilIdle(); |
159 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded, | 163 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded, |
160 webkit_source_.readyState()); | 164 webkit_source_.readyState()); |
161 // Verify that MediaStreamSource::SourceStoppedCallback has been triggered. | 165 // Verify that MediaStreamSource::SourceStoppedCallback has been triggered. |
162 EXPECT_TRUE(source_stopped_); | 166 EXPECT_TRUE(source_stopped_); |
163 } | 167 } |
164 | 168 |
165 class FakeMediaStreamVideoSink : public MediaStreamVideoSink { | 169 class FakeMediaStreamVideoSink : public MediaStreamVideoSink { |
166 public: | 170 public: |
167 FakeMediaStreamVideoSink(base::TimeTicks* capture_time, | 171 FakeMediaStreamVideoSink(base::TimeTicks* capture_time, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 base::Bind(deliver_frame_cb, | 222 base::Bind(deliver_frame_cb, |
219 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), | 223 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), |
220 media::VideoCaptureFormat(), | 224 media::VideoCaptureFormat(), |
221 reference_capture_time)); | 225 reference_capture_time)); |
222 run_loop.Run(); | 226 run_loop.Run(); |
223 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); | 227 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); |
224 EXPECT_EQ(reference_capture_time, capture_time); | 228 EXPECT_EQ(reference_capture_time, capture_time); |
225 } | 229 } |
226 | 230 |
227 } // namespace content | 231 } // namespace content |
OLD | NEW |