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/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"
25 #include "base/process/process_handle.h"
28 #if defined(OS_MACOSX)
29 #include "base/mac/mac_util.h"
30 #include "base/power_monitor/power_monitor_device_source.h"
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"
40 #include "content/browser/aura/image_transport_factory.h"
41 #include "ui/compositor/test/test_context_factory.h"
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. "
60 base::debug::StackTrace().Print();
64 #endif // defined(OS_POSIX)
66 void RunTaskOnRendererThread(const base::Closure& task,
67 const base::Closure& quit_task) {
69 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
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
76 class LocalHostResolverProc : public net::HostResolverProc {
78 LocalHostResolverProc() : HostResolverProc(NULL) {}
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"};
88 if (host == net::GetHostName()) {
91 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
92 if (host == kLocalHostNames[i]) {
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.
104 DVLOG(1) << "To avoid external dependencies, simulating failure for "
105 "external DNS lookup of " << host;
106 return net::ERR_NOT_IMPLEMENTED;
109 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
114 virtual ~LocalHostResolverProc() {}
119 extern int BrowserMain(const MainFunctionParams&);
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();
129 #if defined(OS_POSIX)
130 handle_sigterm_ = true;
133 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
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);
144 void BrowserTestBase::SetUp() {
145 CommandLine* command_line = CommandLine::ForCurrentProcess();
147 // The tests assume that file:// URIs can freely access other file:// URIs.
148 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
150 command_line->AppendSwitch(switches::kDomAutomationController);
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);
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;
163 #if defined(USE_AURA)
164 if (command_line->HasSwitch(switches::kDisableTestCompositor))
165 allow_test_contexts_ = false;
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));
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;
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;
184 #if defined(OS_MACOSX)
185 // On Mac we always use a real GPU.
186 allow_osmesa_ = false;
189 #if defined(OS_ANDROID)
190 // On Android we always use a real GPU.
191 allow_osmesa_ = false;
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;
202 if (command_line->HasSwitch(switches::kUseGL)) {
204 "kUseGL should not be used with tests. Try kUseGpuInTests instead.";
208 command_line->AppendSwitchASCII(
209 switches::kUseGL, gfx::kGLImplementationOSMesaName);
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());
220 SetUpInProcessBrowserTestFixture();
222 MainFunctionParams params(*command_line);
225 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
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),
239 TearDownInProcessBrowserTestFixture();
242 void BrowserTestBase::TearDown() {
245 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
246 #if defined(OS_POSIX)
247 if (handle_sigterm_) {
248 g_browser_process_pid = base::GetCurrentProcId();
249 signal(SIGTERM, DumpStackTraceSignalHandler);
251 #endif // defined(OS_POSIX)
252 RunTestOnMainThreadLoop();
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,
263 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
264 const base::Closure& task) {
265 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess));
267 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
269 base::MessageLoop* renderer_loop =
270 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
271 CHECK(renderer_loop);
273 renderer_loop->PostTask(
275 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
279 } // namespace content