Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / gpu / gpu_ipc_browsertests.cc
blob817d73229b481d8fc211a1d5c0a4dce1482b768e
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 OnContextLost(const base::Closure callback, int* counter) {
95 (*counter)++;
96 callback.Run();
99 protected:
100 BrowserGpuChannelHostFactory* GetFactory() {
101 return BrowserGpuChannelHostFactory::instance();
104 bool IsChannelEstablished() {
105 return GetFactory()->GetGpuChannel() != NULL;
108 void EstablishAndWait() {
109 base::RunLoop run_loop;
110 GetFactory()->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
111 run_loop.Run();
114 GpuChannelHost* GetGpuChannel() {
115 return GetFactory()->GetGpuChannel();
118 static void Signal(bool *event) {
119 CHECK_EQ(*event, false);
120 *event = true;
123 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateContext() {
124 bool lose_context_when_out_of_memory = false;
125 return make_scoped_ptr(
126 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
127 GetGpuChannel(),
128 blink::WebGraphicsContext3D::Attributes(),
129 lose_context_when_out_of_memory,
130 GURL(),
131 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits(),
132 NULL));
136 // Fails since UI Compositor establishes a GpuChannel.
137 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest, DISABLED_Basic) {
138 DCHECK(!IsChannelEstablished());
139 EstablishAndWait();
140 EXPECT_TRUE(GetGpuChannel() != NULL);
143 // Fails since UI Compositor establishes a GpuChannel.
144 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
145 DISABLED_EstablishAndTerminate) {
146 DCHECK(!IsChannelEstablished());
147 base::RunLoop run_loop;
148 GetFactory()->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
149 GetFactory()->Terminate();
151 // The callback should still trigger.
152 run_loop.Run();
155 #if !defined(OS_ANDROID)
156 // Fails since UI Compositor establishes a GpuChannel.
157 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
158 DISABLED_AlreadyEstablished) {
159 DCHECK(!IsChannelEstablished());
160 scoped_refptr<GpuChannelHost> gpu_channel =
161 GetFactory()->EstablishGpuChannelSync(kInitCause);
163 // Expect established callback immediately.
164 bool event = false;
165 GetFactory()->EstablishGpuChannel(
166 kInitCause,
167 base::Bind(&BrowserGpuChannelHostFactoryTest::Signal, &event));
168 EXPECT_TRUE(event);
169 EXPECT_EQ(gpu_channel.get(), GetGpuChannel());
171 #endif
173 // Fails since UI Compositor establishes a GpuChannel.
174 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
175 DISABLED_CrashAndRecover) {
176 DCHECK(!IsChannelEstablished());
177 EstablishAndWait();
178 scoped_refptr<GpuChannelHost> host = GetGpuChannel();
180 scoped_refptr<ContextProviderCommandBuffer> provider =
181 ContextProviderCommandBuffer::Create(CreateContext(),
182 "BrowserGpuChannelHostFactoryTest");
183 base::RunLoop run_loop;
184 int counter = 0;
185 provider->SetLostContextCallback(
186 base::Bind(&BrowserGpuChannelHostFactoryTest::OnContextLost,
187 base::Unretained(this), run_loop.QuitClosure(), &counter));
188 EXPECT_TRUE(provider->BindToCurrentThread());
189 GpuProcessHostUIShim* shim =
190 GpuProcessHostUIShim::FromID(GetFactory()->GpuProcessHostId());
191 EXPECT_TRUE(shim != NULL);
192 shim->SimulateCrash();
193 run_loop.Run();
195 EXPECT_EQ(1, counter);
196 EXPECT_FALSE(IsChannelEstablished());
197 EstablishAndWait();
198 EXPECT_TRUE(IsChannelEstablished());
201 } // namespace content