[Offline pages] Moving browser code to android path
[chromium-blink-merge.git] / content / browser / gpu / gpu_ipc_browsertests.cc
blob7154c2eea99418188c12363a5cf57a685cc1a1ed
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 "base/command_line.h"
6 #include "base/run_loop.h"
7 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
8 #include "content/browser/gpu/gpu_process_host_ui_shim.h"
9 #include "content/common/gpu/client/context_provider_command_buffer.h"
10 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
11 #include "content/common/gpu/gpu_process_launch_causes.h"
12 #include "content/public/browser/gpu_data_manager.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
16 #include "ui/gl/gl_switches.h"
18 namespace {
20 using content::WebGraphicsContext3DCommandBufferImpl;
22 const content::CauseForGpuLaunch kInitCause =
23 content::
24 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
26 class ContextTestBase : public content::ContentBrowserTest {
27 public:
28 void SetUpOnMainThread() override {
29 if (!content::BrowserGpuChannelHostFactory::CanUseForTesting())
30 return;
32 if (!content::BrowserGpuChannelHostFactory::instance())
33 content::BrowserGpuChannelHostFactory::Initialize(true);
35 content::BrowserGpuChannelHostFactory* factory =
36 content::BrowserGpuChannelHostFactory::instance();
37 CHECK(factory);
38 bool lose_context_when_out_of_memory = false;
39 base::RunLoop run_loop;
40 factory->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
41 run_loop.Run();
42 scoped_refptr<content::GpuChannelHost>
43 gpu_channel_host(factory->GetGpuChannel());
44 DCHECK(gpu_channel_host.get());
45 context_.reset(
46 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
47 gpu_channel_host.get(),
48 blink::WebGraphicsContext3D::Attributes(),
49 lose_context_when_out_of_memory,
50 GURL(),
51 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
52 NULL));
53 CHECK(context_.get());
54 context_->InitializeOnCurrentThread();
55 context_support_ = context_->GetContextSupport();
56 ContentBrowserTest::SetUpOnMainThread();
59 void TearDownOnMainThread() override {
60 // Must delete the context first.
61 context_.reset(NULL);
62 ContentBrowserTest::TearDownOnMainThread();
65 protected:
66 scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl> context_;
67 gpu::ContextSupport* context_support_;
70 } // namespace
72 // Include the shared tests.
73 #define CONTEXT_TEST_F IN_PROC_BROWSER_TEST_F
74 #include "content/common/gpu/client/gpu_context_tests.h"
76 namespace content {
78 class BrowserGpuChannelHostFactoryTest : public ContentBrowserTest {
79 public:
80 void SetUpOnMainThread() override {
81 if (!BrowserGpuChannelHostFactory::CanUseForTesting())
82 return;
84 // Start all tests without a gpu channel so that the tests exercise a
85 // consistent codepath.
86 if (!BrowserGpuChannelHostFactory::instance())
87 BrowserGpuChannelHostFactory::Initialize(false);
89 CHECK(GetFactory());
91 ContentBrowserTest::SetUpOnMainThread();
94 void SetUpCommandLine(base::CommandLine* command_line) override {
95 // Start all tests without a gpu channel so that the tests exercise a
96 // consistent codepath.
97 command_line->AppendSwitch(switches::kDisableGpuEarlyInit);
100 void OnContextLost(const base::Closure callback, int* counter) {
101 (*counter)++;
102 callback.Run();
105 protected:
106 BrowserGpuChannelHostFactory* GetFactory() {
107 return BrowserGpuChannelHostFactory::instance();
110 bool IsChannelEstablished() {
111 return GetFactory()->GetGpuChannel() != NULL;
114 void EstablishAndWait() {
115 base::RunLoop run_loop;
116 GetFactory()->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
117 run_loop.Run();
120 GpuChannelHost* GetGpuChannel() {
121 return GetFactory()->GetGpuChannel();
124 static void Signal(bool *event) {
125 CHECK_EQ(*event, false);
126 *event = true;
129 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateContext() {
130 bool lose_context_when_out_of_memory = false;
131 return make_scoped_ptr(
132 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
133 GetGpuChannel(),
134 blink::WebGraphicsContext3D::Attributes(),
135 lose_context_when_out_of_memory,
136 GURL(),
137 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
138 NULL));
142 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
143 // establishes a GPU channel.
144 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
145 #define MAYBE_Basic Basic
146 #else
147 #define MAYBE_Basic DISABLED_Basic
148 #endif
149 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest, MAYBE_Basic) {
150 DCHECK(!IsChannelEstablished());
151 EstablishAndWait();
152 EXPECT_TRUE(GetGpuChannel() != NULL);
155 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
156 // establishes a GPU channel.
157 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
158 #define MAYBE_EstablishAndTerminate EstablishAndTerminate
159 #else
160 #define MAYBE_EstablishAndTerminate DISABLED_EstablishAndTerminate
161 #endif
162 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
163 MAYBE_EstablishAndTerminate) {
164 DCHECK(!IsChannelEstablished());
165 base::RunLoop run_loop;
166 GetFactory()->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
167 GetFactory()->Terminate();
169 // The callback should still trigger.
170 run_loop.Run();
173 #if !defined(OS_ANDROID)
174 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
175 // establishes a GPU channel.
176 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
177 #define MAYBE_AlreadyEstablished AlreadyEstablished
178 #else
179 #define MAYBE_AlreadyEstablished DISABLED_AlreadyEstablished
180 #endif
181 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
182 MAYBE_AlreadyEstablished) {
183 DCHECK(!IsChannelEstablished());
184 scoped_refptr<GpuChannelHost> gpu_channel =
185 GetFactory()->EstablishGpuChannelSync(kInitCause);
187 // Expect established callback immediately.
188 bool event = false;
189 GetFactory()->EstablishGpuChannel(
190 kInitCause,
191 base::Bind(&BrowserGpuChannelHostFactoryTest::Signal, &event));
192 EXPECT_TRUE(event);
193 EXPECT_EQ(gpu_channel.get(), GetGpuChannel());
195 #endif
197 // Test fails on Chromeos + Mac, flaky on Windows because UI Compositor
198 // establishes a GPU channel.
199 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
200 #define MAYBE_CrashAndRecover
201 #else
202 #define MAYBE_CrashAndRecover DISABLED_CrashAndRecover
203 #endif
204 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
205 MAYBE_CrashAndRecover) {
206 DCHECK(!IsChannelEstablished());
207 EstablishAndWait();
208 scoped_refptr<GpuChannelHost> host = GetGpuChannel();
210 scoped_refptr<ContextProviderCommandBuffer> provider =
211 ContextProviderCommandBuffer::Create(CreateContext(),
212 OFFSCREEN_CONTEXT_FOR_TESTING);
213 base::RunLoop run_loop;
214 int counter = 0;
215 provider->SetLostContextCallback(
216 base::Bind(&BrowserGpuChannelHostFactoryTest::OnContextLost,
217 base::Unretained(this), run_loop.QuitClosure(), &counter));
218 EXPECT_TRUE(provider->BindToCurrentThread());
219 GpuProcessHostUIShim* shim =
220 GpuProcessHostUIShim::FromID(GetFactory()->GpuProcessHostId());
221 EXPECT_TRUE(shim != NULL);
222 shim->SimulateCrash();
223 run_loop.Run();
225 EXPECT_EQ(1, counter);
226 EXPECT_FALSE(IsChannelEstablished());
227 EstablishAndWait();
228 EXPECT_TRUE(IsChannelEstablished());
231 } // namespace content