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.
5 #include "base/message_loop/message_loop.h"
6 #include "cc/output/compositor_frame.h"
7 #include "content/browser/compositor/browser_compositor_output_surface_proxy.h"
8 #include "content/browser/compositor/software_browser_compositor_output_surface.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/compositor/compositor.h"
11 #include "ui/compositor/test/context_factories_for_test.h"
12 #include "ui/gfx/vsync_provider.h"
16 class FakeVSyncProvider
: public gfx::VSyncProvider
{
18 FakeVSyncProvider() : call_count_(0) {}
19 virtual ~FakeVSyncProvider() {}
21 virtual void GetVSyncParameters(const UpdateVSyncCallback
& callback
)
23 callback
.Run(timebase_
, interval_
);
27 int call_count() const { return call_count_
; }
29 void set_timebase(base::TimeTicks timebase
) { timebase_
= timebase
; }
30 void set_interval(base::TimeDelta interval
) { interval_
= interval
; }
33 base::TimeTicks timebase_
;
34 base::TimeDelta interval_
;
38 DISALLOW_COPY_AND_ASSIGN(FakeVSyncProvider
);
41 class FakeSoftwareOutputDevice
: public cc::SoftwareOutputDevice
{
43 FakeSoftwareOutputDevice() : vsync_provider_(new FakeVSyncProvider()) {}
44 virtual ~FakeSoftwareOutputDevice() {}
46 virtual gfx::VSyncProvider
* GetVSyncProvider() OVERRIDE
{
47 return vsync_provider_
.get();
51 scoped_ptr
<gfx::VSyncProvider
> vsync_provider_
;
53 DISALLOW_COPY_AND_ASSIGN(FakeSoftwareOutputDevice
);
58 class SoftwareBrowserCompositorOutputSurfaceTest
: public testing::Test
{
60 SoftwareBrowserCompositorOutputSurfaceTest();
61 virtual ~SoftwareBrowserCompositorOutputSurfaceTest();
63 virtual void SetUp() OVERRIDE
;
64 virtual void TearDown() OVERRIDE
;
66 scoped_ptr
<content::BrowserCompositorOutputSurface
> CreateSurface(
67 scoped_ptr
<cc::SoftwareOutputDevice
> device
);
70 scoped_ptr
<content::BrowserCompositorOutputSurface
> output_surface_
;
72 scoped_ptr
<base::MessageLoop
> message_loop_
;
73 scoped_ptr
<ui::Compositor
> compositor_
;
75 IDMap
<content::BrowserCompositorOutputSurface
> surface_map_
;
76 scoped_refptr
<content::BrowserCompositorOutputSurfaceProxy
> surface_proxy_
;
78 DISALLOW_COPY_AND_ASSIGN(SoftwareBrowserCompositorOutputSurfaceTest
);
81 SoftwareBrowserCompositorOutputSurfaceTest::
82 SoftwareBrowserCompositorOutputSurfaceTest() {
83 // |message_loop_| is not used, but the main thread still has to exist for the
85 message_loop_
.reset(new base::MessageLoopForUI
);
88 SoftwareBrowserCompositorOutputSurfaceTest::
89 ~SoftwareBrowserCompositorOutputSurfaceTest() {}
91 void SoftwareBrowserCompositorOutputSurfaceTest::SetUp() {
92 bool enable_pixel_output
= false;
93 ui::ContextFactory
* context_factory
=
94 ui::InitializeContextFactoryForTests(enable_pixel_output
);
96 compositor_
.reset(new ui::Compositor(gfx::kNullAcceleratedWidget
,
98 base::MessageLoopProxy::current()));
100 new content::BrowserCompositorOutputSurfaceProxy(&surface_map_
);
103 void SoftwareBrowserCompositorOutputSurfaceTest::TearDown() {
104 output_surface_
.reset();
107 EXPECT_TRUE(surface_map_
.IsEmpty());
109 surface_map_
.Clear();
110 ui::TerminateContextFactoryForTests();
113 scoped_ptr
<content::BrowserCompositorOutputSurface
>
114 SoftwareBrowserCompositorOutputSurfaceTest::CreateSurface(
115 scoped_ptr
<cc::SoftwareOutputDevice
> device
) {
116 return scoped_ptr
<content::BrowserCompositorOutputSurface
>(
117 new content::SoftwareBrowserCompositorOutputSurface(
122 compositor_
->vsync_manager()));
125 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest
, NoVSyncProvider
) {
126 scoped_ptr
<cc::SoftwareOutputDevice
> software_device(
127 new cc::SoftwareOutputDevice());
128 output_surface_
= CreateSurface(software_device
.Pass());
130 cc::CompositorFrame frame
;
131 output_surface_
->SwapBuffers(&frame
);
133 EXPECT_EQ(NULL
, output_surface_
->software_device()->GetVSyncProvider());
136 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest
, VSyncProviderUpdates
) {
137 scoped_ptr
<cc::SoftwareOutputDevice
> software_device(
138 new FakeSoftwareOutputDevice());
139 output_surface_
= CreateSurface(software_device
.Pass());
141 FakeVSyncProvider
* vsync_provider
= static_cast<FakeVSyncProvider
*>(
142 output_surface_
->software_device()->GetVSyncProvider());
143 EXPECT_EQ(0, vsync_provider
->call_count());
145 cc::CompositorFrame frame
;
146 output_surface_
->SwapBuffers(&frame
);
148 EXPECT_EQ(1, vsync_provider
->call_count());