Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / media / webrtc / media_stream_remote_video_source_unittest.cc
blob00fd9980bc1d29d2023ad9e741ff30312341a0f9
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/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/child/child_process.h"
9 #include "content/renderer/media/media_stream_video_track.h"
10 #include "content/renderer/media/mock_media_stream_video_sink.h"
11 #include "content/renderer/media/webrtc/media_stream_remote_video_source.h"
12 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
13 #include "media/base/video_frame.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/libjingle/source/talk/media/webrtc/webrtcvideoframe.h"
17 namespace content {
19 ACTION_P(RunClosure, closure) {
20 closure.Run();
23 class MediaStreamRemoteVideoSourceUnderTest
24 : public MediaStreamRemoteVideoSource {
25 public:
26 MediaStreamRemoteVideoSourceUnderTest(webrtc::VideoTrackInterface* track)
27 : MediaStreamRemoteVideoSource(track) {
29 using MediaStreamRemoteVideoSource::RenderInterfaceForTest;
32 class MediaStreamRemoteVideoSourceTest
33 : public ::testing::Test {
34 public:
35 MediaStreamRemoteVideoSourceTest()
36 : child_process_(new ChildProcess()),
37 mock_factory_(new MockPeerConnectionDependencyFactory()),
38 webrtc_video_track_(mock_factory_->CreateLocalVideoTrack(
39 "test",
40 static_cast<cricket::VideoCapturer*>(NULL))),
41 remote_source_(new MediaStreamRemoteVideoSourceUnderTest(
42 webrtc_video_track_.get())),
43 number_of_successful_constraints_applied_(0),
44 number_of_failed_constraints_applied_(0) {
45 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"),
46 blink::WebMediaStreamSource::TypeVideo,
47 base::UTF8ToUTF16("dummy_source_name"));
48 webkit_source_.setExtraData(remote_source_);
51 MediaStreamRemoteVideoSourceUnderTest* source() {
52 return remote_source_;
55 MediaStreamVideoTrack* CreateTrack() {
56 bool enabled = true;
57 blink::WebMediaConstraints constraints;
58 constraints.initialize();
59 return new MediaStreamVideoTrack(
60 source(),
61 constraints,
62 base::Bind(
63 &MediaStreamRemoteVideoSourceTest::OnConstraintsApplied,
64 base::Unretained(this)),
65 enabled);
68 int NumberOfSuccessConstraintsCallbacks() const {
69 return number_of_successful_constraints_applied_;
72 int NumberOfFailedConstraintsCallbacks() const {
73 return number_of_failed_constraints_applied_;
76 void StopWebRtcTrack() {
77 static_cast<MockWebRtcVideoTrack*>(webrtc_video_track_.get())->set_state(
78 webrtc::MediaStreamTrackInterface::kEnded);
81 const blink::WebMediaStreamSource& webkit_source() const {
82 return webkit_source_;
85 private:
86 void OnConstraintsApplied(MediaStreamSource* source,
87 MediaStreamRequestResult result,
88 const blink::WebString& result_name) {
89 ASSERT_EQ(source, remote_source_);
90 if (result == MEDIA_DEVICE_OK)
91 ++number_of_successful_constraints_applied_;
92 else
93 ++number_of_failed_constraints_applied_;
96 base::MessageLoopForUI message_loop_;
97 scoped_ptr<ChildProcess> child_process_;
98 scoped_ptr<MockPeerConnectionDependencyFactory> mock_factory_;
99 scoped_refptr<webrtc::VideoTrackInterface> webrtc_video_track_;
100 // |remote_source_| is owned by |webkit_source_|.
101 MediaStreamRemoteVideoSourceUnderTest* remote_source_;
102 blink::WebMediaStreamSource webkit_source_;
103 int number_of_successful_constraints_applied_;
104 int number_of_failed_constraints_applied_;
107 TEST_F(MediaStreamRemoteVideoSourceTest, StartTrack) {
108 scoped_ptr<MediaStreamVideoTrack> track(CreateTrack());
109 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks());
111 MockMediaStreamVideoSink sink;
112 track->AddSink(&sink, sink.GetDeliverFrameCB());
113 base::RunLoop run_loop;
114 base::Closure quit_closure = run_loop.QuitClosure();
115 EXPECT_CALL(sink, OnVideoFrame()).WillOnce(
116 RunClosure(quit_closure));
117 cricket::WebRtcVideoFrame webrtc_frame;
118 webrtc_frame.InitToBlack(320, 240, 1, 1, 0, 1);
119 source()->RenderInterfaceForTest()->RenderFrame(&webrtc_frame);
120 run_loop.Run();
122 EXPECT_EQ(1, sink.number_of_frames());
123 track->RemoveSink(&sink);
126 TEST_F(MediaStreamRemoteVideoSourceTest, RemoteTrackStop) {
127 scoped_ptr<MediaStreamVideoTrack> track(CreateTrack());
129 MockMediaStreamVideoSink sink;
130 track->AddSink(&sink, sink.GetDeliverFrameCB());
131 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive, sink.state());
132 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive,
133 webkit_source().readyState());
134 StopWebRtcTrack();
135 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded,
136 webkit_source().readyState());
137 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded, sink.state());
139 track->RemoveSink(&sink);
142 } // namespace content