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.
5 #include "content/public/test/browser_test_base.h"
8 #include "base/command_line.h"
9 #include "base/debug/stack_trace.h"
10 #include "base/i18n/icu_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/sys_info.h"
13 #include "content/public/app/content_main.h"
14 #include "content/browser/renderer_host/render_process_host_impl.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/main_function_params.h"
18 #include "content/public/test/test_launcher.h"
19 #include "content/public/test/test_utils.h"
20 #include "net/base/net_errors.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h"
23 #include "ui/compositor/compositor_switches.h"
24 #include "ui/gl/gl_implementation.h"
25 #include "ui/gl/gl_switches.h"
28 #include "base/process/process_handle.h"
31 #if defined(OS_MACOSX)
32 #include "base/mac/mac_util.h"
35 #if defined(OS_ANDROID)
36 #include "base/threading/thread_restrictions.h"
37 #include "content/public/browser/browser_main_runner.h"
38 #include "content/public/browser/browser_thread.h"
42 #include "content/browser/compositor/image_transport_factory.h"
44 #include "ui/aura/window_tree_host_x11.h"
52 // On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
53 // debugging easier) and also exit with a known error code (so that the test
54 // framework considers this a failure -- http://crbug.com/57578).
55 // Note: We only want to do this in the browser process, and not forked
56 // processes. That might lead to hangs because of locks inside tcmalloc or the
57 // OS. See http://crbug.com/141302.
58 static int g_browser_process_pid
;
59 static void DumpStackTraceSignalHandler(int signal
) {
60 if (g_browser_process_pid
== base::GetCurrentProcId()) {
61 logging::RawLog(logging::LOG_ERROR
,
62 "BrowserTestBase signal handler received SIGTERM. "
64 base::debug::StackTrace().Print();
68 #endif // defined(OS_POSIX)
70 void RunTaskOnRendererThread(const base::Closure
& task
,
71 const base::Closure
& quit_task
) {
73 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, quit_task
);
76 // In many cases it may be not obvious that a test makes a real DNS lookup.
77 // We generally don't want to rely on external DNS servers for our tests,
78 // so this host resolver procedure catches external queries and returns a failed
80 class LocalHostResolverProc
: public net::HostResolverProc
{
82 LocalHostResolverProc() : HostResolverProc(NULL
) {}
84 virtual int Resolve(const std::string
& host
,
85 net::AddressFamily address_family
,
86 net::HostResolverFlags host_resolver_flags
,
87 net::AddressList
* addrlist
,
88 int* os_error
) OVERRIDE
{
89 const char* kLocalHostNames
[] = {"localhost", "127.0.0.1", "::1"};
92 if (host
== net::GetHostName()) {
95 for (size_t i
= 0; i
< arraysize(kLocalHostNames
); i
++)
96 if (host
== kLocalHostNames
[i
]) {
102 // To avoid depending on external resources and to reduce (if not preclude)
103 // network interactions from tests, we simulate failure for non-local DNS
104 // queries, rather than perform them.
105 // If you really need to make an external DNS query, use
106 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
108 DVLOG(1) << "To avoid external dependencies, simulating failure for "
109 "external DNS lookup of " << host
;
110 return net::ERR_NOT_IMPLEMENTED
;
113 return ResolveUsingPrevious(host
, address_family
, host_resolver_flags
,
118 virtual ~LocalHostResolverProc() {}
123 extern int BrowserMain(const MainFunctionParams
&);
125 BrowserTestBase::BrowserTestBase()
126 : enable_pixel_output_(false), use_software_compositing_(false) {
127 #if defined(OS_MACOSX)
128 base::mac::SetOverrideAmIBundled(true);
131 #if defined(USE_AURA) && defined(USE_X11)
132 aura::test::SetUseOverrideRedirectWindowByDefault(true);
135 #if defined(OS_POSIX)
136 handle_sigterm_
= true;
139 // This is called through base::TestSuite initially. It'll also be called
140 // inside BrowserMain, so tell the code to ignore the check that it's being
141 // called more than once
142 base::i18n::AllowMultipleInitializeCallsForTesting();
144 embedded_test_server_
.reset(new net::test_server::EmbeddedTestServer
);
147 BrowserTestBase::~BrowserTestBase() {
148 #if defined(OS_ANDROID)
149 // RemoteTestServer can cause wait on the UI thread.
150 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
151 test_server_
.reset(NULL
);
155 void BrowserTestBase::SetUp() {
156 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
158 // The tests assume that file:// URIs can freely access other file:// URIs.
159 command_line
->AppendSwitch(switches::kAllowFileAccessFromFiles
);
161 command_line
->AppendSwitch(switches::kDomAutomationController
);
163 // It is sometimes useful when looking at browser test failures to know which
164 // GPU blacklisting decisions were made.
165 command_line
->AppendSwitch(switches::kLogGpuControlListDecisions
);
167 if (use_software_compositing_
) {
168 command_line
->AppendSwitch(switches::kDisableGpu
);
169 #if defined(USE_AURA)
170 command_line
->AppendSwitch(switches::kUIDisableThreadedCompositing
);
174 #if defined(USE_AURA)
175 // Most tests do not need pixel output, so we don't produce any. The command
176 // line can override this behaviour to allow for visual debugging.
177 if (command_line
->HasSwitch(switches::kEnablePixelOutputInTests
))
178 enable_pixel_output_
= true;
180 if (command_line
->HasSwitch(switches::kDisableGLDrawingForTests
)) {
181 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
182 "is chosen by tests. Use kEnablePixelOutputInTests "
183 "to enable pixel output.";
186 // Don't enable pixel output for browser tests unless they override and force
187 // us to, or it's requested on the command line.
188 if (!enable_pixel_output_
&& !use_software_compositing_
)
189 command_line
->AppendSwitch(switches::kDisableGLDrawingForTests
);
192 bool use_osmesa
= true;
194 // We usually use OSMesa as this works on all bots. The command line can
195 // override this behaviour to use hardware GL.
196 if (command_line
->HasSwitch(switches::kUseGpuInTests
))
199 // Some bots pass this flag when they want to use hardware GL.
200 if (command_line
->HasSwitch("enable-gpu"))
203 #if defined(OS_MACOSX)
204 // On Mac we always use hardware GL.
208 #if defined(OS_ANDROID)
209 // On Android we always use hardware GL.
213 #if defined(OS_CHROMEOS)
214 // If the test is running on the chromeos envrionment (such as
215 // device or vm bots), we use hardware GL.
216 if (base::SysInfo::IsRunningOnChromeOS())
220 if (use_osmesa
&& !use_software_compositing_
)
221 command_line
->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests
);
223 scoped_refptr
<net::HostResolverProc
> local_resolver
=
224 new LocalHostResolverProc();
225 rule_based_resolver_
=
226 new net::RuleBasedHostResolverProc(local_resolver
.get());
227 rule_based_resolver_
->AddSimulatedFailure("wpad");
228 net::ScopedDefaultHostResolverProc
scoped_local_host_resolver_proc(
229 rule_based_resolver_
.get());
230 SetUpInProcessBrowserTestFixture();
232 base::Closure
* ui_task
=
234 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop
, this));
236 #if defined(OS_ANDROID)
237 MainFunctionParams
params(*command_line
);
238 params
.ui_task
= ui_task
;
239 BrowserMainRunner::Create()->Initialize(params
);
240 // We are done running the test by now. During teardown we
241 // need to be able to perform IO.
242 base::ThreadRestrictions::SetIOAllowed(true);
243 BrowserThread::PostTask(
244 BrowserThread::IO
, FROM_HERE
,
245 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed
),
248 GetContentMainParams()->ui_task
= ui_task
;
249 ContentMain(*GetContentMainParams());
251 TearDownInProcessBrowserTestFixture();
254 void BrowserTestBase::TearDown() {
257 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
258 #if defined(OS_POSIX)
259 if (handle_sigterm_
) {
260 g_browser_process_pid
= base::GetCurrentProcId();
261 signal(SIGTERM
, DumpStackTraceSignalHandler
);
263 #endif // defined(OS_POSIX)
264 RunTestOnMainThreadLoop();
267 void BrowserTestBase::CreateTestServer(const base::FilePath
& test_server_base
) {
268 CHECK(!test_server_
.get());
269 test_server_
.reset(new net::SpawnedTestServer(
270 net::SpawnedTestServer::TYPE_HTTP
,
271 net::SpawnedTestServer::kLocalhost
,
275 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
276 const base::Closure
& task
) {
277 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
));
279 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
281 base::MessageLoop
* renderer_loop
=
282 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
283 CHECK(renderer_loop
);
285 renderer_loop
->PostTask(
287 base::Bind(&RunTaskOnRendererThread
, task
, runner
->QuitClosure()));
291 void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_
= true; }
293 void BrowserTestBase::UseSoftwareCompositing() {
294 #if !defined(USE_AURA) && !defined(OS_MACOSX)
295 // TODO(danakj): Remove when GTK linux is no more.
298 use_software_compositing_
= true;
301 bool BrowserTestBase::UsingOSMesa() const {
302 CommandLine
* cmd
= CommandLine::ForCurrentProcess();
303 return cmd
->GetSwitchValueASCII(switches::kUseGL
) ==
304 gfx::kGLImplementationOSMesaName
;
307 } // namespace content