blob: 04d6ff0d19ef4ece298c870e33346041343527cb [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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/process_util.h"
11#include "content/public/common/content_switches.h"
12#include "content/public/common/main_function_params.h"
13
14#if defined(OS_MACOSX)
15#include "base/mac/mac_util.h"
16#include "base/system_monitor/system_monitor.h"
17#endif
18
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019#if defined(OS_ANDROID)
20#include "base/threading/thread_restrictions.h"
21#include "content/public/browser/browser_main_runner.h"
22#include "content/public/browser/browser_thread.h"
23#endif
24
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025namespace {
26
27#if defined(OS_POSIX)
28// On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
29// debugging easier) and also exit with a known error code (so that the test
30// framework considers this a failure -- http://crbug.com/57578).
31// Note: We only want to do this in the browser process, and not forked
32// processes. That might lead to hangs because of locks inside tcmalloc or the
33// OS. See http://crbug.com/141302.
34static int g_browser_process_pid;
35static void DumpStackTraceSignalHandler(int signal) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000036 if (g_browser_process_pid == base::GetCurrentProcId()) {
37 logging::RawLog(logging::LOG_ERROR,
38 "BrowserTestBase signal handler received SIGTERM. "
39 "Backtrace:\n");
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040 base::debug::StackTrace().PrintBacktrace();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000041 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000042 _exit(128 + signal);
43}
44#endif // defined(OS_POSIX)
45
46} // namespace
47
48namespace content {
49
50extern int BrowserMain(const content::MainFunctionParams&);
51
52BrowserTestBase::BrowserTestBase() {
53#if defined(OS_MACOSX)
54 base::mac::SetOverrideAmIBundled(true);
55 base::SystemMonitor::AllocateSystemIOPorts();
56#endif
57
58#if defined(OS_POSIX)
59 handle_sigterm_ = true;
60#endif
61}
62
63BrowserTestBase::~BrowserTestBase() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064#if defined(OS_ANDROID)
65 // RemoteTestServer can cause wait on the UI thread.
66 base::ThreadRestrictions::ScopedAllowWait allow_wait;
67 test_server_.reset(NULL);
68#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +000069}
70
71void BrowserTestBase::SetUp() {
72 CommandLine* command_line = CommandLine::ForCurrentProcess();
73
74 // The tests assume that file:// URIs can freely access other file:// URIs.
75 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
76
77 command_line->AppendSwitch(switches::kDomAutomationController);
78
79 command_line->AppendSwitch(switches::kSkipGpuDataLoading);
80
81 MainFunctionParams params(*command_line);
82 params.ui_task =
83 new base::Closure(
84 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
85
86 SetUpInProcessBrowserTestFixture();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000087#if defined(OS_ANDROID)
88 BrowserMainRunner::Create()->Initialize(params);
89 // We are done running the test by now. During teardown we
90 // need to be able to perform IO.
91 base::ThreadRestrictions::SetIOAllowed(true);
92 BrowserThread::PostTask(
93 BrowserThread::IO, FROM_HERE,
94 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
95 true));
96#else
Torne (Richard Coles)58218062012-11-14 11:43:16 +000097 BrowserMain(params);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000098#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +000099 TearDownInProcessBrowserTestFixture();
100}
101
102void BrowserTestBase::TearDown() {
103}
104
105void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
106#if defined(OS_POSIX)
107 if (handle_sigterm_) {
108 g_browser_process_pid = base::GetCurrentProcId();
109 signal(SIGTERM, DumpStackTraceSignalHandler);
110 }
111#endif // defined(OS_POSIX)
112 RunTestOnMainThreadLoop();
113}
114
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000115void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000116 CHECK(!test_server_.get());
117 test_server_.reset(new net::TestServer(
118 net::TestServer::TYPE_HTTP,
119 net::TestServer::kLocalhost,
120 test_server_base));
121}
122
123} // namespace content