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 "cc/layers/video_frame_provider.h"
7 #include "media/base/video_frame.h"
8 #include "media/blink/video_frame_compositor.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class VideoFrameCompositorTest
: public testing::Test
,
14 public cc::VideoFrameProvider::Client
{
16 VideoFrameCompositorTest()
17 : compositor_(new VideoFrameCompositor(
18 base::Bind(&VideoFrameCompositorTest::NaturalSizeChanged
,
19 base::Unretained(this)),
20 base::Bind(&VideoFrameCompositorTest::OpacityChanged
,
21 base::Unretained(this)))),
22 did_receive_frame_count_(0),
23 natural_size_changed_count_(0),
24 opacity_changed_count_(0),
26 compositor_
->SetVideoFrameProviderClient(this);
29 ~VideoFrameCompositorTest() override
{
30 compositor_
->SetVideoFrameProviderClient(NULL
);
33 VideoFrameCompositor
* compositor() { return compositor_
.get(); }
34 int did_receive_frame_count() { return did_receive_frame_count_
; }
35 int natural_size_changed_count() { return natural_size_changed_count_
; }
36 gfx::Size
natural_size() { return natural_size_
; }
38 int opacity_changed_count() { return opacity_changed_count_
; }
39 bool opaque() { return opaque_
; }
42 // cc::VideoFrameProvider::Client implementation.
43 void StopUsingProvider() override
{}
44 void DidReceiveFrame() override
{
45 ++did_receive_frame_count_
;
47 void DidUpdateMatrix(const float* matrix
) override
{}
49 void NaturalSizeChanged(gfx::Size natural_size
) {
50 ++natural_size_changed_count_
;
51 natural_size_
= natural_size
;
54 void OpacityChanged(bool opaque
) {
55 ++opacity_changed_count_
;
59 scoped_ptr
<VideoFrameCompositor
> compositor_
;
60 int did_receive_frame_count_
;
61 int natural_size_changed_count_
;
62 gfx::Size natural_size_
;
63 int opacity_changed_count_
;
66 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest
);
69 TEST_F(VideoFrameCompositorTest
, InitialValues
) {
70 EXPECT_FALSE(compositor()->GetCurrentFrame().get());
73 TEST_F(VideoFrameCompositorTest
, UpdateCurrentFrame
) {
74 scoped_refptr
<VideoFrame
> expected
= VideoFrame::CreateEOSFrame();
76 // Should notify compositor synchronously.
77 EXPECT_EQ(0, did_receive_frame_count());
78 compositor()->UpdateCurrentFrame(expected
);
79 scoped_refptr
<VideoFrame
> actual
= compositor()->GetCurrentFrame();
80 EXPECT_EQ(expected
, actual
);
81 EXPECT_EQ(1, did_receive_frame_count());
84 TEST_F(VideoFrameCompositorTest
, NaturalSizeChanged
) {
85 gfx::Size
initial_size(8, 8);
86 scoped_refptr
<VideoFrame
> initial_frame
=
87 VideoFrame::CreateBlackFrame(initial_size
);
89 gfx::Size
larger_size(16, 16);
90 scoped_refptr
<VideoFrame
> larger_frame
=
91 VideoFrame::CreateBlackFrame(larger_size
);
93 // Initial expectations.
94 EXPECT_EQ(0, natural_size().width());
95 EXPECT_EQ(0, natural_size().height());
96 EXPECT_EQ(0, natural_size_changed_count());
98 // Callback isn't fired for the first frame.
99 compositor()->UpdateCurrentFrame(initial_frame
);
100 EXPECT_EQ(0, natural_size().width());
101 EXPECT_EQ(0, natural_size().height());
102 EXPECT_EQ(0, natural_size_changed_count());
104 // Callback should be fired once.
105 compositor()->UpdateCurrentFrame(larger_frame
);
106 EXPECT_EQ(larger_size
.width(), natural_size().width());
107 EXPECT_EQ(larger_size
.height(), natural_size().height());
108 EXPECT_EQ(1, natural_size_changed_count());
110 compositor()->UpdateCurrentFrame(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 // Callback is fired once more when switching back to initial size.
116 compositor()->UpdateCurrentFrame(initial_frame
);
117 EXPECT_EQ(initial_size
.width(), natural_size().width());
118 EXPECT_EQ(initial_size
.height(), natural_size().height());
119 EXPECT_EQ(2, natural_size_changed_count());
121 compositor()->UpdateCurrentFrame(initial_frame
);
122 EXPECT_EQ(initial_size
.width(), natural_size().width());
123 EXPECT_EQ(initial_size
, natural_size());
124 EXPECT_EQ(2, natural_size_changed_count());
127 TEST_F(VideoFrameCompositorTest
, OpacityChanged
) {
128 gfx::Size
size(8, 8);
129 gfx::Rect
rect(gfx::Point(0, 0), size
);
130 scoped_refptr
<VideoFrame
> opaque_frame
= VideoFrame::CreateFrame(
131 VideoFrame::YV12
, size
, rect
, size
, base::TimeDelta());
132 scoped_refptr
<VideoFrame
> not_opaque_frame
= VideoFrame::CreateFrame(
133 VideoFrame::YV12A
, size
, rect
, size
, base::TimeDelta());
135 // Initial expectations.
136 EXPECT_FALSE(opaque());
137 EXPECT_EQ(0, opacity_changed_count());
139 // Callback is fired for the first frame.
140 compositor()->UpdateCurrentFrame(not_opaque_frame
);
141 EXPECT_FALSE(opaque());
142 EXPECT_EQ(1, opacity_changed_count());
144 // Callback shouldn't be first subsequent times with same opaqueness.
145 compositor()->UpdateCurrentFrame(not_opaque_frame
);
146 EXPECT_FALSE(opaque());
147 EXPECT_EQ(1, opacity_changed_count());
149 // Callback is fired when using opacity changes.
150 compositor()->UpdateCurrentFrame(opaque_frame
);
151 EXPECT_TRUE(opaque());
152 EXPECT_EQ(2, opacity_changed_count());
154 // Callback shouldn't be first subsequent times with same opaqueness.
155 compositor()->UpdateCurrentFrame(opaque_frame
);
156 EXPECT_TRUE(opaque());
157 EXPECT_EQ(2, opacity_changed_count());