Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / media / video_source_handler_unittest.cc
blob94dce46a19511a5504654eca553ce5fe10124ef8
1 // Copyright (c) 2013 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 <string>
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/child/child_process.h"
10 #include "content/common/media/video_capture.h"
11 #include "content/public/renderer/media_stream_video_sink.h"
12 #include "content/renderer/media/media_stream.h"
13 #include "content/renderer/media/media_stream_registry_interface.h"
14 #include "content/renderer/media/mock_media_stream_registry.h"
15 #include "content/renderer/media/video_source_handler.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
21 namespace content {
23 static const std::string kTestStreamUrl = "stream_url";
24 static const std::string kTestVideoTrackId = "video_track_id";
25 static const std::string kUnknownStreamUrl = "unknown_stream_url";
27 class FakeFrameReader : public FrameReaderInterface {
28 public:
29 virtual bool GotFrame(
30 const scoped_refptr<media::VideoFrame>& frame) OVERRIDE {
31 last_frame_ = frame;
32 return true;
35 const media::VideoFrame* last_frame() {
36 return last_frame_.get();
39 private:
40 scoped_refptr<media::VideoFrame> last_frame_;
43 class VideoSourceHandlerTest : public ::testing::Test {
44 public:
45 VideoSourceHandlerTest()
46 : child_process_(new ChildProcess()),
47 registry_() {
48 handler_.reset(new VideoSourceHandler(&registry_));
49 registry_.Init(kTestStreamUrl);
50 registry_.AddVideoTrack(kTestVideoTrackId);
53 protected:
54 base::MessageLoop message_loop_;
55 scoped_ptr<ChildProcess> child_process_;
56 scoped_ptr<VideoSourceHandler> handler_;
57 MockMediaStreamRegistry registry_;
60 TEST_F(VideoSourceHandlerTest, OpenClose) {
61 FakeFrameReader reader;
62 // Unknow url will return false.
63 EXPECT_FALSE(handler_->Open(kUnknownStreamUrl, &reader));
64 EXPECT_TRUE(handler_->Open(kTestStreamUrl, &reader));
66 int width = 640;
67 int height = 360;
68 base::TimeDelta ts = base::TimeDelta::FromInternalValue(789012);
70 // A new frame is captured.
71 scoped_refptr<media::VideoFrame> captured_frame =
72 media::VideoFrame::CreateBlackFrame(gfx::Size(width, height));
73 captured_frame->set_timestamp(ts);
75 // The frame is delivered to VideoSourceHandler.
76 handler_->DeliverFrameForTesting(&reader, captured_frame);
78 // Compare |frame| to |captured_frame|.
79 const media::VideoFrame* frame = reader.last_frame();
80 ASSERT_TRUE(frame != NULL);
81 EXPECT_EQ(width, frame->coded_size().width());
82 EXPECT_EQ(height, frame->coded_size().height());
83 EXPECT_EQ(ts, frame->timestamp());
84 EXPECT_EQ(captured_frame->data(media::VideoFrame::kYPlane),
85 frame->data(media::VideoFrame::kYPlane));
87 EXPECT_FALSE(handler_->Close(NULL));
88 EXPECT_TRUE(handler_->Close(&reader));
91 TEST_F(VideoSourceHandlerTest, OpenWithoutClose) {
92 FakeFrameReader reader;
93 EXPECT_TRUE(handler_->Open(kTestStreamUrl, &reader));
96 } // namespace content