[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / public / test / browser_test_base.cc
blobc5078fc19b525c20c2d024dbd88b489c8003d30c
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 #if defined(OS_MACOSX)
142 base::mac::SetOverrideAmIBundled(true);
143 #endif
145 #if defined(USE_AURA)
146 #if defined(USE_X11)
147 aura::test::SetUseOverrideRedirectWindowByDefault(true);
148 #endif
149 aura::test::InitializeAuraEventGeneratorDelegate();
150 #endif
152 #if defined(OS_POSIX)
153 handle_sigterm_ = true;
154 #endif
156 // This is called through base::TestSuite initially. It'll also be called
157 // inside BrowserMain, so tell the code to ignore the check that it's being
158 // called more than once
159 base::i18n::AllowMultipleInitializeCallsForTesting();
161 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
164 BrowserTestBase::~BrowserTestBase() {
165 #if defined(OS_ANDROID)
166 // RemoteTestServer can cause wait on the UI thread.
167 base::ThreadRestrictions::ScopedAllowWait allow_wait;
168 test_server_.reset(NULL);
169 #endif
172 void BrowserTestBase::SetUp() {
173 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
175 // Override the child process connection timeout since tests can exceed that
176 // when sharded.
177 command_line->AppendSwitchASCII(
178 switches::kIPCConnectionTimeout,
179 base::IntToString(TestTimeouts::action_max_timeout().InSeconds()));
181 // The tests assume that file:// URIs can freely access other file:// URIs.
182 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
184 command_line->AppendSwitch(switches::kDomAutomationController);
186 // It is sometimes useful when looking at browser test failures to know which
187 // GPU blacklisting decisions were made.
188 command_line->AppendSwitch(switches::kLogGpuControlListDecisions);
190 if (use_software_compositing_) {
191 command_line->AppendSwitch(switches::kDisableGpu);
192 #if defined(USE_AURA)
193 command_line->AppendSwitch(switches::kUIDisableThreadedCompositing);
194 #endif
197 #if defined(USE_AURA)
198 // Most tests do not need pixel output, so we don't produce any. The command
199 // line can override this behaviour to allow for visual debugging.
200 if (command_line->HasSwitch(switches::kEnablePixelOutputInTests))
201 enable_pixel_output_ = true;
203 if (command_line->HasSwitch(switches::kDisableGLDrawingForTests)) {
204 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
205 "is chosen by tests. Use kEnablePixelOutputInTests "
206 "to enable pixel output.";
209 // Don't enable pixel output for browser tests unless they override and force
210 // us to, or it's requested on the command line.
211 if (!enable_pixel_output_ && !use_software_compositing_)
212 command_line->AppendSwitch(switches::kDisableGLDrawingForTests);
213 #endif
215 bool use_osmesa = true;
217 // We usually use OSMesa as this works on all bots. The command line can
218 // override this behaviour to use hardware GL.
219 if (command_line->HasSwitch(switches::kUseGpuInTests))
220 use_osmesa = false;
222 // Some bots pass this flag when they want to use hardware GL.
223 if (command_line->HasSwitch("enable-gpu"))
224 use_osmesa = false;
226 #if defined(OS_MACOSX)
227 // On Mac we always use hardware GL.
228 use_osmesa = false;
229 #endif
231 #if defined(OS_ANDROID)
232 // On Android we always use hardware GL.
233 use_osmesa = false;
234 #endif
236 #if defined(OS_CHROMEOS)
237 // If the test is running on the chromeos envrionment (such as
238 // device or vm bots), we use hardware GL.
239 if (base::SysInfo::IsRunningOnChromeOS())
240 use_osmesa = false;
241 #endif
243 if (use_osmesa && !use_software_compositing_)
244 command_line->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests);
246 scoped_refptr<net::HostResolverProc> local_resolver =
247 new LocalHostResolverProc();
248 rule_based_resolver_ =
249 new net::RuleBasedHostResolverProc(local_resolver.get());
250 rule_based_resolver_->AddSimulatedFailure("wpad");
251 net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
252 rule_based_resolver_.get());
254 ContentBrowserSanityChecker scoped_enable_sanity_checks;
256 SetUpInProcessBrowserTestFixture();
258 base::Closure* ui_task =
259 new base::Closure(
260 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
262 #if defined(OS_ANDROID)
263 MainFunctionParams params(*command_line);
264 params.ui_task = ui_task;
265 // TODO(phajdan.jr): Check return code, http://crbug.com/374738 .
266 BrowserMain(params);
267 #else
268 GetContentMainParams()->ui_task = ui_task;
269 EXPECT_EQ(expected_exit_code_, ContentMain(*GetContentMainParams()));
270 #endif
271 TearDownInProcessBrowserTestFixture();
274 void BrowserTestBase::TearDown() {
277 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
278 #if defined(OS_POSIX)
279 if (handle_sigterm_) {
280 g_browser_process_pid = base::GetCurrentProcId();
281 signal(SIGTERM, DumpStackTraceSignalHandler);
283 #endif // defined(OS_POSIX)
285 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
286 switches::kEnableTracing)) {
287 base::trace_event::TraceConfig trace_config(
288 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
289 switches::kEnableTracing),
290 base::trace_event::RECORD_CONTINUOUSLY);
291 TracingController::GetInstance()->EnableRecording(
292 trace_config,
293 TracingController::EnableRecordingDoneCallback());
296 RunTestOnMainThreadLoop();
298 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
299 switches::kEnableTracing)) {
300 base::FilePath trace_file =
301 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
302 switches::kEnableTracingOutput);
303 // If there was no file specified, put a hardcoded one in the current
304 // working directory.
305 if (trace_file.empty())
306 trace_file = base::FilePath().AppendASCII("trace.json");
308 // Wait for tracing to collect results from the renderers.
309 base::RunLoop run_loop;
310 TracingController::GetInstance()->DisableRecording(
311 TracingControllerImpl::CreateFileSink(
312 trace_file,
313 base::Bind(&TraceDisableRecordingComplete,
314 run_loop.QuitClosure(),
315 trace_file)));
316 run_loop.Run();
320 void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
321 CHECK(!test_server_.get());
322 test_server_.reset(new net::SpawnedTestServer(
323 net::SpawnedTestServer::TYPE_HTTP,
324 net::SpawnedTestServer::kLocalhost,
325 test_server_base));
328 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
329 const base::Closure& task) {
330 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
331 switches::kSingleProcess));
333 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
335 base::MessageLoop* renderer_loop =
336 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
337 CHECK(renderer_loop);
339 renderer_loop->task_runner()->PostTask(
340 FROM_HERE,
341 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
342 runner->Run();
345 void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_ = true; }
347 void BrowserTestBase::UseSoftwareCompositing() {
348 use_software_compositing_ = true;
351 bool BrowserTestBase::UsingOSMesa() const {
352 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
353 return cmd->GetSwitchValueASCII(switches::kUseGL) ==
354 gfx::kGLImplementationOSMesaName;
357 } // namespace content