This source file includes following definitions.
- HandleMessage
- MissingField
- InternalError
- Log
- HandleStructuredMessage
- test_passed
- Log
- HandleStructuredMessage
- GetNaClVariantRoot
- AddPnaclParm
- AddPnaclDisabledParm
- SetUpCommandLine
- SetUpOnMainThread
- GetDocumentRoot
- IsAPnaclTest
- IsPnaclDisabled
- TestURL
- RunJavascriptTest
- RunLoadTest
- RunNaClIntegrationTest
- StartTestServer
- Variant
- Variant
- Variant
- IsAPnaclTest
- Variant
- IsAPnaclTest
- IsPnaclDisabled
- SetUpCommandLine
- Variant
- SetUpCommandLine
- Variant
- GetDocumentRoot
#include "chrome/test/nacl/nacl_browsertest_util.h"
#include <stdlib.h>
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/path_service.h"
#include "base/values.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/nacl/common/nacl_switches.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/webplugininfo.h"
#include "net/base/net_util.h"
typedef TestMessageHandler::MessageResponse MessageResponse;
MessageResponse StructuredMessageHandler::HandleMessage(
const std::string& json) {
scoped_ptr<base::Value> value;
base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
value.reset(reader.ReadToValue(json));
if (!value.get())
return InternalError("Could parse automation JSON: " + json +
" because " + reader.GetErrorMessage());
std::string temp;
if (!value->GetAsString(&temp))
return InternalError("Message was not a string: " + json);
value.reset(reader.ReadToValue(temp));
if (!value.get())
return InternalError("Could not parse message JSON: " + temp +
" because " + reader.GetErrorMessage());
base::DictionaryValue* msg;
if (!value->GetAsDictionary(&msg))
return InternalError("Message was not an object: " + temp);
std::string type;
if (!msg->GetString("type", &type))
return MissingField("unknown", "type");
return HandleStructuredMessage(type, msg);
}
MessageResponse StructuredMessageHandler::MissingField(
const std::string& type,
const std::string& field) {
return InternalError(type + " message did not have field: " + field);
}
MessageResponse StructuredMessageHandler::InternalError(
const std::string& reason) {
SetError(reason);
return DONE;
}
LoadTestMessageHandler::LoadTestMessageHandler()
: test_passed_(false) {
}
void LoadTestMessageHandler::Log(const std::string& type,
const std::string& message) {
LOG(INFO) << type << " " << message;
}
MessageResponse LoadTestMessageHandler::HandleStructuredMessage(
const std::string& type,
base::DictionaryValue* msg) {
if (type == "Log") {
std::string message;
if (!msg->GetString("message", &message))
return MissingField(type, "message");
Log("LOG", message);
return CONTINUE;
} else if (type == "Shutdown") {
std::string message;
if (!msg->GetString("message", &message))
return MissingField(type, "message");
if (!msg->GetBoolean("passed", &test_passed_))
return MissingField(type, "passed");
Log("SHUTDOWN", message);
return DONE;
} else {
return InternalError("Unknown message type: " + type);
}
}
class NaClIntegrationMessageHandler : public StructuredMessageHandler {
public:
NaClIntegrationMessageHandler();
void Log(const std::string& message);
virtual MessageResponse HandleStructuredMessage(
const std::string& type,
base::DictionaryValue* msg) OVERRIDE;
bool test_passed() const {
return test_passed_;
}
private:
bool test_passed_;
DISALLOW_COPY_AND_ASSIGN(NaClIntegrationMessageHandler);
};
NaClIntegrationMessageHandler::NaClIntegrationMessageHandler()
: test_passed_(false) {
}
void NaClIntegrationMessageHandler::Log(const std::string& message) {
LOG(INFO) << "|||| " << message;
}
MessageResponse NaClIntegrationMessageHandler::HandleStructuredMessage(
const std::string& type,
base::DictionaryValue* msg) {
if (type == "TestLog") {
std::string message;
if (!msg->GetString("message", &message))
return MissingField(type, "message");
Log(message);
return CONTINUE;
} else if (type == "Shutdown") {
std::string message;
if (!msg->GetString("message", &message))
return MissingField(type, "message");
if (!msg->GetBoolean("passed", &test_passed_))
return MissingField(type, "passed");
Log(message);
return DONE;
} else if (type == "Ping") {
return CONTINUE;
} else if (type == "JavaScriptIsAlive") {
return CONTINUE;
} else {
return InternalError("Unknown message type: " + type);
}
}
static bool GetNaClVariantRoot(const base::FilePath::StringType& variant,
base::FilePath* document_root) {
if (!ui_test_utils::GetRelativeBuildDirectory(document_root))
return false;
*document_root = document_root->Append(FILE_PATH_LITERAL("nacl_test_data"));
*document_root = document_root->Append(variant);
return true;
}
static void AddPnaclParm(const base::FilePath::StringType& url,
base::FilePath::StringType* url_with_parm) {
if (url.find(FILE_PATH_LITERAL("?")) == base::FilePath::StringType::npos) {
*url_with_parm = url + FILE_PATH_LITERAL("?pnacl=1");
} else {
*url_with_parm = url + FILE_PATH_LITERAL("&pnacl=1");
}
}
static void AddPnaclDisabledParm(const base::FilePath::StringType& url,
base::FilePath::StringType* url_with_parm) {
if (url.find(FILE_PATH_LITERAL("?")) == base::FilePath::StringType::npos) {
*url_with_parm = url + FILE_PATH_LITERAL("?pnacl_disabled=1");
} else {
*url_with_parm = url + FILE_PATH_LITERAL("&pnacl_disabled=1");
}
}
NaClBrowserTestBase::NaClBrowserTestBase() {
}
NaClBrowserTestBase::~NaClBrowserTestBase() {
}
void NaClBrowserTestBase::SetUpCommandLine(base::CommandLine* command_line) {
command_line->AppendSwitch(switches::kEnableNaCl);
}
void NaClBrowserTestBase::SetUpOnMainThread() {
base::FilePath plugin_lib;
ASSERT_TRUE(PathService::Get(chrome::FILE_NACL_PLUGIN, &plugin_lib));
ASSERT_TRUE(base::PathExists(plugin_lib)) << plugin_lib.value();
ASSERT_TRUE(StartTestServer()) << "Cannot start test server.";
}
bool NaClBrowserTestBase::GetDocumentRoot(base::FilePath* document_root) {
return GetNaClVariantRoot(Variant(), document_root);
}
bool NaClBrowserTestBase::IsAPnaclTest() {
return false;
}
bool NaClBrowserTestBase::IsPnaclDisabled() {
return false;
}
GURL NaClBrowserTestBase::TestURL(
const base::FilePath::StringType& url_fragment) {
base::FilePath expanded_url = base::FilePath(FILE_PATH_LITERAL("files"));
expanded_url = expanded_url.Append(url_fragment);
return test_server_->GetURL(expanded_url.MaybeAsASCII());
}
bool NaClBrowserTestBase::RunJavascriptTest(const GURL& url,
TestMessageHandler* handler) {
JavascriptTestObserver observer(
browser()->tab_strip_model()->GetActiveWebContents(),
handler);
ui_test_utils::NavigateToURL(browser(), url);
return observer.Run();
}
void NaClBrowserTestBase::RunLoadTest(
const base::FilePath::StringType& test_file) {
LoadTestMessageHandler handler;
base::FilePath::StringType test_file_with_pnacl = test_file;
if (IsAPnaclTest()) {
AddPnaclParm(test_file, &test_file_with_pnacl);
}
base::FilePath::StringType test_file_with_both = test_file_with_pnacl;
if (IsPnaclDisabled()) {
AddPnaclDisabledParm(test_file_with_pnacl, &test_file_with_both);
}
bool ok = RunJavascriptTest(TestURL(test_file_with_both), &handler);
ASSERT_TRUE(ok) << handler.error_message();
ASSERT_TRUE(handler.test_passed()) << "Test failed.";
}
void NaClBrowserTestBase::RunNaClIntegrationTest(
const base::FilePath::StringType& url_fragment) {
NaClIntegrationMessageHandler handler;
base::FilePath::StringType url_fragment_with_pnacl = url_fragment;
if (IsAPnaclTest()) {
AddPnaclParm(url_fragment, &url_fragment_with_pnacl);
}
base::FilePath::StringType url_fragment_with_both = url_fragment_with_pnacl;
if (IsPnaclDisabled()) {
AddPnaclDisabledParm(url_fragment_with_pnacl, &url_fragment_with_both);
}
bool ok = RunJavascriptTest(TestURL(url_fragment_with_both), &handler);
ASSERT_TRUE(ok) << handler.error_message();
ASSERT_TRUE(handler.test_passed()) << "Test failed.";
}
bool NaClBrowserTestBase::StartTestServer() {
base::FilePath document_root;
if (!GetDocumentRoot(&document_root))
return false;
test_server_.reset(new net::SpawnedTestServer(
net::SpawnedTestServer::TYPE_HTTP,
net::SpawnedTestServer::kLocalhost,
document_root));
return test_server_->Start();
}
base::FilePath::StringType NaClBrowserTestNewlib::Variant() {
return FILE_PATH_LITERAL("newlib");
}
base::FilePath::StringType NaClBrowserTestGLibc::Variant() {
return FILE_PATH_LITERAL("glibc");
}
base::FilePath::StringType NaClBrowserTestPnacl::Variant() {
return FILE_PATH_LITERAL("pnacl");
}
bool NaClBrowserTestPnacl::IsAPnaclTest() {
return true;
}
base::FilePath::StringType NaClBrowserTestPnaclDisabled::Variant() {
return FILE_PATH_LITERAL("pnacl");
}
bool NaClBrowserTestPnaclDisabled::IsAPnaclTest() {
return true;
}
bool NaClBrowserTestPnaclDisabled::IsPnaclDisabled() {
return true;
}
void NaClBrowserTestPnaclDisabled::SetUpCommandLine(
base::CommandLine* command_line) {
NaClBrowserTestBase::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kDisablePnacl);
}
base::FilePath::StringType NaClBrowserTestNonSfiMode::Variant() {
return FILE_PATH_LITERAL("libc-free");
}
void NaClBrowserTestNonSfiMode::SetUpCommandLine(
base::CommandLine* command_line) {
NaClBrowserTestBase::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnableNaClNonSfiMode);
}
base::FilePath::StringType NaClBrowserTestStatic::Variant() {
return FILE_PATH_LITERAL("static");
}
bool NaClBrowserTestStatic::GetDocumentRoot(base::FilePath* document_root) {
*document_root = base::FilePath(FILE_PATH_LITERAL("chrome/test/data/nacl"));
return true;
}