Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / media_stream_video_renderer_sink_unittest.cc
blob195a1c7a08abc3186366c87421b47f172cdae05e
1 // Copyright 2015 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/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_renderer_sink.h"
10 #include "content/renderer/media/mock_media_stream_registry.h"
11 #include "media/base/video_frame.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
16 using ::testing::_;
17 using ::testing::AtLeast;
18 using ::testing::InSequence;
19 using ::testing::Lt;
20 using ::testing::Mock;
22 namespace content {
24 ACTION_P(RunClosure, closure) {
25 closure.Run();
28 static const std::string kTestStreamUrl = "stream_url";
29 static const std::string kTestVideoTrackId = "video_track_id";
31 class MediaStreamVideoRendererSinkTest : public testing::Test {
32 public:
33 MediaStreamVideoRendererSinkTest() {
34 registry_.Init(kTestStreamUrl);
35 registry_.AddVideoTrack(kTestVideoTrackId);
37 // Extract the Blink Video Track for the MSVRSink.
38 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
39 registry_.test_stream().videoTracks(video_tracks);
40 EXPECT_EQ(1u, video_tracks.size());
42 media_stream_video_renderer_sink_ = new MediaStreamVideoRendererSink(
43 video_tracks[0],
44 base::Bind(&MediaStreamVideoRendererSinkTest::ErrorCallback,
45 base::Unretained(this)),
46 base::Bind(&MediaStreamVideoRendererSinkTest::RepaintCallback,
47 base::Unretained(this)));
49 EXPECT_TRUE(IsInStoppedState());
52 MOCK_METHOD1(RepaintCallback, void(const scoped_refptr<media::VideoFrame>&));
53 MOCK_METHOD0(ErrorCallback, void(void));
55 bool IsInStartedState() const {
56 return media_stream_video_renderer_sink_->state_ ==
57 MediaStreamVideoRendererSink::STARTED;
59 bool IsInStoppedState() const {
60 return media_stream_video_renderer_sink_->state_ ==
61 MediaStreamVideoRendererSink::STOPPED;
63 bool IsInPausedState() const {
64 return media_stream_video_renderer_sink_->state_ ==
65 MediaStreamVideoRendererSink::PAUSED;
68 void OnVideoFrame(const scoped_refptr<media::VideoFrame>& frame) {
69 media_stream_video_renderer_sink_->OnVideoFrame(frame,
70 base::TimeTicks::Now());
73 scoped_refptr<MediaStreamVideoRendererSink> media_stream_video_renderer_sink_;
75 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks
76 // and Sources in |registry_| into believing they are on the right threads.
77 const base::MessageLoopForUI message_loop_;
78 const ChildProcess child_process_;
79 MockMediaStreamRegistry registry_;
81 private:
82 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoRendererSinkTest);
85 // Checks that the initialization-destruction sequence works fine.
86 TEST_F(MediaStreamVideoRendererSinkTest, StartStop) {
87 EXPECT_TRUE(IsInStoppedState());
89 media_stream_video_renderer_sink_->Start();
90 EXPECT_TRUE(IsInStartedState());
92 media_stream_video_renderer_sink_->Pause();
93 EXPECT_TRUE(IsInPausedState());
95 media_stream_video_renderer_sink_->Play(); // Should be called Resume().
96 EXPECT_TRUE(IsInStartedState());
98 media_stream_video_renderer_sink_->Stop();
99 EXPECT_TRUE(IsInStoppedState());
102 // Sends 2 frames and expect them as WebM contained encoded data in writeData().
103 TEST_F(MediaStreamVideoRendererSinkTest, EncodeVideoFrames) {
104 media_stream_video_renderer_sink_->Start();
106 InSequence s;
107 const scoped_refptr<media::VideoFrame> video_frame =
108 media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80));
110 EXPECT_CALL(*this, RepaintCallback(video_frame)).Times(1);
111 OnVideoFrame(video_frame);
113 media_stream_video_renderer_sink_->Stop();
116 } // namespace content