blob: e31d9372968ef42fdb3df519a05d824ca265d903 [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"
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000010#include "base/i18n/icu_util.h"
Torne (Richard Coles)58537e22013-09-12 12:10:22 +010011#include "base/message_loop/message_loop.h"
Torne (Richard Coles)6d86b772014-06-25 10:30:53 +010012#include "base/strings/string_number_conversions.h"
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010013#include "base/sys_info.h"
Torne (Richard Coles)6d86b772014-06-25 10:30:53 +010014#include "base/test/test_timeouts.h"
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000015#include "content/public/app/content_main.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010016#include "content/browser/renderer_host/render_process_host_impl.h"
17#include "content/public/browser/browser_thread.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018#include "content/public/common/content_switches.h"
19#include "content/public/common/main_function_params.h"
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000020#include "content/public/test/test_launcher.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010021#include "content/public/test/test_utils.h"
Torne (Richard Coles)68043e12013-09-26 13:24:57 +010022#include "net/base/net_errors.h"
23#include "net/dns/mock_host_resolver.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010024#include "net/test/embedded_test_server/embedded_test_server.h"
Ben Murdochc2db58b2013-08-14 11:51:42 +010025#include "ui/compositor/compositor_switches.h"
26#include "ui/gl/gl_implementation.h"
27#include "ui/gl/gl_switches.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000028
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010029#if defined(OS_POSIX)
30#include "base/process/process_handle.h"
31#endif
32
Torne (Richard Coles)58218062012-11-14 11:43:16 +000033#if defined(OS_MACOSX)
34#include "base/mac/mac_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000035#endif
36
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000037#if defined(OS_ANDROID)
38#include "base/threading/thread_restrictions.h"
39#include "content/public/browser/browser_main_runner.h"
40#include "content/public/browser/browser_thread.h"
41#endif
42
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000043#if defined(USE_AURA)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000044#include "content/browser/compositor/image_transport_factory.h"
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +010045#include "ui/aura/test/event_generator_delegate_aura.h"
Ben Murdoch0529e5d2014-04-24 10:50:13 +010046#if defined(USE_X11)
47#include "ui/aura/window_tree_host_x11.h"
48#endif
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000049#endif
50
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010051namespace content {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052namespace {
53
54#if defined(OS_POSIX)
55// On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
56// debugging easier) and also exit with a known error code (so that the test
57// framework considers this a failure -- http://crbug.com/57578).
58// Note: We only want to do this in the browser process, and not forked
59// processes. That might lead to hangs because of locks inside tcmalloc or the
60// OS. See http://crbug.com/141302.
61static int g_browser_process_pid;
62static void DumpStackTraceSignalHandler(int signal) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063 if (g_browser_process_pid == base::GetCurrentProcId()) {
64 logging::RawLog(logging::LOG_ERROR,
65 "BrowserTestBase signal handler received SIGTERM. "
66 "Backtrace:\n");
Torne (Richard Coles)424c4d72013-08-30 15:14:49 +010067 base::debug::StackTrace().Print();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000068 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000069 _exit(128 + signal);
70}
71#endif // defined(OS_POSIX)
72
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010073void RunTaskOnRendererThread(const base::Closure& task,
74 const base::Closure& quit_task) {
75 task.Run();
76 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
77}
78
Torne (Richard Coles)68043e12013-09-26 13:24:57 +010079// In many cases it may be not obvious that a test makes a real DNS lookup.
80// We generally don't want to rely on external DNS servers for our tests,
81// so this host resolver procedure catches external queries and returns a failed
82// lookup result.
83class LocalHostResolverProc : public net::HostResolverProc {
84 public:
85 LocalHostResolverProc() : HostResolverProc(NULL) {}
86
87 virtual int Resolve(const std::string& host,
88 net::AddressFamily address_family,
89 net::HostResolverFlags host_resolver_flags,
90 net::AddressList* addrlist,
91 int* os_error) OVERRIDE {
92 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
93 bool local = false;
94
95 if (host == net::GetHostName()) {
96 local = true;
97 } else {
98 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
99 if (host == kLocalHostNames[i]) {
100 local = true;
101 break;
102 }
103 }
104
105 // To avoid depending on external resources and to reduce (if not preclude)
106 // network interactions from tests, we simulate failure for non-local DNS
107 // queries, rather than perform them.
108 // If you really need to make an external DNS query, use
109 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
110 if (!local) {
111 DVLOG(1) << "To avoid external dependencies, simulating failure for "
112 "external DNS lookup of " << host;
113 return net::ERR_NOT_IMPLEMENTED;
114 }
115
116 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
117 addrlist, os_error);
118 }
119
120 private:
121 virtual ~LocalHostResolverProc() {}
122};
123
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000124} // namespace
125
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100126extern int BrowserMain(const MainFunctionParams&);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000127
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100128BrowserTestBase::BrowserTestBase()
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100129 : expected_exit_code_(0),
130 enable_pixel_output_(false),
131 use_software_compositing_(false) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000132#if defined(OS_MACOSX)
133 base::mac::SetOverrideAmIBundled(true);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000134#endif
135
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +0100136#if defined(USE_AURA)
137#if defined(USE_X11)
Ben Murdoch0529e5d2014-04-24 10:50:13 +0100138 aura::test::SetUseOverrideRedirectWindowByDefault(true);
139#endif
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +0100140 aura::test::InitializeAuraEventGeneratorDelegate();
141#endif
Ben Murdoch0529e5d2014-04-24 10:50:13 +0100142
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000143#if defined(OS_POSIX)
144 handle_sigterm_ = true;
145#endif
Torne (Richard Coles)58537e22013-09-12 12:10:22 +0100146
Torne (Richard Coles)23730a62014-03-21 14:25:57 +0000147 // This is called through base::TestSuite initially. It'll also be called
148 // inside BrowserMain, so tell the code to ignore the check that it's being
149 // called more than once
150 base::i18n::AllowMultipleInitializeCallsForTesting();
151
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000152 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000153}
154
155BrowserTestBase::~BrowserTestBase() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000156#if defined(OS_ANDROID)
157 // RemoteTestServer can cause wait on the UI thread.
158 base::ThreadRestrictions::ScopedAllowWait allow_wait;
159 test_server_.reset(NULL);
160#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000161}
162
163void BrowserTestBase::SetUp() {
164 CommandLine* command_line = CommandLine::ForCurrentProcess();
165
Torne (Richard Coles)6d86b772014-06-25 10:30:53 +0100166 // Override the child process connection timeout since tests can exceed that
167 // when sharded.
168 command_line->AppendSwitchASCII(
169 switches::kIPCConnectionTimeout,
170 base::IntToString(TestTimeouts::action_max_timeout().InSeconds()));
171
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000172 // The tests assume that file:// URIs can freely access other file:// URIs.
173 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
174
175 command_line->AppendSwitch(switches::kDomAutomationController);
176
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +0100177 // It is sometimes useful when looking at browser test failures to know which
178 // GPU blacklisting decisions were made.
179 command_line->AppendSwitch(switches::kLogGpuControlListDecisions);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000180
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000181 if (use_software_compositing_) {
182 command_line->AppendSwitch(switches::kDisableGpu);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000183#if defined(USE_AURA)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000184 command_line->AppendSwitch(switches::kUIDisableThreadedCompositing);
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000185#endif
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000186 }
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000187
Ben Murdochc2db58b2013-08-14 11:51:42 +0100188#if defined(USE_AURA)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000189 // Most tests do not need pixel output, so we don't produce any. The command
190 // line can override this behaviour to allow for visual debugging.
191 if (command_line->HasSwitch(switches::kEnablePixelOutputInTests))
192 enable_pixel_output_ = true;
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000193
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000194 if (command_line->HasSwitch(switches::kDisableGLDrawingForTests)) {
195 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
196 "is chosen by tests. Use kEnablePixelOutputInTests "
197 "to enable pixel output.";
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000198 }
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000199
200 // Don't enable pixel output for browser tests unless they override and force
201 // us to, or it's requested on the command line.
202 if (!enable_pixel_output_ && !use_software_compositing_)
203 command_line->AppendSwitch(switches::kDisableGLDrawingForTests);
Ben Murdochc2db58b2013-08-14 11:51:42 +0100204#endif
205
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000206 bool use_osmesa = true;
Ben Murdochc2db58b2013-08-14 11:51:42 +0100207
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000208 // We usually use OSMesa as this works on all bots. The command line can
209 // override this behaviour to use hardware GL.
210 if (command_line->HasSwitch(switches::kUseGpuInTests))
211 use_osmesa = false;
212
213 // Some bots pass this flag when they want to use hardware GL.
Ben Murdochc2db58b2013-08-14 11:51:42 +0100214 if (command_line->HasSwitch("enable-gpu"))
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000215 use_osmesa = false;
Ben Murdochc2db58b2013-08-14 11:51:42 +0100216
217#if defined(OS_MACOSX)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000218 // On Mac we always use hardware GL.
219 use_osmesa = false;
Ben Murdochc2db58b2013-08-14 11:51:42 +0100220#endif
221
222#if defined(OS_ANDROID)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000223 // On Android we always use hardware GL.
224 use_osmesa = false;
Ben Murdochc2db58b2013-08-14 11:51:42 +0100225#endif
226
227#if defined(OS_CHROMEOS)
228 // If the test is running on the chromeos envrionment (such as
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000229 // device or vm bots), we use hardware GL.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +0100230 if (base::SysInfo::IsRunningOnChromeOS())
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000231 use_osmesa = false;
Ben Murdochc2db58b2013-08-14 11:51:42 +0100232#endif
233
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100234 if (use_osmesa && !use_software_compositing_)
235 command_line->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests);
Ben Murdochc2db58b2013-08-14 11:51:42 +0100236
Torne (Richard Coles)68043e12013-09-26 13:24:57 +0100237 scoped_refptr<net::HostResolverProc> local_resolver =
238 new LocalHostResolverProc();
239 rule_based_resolver_ =
240 new net::RuleBasedHostResolverProc(local_resolver.get());
241 rule_based_resolver_->AddSimulatedFailure("wpad");
242 net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
243 rule_based_resolver_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000244 SetUpInProcessBrowserTestFixture();
Torne (Richard Coles)23730a62014-03-21 14:25:57 +0000245
246 base::Closure* ui_task =
Torne (Richard Coles)3551c9c2013-08-23 16:39:15 +0100247 new base::Closure(
248 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
249
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000250#if defined(OS_ANDROID)
Torne (Richard Coles)23730a62014-03-21 14:25:57 +0000251 MainFunctionParams params(*command_line);
252 params.ui_task = ui_task;
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100253 // TODO(phajdan.jr): Check return code, http://crbug.com/374738 .
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100254 BrowserMain(params);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000255#else
Torne (Richard Coles)23730a62014-03-21 14:25:57 +0000256 GetContentMainParams()->ui_task = ui_task;
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100257 EXPECT_EQ(expected_exit_code_, ContentMain(*GetContentMainParams()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000258#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000259 TearDownInProcessBrowserTestFixture();
260}
261
262void BrowserTestBase::TearDown() {
263}
264
265void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
266#if defined(OS_POSIX)
267 if (handle_sigterm_) {
268 g_browser_process_pid = base::GetCurrentProcId();
269 signal(SIGTERM, DumpStackTraceSignalHandler);
270 }
271#endif // defined(OS_POSIX)
272 RunTestOnMainThreadLoop();
273}
274
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000275void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000276 CHECK(!test_server_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100277 test_server_.reset(new net::SpawnedTestServer(
278 net::SpawnedTestServer::TYPE_HTTP,
279 net::SpawnedTestServer::kLocalhost,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000280 test_server_base));
281}
282
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100283void BrowserTestBase::PostTaskToInProcessRendererAndWait(
284 const base::Closure& task) {
285 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess));
286
287 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
288
289 base::MessageLoop* renderer_loop =
290 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
291 CHECK(renderer_loop);
292
293 renderer_loop->PostTask(
294 FROM_HERE,
295 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
296 runner->Run();
297}
298
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000299void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_ = true; }
300
301void BrowserTestBase::UseSoftwareCompositing() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000302 use_software_compositing_ = true;
303}
304
305bool BrowserTestBase::UsingOSMesa() const {
306 CommandLine* cmd = CommandLine::ForCurrentProcess();
307 return cmd->GetSwitchValueASCII(switches::kUseGL) ==
308 gfx::kGLImplementationOSMesaName;
309}
310
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000311} // namespace content