1 // Copyright 2014 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.
6 #include "base/message_loop/message_loop.h"
7 #include "cc/layers/video_frame_provider.h"
8 #include "media/base/video_frame.h"
9 #include "media/blink/video_frame_compositor.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class VideoFrameCompositorTest
: public testing::Test
,
15 public cc::VideoFrameProvider::Client
{
17 VideoFrameCompositorTest()
18 : compositor_(new VideoFrameCompositor(
19 message_loop
.task_runner(),
20 base::Bind(&VideoFrameCompositorTest::NaturalSizeChanged
,
21 base::Unretained(this)),
22 base::Bind(&VideoFrameCompositorTest::OpacityChanged
,
23 base::Unretained(this)))),
24 did_receive_frame_count_(0),
25 natural_size_changed_count_(0),
26 opacity_changed_count_(0),
28 compositor_
->SetVideoFrameProviderClient(this);
31 ~VideoFrameCompositorTest() override
{
32 compositor_
->SetVideoFrameProviderClient(NULL
);
35 VideoFrameCompositor
* compositor() { return compositor_
.get(); }
36 int did_receive_frame_count() { return did_receive_frame_count_
; }
37 int natural_size_changed_count() { return natural_size_changed_count_
; }
38 gfx::Size
natural_size() { return natural_size_
; }
40 int opacity_changed_count() { return opacity_changed_count_
; }
41 bool opaque() { return opaque_
; }
44 // cc::VideoFrameProvider::Client implementation.
45 void StopUsingProvider() override
{}
46 void StartRendering() override
{};
47 void StopRendering() override
{};
48 void DidReceiveFrame() override
{
49 ++did_receive_frame_count_
;
51 void DidUpdateMatrix(const float* matrix
) override
{}
53 void NaturalSizeChanged(gfx::Size natural_size
) {
54 ++natural_size_changed_count_
;
55 natural_size_
= natural_size
;
58 void OpacityChanged(bool opaque
) {
59 ++opacity_changed_count_
;
63 base::MessageLoop message_loop
;
64 scoped_ptr
<VideoFrameCompositor
> compositor_
;
65 int did_receive_frame_count_
;
66 int natural_size_changed_count_
;
67 gfx::Size natural_size_
;
68 int opacity_changed_count_
;
71 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest
);
74 TEST_F(VideoFrameCompositorTest
, InitialValues
) {
75 EXPECT_FALSE(compositor()->GetCurrentFrame().get());
78 TEST_F(VideoFrameCompositorTest
, PaintFrameUsingOldRenderingPath
) {
79 scoped_refptr
<VideoFrame
> expected
= VideoFrame::CreateEOSFrame();
81 // Should notify compositor synchronously.
82 EXPECT_EQ(0, did_receive_frame_count());
83 compositor()->PaintFrameUsingOldRenderingPath(expected
);
84 scoped_refptr
<VideoFrame
> actual
= compositor()->GetCurrentFrame();
85 EXPECT_EQ(expected
, actual
);
86 EXPECT_EQ(1, did_receive_frame_count());
89 TEST_F(VideoFrameCompositorTest
, NaturalSizeChanged
) {
90 gfx::Size
initial_size(8, 8);
91 scoped_refptr
<VideoFrame
> initial_frame
=
92 VideoFrame::CreateBlackFrame(initial_size
);
94 gfx::Size
larger_size(16, 16);
95 scoped_refptr
<VideoFrame
> larger_frame
=
96 VideoFrame::CreateBlackFrame(larger_size
);
98 // Initial expectations.
99 EXPECT_EQ(0, natural_size().width());
100 EXPECT_EQ(0, natural_size().height());
101 EXPECT_EQ(0, natural_size_changed_count());
103 // Callback isn't fired for the first frame.
104 compositor()->PaintFrameUsingOldRenderingPath(initial_frame
);
105 EXPECT_EQ(0, natural_size().width());
106 EXPECT_EQ(0, natural_size().height());
107 EXPECT_EQ(0, natural_size_changed_count());
109 // Callback should be fired once.
110 compositor()->PaintFrameUsingOldRenderingPath(larger_frame
);
111 EXPECT_EQ(larger_size
.width(), natural_size().width());
112 EXPECT_EQ(larger_size
.height(), natural_size().height());
113 EXPECT_EQ(1, natural_size_changed_count());
115 compositor()->PaintFrameUsingOldRenderingPath(larger_frame
);
116 EXPECT_EQ(larger_size
.width(), natural_size().width());
117 EXPECT_EQ(larger_size
.height(), natural_size().height());
118 EXPECT_EQ(1, natural_size_changed_count());
120 // Callback is fired once more when switching back to initial size.
121 compositor()->PaintFrameUsingOldRenderingPath(initial_frame
);
122 EXPECT_EQ(initial_size
.width(), natural_size().width());
123 EXPECT_EQ(initial_size
.height(), natural_size().height());
124 EXPECT_EQ(2, natural_size_changed_count());
126 compositor()->PaintFrameUsingOldRenderingPath(initial_frame
);
127 EXPECT_EQ(initial_size
.width(), natural_size().width());
128 EXPECT_EQ(initial_size
, natural_size());
129 EXPECT_EQ(2, natural_size_changed_count());
132 TEST_F(VideoFrameCompositorTest
, OpacityChanged
) {
133 gfx::Size
size(8, 8);
134 gfx::Rect
rect(gfx::Point(0, 0), size
);
135 scoped_refptr
<VideoFrame
> opaque_frame
= VideoFrame::CreateFrame(
136 VideoFrame::YV12
, size
, rect
, size
, base::TimeDelta());
137 scoped_refptr
<VideoFrame
> not_opaque_frame
= VideoFrame::CreateFrame(
138 VideoFrame::YV12A
, size
, rect
, size
, base::TimeDelta());
140 // Initial expectations.
141 EXPECT_FALSE(opaque());
142 EXPECT_EQ(0, opacity_changed_count());
144 // Callback is fired for the first frame.
145 compositor()->PaintFrameUsingOldRenderingPath(not_opaque_frame
);
146 EXPECT_FALSE(opaque());
147 EXPECT_EQ(1, opacity_changed_count());
149 // Callback shouldn't be first subsequent times with same opaqueness.
150 compositor()->PaintFrameUsingOldRenderingPath(not_opaque_frame
);
151 EXPECT_FALSE(opaque());
152 EXPECT_EQ(1, opacity_changed_count());
154 // Callback is fired when using opacity changes.
155 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame
);
156 EXPECT_TRUE(opaque());
157 EXPECT_EQ(2, opacity_changed_count());
159 // Callback shouldn't be first subsequent times with same opaqueness.
160 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame
);
161 EXPECT_TRUE(opaque());
162 EXPECT_EQ(2, opacity_changed_count());