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;
DumpStackTraceSignalHandler(int signal)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
RunTaskOnRendererThread(const base::Closure & task,const base::Closure & quit_task)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:
LocalHostResolverProc()78 LocalHostResolverProc() : HostResolverProc(NULL) {}
79
Resolve(const std::string & host,net::AddressFamily address_family,net::HostResolverFlags host_resolver_flags,net::AddressList * addrlist,int * os_error)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:
~LocalHostResolverProc()114 virtual ~LocalHostResolverProc() {}
115 };
116
117 } // namespace
118
119 extern int BrowserMain(const MainFunctionParams&);
120
BrowserTestBase()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
~BrowserTestBase()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
SetUp()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 SetUpInProcessBrowserTestFixture();
220 MainFunctionParams params(*command_line);
221 params.ui_task =
222 new base::Closure(
223 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
224
225 #if defined(OS_ANDROID)
226 BrowserMainRunner::Create()->Initialize(params);
227 // We are done running the test by now. During teardown we
228 // need to be able to perform IO.
229 base::ThreadRestrictions::SetIOAllowed(true);
230 BrowserThread::PostTask(
231 BrowserThread::IO, FROM_HERE,
232 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
233 true));
234 #else
235 BrowserMain(params);
236 #endif
237 TearDownInProcessBrowserTestFixture();
238 }
239
TearDown()240 void BrowserTestBase::TearDown() {
241 }
242
ProxyRunTestOnMainThreadLoop()243 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
244 #if defined(OS_POSIX)
245 if (handle_sigterm_) {
246 g_browser_process_pid = base::GetCurrentProcId();
247 signal(SIGTERM, DumpStackTraceSignalHandler);
248 }
249 #endif // defined(OS_POSIX)
250 RunTestOnMainThreadLoop();
251 }
252
CreateTestServer(const base::FilePath & test_server_base)253 void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
254 CHECK(!test_server_.get());
255 test_server_.reset(new net::SpawnedTestServer(
256 net::SpawnedTestServer::TYPE_HTTP,
257 net::SpawnedTestServer::kLocalhost,
258 test_server_base));
259 }
260
PostTaskToInProcessRendererAndWait(const base::Closure & task)261 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
262 const base::Closure& task) {
263 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess));
264
265 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
266
267 base::MessageLoop* renderer_loop =
268 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
269 CHECK(renderer_loop);
270
271 renderer_loop->PostTask(
272 FROM_HERE,
273 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
274 runner->Run();
275 }
276
277 } // namespace content
278