Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / compositor / software_browser_compositor_output_surface_unittest.cc
blob904fca2d2f4d22b82e46026d91f4535aea515f7d
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 "cc/test/fake_output_surface_client.h"
8 #include "content/browser/compositor/browser_compositor_output_surface_proxy.h"
9 #include "content/browser/compositor/software_browser_compositor_output_surface.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/compositor/compositor.h"
12 #include "ui/compositor/test/context_factories_for_test.h"
13 #include "ui/gfx/vsync_provider.h"
15 namespace {
17 class FakeVSyncProvider : public gfx::VSyncProvider {
18 public:
19 FakeVSyncProvider() : call_count_(0) {}
20 ~FakeVSyncProvider() override {}
22 void GetVSyncParameters(const UpdateVSyncCallback& callback) override {
23 callback.Run(timebase_, interval_);
24 call_count_++;
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; }
32 private:
33 base::TimeTicks timebase_;
34 base::TimeDelta interval_;
36 int call_count_;
38 DISALLOW_COPY_AND_ASSIGN(FakeVSyncProvider);
41 class FakeSoftwareOutputDevice : public cc::SoftwareOutputDevice {
42 public:
43 FakeSoftwareOutputDevice() : vsync_provider_(new FakeVSyncProvider()) {}
44 ~FakeSoftwareOutputDevice() override {}
46 gfx::VSyncProvider* GetVSyncProvider() override {
47 return vsync_provider_.get();
50 private:
51 scoped_ptr<gfx::VSyncProvider> vsync_provider_;
53 DISALLOW_COPY_AND_ASSIGN(FakeSoftwareOutputDevice);
56 } // namespace
58 class SoftwareBrowserCompositorOutputSurfaceTest : public testing::Test {
59 public:
60 SoftwareBrowserCompositorOutputSurfaceTest();
61 ~SoftwareBrowserCompositorOutputSurfaceTest() override;
63 void SetUp() override;
64 void TearDown() override;
66 scoped_ptr<content::BrowserCompositorOutputSurface> CreateSurface(
67 scoped_ptr<cc::SoftwareOutputDevice> device);
69 protected:
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
84 // compositor to use.
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,
97 context_factory,
98 base::MessageLoopProxy::current()));
99 surface_proxy_ =
100 new content::BrowserCompositorOutputSurfaceProxy(&surface_map_);
103 void SoftwareBrowserCompositorOutputSurfaceTest::TearDown() {
104 output_surface_.reset();
105 compositor_.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(
118 surface_proxy_,
119 device.Pass(),
121 &surface_map_,
122 compositor_->vsync_manager()));
125 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest, NoVSyncProvider) {
126 cc::FakeOutputSurfaceClient output_surface_client;
127 scoped_ptr<cc::SoftwareOutputDevice> software_device(
128 new cc::SoftwareOutputDevice());
129 output_surface_ = CreateSurface(software_device.Pass());
130 CHECK(output_surface_->BindToClient(&output_surface_client));
132 cc::CompositorFrame frame;
133 output_surface_->SwapBuffers(&frame);
135 EXPECT_EQ(1, output_surface_client.swap_count());
136 EXPECT_EQ(NULL, output_surface_->software_device()->GetVSyncProvider());
139 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest, VSyncProviderUpdates) {
140 cc::FakeOutputSurfaceClient output_surface_client;
141 scoped_ptr<cc::SoftwareOutputDevice> software_device(
142 new FakeSoftwareOutputDevice());
143 output_surface_ = CreateSurface(software_device.Pass());
144 CHECK(output_surface_->BindToClient(&output_surface_client));
146 FakeVSyncProvider* vsync_provider = static_cast<FakeVSyncProvider*>(
147 output_surface_->software_device()->GetVSyncProvider());
148 EXPECT_EQ(0, vsync_provider->call_count());
150 cc::CompositorFrame frame;
151 output_surface_->SwapBuffers(&frame);
153 EXPECT_EQ(1, output_surface_client.swap_count());
154 EXPECT_EQ(1, vsync_provider->call_count());