Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / public / test / browser_test_base.cc
blob3455988be8118171d5b64d56e4430329ccce27c6
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"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/debug/stack_trace.h"
10 #include "base/i18n/icu_util.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/sys_info.h"
15 #include "base/test/test_timeouts.h"
16 #include "content/browser/renderer_host/render_process_host_impl.h"
17 #include "content/browser/tracing/tracing_controller_impl.h"
18 #include "content/public/app/content_main.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/main_function_params.h"
22 #include "content/public/test/test_launcher.h"
23 #include "content/public/test/test_utils.h"
24 #include "content/test/content_browser_sanity_checker.h"
25 #include "net/base/net_errors.h"
26 #include "net/dns/mock_host_resolver.h"
27 #include "net/test/embedded_test_server/embedded_test_server.h"
28 #include "ui/compositor/compositor_switches.h"
29 #include "ui/gl/gl_implementation.h"
30 #include "ui/gl/gl_switches.h"
32 #if defined(OS_POSIX)
33 #include "base/process/process_handle.h"
34 #endif
36 #if defined(OS_MACOSX)
37 #include "base/mac/foundation_util.h"
38 #endif
40 #if defined(OS_ANDROID)
41 #include "base/threading/thread_restrictions.h"
42 #include "content/public/browser/browser_main_runner.h"
43 #include "content/public/browser/browser_thread.h"
44 #endif
46 #if defined(USE_AURA)
47 #include "content/browser/compositor/image_transport_factory.h"
48 #include "ui/aura/test/event_generator_delegate_aura.h"
49 #if defined(USE_X11)
50 #include "ui/aura/window_tree_host_x11.h"
51 #endif
52 #endif
54 namespace content {
55 namespace {
57 #if defined(OS_POSIX)
58 // On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
59 // debugging easier) and also exit with a known error code (so that the test
60 // framework considers this a failure -- http://crbug.com/57578).
61 // Note: We only want to do this in the browser process, and not forked
62 // processes. That might lead to hangs because of locks inside tcmalloc or the
63 // OS. See http://crbug.com/141302.
64 static int g_browser_process_pid;
65 static void DumpStackTraceSignalHandler(int signal) {
66 if (g_browser_process_pid == base::GetCurrentProcId()) {
67 logging::RawLog(logging::LOG_ERROR,
68 "BrowserTestBase signal handler received SIGTERM. "
69 "Backtrace:\n");
70 base::debug::StackTrace().Print();
72 _exit(128 + signal);
74 #endif // defined(OS_POSIX)
76 void RunTaskOnRendererThread(const base::Closure& task,
77 const base::Closure& quit_task) {
78 task.Run();
79 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
82 // In many cases it may be not obvious that a test makes a real DNS lookup.
83 // We generally don't want to rely on external DNS servers for our tests,
84 // so this host resolver procedure catches external queries and returns a failed
85 // lookup result.
86 class LocalHostResolverProc : public net::HostResolverProc {
87 public:
88 LocalHostResolverProc() : HostResolverProc(NULL) {}
90 int Resolve(const std::string& host,
91 net::AddressFamily address_family,
92 net::HostResolverFlags host_resolver_flags,
93 net::AddressList* addrlist,
94 int* os_error) override {
95 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
96 bool local = false;
98 if (host == net::GetHostName()) {
99 local = true;
100 } else {
101 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
102 if (host == kLocalHostNames[i]) {
103 local = true;
104 break;
108 // To avoid depending on external resources and to reduce (if not preclude)
109 // network interactions from tests, we simulate failure for non-local DNS
110 // queries, rather than perform them.
111 // If you really need to make an external DNS query, use
112 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
113 if (!local) {
114 DVLOG(1) << "To avoid external dependencies, simulating failure for "
115 "external DNS lookup of " << host;
116 return net::ERR_NOT_IMPLEMENTED;
119 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
120 addrlist, os_error);
123 private:
124 ~LocalHostResolverProc() override {}
127 void TraceDisableRecordingComplete(const base::Closure& quit,
128 const base::FilePath& file_path) {
129 LOG(ERROR) << "Tracing written to: " << file_path.value();
130 quit.Run();
133 } // namespace
135 extern int BrowserMain(const MainFunctionParams&);
137 BrowserTestBase::BrowserTestBase()
138 : expected_exit_code_(0),
139 enable_pixel_output_(false),
140 use_software_compositing_(false),
141 set_up_called_(false) {
142 #if defined(OS_MACOSX)
143 base::mac::SetOverrideAmIBundled(true);
144 #endif
146 #if defined(USE_AURA)
147 #if defined(USE_X11)
148 aura::test::SetUseOverrideRedirectWindowByDefault(true);
149 #endif
150 aura::test::InitializeAuraEventGeneratorDelegate();
151 #endif
153 #if defined(OS_POSIX)
154 handle_sigterm_ = true;
155 #endif
157 // This is called through base::TestSuite initially. It'll also be called
158 // inside BrowserMain, so tell the code to ignore the check that it's being
159 // called more than once
160 base::i18n::AllowMultipleInitializeCallsForTesting();
162 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
165 BrowserTestBase::~BrowserTestBase() {
166 #if defined(OS_ANDROID)
167 // RemoteTestServer can cause wait on the UI thread.
168 base::ThreadRestrictions::ScopedAllowWait allow_wait;
169 test_server_.reset(NULL);
170 #endif
172 CHECK(set_up_called_) << "SetUp was not called. This probably means that the "
173 "developer has overridden the method and not called "
174 "the superclass version. In this case, the test "
175 "does not run and reports a false positive result.";
178 void BrowserTestBase::SetUp() {
179 set_up_called_ = true;
181 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
183 // Override the child process connection timeout since tests can exceed that
184 // when sharded.
185 command_line->AppendSwitchASCII(
186 switches::kIPCConnectionTimeout,
187 base::IntToString(TestTimeouts::action_max_timeout().InSeconds()));
189 // The tests assume that file:// URIs can freely access other file:// URIs.
190 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
192 command_line->AppendSwitch(switches::kDomAutomationController);
194 // It is sometimes useful when looking at browser test failures to know which
195 // GPU blacklisting decisions were made.
196 command_line->AppendSwitch(switches::kLogGpuControlListDecisions);
198 if (use_software_compositing_) {
199 command_line->AppendSwitch(switches::kDisableGpu);
200 #if defined(USE_AURA)
201 command_line->AppendSwitch(switches::kUIDisableThreadedCompositing);
202 #endif
205 #if defined(USE_AURA)
206 // Most tests do not need pixel output, so we don't produce any. The command
207 // line can override this behaviour to allow for visual debugging.
208 if (command_line->HasSwitch(switches::kEnablePixelOutputInTests))
209 enable_pixel_output_ = true;
211 if (command_line->HasSwitch(switches::kDisableGLDrawingForTests)) {
212 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
213 "is chosen by tests. Use kEnablePixelOutputInTests "
214 "to enable pixel output.";
217 // Don't enable pixel output for browser tests unless they override and force
218 // us to, or it's requested on the command line.
219 if (!enable_pixel_output_ && !use_software_compositing_)
220 command_line->AppendSwitch(switches::kDisableGLDrawingForTests);
221 #endif
223 bool use_osmesa = true;
225 // We usually use OSMesa as this works on all bots. The command line can
226 // override this behaviour to use hardware GL.
227 if (command_line->HasSwitch(switches::kUseGpuInTests))
228 use_osmesa = false;
230 // Some bots pass this flag when they want to use hardware GL.
231 if (command_line->HasSwitch("enable-gpu"))
232 use_osmesa = false;
234 #if defined(OS_MACOSX)
235 // On Mac we always use hardware GL.
236 use_osmesa = false;
237 #endif
239 #if defined(OS_ANDROID)
240 // On Android we always use hardware GL.
241 use_osmesa = false;
242 #endif
244 #if defined(OS_CHROMEOS)
245 // If the test is running on the chromeos envrionment (such as
246 // device or vm bots), we use hardware GL.
247 if (base::SysInfo::IsRunningOnChromeOS())
248 use_osmesa = false;
249 #endif
251 if (use_osmesa && !use_software_compositing_)
252 command_line->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests);
254 scoped_refptr<net::HostResolverProc> local_resolver =
255 new LocalHostResolverProc();
256 rule_based_resolver_ =
257 new net::RuleBasedHostResolverProc(local_resolver.get());
258 rule_based_resolver_->AddSimulatedFailure("wpad");
259 net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
260 rule_based_resolver_.get());
262 ContentBrowserSanityChecker scoped_enable_sanity_checks;
264 SetUpInProcessBrowserTestFixture();
266 base::Closure* ui_task =
267 new base::Closure(
268 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
270 #if defined(OS_ANDROID)
271 MainFunctionParams params(*command_line);
272 params.ui_task = ui_task;
273 // TODO(phajdan.jr): Check return code, http://crbug.com/374738 .
274 BrowserMain(params);
275 #else
276 GetContentMainParams()->ui_task = ui_task;
277 EXPECT_EQ(expected_exit_code_, ContentMain(*GetContentMainParams()));
278 #endif
279 TearDownInProcessBrowserTestFixture();
282 void BrowserTestBase::TearDown() {
285 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
286 #if defined(OS_POSIX)
287 if (handle_sigterm_) {
288 g_browser_process_pid = base::GetCurrentProcId();
289 signal(SIGTERM, DumpStackTraceSignalHandler);
291 #endif // defined(OS_POSIX)
293 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
294 switches::kEnableTracing)) {
295 base::trace_event::TraceConfig trace_config(
296 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
297 switches::kEnableTracing),
298 base::trace_event::RECORD_CONTINUOUSLY);
299 TracingController::GetInstance()->EnableRecording(
300 trace_config,
301 TracingController::EnableRecordingDoneCallback());
304 RunTestOnMainThreadLoop();
306 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
307 switches::kEnableTracing)) {
308 base::FilePath trace_file =
309 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
310 switches::kEnableTracingOutput);
311 // If there was no file specified, put a hardcoded one in the current
312 // working directory.
313 if (trace_file.empty())
314 trace_file = base::FilePath().AppendASCII("trace.json");
316 // Wait for tracing to collect results from the renderers.
317 base::RunLoop run_loop;
318 TracingController::GetInstance()->DisableRecording(
319 TracingControllerImpl::CreateFileSink(
320 trace_file,
321 base::Bind(&TraceDisableRecordingComplete,
322 run_loop.QuitClosure(),
323 trace_file)));
324 run_loop.Run();
328 void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
329 CHECK(!test_server_.get());
330 test_server_.reset(new net::SpawnedTestServer(
331 net::SpawnedTestServer::TYPE_HTTP,
332 net::SpawnedTestServer::kLocalhost,
333 test_server_base));
336 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
337 const base::Closure& task) {
338 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
339 switches::kSingleProcess));
341 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
343 base::MessageLoop* renderer_loop =
344 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
345 CHECK(renderer_loop);
347 renderer_loop->task_runner()->PostTask(
348 FROM_HERE,
349 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
350 runner->Run();
353 void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_ = true; }
355 void BrowserTestBase::UseSoftwareCompositing() {
356 use_software_compositing_ = true;
359 bool BrowserTestBase::UsingOSMesa() const {
360 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
361 return cmd->GetSwitchValueASCII(switches::kUseGL) ==
362 gfx::kGLImplementationOSMesaName;
365 } // namespace content