[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / public / test / browser_test_base.cc
bloba29beb1632c3defdbbae13d41c42256e1150396e
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);
201 #if defined(USE_AURA)
202 // Most tests do not need pixel output, so we don't produce any. The command
203 // line can override this behaviour to allow for visual debugging.
204 if (command_line->HasSwitch(switches::kEnablePixelOutputInTests))
205 enable_pixel_output_ = true;
207 if (command_line->HasSwitch(switches::kDisableGLDrawingForTests)) {
208 NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
209 "is chosen by tests. Use kEnablePixelOutputInTests "
210 "to enable pixel output.";
213 // Don't enable pixel output for browser tests unless they override and force
214 // us to, or it's requested on the command line.
215 if (!enable_pixel_output_ && !use_software_compositing_)
216 command_line->AppendSwitch(switches::kDisableGLDrawingForTests);
217 #endif
219 bool use_osmesa = true;
221 // We usually use OSMesa as this works on all bots. The command line can
222 // override this behaviour to use hardware GL.
223 if (command_line->HasSwitch(switches::kUseGpuInTests))
224 use_osmesa = false;
226 // Some bots pass this flag when they want to use hardware GL.
227 if (command_line->HasSwitch("enable-gpu"))
228 use_osmesa = false;
230 #if defined(OS_MACOSX)
231 // On Mac we always use hardware GL.
232 use_osmesa = false;
233 #endif
235 #if defined(OS_ANDROID)
236 // On Android we always use hardware GL.
237 use_osmesa = false;
238 #endif
240 #if defined(OS_CHROMEOS)
241 // If the test is running on the chromeos envrionment (such as
242 // device or vm bots), we use hardware GL.
243 if (base::SysInfo::IsRunningOnChromeOS())
244 use_osmesa = false;
245 #endif
247 if (use_osmesa && !use_software_compositing_)
248 command_line->AppendSwitch(switches::kOverrideUseGLWithOSMesaForTests);
250 scoped_refptr<net::HostResolverProc> local_resolver =
251 new LocalHostResolverProc();
252 rule_based_resolver_ =
253 new net::RuleBasedHostResolverProc(local_resolver.get());
254 rule_based_resolver_->AddSimulatedFailure("wpad");
255 net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
256 rule_based_resolver_.get());
258 ContentBrowserSanityChecker scoped_enable_sanity_checks;
260 SetUpInProcessBrowserTestFixture();
262 base::Closure* ui_task =
263 new base::Closure(
264 base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
266 #if defined(OS_ANDROID)
267 MainFunctionParams params(*command_line);
268 params.ui_task = ui_task;
269 // TODO(phajdan.jr): Check return code, http://crbug.com/374738 .
270 BrowserMain(params);
271 #else
272 GetContentMainParams()->ui_task = ui_task;
273 EXPECT_EQ(expected_exit_code_, ContentMain(*GetContentMainParams()));
274 #endif
275 TearDownInProcessBrowserTestFixture();
278 void BrowserTestBase::TearDown() {
281 void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
282 #if defined(OS_POSIX)
283 if (handle_sigterm_) {
284 g_browser_process_pid = base::GetCurrentProcId();
285 signal(SIGTERM, DumpStackTraceSignalHandler);
287 #endif // defined(OS_POSIX)
289 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
290 switches::kEnableTracing)) {
291 base::trace_event::TraceConfig trace_config(
292 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
293 switches::kEnableTracing),
294 base::trace_event::RECORD_CONTINUOUSLY);
295 TracingController::GetInstance()->EnableRecording(
296 trace_config,
297 TracingController::EnableRecordingDoneCallback());
300 RunTestOnMainThreadLoop();
302 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
303 switches::kEnableTracing)) {
304 base::FilePath trace_file =
305 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
306 switches::kEnableTracingOutput);
307 // If there was no file specified, put a hardcoded one in the current
308 // working directory.
309 if (trace_file.empty())
310 trace_file = base::FilePath().AppendASCII("trace.json");
312 // Wait for tracing to collect results from the renderers.
313 base::RunLoop run_loop;
314 TracingController::GetInstance()->DisableRecording(
315 TracingControllerImpl::CreateFileSink(
316 trace_file,
317 base::Bind(&TraceDisableRecordingComplete,
318 run_loop.QuitClosure(),
319 trace_file)));
320 run_loop.Run();
324 void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
325 CHECK(!test_server_.get());
326 test_server_.reset(new net::SpawnedTestServer(
327 net::SpawnedTestServer::TYPE_HTTP,
328 net::SpawnedTestServer::kLocalhost,
329 test_server_base));
332 void BrowserTestBase::PostTaskToInProcessRendererAndWait(
333 const base::Closure& task) {
334 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
335 switches::kSingleProcess));
337 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
339 base::MessageLoop* renderer_loop =
340 RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
341 CHECK(renderer_loop);
343 renderer_loop->task_runner()->PostTask(
344 FROM_HERE,
345 base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
346 runner->Run();
349 void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_ = true; }
351 void BrowserTestBase::UseSoftwareCompositing() {
352 use_software_compositing_ = true;
355 bool BrowserTestBase::UsingOSMesa() const {
356 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
357 return cmd->GetSwitchValueASCII(switches::kUseGL) ==
358 gfx::kGLImplementationOSMesaName;
361 } // namespace content