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/strings/string_number_conversions.h"
13 #include "base/sys_info.h"
14 #include "base/test/test_timeouts.h"
15 #include "content/browser/renderer_host/render_process_host_impl.h"
16 #include "content/browser/tracing/tracing_controller_impl.h"
17 #include "content/public/app/content_main.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/main_function_params.h"
21 #include "content/public/test/content_browser_sanity_checker.h"
22 #include "content/public/test/test_launcher.h"
23 #include "content/public/test/test_utils.h"
24 #include "net/base/net_errors.h"
25 #include "net/dns/mock_host_resolver.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "ui/compositor/compositor_switches.h"
28 #include "ui/gl/gl_implementation.h"
29 #include "ui/gl/gl_switches.h"
32 #include "base/process/process_handle.h"
35 #if defined(OS_MACOSX)
36 #include "base/mac/foundation_util.h"
39 #if defined(OS_ANDROID)
40 #include "base/threading/thread_restrictions.h"
41 #include "content/public/browser/browser_main_runner.h"
42 #include "content/public/browser/browser_thread.h"
46 #include "content/browser/compositor/image_transport_factory.h"
47 #include "ui/aura/test/event_generator_delegate_aura.h"
49 #include "ui/aura/window_tree_host_x11.h"
57 // On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
58 // debugging easier) and also exit with a known error code (so that the test
59 // framework considers this a failure -- http://crbug.com/57578).
60 // Note: We only want to do this in the browser process, and not forked
61 // processes. That might lead to hangs because of locks inside tcmalloc or the
62 // OS. See http://crbug.com/141302.
63 static int g_browser_process_pid
;
64 static void DumpStackTraceSignalHandler(int signal
) {
65 if (g_browser_process_pid
== base::GetCurrentProcId()) {
66 logging::RawLog(logging::LOG_ERROR
,
67 "BrowserTestBase signal handler received SIGTERM. "
69 base::debug::StackTrace().Print();
73 #endif // defined(OS_POSIX)
75 void RunTaskOnRendererThread(const base::Closure
& task
,
76 const base::Closure
& quit_task
) {
78 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, quit_task
);
81 // In many cases it may be not obvious that a test makes a real DNS lookup.
82 // We generally don't want to rely on external DNS servers for our tests,
83 // so this host resolver procedure catches external queries and returns a failed
85 class LocalHostResolverProc
: public net::HostResolverProc
{
87 LocalHostResolverProc() : HostResolverProc(NULL
) {}
89 int Resolve(const std::string
& host
,
90 net::AddressFamily address_family
,
91 net::HostResolverFlags host_resolver_flags
,
92 net::AddressList
* addrlist
,
93 int* os_error
) override
{
94 const char* kLocalHostNames
[] = {"localhost", "127.0.0.1", "::1"};
97 if (host
== net::GetHostName()) {
100 for (size_t i
= 0; i
< arraysize(kLocalHostNames
); i
++)
101 if (host
== kLocalHostNames
[i
]) {
107 // To avoid depending on external resources and to reduce (if not preclude)
108 // network interactions from tests, we simulate failure for non-local DNS
109 // queries, rather than perform them.
110 // If you really need to make an external DNS query, use
111 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
113 DVLOG(1) << "To avoid external dependencies, simulating failure for "
114 "external DNS lookup of " << host
;
115 return net::ERR_NOT_IMPLEMENTED
;
118 return ResolveUsingPrevious(host
, address_family
, host_resolver_flags
,
123 ~LocalHostResolverProc() override
{}
126 void TraceDisableRecordingComplete(const base::Closure
& quit
,
127 const base::FilePath
& file_path
) {
128 LOG(ERROR
) << "Tracing written to: " << file_path
.value();
134 extern int BrowserMain(const MainFunctionParams
&);
136 BrowserTestBase::BrowserTestBase()
137 : expected_exit_code_(0),
138 enable_pixel_output_(false),
139 use_software_compositing_(false) {
140 #if defined(OS_MACOSX)
141 base::mac::SetOverrideAmIBundled(true);
144 #if defined(USE_AURA)
146 aura::test::SetUseOverrideRedirectWindowByDefault(true);
148 aura::test::InitializeAuraEventGeneratorDelegate();
151 #if defined(OS_POSIX)
152 handle_sigterm_
= true;
155 // This is called through base::TestSuite initially. It'll also be called
156 // inside BrowserMain, so tell the code to ignore the check that it's being
157 // called more than once
158 base::i18n::AllowMultipleInitializeCallsForTesting();
160 embedded_test_server_
.reset(new net::test_server::EmbeddedTestServer
);
163 BrowserTestBase::~BrowserTestBase() {
164 #if defined(OS_ANDROID)
165 // RemoteTestServer can cause wait on the UI thread.
166 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
167 test_server_
.reset(NULL
);
171 void BrowserTestBase::SetUp() {
172 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
174 // Override the child process connection timeout since tests can exceed that
176 command_line
->AppendSwitchASCII(
177 switches::kIPCConnectionTimeout
,
178 base::IntToString(TestTimeouts::action_max_timeout().InSeconds()));
180 // The tests assume that file:// URIs can freely access other file:// URIs.
181 command_line
->AppendSwitch(switches::kAllowFileAccessFromFiles
);
183 command_line
->AppendSwitch(switches::kDomAutomationController
);
185 // It is sometimes useful when looking at browser test failures to know which
186 // GPU blacklisting decisions were made.
187 command_line
->AppendSwitch(switches::kLogGpuControlListDecisions
);
189 if (use_software_compositing_
) {
190 command_line
->AppendSwitch(switches::kDisableGpu
);
191 #if defined(USE_AURA)
192 command_line
->AppendSwitch(switches::kUIDisableThreadedCompositing
);
196 #if defined(USE_AURA)
197 // Most tests do not need pixel output, so we don't produce any. The command
198 // line can override this behaviour to allow for visual debugging.
199 if (command_line
->HasSwitch(switches::kEnablePixelOutputInTests
))
200 enable_pixel_output_
= true;
202 if (command_line
->HasSwitch(switches::kDisableGLDrawingForTests
)) {
203 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
204 "is chosen by tests. Use kEnablePixelOutputInTests "
205 "to enable pixel output.";
208 // Don't enable pixel output for browser tests unless they override and force
209 // us to, or it's requested on the command line.
210 if (!enable_pixel_output_
&& !use_software_compositing_
)
211 command_line
->AppendSwitch(switches::kDisableGLDrawingForTests
);
214 bool use_osmesa
= true;
216 // We usually use OSMesa as this works on all bots. The command line can
217 // override this behaviour to use hardware GL.
218 if (command_line
->HasSwitch(switches::kUseGpuInTests
))
221 // Some bots pass this flag when they want to use hardware GL.
222 if (command_line
->HasSwitch("enable-gpu"))
225 #if defined(OS_MACOSX)
226 // On Mac we always use hardware GL.
230 #if defined(OS_ANDROID)
231 // On Android we always use hardware GL.
235 #if defined(OS_CHROMEOS)
236 // If the test is running on the chromeos envrionment (such as
237 // device or vm bots), we use hardware GL.
238 if (base::SysInfo::IsRunningOnChromeOS())
242 if (use_osmesa
&& !use_software_compositing_
)
243 command_line
->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests
);
245 scoped_refptr
<net::HostResolverProc
> local_resolver
=
246 new LocalHostResolverProc();
247 rule_based_resolver_
=
248 new net::RuleBasedHostResolverProc(local_resolver
.get());
249 rule_based_resolver_
->AddSimulatedFailure("wpad");
250 net::ScopedDefaultHostResolverProc
scoped_local_host_resolver_proc(
251 rule_based_resolver_
.get());
253 ContentBrowserSanityChecker scoped_enable_sanity_checks
;
255 SetUpInProcessBrowserTestFixture();
257 base::Closure
* ui_task
=
259 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop
, this));
261 #if defined(OS_ANDROID)
262 MainFunctionParams
params(*command_line
);
263 params
.ui_task
= ui_task
;
264 // TODO(phajdan.jr): Check return code, http://crbug.com/374738 .
267 GetContentMainParams()->ui_task
= ui_task
;
268 EXPECT_EQ(expected_exit_code_
, ContentMain(*GetContentMainParams()));
270 TearDownInProcessBrowserTestFixture();
273 void BrowserTestBase::TearDown() {
276 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
277 #if defined(OS_POSIX)
278 if (handle_sigterm_
) {
279 g_browser_process_pid
= base::GetCurrentProcId();
280 signal(SIGTERM
, DumpStackTraceSignalHandler
);
282 #endif // defined(OS_POSIX)
284 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
285 switches::kEnableTracing
)) {
286 base::trace_event::CategoryFilter
category_filter(
287 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
288 switches::kEnableTracing
));
289 TracingController::GetInstance()->EnableRecording(
291 base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY
),
292 TracingController::EnableRecordingDoneCallback());
295 RunTestOnMainThreadLoop();
297 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
298 switches::kEnableTracing
)) {
299 base::FilePath trace_file
=
300 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
301 switches::kEnableTracingOutput
);
302 // If there was no file specified, put a hardcoded one in the current
303 // working directory.
304 if (trace_file
.empty())
305 trace_file
= base::FilePath().AppendASCII("trace.json");
307 // Wait for tracing to collect results from the renderers.
308 base::RunLoop run_loop
;
309 TracingController::GetInstance()->DisableRecording(
310 TracingControllerImpl::CreateFileSink(
312 base::Bind(&TraceDisableRecordingComplete
,
313 run_loop
.QuitClosure(),
319 void BrowserTestBase::CreateTestServer(const base::FilePath
& test_server_base
) {
320 CHECK(!test_server_
.get());
321 test_server_
.reset(new net::SpawnedTestServer(
322 net::SpawnedTestServer::TYPE_HTTP
,
323 net::SpawnedTestServer::kLocalhost
,
327 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
328 const base::Closure
& task
) {
329 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
330 switches::kSingleProcess
));
332 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
334 base::MessageLoop
* renderer_loop
=
335 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
336 CHECK(renderer_loop
);
338 renderer_loop
->PostTask(
340 base::Bind(&RunTaskOnRendererThread
, task
, runner
->QuitClosure()));
344 void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_
= true; }
346 void BrowserTestBase::UseSoftwareCompositing() {
347 use_software_compositing_
= true;
350 bool BrowserTestBase::UsingOSMesa() const {
351 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
352 return cmd
->GetSwitchValueASCII(switches::kUseGL
) ==
353 gfx::kGLImplementationOSMesaName
;
356 } // namespace content