- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / test / browser_test_base.cc
1 // Copyright (c) 2011 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 "content/public/test/browser_test_base.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/debug/stack_trace.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/sys_info.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/main_function_params.h"
16 #include "content/public/test/test_utils.h"
17 #include "net/base/net_errors.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "ui/compositor/compositor_switches.h"
21 #include "ui/gl/gl_implementation.h"
22 #include "ui/gl/gl_switches.h"
23
24 #if defined(OS_POSIX)
25 #include "base/process/process_handle.h"
26 #endif
27
28 #if defined(OS_MACOSX)
29 #include "base/mac/mac_util.h"
30 #include "base/power_monitor/power_monitor_device_source.h"
31 #endif
32
33 #if defined(OS_ANDROID)
34 #include "base/threading/thread_restrictions.h"
35 #include "content/public/browser/browser_main_runner.h"
36 #include "content/public/browser/browser_thread.h"
37 #endif
38
39 #if defined(USE_AURA)
40 #include "content/browser/aura/image_transport_factory.h"
41 #include "ui/compositor/test/test_context_factory.h"
42 #endif
43
44 namespace content {
45 namespace {
46
47 #if defined(OS_POSIX)
48 // On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
49 // debugging easier) and also exit with a known error code (so that the test
50 // framework considers this a failure -- http://crbug.com/57578).
51 // Note: We only want to do this in the browser process, and not forked
52 // processes. That might lead to hangs because of locks inside tcmalloc or the
53 // OS. See http://crbug.com/141302.
54 static int g_browser_process_pid;
55 static void DumpStackTraceSignalHandler(int signal) {
56   if (g_browser_process_pid == base::GetCurrentProcId()) {
57     logging::RawLog(logging::LOG_ERROR,
58                     "BrowserTestBase signal handler received SIGTERM. "
59                     "Backtrace:\n");
60     base::debug::StackTrace().Print();
61   }
62   _exit(128 + signal);
63 }
64 #endif  // defined(OS_POSIX)
65
66 void RunTaskOnRendererThread(const base::Closure& task,
67                              const base::Closure& quit_task) {
68   task.Run();
69   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
70 }
71
72 // In many cases it may be not obvious that a test makes a real DNS lookup.
73 // We generally don't want to rely on external DNS servers for our tests,
74 // so this host resolver procedure catches external queries and returns a failed
75 // lookup result.
76 class LocalHostResolverProc : public net::HostResolverProc {
77  public:
78   LocalHostResolverProc() : HostResolverProc(NULL) {}
79
80   virtual int Resolve(const std::string& host,
81                       net::AddressFamily address_family,
82                       net::HostResolverFlags host_resolver_flags,
83                       net::AddressList* addrlist,
84                       int* os_error) OVERRIDE {
85     const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
86     bool local = false;
87
88     if (host == net::GetHostName()) {
89       local = true;
90     } else {
91       for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
92         if (host == kLocalHostNames[i]) {
93           local = true;
94           break;
95         }
96     }
97
98     // To avoid depending on external resources and to reduce (if not preclude)
99     // network interactions from tests, we simulate failure for non-local DNS
100     // queries, rather than perform them.
101     // If you really need to make an external DNS query, use
102     // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
103     if (!local) {
104       DVLOG(1) << "To avoid external dependencies, simulating failure for "
105           "external DNS lookup of " << host;
106       return net::ERR_NOT_IMPLEMENTED;
107     }
108
109     return ResolveUsingPrevious(host, address_family, host_resolver_flags,
110                                 addrlist, os_error);
111   }
112
113  private:
114   virtual ~LocalHostResolverProc() {}
115 };
116
117 }  // namespace
118
119 extern int BrowserMain(const MainFunctionParams&);
120
121 BrowserTestBase::BrowserTestBase()
122     : allow_test_contexts_(true),
123       allow_osmesa_(true) {
124 #if defined(OS_MACOSX)
125   base::mac::SetOverrideAmIBundled(true);
126   base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
127 #endif
128
129 #if defined(OS_POSIX)
130   handle_sigterm_ = true;
131 #endif
132
133   embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
134 }
135
136 BrowserTestBase::~BrowserTestBase() {
137 #if defined(OS_ANDROID)
138   // RemoteTestServer can cause wait on the UI thread.
139   base::ThreadRestrictions::ScopedAllowWait allow_wait;
140   test_server_.reset(NULL);
141 #endif
142 }
143
144 void BrowserTestBase::SetUp() {
145   CommandLine* command_line = CommandLine::ForCurrentProcess();
146
147   // The tests assume that file:// URIs can freely access other file:// URIs.
148   command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
149
150   command_line->AppendSwitch(switches::kDomAutomationController);
151
152   // It is sometimes useful when looking at browser test failures to know which
153   // GPU blacklisting decisions were made.
154   command_line->AppendSwitch(switches::kLogGpuControlListDecisions);
155
156 #if defined(OS_CHROMEOS)
157   // If the test is running on the chromeos envrionment (such as
158   // device or vm bots), always use real contexts.
159   if (base::SysInfo::IsRunningOnChromeOS())
160     allow_test_contexts_ = false;
161 #endif
162
163 #if defined(USE_AURA)
164   if (command_line->HasSwitch(switches::kDisableTestCompositor))
165     allow_test_contexts_  = false;
166
167   // Use test contexts for browser tests unless they override and force us to
168   // use a real context.
169   if (allow_test_contexts_) {
170     content::ImageTransportFactory::InitializeForUnitTests(
171         scoped_ptr<ui::ContextFactory>(new ui::TestContextFactory));
172   }
173 #endif
174
175   // When using real GL contexts, we usually use OSMesa as this works on all
176   // bots. The command line can override this behaviour to use a real GPU.
177   if (command_line->HasSwitch(switches::kUseGpuInTests))
178     allow_osmesa_ = false;
179
180   // Some bots pass this flag when they want to use a real GPU.
181   if (command_line->HasSwitch("enable-gpu"))
182     allow_osmesa_ = false;
183
184 #if defined(OS_MACOSX)
185   // On Mac we always use a real GPU.
186   allow_osmesa_ = false;
187 #endif
188
189 #if defined(OS_ANDROID)
190   // On Android we always use a real GPU.
191   allow_osmesa_ = false;
192 #endif
193
194 #if defined(OS_CHROMEOS)
195   // If the test is running on the chromeos envrionment (such as
196   // device or vm bots), the compositor will use real GL contexts, and
197   // we should use real GL bindings with it.
198   if (base::SysInfo::IsRunningOnChromeOS())
199     allow_osmesa_ = false;
200 #endif
201
202   if (command_line->HasSwitch(switches::kUseGL)) {
203     NOTREACHED() <<
204         "kUseGL should not be used with tests. Try kUseGpuInTests instead.";
205   }
206
207   if (allow_osmesa_) {
208     command_line->AppendSwitchASCII(
209         switches::kUseGL, gfx::kGLImplementationOSMesaName);
210   }
211
212   scoped_refptr<net::HostResolverProc> local_resolver =
213       new LocalHostResolverProc();
214   rule_based_resolver_ =
215       new net::RuleBasedHostResolverProc(local_resolver.get());
216   rule_based_resolver_->AddSimulatedFailure("wpad");
217   net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
218       rule_based_resolver_.get());
219
220   SetUpInProcessBrowserTestFixture();
221
222   MainFunctionParams params(*command_line);
223   params.ui_task =
224       new base::Closure(
225           base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
226
227 #if defined(OS_ANDROID)
228   BrowserMainRunner::Create()->Initialize(params);
229   // We are done running the test by now. During teardown we
230   // need to be able to perform IO.
231   base::ThreadRestrictions::SetIOAllowed(true);
232   BrowserThread::PostTask(
233       BrowserThread::IO, FROM_HERE,
234       base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
235                  true));
236 #else
237   BrowserMain(params);
238 #endif
239   TearDownInProcessBrowserTestFixture();
240 }
241
242 void BrowserTestBase::TearDown() {
243 }
244
245 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
246 #if defined(OS_POSIX)
247   if (handle_sigterm_) {
248     g_browser_process_pid = base::GetCurrentProcId();
249     signal(SIGTERM, DumpStackTraceSignalHandler);
250   }
251 #endif  // defined(OS_POSIX)
252   RunTestOnMainThreadLoop();
253 }
254
255 void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
256   CHECK(!test_server_.get());
257   test_server_.reset(new net::SpawnedTestServer(
258       net::SpawnedTestServer::TYPE_HTTP,
259       net::SpawnedTestServer::kLocalhost,
260       test_server_base));
261 }
262
263 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
264     const base::Closure& task) {
265   CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess));
266
267   scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
268
269   base::MessageLoop* renderer_loop =
270       RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
271   CHECK(renderer_loop);
272
273   renderer_loop->PostTask(
274       FROM_HERE,
275       base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
276   runner->Run();
277 }
278
279 }  // namespace content