This source file includes following definitions.
- DumpStackTraceSignalHandler
- RunTaskOnRendererThread
- Resolve
- use_software_compositing_
- SetUp
- TearDown
- ProxyRunTestOnMainThreadLoop
- CreateTestServer
- PostTaskToInProcessRendererAndWait
- EnablePixelOutput
- UseSoftwareCompositing
- UsingOSMesa
#include "content/public/test/browser_test_base.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/debug/stack_trace.h"
#include "base/i18n/icu_util.h"
#include "base/message_loop/message_loop.h"
#include "base/sys_info.h"
#include "content/public/app/content_main.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/main_function_params.h"
#include "content/public/test/test_launcher.h"
#include "content/public/test/test_utils.h"
#include "net/base/net_errors.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/compositor/compositor_switches.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_switches.h"
#if defined(OS_POSIX)
#include "base/process/process_handle.h"
#endif
#if defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#endif
#if defined(OS_ANDROID)
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_main_runner.h"
#include "content/public/browser/browser_thread.h"
#endif
#if defined(USE_AURA)
#include "content/browser/compositor/image_transport_factory.h"
#endif
namespace content {
namespace {
#if defined(OS_POSIX)
static int g_browser_process_pid;
static void DumpStackTraceSignalHandler(int signal) {
if (g_browser_process_pid == base::GetCurrentProcId()) {
logging::RawLog(logging::LOG_ERROR,
"BrowserTestBase signal handler received SIGTERM. "
"Backtrace:\n");
base::debug::StackTrace().Print();
}
_exit(128 + signal);
}
#endif
void RunTaskOnRendererThread(const base::Closure& task,
const base::Closure& quit_task) {
task.Run();
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
}
class LocalHostResolverProc : public net::HostResolverProc {
public:
LocalHostResolverProc() : HostResolverProc(NULL) {}
virtual int Resolve(const std::string& host,
net::AddressFamily address_family,
net::HostResolverFlags host_resolver_flags,
net::AddressList* addrlist,
int* os_error) OVERRIDE {
const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
bool local = false;
if (host == net::GetHostName()) {
local = true;
} else {
for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
if (host == kLocalHostNames[i]) {
local = true;
break;
}
}
if (!local) {
DVLOG(1) << "To avoid external dependencies, simulating failure for "
"external DNS lookup of " << host;
return net::ERR_NOT_IMPLEMENTED;
}
return ResolveUsingPrevious(host, address_family, host_resolver_flags,
addrlist, os_error);
}
private:
virtual ~LocalHostResolverProc() {}
};
}
extern int BrowserMain(const MainFunctionParams&);
BrowserTestBase::BrowserTestBase()
: enable_pixel_output_(false), use_software_compositing_(false) {
#if defined(OS_MACOSX)
base::mac::SetOverrideAmIBundled(true);
#endif
#if defined(OS_POSIX)
handle_sigterm_ = true;
#endif
base::i18n::AllowMultipleInitializeCallsForTesting();
embedded_test_server_.reset(new net::test_server::EmbeddedTestServer);
}
BrowserTestBase::~BrowserTestBase() {
#if defined(OS_ANDROID)
base::ThreadRestrictions::ScopedAllowWait allow_wait;
test_server_.reset(NULL);
#endif
}
void BrowserTestBase::SetUp() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
command_line->AppendSwitch(switches::kDomAutomationController);
command_line->AppendSwitch(switches::kLogGpuControlListDecisions);
if (use_software_compositing_) {
command_line->AppendSwitch(switches::kDisableGpu);
command_line->AppendSwitch(switches::kEnableSoftwareCompositing);
#if defined(USE_AURA)
command_line->AppendSwitch(switches::kUIDisableThreadedCompositing);
#endif
}
#if defined(USE_AURA)
if (command_line->HasSwitch(switches::kEnablePixelOutputInTests))
enable_pixel_output_ = true;
if (command_line->HasSwitch(switches::kDisableGLDrawingForTests)) {
NOTREACHED() << "kDisableGLDrawingForTests should not be used as it"
"is chosen by tests. Use kEnablePixelOutputInTests "
"to enable pixel output.";
}
if (!enable_pixel_output_ && !use_software_compositing_)
command_line->AppendSwitch(switches::kDisableGLDrawingForTests);
#endif
bool use_osmesa = true;
if (command_line->HasSwitch(switches::kUseGpuInTests))
use_osmesa = false;
if (command_line->HasSwitch("enable-gpu"))
use_osmesa = false;
#if defined(OS_MACOSX)
use_osmesa = false;
#endif
#if defined(OS_ANDROID)
use_osmesa = false;
#endif
#if defined(OS_CHROMEOS)
if (base::SysInfo::IsRunningOnChromeOS())
use_osmesa = false;
#endif
if (command_line->HasSwitch(switches::kUseGL)) {
NOTREACHED() <<
"kUseGL should not be used with tests. Try kUseGpuInTests instead.";
}
if (use_osmesa && !use_software_compositing_) {
command_line->AppendSwitchASCII(
switches::kUseGL, gfx::kGLImplementationOSMesaName);
}
scoped_refptr<net::HostResolverProc> local_resolver =
new LocalHostResolverProc();
rule_based_resolver_ =
new net::RuleBasedHostResolverProc(local_resolver.get());
rule_based_resolver_->AddSimulatedFailure("wpad");
net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
rule_based_resolver_.get());
SetUpInProcessBrowserTestFixture();
base::Closure* ui_task =
new base::Closure(
base::Bind(&BrowserTestBase::ProxyRunTestOnMainThreadLoop, this));
#if defined(OS_ANDROID)
MainFunctionParams params(*command_line);
params.ui_task = ui_task;
BrowserMainRunner::Create()->Initialize(params);
base::ThreadRestrictions::SetIOAllowed(true);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed),
true));
#else
GetContentMainParams()->ui_task = ui_task;
ContentMain(*GetContentMainParams());
#endif
TearDownInProcessBrowserTestFixture();
}
void BrowserTestBase::TearDown() {
}
void BrowserTestBase::ProxyRunTestOnMainThreadLoop() {
#if defined(OS_POSIX)
if (handle_sigterm_) {
g_browser_process_pid = base::GetCurrentProcId();
signal(SIGTERM, DumpStackTraceSignalHandler);
}
#endif
RunTestOnMainThreadLoop();
}
void BrowserTestBase::CreateTestServer(const base::FilePath& test_server_base) {
CHECK(!test_server_.get());
test_server_.reset(new net::SpawnedTestServer(
net::SpawnedTestServer::TYPE_HTTP,
net::SpawnedTestServer::kLocalhost,
test_server_base));
}
void BrowserTestBase::PostTaskToInProcessRendererAndWait(
const base::Closure& task) {
CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess));
scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
base::MessageLoop* renderer_loop =
RenderProcessHostImpl::GetInProcessRendererThreadForTesting();
CHECK(renderer_loop);
renderer_loop->PostTask(
FROM_HERE,
base::Bind(&RunTaskOnRendererThread, task, runner->QuitClosure()));
runner->Run();
}
void BrowserTestBase::EnablePixelOutput() { enable_pixel_output_ = true; }
void BrowserTestBase::UseSoftwareCompositing() {
#if !defined(USE_AURA) && !defined(OS_MACOSX)
NOTREACHED();
#endif
use_software_compositing_ = true;
}
bool BrowserTestBase::UsingOSMesa() const {
CommandLine* cmd = CommandLine::ForCurrentProcess();
return cmd->GetSwitchValueASCII(switches::kUseGL) ==
gfx::kGLImplementationOSMesaName;
}
}