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 "content/renderer/media/video_frame_compositor.h"
8 #include "media/base/video_frame.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 using media::VideoFrame
;
15 class VideoFrameCompositorTest
: public testing::Test
,
16 public cc::VideoFrameProvider::Client
{
18 VideoFrameCompositorTest()
19 : compositor_(new VideoFrameCompositor(
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 virtual ~VideoFrameCompositorTest() {
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 virtual void StopUsingProvider() OVERRIDE
{}
46 virtual void DidReceiveFrame() OVERRIDE
{
47 ++did_receive_frame_count_
;
49 virtual void DidUpdateMatrix(const float* matrix
) OVERRIDE
{}
51 void NaturalSizeChanged(gfx::Size natural_size
) {
52 ++natural_size_changed_count_
;
53 natural_size_
= natural_size
;
56 void OpacityChanged(bool opaque
) {
57 ++opacity_changed_count_
;
61 scoped_ptr
<VideoFrameCompositor
> compositor_
;
62 int did_receive_frame_count_
;
63 int natural_size_changed_count_
;
64 gfx::Size natural_size_
;
65 int opacity_changed_count_
;
68 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest
);
71 TEST_F(VideoFrameCompositorTest
, InitialValues
) {
72 EXPECT_FALSE(compositor()->GetCurrentFrame());
75 TEST_F(VideoFrameCompositorTest
, UpdateCurrentFrame
) {
76 scoped_refptr
<VideoFrame
> expected
= VideoFrame::CreateEOSFrame();
78 // Should notify compositor synchronously.
79 EXPECT_EQ(0, did_receive_frame_count());
80 compositor()->UpdateCurrentFrame(expected
);
81 scoped_refptr
<VideoFrame
> actual
= compositor()->GetCurrentFrame();
82 EXPECT_EQ(expected
, actual
);
83 EXPECT_EQ(1, did_receive_frame_count());
86 TEST_F(VideoFrameCompositorTest
, NaturalSizeChanged
) {
87 gfx::Size
initial_size(8, 8);
88 scoped_refptr
<VideoFrame
> initial_frame
=
89 VideoFrame::CreateBlackFrame(initial_size
);
91 gfx::Size
larger_size(16, 16);
92 scoped_refptr
<VideoFrame
> larger_frame
=
93 VideoFrame::CreateBlackFrame(larger_size
);
95 // Initial expectations.
96 EXPECT_EQ(0, natural_size().width());
97 EXPECT_EQ(0, natural_size().height());
98 EXPECT_EQ(0, natural_size_changed_count());
100 // Callback isn't fired for the first frame.
101 compositor()->UpdateCurrentFrame(initial_frame
);
102 EXPECT_EQ(0, natural_size().width());
103 EXPECT_EQ(0, natural_size().height());
104 EXPECT_EQ(0, natural_size_changed_count());
106 // Callback should be fired once.
107 compositor()->UpdateCurrentFrame(larger_frame
);
108 EXPECT_EQ(larger_size
.width(), natural_size().width());
109 EXPECT_EQ(larger_size
.height(), natural_size().height());
110 EXPECT_EQ(1, natural_size_changed_count());
112 compositor()->UpdateCurrentFrame(larger_frame
);
113 EXPECT_EQ(larger_size
.width(), natural_size().width());
114 EXPECT_EQ(larger_size
.height(), natural_size().height());
115 EXPECT_EQ(1, natural_size_changed_count());
117 // Callback is fired once more when switching back to initial size.
118 compositor()->UpdateCurrentFrame(initial_frame
);
119 EXPECT_EQ(initial_size
.width(), natural_size().width());
120 EXPECT_EQ(initial_size
.height(), natural_size().height());
121 EXPECT_EQ(2, natural_size_changed_count());
123 compositor()->UpdateCurrentFrame(initial_frame
);
124 EXPECT_EQ(initial_size
.width(), natural_size().width());
125 EXPECT_EQ(initial_size
, natural_size());
126 EXPECT_EQ(2, natural_size_changed_count());
129 TEST_F(VideoFrameCompositorTest
, OpacityChanged
) {
130 gfx::Size
size(8, 8);
131 gfx::Rect
rect(gfx::Point(0, 0), size
);
132 scoped_refptr
<VideoFrame
> opaque_frame
= VideoFrame::CreateFrame(
133 VideoFrame::YV12
, size
, rect
, size
, base::TimeDelta());
134 scoped_refptr
<VideoFrame
> not_opaque_frame
= VideoFrame::CreateFrame(
135 VideoFrame::YV12A
, size
, rect
, size
, base::TimeDelta());
137 // Initial expectations.
138 EXPECT_FALSE(opaque());
139 EXPECT_EQ(0, opacity_changed_count());
141 // Callback is fired for the first frame.
142 compositor()->UpdateCurrentFrame(not_opaque_frame
);
143 EXPECT_FALSE(opaque());
144 EXPECT_EQ(1, opacity_changed_count());
146 // Callback shouldn't be first subsequent times with same opaqueness.
147 compositor()->UpdateCurrentFrame(not_opaque_frame
);
148 EXPECT_FALSE(opaque());
149 EXPECT_EQ(1, opacity_changed_count());
151 // Callback is fired when using opacity changes.
152 compositor()->UpdateCurrentFrame(opaque_frame
);
153 EXPECT_TRUE(opaque());
154 EXPECT_EQ(2, opacity_changed_count());
156 // Callback shouldn't be first subsequent times with same opaqueness.
157 compositor()->UpdateCurrentFrame(opaque_frame
);
158 EXPECT_TRUE(opaque());
159 EXPECT_EQ(2, opacity_changed_count());
162 } // namespace content