Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / gpu / gpu_ipc_browsertests.cc
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.
4
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/test/content_browser_test.h"
15 #include "ui/gl/gl_switches.h"
16 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
17
18 namespace {
19
20 using content::WebGraphicsContext3DCommandBufferImpl;
21
22 const content::CauseForGpuLaunch kInitCause =
23     content::
24         CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
25
26 class ContextTestBase : public content::ContentBrowserTest {
27  public:
28   virtual void SetUpOnMainThread() OVERRIDE {
29     if (!content::BrowserGpuChannelHostFactory::CanUseForTesting())
30       return;
31
32     if (!content::BrowserGpuChannelHostFactory::instance())
33       content::BrowserGpuChannelHostFactory::Initialize(true);
34
35     content::BrowserGpuChannelHostFactory* factory =
36         content::BrowserGpuChannelHostFactory::instance();
37     CHECK(factory);
38     scoped_refptr<content::GpuChannelHost> gpu_channel_host(
39         factory->EstablishGpuChannelSync(kInitCause));
40     context_.reset(
41         WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
42             gpu_channel_host.get(),
43             blink::WebGraphicsContext3D::Attributes(),
44             GURL(),
45             WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits()));
46     CHECK(context_.get());
47     context_->makeContextCurrent();
48     context_support_ = context_->GetContextSupport();
49     ContentBrowserTest::SetUpOnMainThread();
50   }
51
52   virtual void TearDownOnMainThread() OVERRIDE {
53     // Must delete the context first.
54     context_.reset(NULL);
55     ContentBrowserTest::TearDownOnMainThread();
56   }
57
58  protected:
59   scoped_ptr<content::WebGraphicsContext3DCommandBufferImpl> context_;
60   gpu::ContextSupport* context_support_;
61 };
62
63 }  // namespace
64
65 // Include the shared tests.
66 #define CONTEXT_TEST_F IN_PROC_BROWSER_TEST_F
67 #include "content/common/gpu/client/gpu_context_tests.h"
68
69 namespace content {
70
71 class BrowserGpuChannelHostFactoryTest : public ContextTestBase {
72  public:
73   virtual void SetUpOnMainThread() OVERRIDE {
74     if (!BrowserGpuChannelHostFactory::CanUseForTesting())
75       return;
76
77     // Start all tests without a gpu channel so that the tests exercise a
78     // consistent codepath.
79     if (!BrowserGpuChannelHostFactory::instance())
80       BrowserGpuChannelHostFactory::Initialize(false);
81
82     CHECK(GetFactory());
83
84     ContentBrowserTest::SetUpOnMainThread();
85   }
86
87   virtual void TearDownOnMainThread() OVERRIDE {
88     ContextTestBase::TearDownOnMainThread();
89   }
90
91   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
92     // Start all tests without a gpu channel so that the tests exercise a
93     // consistent codepath.
94     command_line->AppendSwitch(switches::kDisableGpuProcessPrelaunch);
95   }
96
97   void OnContextLost(const base::Closure callback, int* counter) {
98     (*counter)++;
99     callback.Run();
100   }
101
102  protected:
103   BrowserGpuChannelHostFactory* GetFactory() {
104     return BrowserGpuChannelHostFactory::instance();
105   }
106
107   bool IsChannelEstablished() {
108     return GetFactory()->GetGpuChannel() != NULL;
109   }
110
111   void EstablishAndWait() {
112     base::RunLoop run_loop;
113     GetFactory()->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
114     run_loop.Run();
115   }
116
117   GpuChannelHost* GetGpuChannel() {
118     return GetFactory()->GetGpuChannel();
119   }
120
121   static void Signal(bool *event) {
122     CHECK_EQ(*event, false);
123     *event = true;
124   }
125
126   scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateContext() {
127     return make_scoped_ptr(
128         WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
129             GetGpuChannel(),
130             blink::WebGraphicsContext3D::Attributes(),
131             GURL(),
132             WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits()));
133   }
134 };
135
136 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest, Basic) {
137   if (!context_)
138     return;
139
140   DCHECK(!IsChannelEstablished());
141   EstablishAndWait();
142   EXPECT_TRUE(GetGpuChannel() != NULL);
143 }
144
145 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest,
146                        EstablishAndTerminate) {
147   if (!context_)
148     return;
149
150   DCHECK(!IsChannelEstablished());
151   base::RunLoop run_loop;
152   GetFactory()->EstablishGpuChannel(kInitCause, run_loop.QuitClosure());
153   GetFactory()->Terminate();
154
155   // The callback should still trigger.
156   run_loop.Run();
157 }
158
159 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest, AlreadyEstablished) {
160   if (!context_)
161     return;
162
163   DCHECK(!IsChannelEstablished());
164   scoped_refptr<GpuChannelHost> gpu_channel =
165       GetFactory()->EstablishGpuChannelSync(kInitCause);
166
167   // Expect established callback immediately.
168   bool event = false;
169   GetFactory()->EstablishGpuChannel(
170       kInitCause,
171       base::Bind(&BrowserGpuChannelHostFactoryTest::Signal, &event));
172   EXPECT_TRUE(event);
173   EXPECT_EQ(gpu_channel, GetGpuChannel());
174 }
175
176 IN_PROC_BROWSER_TEST_F(BrowserGpuChannelHostFactoryTest, CrashAndRecover) {
177   if (!context_)
178     return;
179
180   DCHECK(!IsChannelEstablished());
181   EstablishAndWait();
182   scoped_refptr<GpuChannelHost> host = GetGpuChannel();
183
184   scoped_refptr<ContextProviderCommandBuffer> provider =
185       ContextProviderCommandBuffer::Create(CreateContext(),
186                                            "BrowserGpuChannelHostFactoryTest");
187   base::RunLoop run_loop;
188   int counter = 0;
189   provider->SetLostContextCallback(
190       base::Bind(&BrowserGpuChannelHostFactoryTest::OnContextLost,
191                  base::Unretained(this), run_loop.QuitClosure(), &counter));
192   EXPECT_TRUE(provider->BindToCurrentThread());
193   GpuProcessHostUIShim* shim =
194       GpuProcessHostUIShim::FromID(GetFactory()->GpuProcessHostId());
195   EXPECT_TRUE(shim != NULL);
196   shim->SimulateCrash();
197   run_loop.Run();
198
199   EXPECT_EQ(1, counter);
200   EXPECT_FALSE(IsChannelEstablished());
201   EstablishAndWait();
202   EXPECT_TRUE(IsChannelEstablished());
203 }
204
205 }  // namespace content