Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / public / test / browser_test_base.cc
blobffe47b8f080909eb633c5ea4709d6c7807b5bd1a
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/base/net_util.h"
27 #include "net/dns/mock_host_resolver.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h"
29 #include "ui/compositor/compositor_switches.h"
30 #include "ui/gl/gl_implementation.h"
31 #include "ui/gl/gl_switches.h"
33 #if defined(OS_POSIX)
34 #include "base/process/process_handle.h"
35 #endif
37 #if defined(OS_MACOSX)
38 #include "base/mac/foundation_util.h"
39 #endif
41 #if defined(OS_ANDROID)
42 #include "base/threading/thread_restrictions.h"
43 #include "content/public/browser/browser_main_runner.h"
44 #include "content/public/browser/browser_thread.h"
45 #endif
47 #if defined(USE_AURA)
48 #include "content/browser/compositor/image_transport_factory.h"
49 #include "ui/aura/test/event_generator_delegate_aura.h"
50 #if defined(USE_X11)
51 #include "ui/aura/window_tree_host_x11.h"
52 #endif
53 #endif
55 namespace content {
56 namespace {
58 #if defined(OS_POSIX)
59 // On SIGTERM (sent by the runner on timeouts), dump a stack trace (to make
60 // debugging easier) and also exit with a known error code (so that the test
61 // framework considers this a failure -- http://crbug.com/57578).
62 // Note: We only want to do this in the browser process, and not forked
63 // processes. That might lead to hangs because of locks inside tcmalloc or the
64 // OS. See http://crbug.com/141302.
65 static int g_browser_process_pid;
66 static void DumpStackTraceSignalHandler(int signal) {
67 if (g_browser_process_pid == base::GetCurrentProcId()) {
68 logging::RawLog(logging::LOG_ERROR,
69 "BrowserTestBase signal handler received SIGTERM. "
70 "Backtrace:\n");
71 base::debug::StackTrace().Print();
73 _exit(128 + signal);
75 #endif // defined(OS_POSIX)
77 void RunTaskOnRendererThread(const base::Closure& task,
78 const base::Closure& quit_task) {
79 task.Run();
80 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
83 // In many cases it may be not obvious that a test makes a real DNS lookup.
84 // We generally don't want to rely on external DNS servers for our tests,
85 // so this host resolver procedure catches external queries and returns a failed
86 // lookup result.
87 class LocalHostResolverProc : public net::HostResolverProc {
88 public:
89 LocalHostResolverProc() : HostResolverProc(NULL) {}
91 int Resolve(const std::string& host,
92 net::AddressFamily address_family,
93 net::HostResolverFlags host_resolver_flags,
94 net::AddressList* addrlist,
95 int* os_error) override {
96 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
97 bool local = false;
99 if (host == net::GetHostName()) {
100 local = true;
101 } else {
102 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
103 if (host == kLocalHostNames[i]) {
104 local = true;
105 break;
109 // To avoid depending on external resources and to reduce (if not preclude)
110 // network interactions from tests, we simulate failure for non-local DNS
111 // queries, rather than perform them.
112 // If you really need to make an external DNS query, use
113 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
114 if (!local) {
115 DVLOG(1) << "To avoid external dependencies, simulating failure for "
116 "external DNS lookup of " << host;
117 return net::ERR_NOT_IMPLEMENTED;
120 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
121 addrlist, os_error);
124 private:
125 ~LocalHostResolverProc() override {}
128 void TraceDisableRecordingComplete(const base::Closure& quit,
129 const base::FilePath& file_path) {
130 LOG(ERROR) << "Tracing written to: " << file_path.value();
131 quit.Run();
134 } // namespace
136 extern int BrowserMain(const MainFunctionParams&);
138 BrowserTestBase::BrowserTestBase()
139 : expected_exit_code_(0),
140 enable_pixel_output_(false),
141 use_software_compositing_(false),
142 set_up_called_(false) {
143 #if defined(OS_MACOSX)
144 base::mac::SetOverrideAmIBundled(true);
145 #endif
147 #if defined(USE_AURA)
148 #if defined(USE_X11)
149 aura::test::SetUseOverrideRedirectWindowByDefault(true);
150 #endif
151 aura::test::InitializeAuraEventGeneratorDelegate();
152 #endif
154 #if defined(OS_POSIX)
155 handle_sigterm_ = true;
156 #endif
158 // This is called through base::TestSuite initially. It'll also be called
159 // inside BrowserMain, so tell the code to ignore the check that it's being
160 // called more than once
161 base::i18n::AllowMultipleInitializeCallsForTesting();
163 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
166 BrowserTestBase::~BrowserTestBase() {
167 #if defined(OS_ANDROID)
168 // RemoteTestServer can cause wait on the UI thread.
169 base::ThreadRestrictions::ScopedAllowWait allow_wait;
170 test_server_.reset(NULL);
171 #endif
173 CHECK(set_up_called_) << "SetUp was not called. This probably means that the "
174 "developer has overridden the method and not called "
175 "the superclass version. In this case, the test "
176 "does not run and reports a false positive result.";
179 void BrowserTestBase::SetUp() {
180 set_up_called_ = true;
182 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
184 // Override the child process connection timeout since tests can exceed that
185 // when sharded.
186 command_line->AppendSwitchASCII(
187 switches::kIPCConnectionTimeout,
188 base::IntToString(TestTimeouts::action_max_timeout().InSeconds()));
190 // The tests assume that file:// URIs can freely access other file:// URIs.
191 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
193 command_line->AppendSwitch(switches::kDomAutomationController);
195 // It is sometimes useful when looking at browser test failures to know which
196 // GPU blacklisting decisions were made.
197 command_line->AppendSwitch(switches::kLogGpuControlListDecisions);
199 if (use_software_compositing_)
200 command_line->AppendSwitch(switches::kDisableGpu);
202 #if defined(USE_AURA)
203 // Most tests do not need pixel output, so we don't produce any. The command
204 // line can override this behaviour to allow for visual debugging.
205 if (command_line->HasSwitch(switches::kEnablePixelOutputInTests))
206 enable_pixel_output_ = true;
208 if (command_line->HasSwitch(switches::kDisableGLDrawingForTests)) {
209 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
210 "is chosen by tests. Use kEnablePixelOutputInTests "
211 "to enable pixel output.";
214 // Don't enable pixel output for browser tests unless they override and force
215 // us to, or it's requested on the command line.
216 if (!enable_pixel_output_ && !use_software_compositing_)
217 command_line->AppendSwitch(switches::kDisableGLDrawingForTests);
218 #endif
220 bool use_osmesa = true;
222 // We usually use OSMesa as this works on all bots. The command line can
223 // override this behaviour to use hardware GL.
224 if (command_line->HasSwitch(switches::kUseGpuInTests))
225 use_osmesa = false;
227 // Some bots pass this flag when they want to use hardware GL.
228 if (command_line->HasSwitch("enable-gpu"))
229 use_osmesa = false;
231 #if defined(OS_MACOSX)
232 // On Mac we always use hardware GL.
233 use_osmesa = false;
234 #endif
236 #if defined(OS_ANDROID)
237 // On Android we always use hardware GL.
238 use_osmesa = false;
239 #endif
241 #if defined(OS_CHROMEOS)
242 // If the test is running on the chromeos envrionment (such as
243 // device or vm bots), we use hardware GL.
244 if (base::SysInfo::IsRunningOnChromeOS())
245 use_osmesa = false;
246 #endif
248 if (use_osmesa && !use_software_compositing_)
249 command_line->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests);
251 scoped_refptr<net::HostResolverProc> local_resolver =
252 new LocalHostResolverProc();
253 rule_based_resolver_ =
254 new net::RuleBasedHostResolverProc(local_resolver.get());
255 rule_based_resolver_->AddSimulatedFailure("wpad");
256 net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
257 rule_based_resolver_.get());
259 ContentBrowserSanityChecker scoped_enable_sanity_checks;
261 SetUpInProcessBrowserTestFixture();
263 base::Closure* ui_task =
264 new base::Closure(
265 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
267 #if defined(OS_ANDROID)
268 MainFunctionParams params(*command_line);
269 params.ui_task = ui_task;
270 // TODO(phajdan.jr): Check return code, http://crbug.com/374738 .
271 BrowserMain(params);
272 #else
273 GetContentMainParams()->ui_task = ui_task;
274 EXPECT_EQ(expected_exit_code_, ContentMain(*GetContentMainParams()));
275 #endif
276 TearDownInProcessBrowserTestFixture();
279 void BrowserTestBase::TearDown() {
282 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
283 #if defined(OS_POSIX)
284 if (handle_sigterm_) {
285 g_browser_process_pid = base::GetCurrentProcId();
286 signal(SIGTERM, DumpStackTraceSignalHandler);
288 #endif // defined(OS_POSIX)
290 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
291 switches::kEnableTracing)) {
292 base::trace_event::TraceConfig trace_config(
293 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
294 switches::kEnableTracing),
295 base::trace_event::RECORD_CONTINUOUSLY);
296 TracingController::GetInstance()->EnableRecording(
297 trace_config,
298 TracingController::EnableRecordingDoneCallback());
301 RunTestOnMainThreadLoop();
303 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
304 switches::kEnableTracing)) {
305 base::FilePath trace_file =
306 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
307 switches::kEnableTracingOutput);
308 // If there was no file specified, put a hardcoded one in the current
309 // working directory.
310 if (trace_file.empty())
311 trace_file = base::FilePath().AppendASCII("trace.json");
313 // Wait for tracing to collect results from the renderers.
314 base::RunLoop run_loop;
315 TracingController::GetInstance()->DisableRecording(
316 TracingControllerImpl::CreateFileSink(
317 trace_file,
318 base::Bind(&TraceDisableRecordingComplete,
319 run_loop.QuitClosure(),
320 trace_file)));
321 run_loop.Run();
325 void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
326 CHECK(!test_server_.get());
327 test_server_.reset(new net::SpawnedTestServer(
328 net::SpawnedTestServer::TYPE_HTTP,
329 net::SpawnedTestServer::kLocalhost,
330 test_server_base));
333 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
334 const base::Closure& task) {
335 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
336 switches::kSingleProcess));
338 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
340 base::MessageLoop* renderer_loop =
341 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
342 CHECK(renderer_loop);
344 renderer_loop->task_runner()->PostTask(
345 FROM_HERE,
346 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
347 runner->Run();
350 void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_ = true; }
352 void BrowserTestBase::UseSoftwareCompositing() {
353 use_software_compositing_ = true;
356 bool BrowserTestBase::UsingOSMesa() const {
357 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
358 return cmd->GetSwitchValueASCII(switches::kUseGL) ==
359 gfx::kGLImplementationOSMesaName;
362 } // namespace content