Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / media / blink / video_frame_compositor_unittest.cc
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.
4
5 #include "base/bind.h"
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"
10
11 namespace media {
12
13 class VideoFrameCompositorTest : public testing::Test,
14                                  public cc::VideoFrameProvider::Client {
15  public:
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),
25         opaque_(false) {
26     compositor_->SetVideoFrameProviderClient(this);
27   }
28
29   virtual ~VideoFrameCompositorTest() {
30     compositor_->SetVideoFrameProviderClient(NULL);
31   }
32
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_; }
37
38   int opacity_changed_count() { return opacity_changed_count_; }
39   bool opaque() { return opaque_; }
40
41  private:
42   // cc::VideoFrameProvider::Client implementation.
43   virtual void StopUsingProvider() OVERRIDE {}
44   virtual void DidReceiveFrame() OVERRIDE {
45     ++did_receive_frame_count_;
46   }
47   virtual void DidUpdateMatrix(const float* matrix) OVERRIDE {}
48
49   void NaturalSizeChanged(gfx::Size natural_size) {
50     ++natural_size_changed_count_;
51     natural_size_ = natural_size;
52   }
53
54   void OpacityChanged(bool opaque) {
55     ++opacity_changed_count_;
56     opaque_ = opaque;
57   }
58
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_;
64   bool opaque_;
65
66   DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest);
67 };
68
69 TEST_F(VideoFrameCompositorTest, InitialValues) {
70   EXPECT_FALSE(compositor()->GetCurrentFrame().get());
71 }
72
73 TEST_F(VideoFrameCompositorTest, UpdateCurrentFrame) {
74   scoped_refptr<VideoFrame> expected = VideoFrame::CreateEOSFrame();
75
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());
82 }
83
84 TEST_F(VideoFrameCompositorTest, NaturalSizeChanged) {
85   gfx::Size initial_size(8, 8);
86   scoped_refptr<VideoFrame> initial_frame =
87       VideoFrame::CreateBlackFrame(initial_size);
88
89   gfx::Size larger_size(16, 16);
90   scoped_refptr<VideoFrame> larger_frame =
91       VideoFrame::CreateBlackFrame(larger_size);
92
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());
97
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());
103
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());
109
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());
114
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());
120
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());
125 }
126
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());
134
135   // Initial expectations.
136   EXPECT_FALSE(opaque());
137   EXPECT_EQ(0, opacity_changed_count());
138
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());
143
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());
148
149   // Callback is fired when using opacity changes.
150   compositor()->UpdateCurrentFrame(opaque_frame);
151   EXPECT_TRUE(opaque());
152   EXPECT_EQ(2, opacity_changed_count());
153
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());
158 }
159
160 }  // namespace media