Enable right clicking on the applist doodle web contents and log the data.
[chromium-blink-merge.git] / content / renderer / media / media_stream_video_capture_source_unittest.cc
blobb6f4833c6b360b33789a30baa5e608db49faf963
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/callback_helpers.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/child/child_process.h"
11 #include "content/public/renderer/media_stream_video_sink.h"
12 #include "content/renderer/media/media_stream_video_capturer_source.h"
13 #include "content/renderer/media/media_stream_video_track.h"
14 #include "content/renderer/media/mock_media_constraint_factory.h"
15 #include "media/base/bind_to_current_loop.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/web/WebHeap.h"
20 namespace content {
22 class MockVideoCapturerDelegate : public VideoCapturerDelegate {
23 public:
24 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info)
25 : VideoCapturerDelegate(device_info) {}
27 MOCK_METHOD4(
28 StartCapture,
29 void(const media::VideoCaptureParams& params,
30 const VideoCaptureDeliverFrameCB& new_frame_callback,
31 scoped_refptr<base::SingleThreadTaskRunner>
32 frame_callback_task_runner,
33 const RunningCallback& running_callback));
34 MOCK_METHOD0(StopCapture, void());
37 class MediaStreamVideoCapturerSourceTest : public testing::Test {
38 public:
39 MediaStreamVideoCapturerSourceTest()
40 : child_process_(new ChildProcess()),
41 source_(NULL),
42 delegate_(NULL),
43 source_stopped_(false) {
46 void TearDown() override {
47 webkit_source_.reset();
48 blink::WebHeap::collectAllGarbageForTesting();
51 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) {
52 scoped_ptr<MockVideoCapturerDelegate> delegate(
53 new MockVideoCapturerDelegate(device_info));
54 delegate_ = delegate.get();
55 source_ = new MediaStreamVideoCapturerSource(
56 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped,
57 base::Unretained(this)),
58 delegate.Pass());
59 source_->SetDeviceInfo(device_info);
61 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"),
62 blink::WebMediaStreamSource::TypeVideo,
63 base::UTF8ToUTF16("dummy_source_name"),
64 false /* remote */ , true /* readonly */);
65 webkit_source_.setExtraData(source_);
66 webkit_source_id_ = webkit_source_.id();
69 blink::WebMediaStreamTrack StartSource() {
70 MockMediaConstraintFactory factory;
71 bool enabled = true;
72 // CreateVideoTrack will trigger OnConstraintsApplied.
73 return MediaStreamVideoTrack::CreateVideoTrack(
74 source_, factory.CreateWebMediaConstraints(),
75 base::Bind(
76 &MediaStreamVideoCapturerSourceTest::OnConstraintsApplied,
77 base::Unretained(this)),
78 enabled);
81 MockVideoCapturerDelegate& mock_delegate() {
82 return *delegate_;
85 void OnSourceStopped(const blink::WebMediaStreamSource& source) {
86 source_stopped_ = true;
87 EXPECT_EQ(source.id(), webkit_source_id_);
90 protected:
91 void OnConstraintsApplied(MediaStreamSource* source,
92 MediaStreamRequestResult result,
93 const blink::WebString& result_name) {
96 base::MessageLoopForUI message_loop_;
97 scoped_ptr<ChildProcess> child_process_;
98 blink::WebMediaStreamSource webkit_source_;
99 MediaStreamVideoCapturerSource* source_; // owned by |webkit_source_|.
100 MockVideoCapturerDelegate* delegate_; // owned by |source|.
101 blink::WebString webkit_source_id_;
102 bool source_stopped_;
105 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) {
106 StreamDeviceInfo device_info;
107 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE;
108 InitWithDeviceInfo(device_info);
110 EXPECT_CALL(mock_delegate(), StartCapture(
111 testing::Field(&media::VideoCaptureParams::resolution_change_policy,
112 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT),
113 testing::_,
114 testing::_,
115 testing::_)).Times(1);
116 blink::WebMediaStreamTrack track = StartSource();
117 // When the track goes out of scope, the source will be stopped.
118 EXPECT_CALL(mock_delegate(), StopCapture());
121 TEST_F(MediaStreamVideoCapturerSourceTest,
122 DesktopCaptureAllowResolutionChange) {
123 StreamDeviceInfo device_info;
124 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE;
125 InitWithDeviceInfo(device_info);
127 EXPECT_CALL(mock_delegate(), StartCapture(
128 testing::Field(&media::VideoCaptureParams::resolution_change_policy,
129 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT),
130 testing::_,
131 testing::_,
132 testing::_)).Times(1);
133 blink::WebMediaStreamTrack track = StartSource();
134 // When the track goes out of scope, the source will be stopped.
135 EXPECT_CALL(mock_delegate(), StopCapture());
138 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) {
139 StreamDeviceInfo device_info;
140 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE;
141 scoped_ptr<VideoCapturerDelegate> delegate(
142 new VideoCapturerDelegate(device_info));
143 VideoCapturerDelegate* delegate_ptr = delegate.get();
144 source_ = new MediaStreamVideoCapturerSource(
145 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped,
146 base::Unretained(this)),
147 delegate.Pass());
148 source_->SetDeviceInfo(device_info);
149 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"),
150 blink::WebMediaStreamSource::TypeVideo,
151 base::UTF8ToUTF16("dummy_source_name"),
152 false /* remote */ , true /* readonly */);
153 webkit_source_.setExtraData(source_);
154 webkit_source_id_ = webkit_source_.id();
155 blink::WebMediaStreamTrack track = StartSource();
156 message_loop_.RunUntilIdle();
158 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED);
159 message_loop_.RunUntilIdle();
160 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive,
161 webkit_source_.readyState());
163 EXPECT_FALSE(source_stopped_);
164 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_ERROR);
165 message_loop_.RunUntilIdle();
166 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded,
167 webkit_source_.readyState());
168 // Verify that MediaStreamSource::SourceStoppedCallback has been triggered.
169 EXPECT_TRUE(source_stopped_);
172 class FakeMediaStreamVideoSink : public MediaStreamVideoSink {
173 public:
174 FakeMediaStreamVideoSink(base::TimeTicks* capture_time,
175 base::Closure got_frame_cb)
176 : capture_time_(capture_time),
177 got_frame_cb_(got_frame_cb) {
180 void OnVideoFrame(const scoped_refptr<media::VideoFrame>& frame,
181 const media::VideoCaptureFormat& format,
182 const base::TimeTicks& capture_time) {
183 *capture_time_ = capture_time;
184 base::ResetAndReturn(&got_frame_cb_).Run();
187 private:
188 base::TimeTicks* capture_time_;
189 base::Closure got_frame_cb_;
192 TEST_F(MediaStreamVideoCapturerSourceTest, CaptureTime) {
193 StreamDeviceInfo device_info;
194 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE;
195 InitWithDeviceInfo(device_info);
197 VideoCaptureDeliverFrameCB deliver_frame_cb;
198 VideoCapturerDelegate::RunningCallback running_cb;
200 EXPECT_CALL(mock_delegate(), StartCapture(
201 testing::_,
202 testing::_,
203 testing::_,
204 testing::_))
205 .Times(1)
206 .WillOnce(testing::DoAll(testing::SaveArg<1>(&deliver_frame_cb),
207 testing::SaveArg<3>(&running_cb)));
208 EXPECT_CALL(mock_delegate(), StopCapture());
209 blink::WebMediaStreamTrack track = StartSource();
210 running_cb.Run(true);
212 base::RunLoop run_loop;
213 base::TimeTicks reference_capture_time =
214 base::TimeTicks::FromInternalValue(60013);
215 base::TimeTicks capture_time;
216 FakeMediaStreamVideoSink fake_sink(
217 &capture_time,
218 media::BindToCurrentLoop(run_loop.QuitClosure()));
219 FakeMediaStreamVideoSink::AddToVideoTrack(
220 &fake_sink,
221 base::Bind(&FakeMediaStreamVideoSink::OnVideoFrame,
222 base::Unretained(&fake_sink)),
223 track);
224 child_process_->io_message_loop()->PostTask(
225 FROM_HERE,
226 base::Bind(deliver_frame_cb,
227 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)),
228 media::VideoCaptureFormat(),
229 reference_capture_time));
230 run_loop.Run();
231 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track);
232 EXPECT_EQ(reference_capture_time, capture_time);
235 } // namespace content