Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / pepper_to_video_track_adapter_unittest.cc
blob219667eed665d67b5bc895c814b771855e3652eb
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/run_loop.h"
8 #include "content/child/child_process.h"
9 #include "content/public/test/mock_render_thread.h"
10 #include "content/renderer/media/media_stream.h"
11 #include "content/renderer/media/media_stream_video_track.h"
12 #include "content/renderer/media/mock_media_stream_registry.h"
13 #include "content/renderer/media/mock_media_stream_video_sink.h"
14 #include "content/renderer/media/pepper_to_video_track_adapter.h"
15 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
16 #include "content/renderer/pepper/ppb_image_data_impl.h"
17 #include "content/test/ppapi_unittest.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
21 #include "third_party/WebKit/public/platform/WebString.h"
22 #include "third_party/WebKit/public/web/WebHeap.h"
24 using ::testing::_;
26 namespace content {
28 ACTION_P(RunClosure, closure) {
29 closure.Run();
32 static const std::string kTestStreamUrl = "stream_url";
33 static const std::string kUnknownStreamUrl = "unknown_stream_url";
35 class PepperToVideoTrackAdapterTest : public PpapiUnittest {
36 public:
37 PepperToVideoTrackAdapterTest() : registry_(new MockMediaStreamRegistry()) {
38 registry_->Init(kTestStreamUrl);
41 void TearDown() override {
42 registry_.reset();
43 blink::WebHeap::collectAllGarbageForTesting();
44 PpapiUnittest::TearDown();
47 protected:
48 // A ChildProcess and a MessageLoop are both needed to fool the Tracks and
49 // Sources inside |registry_| into believing they are on the right threads.
50 const ChildProcess child_process_;
51 const MockRenderThread render_thread_;
52 scoped_ptr<MockMediaStreamRegistry> registry_;
55 TEST_F(PepperToVideoTrackAdapterTest, Open) {
56 // |frame_writer| is a proxy and is owned by whoever call Open.
57 FrameWriterInterface* frame_writer = nullptr;
58 // Unknow url will return false.
59 EXPECT_FALSE(PepperToVideoTrackAdapter::Open(registry_.get(),
60 kUnknownStreamUrl, &frame_writer));
61 EXPECT_TRUE(PepperToVideoTrackAdapter::Open(registry_.get(),
62 kTestStreamUrl, &frame_writer));
63 delete frame_writer;
66 TEST_F(PepperToVideoTrackAdapterTest, PutFrame) {
67 FrameWriterInterface* frame_writer = NULL;
68 EXPECT_TRUE(PepperToVideoTrackAdapter::Open(registry_.get(),
69 kTestStreamUrl, &frame_writer));
70 ASSERT_TRUE(frame_writer);
72 // Verify the video track has been added.
73 const blink::WebMediaStream test_stream = registry_->test_stream();
74 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
75 test_stream.videoTracks(video_tracks);
76 ASSERT_EQ(1u, video_tracks.size());
78 // Verify the native video track has been added.
79 MediaStreamVideoTrack* native_track =
80 MediaStreamVideoTrack::GetVideoTrack(video_tracks[0]);
81 ASSERT_TRUE(native_track != NULL);
83 MockMediaStreamVideoSink sink;
84 native_track->AddSink(&sink, sink.GetDeliverFrameCB());
85 scoped_refptr<PPB_ImageData_Impl> image(
86 new PPB_ImageData_Impl(instance()->pp_instance(),
87 PPB_ImageData_Impl::ForTest()));
88 image->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 640, 360, true);
90 base::RunLoop run_loop;
91 base::Closure quit_closure = run_loop.QuitClosure();
93 EXPECT_CALL(sink, OnVideoFrame()).WillOnce(RunClosure(quit_closure));
94 frame_writer->PutFrame(image.get(), 10);
95 run_loop.Run();
96 // Run all pending tasks to let the the test clean up before the test ends.
97 // This is due to that
98 // FrameWriterDelegate::FrameWriterDelegate::DeliverFrame use
99 // PostTaskAndReply to the IO thread and expects the reply to process
100 // on the main render thread to clean up its resources. However, the
101 // QuitClosure above ends before that.
102 base::MessageLoop::current()->RunUntilIdle();
104 EXPECT_EQ(1, sink.number_of_frames());
105 native_track->RemoveSink(&sink);
107 // The |frame_writer| is a proxy and is owned by whoever call Open.
108 delete frame_writer;
111 } // namespace content