IndexedDB: Protect against use-after-free in ChainedBlobWriter.
[chromium-blink-merge.git] / cc / output / output_surface_unittest.cc
blob3b0270277295950c3b4cc49bdc00af961f08515d
1 // Copyright 2013 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 "cc/output/output_surface.h"
7 #include "base/test/test_simple_task_runner.h"
8 #include "cc/output/managed_memory_policy.h"
9 #include "cc/output/output_surface_client.h"
10 #include "cc/output/software_output_device.h"
11 #include "cc/test/begin_frame_args_test.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_output_surface_client.h"
14 #include "cc/test/test_context_provider.h"
15 #include "cc/test/test_web_graphics_context_3d.h"
16 #include "gpu/GLES2/gl2extchromium.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/frame_time.h"
20 namespace cc {
21 namespace {
23 class TestOutputSurface : public OutputSurface {
24 public:
25 explicit TestOutputSurface(scoped_refptr<ContextProvider> context_provider)
26 : OutputSurface(context_provider) {}
28 TestOutputSurface(scoped_refptr<ContextProvider> context_provider,
29 scoped_refptr<ContextProvider> worker_context_provider)
30 : OutputSurface(worker_context_provider) {}
32 explicit TestOutputSurface(scoped_ptr<SoftwareOutputDevice> software_device)
33 : OutputSurface(software_device.Pass()) {}
35 TestOutputSurface(scoped_refptr<ContextProvider> context_provider,
36 scoped_ptr<SoftwareOutputDevice> software_device)
37 : OutputSurface(context_provider, software_device.Pass()) {}
39 void SwapBuffers(CompositorFrame* frame) override {
40 client_->DidSwapBuffers();
41 client_->DidSwapBuffersComplete();
44 void CommitVSyncParametersForTesting(base::TimeTicks timebase,
45 base::TimeDelta interval) {
46 CommitVSyncParameters(timebase, interval);
49 void DidSwapBuffersForTesting() { client_->DidSwapBuffers(); }
51 void OnSwapBuffersCompleteForTesting() { client_->DidSwapBuffersComplete(); }
53 protected:
56 class TestSoftwareOutputDevice : public SoftwareOutputDevice {
57 public:
58 TestSoftwareOutputDevice();
59 ~TestSoftwareOutputDevice() override;
61 // Overriden from cc:SoftwareOutputDevice
62 void DiscardBackbuffer() override;
63 void EnsureBackbuffer() override;
65 int discard_backbuffer_count() { return discard_backbuffer_count_; }
66 int ensure_backbuffer_count() { return ensure_backbuffer_count_; }
68 private:
69 int discard_backbuffer_count_;
70 int ensure_backbuffer_count_;
73 TestSoftwareOutputDevice::TestSoftwareOutputDevice()
74 : discard_backbuffer_count_(0), ensure_backbuffer_count_(0) {}
76 TestSoftwareOutputDevice::~TestSoftwareOutputDevice() {}
78 void TestSoftwareOutputDevice::DiscardBackbuffer() {
79 SoftwareOutputDevice::DiscardBackbuffer();
80 discard_backbuffer_count_++;
83 void TestSoftwareOutputDevice::EnsureBackbuffer() {
84 SoftwareOutputDevice::EnsureBackbuffer();
85 ensure_backbuffer_count_++;
88 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientSuccess) {
89 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
90 TestOutputSurface output_surface(provider);
91 EXPECT_FALSE(output_surface.HasClient());
93 FakeOutputSurfaceClient client;
94 EXPECT_TRUE(output_surface.BindToClient(&client));
95 EXPECT_TRUE(output_surface.HasClient());
97 // Verify DidLoseOutputSurface callback is hooked up correctly.
98 EXPECT_FALSE(client.did_lose_output_surface_called());
99 output_surface.context_provider()->ContextGL()->LoseContextCHROMIUM(
100 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
101 output_surface.context_provider()->ContextGL()->Flush();
102 EXPECT_TRUE(client.did_lose_output_surface_called());
105 TEST(OutputSurfaceTest, ClientPointerIndicatesWorkerBindToClientSuccess) {
106 scoped_refptr<TestContextProvider> provider = TestContextProvider::Create();
107 scoped_refptr<TestContextProvider> worker_provider =
108 TestContextProvider::Create();
109 TestOutputSurface output_surface(provider, worker_provider);
110 EXPECT_FALSE(output_surface.HasClient());
112 FakeOutputSurfaceClient client;
113 EXPECT_TRUE(output_surface.BindToClient(&client));
114 EXPECT_TRUE(output_surface.HasClient());
116 // Verify DidLoseOutputSurface callback is hooked up correctly.
117 EXPECT_FALSE(client.did_lose_output_surface_called());
118 output_surface.context_provider()->ContextGL()->LoseContextCHROMIUM(
119 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
120 output_surface.context_provider()->ContextGL()->Flush();
121 EXPECT_TRUE(client.did_lose_output_surface_called());
124 TEST(OutputSurfaceTest, ClientPointerIndicatesBindToClientFailure) {
125 scoped_refptr<TestContextProvider> context_provider =
126 TestContextProvider::Create();
128 // Lose the context so BindToClient fails.
129 context_provider->UnboundTestContext3d()->set_context_lost(true);
131 TestOutputSurface output_surface(context_provider);
132 EXPECT_FALSE(output_surface.HasClient());
134 FakeOutputSurfaceClient client;
135 EXPECT_FALSE(output_surface.BindToClient(&client));
136 EXPECT_FALSE(output_surface.HasClient());
139 TEST(OutputSurfaceTest, ClientPointerIndicatesWorkerBindToClientFailure) {
140 scoped_refptr<TestContextProvider> context_provider =
141 TestContextProvider::Create();
142 scoped_refptr<TestContextProvider> worker_context_provider =
143 TestContextProvider::Create();
145 // Lose the context so BindToClient fails.
146 worker_context_provider->UnboundTestContext3d()->set_context_lost(true);
148 TestOutputSurface output_surface(context_provider, worker_context_provider);
149 EXPECT_FALSE(output_surface.HasClient());
151 FakeOutputSurfaceClient client;
152 EXPECT_FALSE(output_surface.BindToClient(&client));
153 EXPECT_FALSE(output_surface.HasClient());
156 TEST(OutputSurfaceTest, MemoryAllocation) {
157 scoped_refptr<TestContextProvider> context_provider =
158 TestContextProvider::Create();
160 TestOutputSurface output_surface(context_provider);
162 FakeOutputSurfaceClient client;
163 EXPECT_TRUE(output_surface.BindToClient(&client));
165 ManagedMemoryPolicy policy(0);
166 policy.bytes_limit_when_visible = 1234;
167 policy.priority_cutoff_when_visible =
168 gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY;
170 context_provider->SetMemoryAllocation(policy);
171 EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
172 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_REQUIRED_ONLY,
173 client.memory_policy().priority_cutoff_when_visible);
175 policy.priority_cutoff_when_visible =
176 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
177 context_provider->SetMemoryAllocation(policy);
178 EXPECT_EQ(gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
179 client.memory_policy().priority_cutoff_when_visible);
181 // 0 bytes limit should be ignored.
182 policy.bytes_limit_when_visible = 0;
183 context_provider->SetMemoryAllocation(policy);
184 EXPECT_EQ(1234u, client.memory_policy().bytes_limit_when_visible);
187 TEST(OutputSurfaceTest, SoftwareOutputDeviceBackbufferManagement) {
188 TestSoftwareOutputDevice* software_output_device =
189 new TestSoftwareOutputDevice();
191 // TestOutputSurface now owns software_output_device and has responsibility to
192 // free it.
193 TestOutputSurface output_surface(make_scoped_ptr(software_output_device));
195 EXPECT_EQ(0, software_output_device->ensure_backbuffer_count());
196 EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
198 output_surface.EnsureBackbuffer();
199 EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
200 EXPECT_EQ(0, software_output_device->discard_backbuffer_count());
201 output_surface.DiscardBackbuffer();
203 EXPECT_EQ(1, software_output_device->ensure_backbuffer_count());
204 EXPECT_EQ(1, software_output_device->discard_backbuffer_count());
207 } // namespace
208 } // namespace cc