summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/media/pepper/pepper_to_video_track_adapter_unittest.cc
blob: 9f8eb121f4cf533bb9a1e3b2a1ad930b44debee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/run_loop.h"
#include "content/child/child_process.h"
#include "content/public/test/mock_render_thread.h"
#include "content/renderer/media/pepper/pepper_to_video_track_adapter.h"
#include "content/renderer/media/stream/media_stream_video_track.h"
#include "content/renderer/media/stream/mock_media_stream_registry.h"
#include "content/renderer/media/stream/mock_media_stream_video_sink.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "content/renderer/pepper/ppb_image_data_impl.h"
#include "content/test/ppapi_unittest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_media_stream_track.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/web_heap.h"

using ::testing::_;

namespace content {

ACTION_P(RunClosure, closure) {
  closure.Run();
}

static const std::string kTestStreamUrl = "stream_url";
static const std::string kUnknownStreamUrl = "unknown_stream_url";

class PepperToVideoTrackAdapterTest : public PpapiUnittest {
 public:
  PepperToVideoTrackAdapterTest() : registry_(new MockMediaStreamRegistry()) {
    registry_->Init(kTestStreamUrl);
  }

  void TearDown() override {
    registry_.reset();
    blink::WebHeap::CollectAllGarbageForTesting();
    PpapiUnittest::TearDown();
  }

 protected:
  // A ChildProcess is needed to fool the Tracks and Sources into believing they
  // are on the right threads. The ScopedTaskEnvironment provided by
  // PpapiUnittest prevents the ChildProcess from leaking a TaskScheduler.
  const ChildProcess child_process_;
  const MockRenderThread render_thread_;
  std::unique_ptr<MockMediaStreamRegistry> registry_;
};

TEST_F(PepperToVideoTrackAdapterTest, Open) {
  // |frame_writer| is a proxy and is owned by whoever call Open.
  FrameWriterInterface* frame_writer = nullptr;
  // Unknow url will return false.
  EXPECT_FALSE(PepperToVideoTrackAdapter::Open(registry_.get(),
                                             kUnknownStreamUrl, &frame_writer));
  EXPECT_TRUE(PepperToVideoTrackAdapter::Open(registry_.get(),
                                            kTestStreamUrl, &frame_writer));
  delete frame_writer;
}

TEST_F(PepperToVideoTrackAdapterTest, PutFrame) {
  FrameWriterInterface* frame_writer = nullptr;
  EXPECT_TRUE(PepperToVideoTrackAdapter::Open(registry_.get(),
                                            kTestStreamUrl, &frame_writer));
  ASSERT_TRUE(frame_writer);

  // Verify the video track has been added.
  const blink::WebMediaStream test_stream = registry_->test_stream();
  blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
  test_stream.VideoTracks(video_tracks);
  ASSERT_EQ(1u, video_tracks.size());

  // Verify the native video track has been added.
  MediaStreamVideoTrack* native_track =
      MediaStreamVideoTrack::GetVideoTrack(video_tracks[0]);
  ASSERT_TRUE(native_track != nullptr);

  MockMediaStreamVideoSink sink;
  native_track->AddSink(&sink, sink.GetDeliverFrameCB(), false);
  scoped_refptr<PPB_ImageData_Impl> image(new PPB_ImageData_Impl(
      instance()->pp_instance(), PPB_ImageData_Impl::ForTest()));
  image->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 640, 360, true);
  {
    base::RunLoop run_loop;
    base::Closure quit_closure = run_loop.QuitClosure();

    EXPECT_CALL(sink, OnVideoFrame())
        .WillOnce(RunClosure(std::move(quit_closure)));
    frame_writer->PutFrame(image.get(), 10);
    run_loop.Run();
    // Run all pending tasks to let the the test clean up before the test ends.
    // This is due to that
    // FrameWriterDelegate::FrameWriterDelegate::DeliverFrame use
    // PostTaskAndReply to the IO thread and expects the reply to process
    // on the main render thread to clean up its resources. However, the
    // QuitClosure above ends before that.
    base::RunLoop().RunUntilIdle();
  }
  EXPECT_EQ(1, sink.number_of_frames());
  native_track->RemoveSink(&sink);

  // The |frame_writer| is a proxy and is owned by whoever call Open.
  delete frame_writer;
}

}  // namespace content