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.
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/test/mock_render_thread.h"
12 #include "content/renderer/media/media_stream.h"
13 #include "content/renderer/media/media_stream_video_track.h"
14 #include "content/renderer/media/mock_media_stream_registry.h"
15 #include "content/renderer/media/mock_media_stream_video_sink.h"
16 #include "content/renderer/media/webrtc/video_destination_handler.h"
17 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
18 #include "content/renderer/pepper/ppb_image_data_impl.h"
19 #include "content/test/ppapi_unittest.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
23 #include "third_party/WebKit/public/platform/WebString.h"
24 #include "third_party/WebKit/public/web/WebHeap.h"
30 ACTION_P(RunClosure
, closure
) {
34 static const std::string kTestStreamUrl
= "stream_url";
35 static const std::string kUnknownStreamUrl
= "unknown_stream_url";
37 class VideoDestinationHandlerTest
: public PpapiUnittest
{
39 VideoDestinationHandlerTest()
40 : child_process_(new ChildProcess()),
41 render_thread_(new MockRenderThread
),
42 registry_(new MockMediaStreamRegistry()) {
43 registry_
->Init(kTestStreamUrl
);
46 void TearDown() override
{
48 blink::WebHeap::collectAllGarbageForTesting();
49 PpapiUnittest::TearDown();
52 base::MessageLoop
* io_message_loop() const {
53 return child_process_
->io_message_loop();
57 scoped_ptr
<ChildProcess
> child_process_
;
58 scoped_ptr
<MockRenderThread
> render_thread_
;
59 scoped_ptr
<MockMediaStreamRegistry
> registry_
;
62 TEST_F(VideoDestinationHandlerTest
, Open
) {
63 FrameWriterInterface
* frame_writer
= NULL
;
64 // Unknow url will return false.
65 EXPECT_FALSE(VideoDestinationHandler::Open(registry_
.get(),
66 kUnknownStreamUrl
, &frame_writer
));
67 EXPECT_TRUE(VideoDestinationHandler::Open(registry_
.get(),
68 kTestStreamUrl
, &frame_writer
));
69 // The |frame_writer| is a proxy and is owned by whoever call Open.
73 TEST_F(VideoDestinationHandlerTest
, PutFrame
) {
74 FrameWriterInterface
* frame_writer
= NULL
;
75 EXPECT_TRUE(VideoDestinationHandler::Open(registry_
.get(),
76 kTestStreamUrl
, &frame_writer
));
77 ASSERT_TRUE(frame_writer
);
79 // Verify the video track has been added.
80 const blink::WebMediaStream test_stream
= registry_
->test_stream();
81 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
82 test_stream
.videoTracks(video_tracks
);
83 ASSERT_EQ(1u, video_tracks
.size());
85 // Verify the native video track has been added.
86 MediaStreamVideoTrack
* native_track
=
87 MediaStreamVideoTrack::GetVideoTrack(video_tracks
[0]);
88 ASSERT_TRUE(native_track
!= NULL
);
90 MockMediaStreamVideoSink sink
;
91 native_track
->AddSink(&sink
, sink
.GetDeliverFrameCB());
92 scoped_refptr
<PPB_ImageData_Impl
> image(
93 new PPB_ImageData_Impl(instance()->pp_instance(),
94 PPB_ImageData_Impl::ForTest()));
95 image
->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL
, 640, 360, true);
97 base::RunLoop run_loop
;
98 base::Closure quit_closure
= run_loop
.QuitClosure();
100 EXPECT_CALL(sink
, OnVideoFrame()).WillOnce(
101 RunClosure(quit_closure
));
102 frame_writer
->PutFrame(image
.get(), 10);
104 // Run all pending tasks to let the the test clean up before the test ends.
105 // This is due to that
106 // FrameWriterDelegate::FrameWriterDelegate::DeliverFrame use
107 // PostTaskAndReply to the IO thread and expects the reply to process
108 // on the main render thread to clean up its resources. However, the
109 // QuitClosure above ends before that.
110 base::MessageLoop::current()->RunUntilIdle();
112 EXPECT_EQ(1, sink
.number_of_frames());
113 native_track
->RemoveSink(&sink
);
115 // The |frame_writer| is a proxy and is owned by whoever call Open.
119 } // namespace content