Commit 1314956e authored by jochen's avatar jochen Committed by Commit bot

Use the blink main thread isolate for v8 unit tests.

The TestWebKitPlatformSupport will initialize it for us anyway, so use
it.

BUG=none
R=andrewhayden@chromium.org,haraken@chromium.org

Review URL: https://codereview.chromium.org/526973003

Cr-Commit-Position: refs/heads/master@{#292951}
parent dab69c8f
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "third_party/WebKit/public/web/WebKit.h"
namespace { namespace {
...@@ -45,7 +46,7 @@ base::FilePath gen_test_data_directory; ...@@ -45,7 +46,7 @@ base::FilePath gen_test_data_directory;
} // namespace } // namespace
V8UnitTest::V8UnitTest() : handle_scope_(isolate_scope_.isolate()) { V8UnitTest::V8UnitTest() : handle_scope_(blink::mainThreadIsolate()) {
InitPathsAndLibraries(); InitPathsAndLibraries();
} }
...@@ -90,13 +91,14 @@ bool V8UnitTest::RunJavascriptTestF(const std::string& testFixture, ...@@ -90,13 +91,14 @@ bool V8UnitTest::RunJavascriptTestF(const std::string& testFixture,
if (!ExecuteJavascriptLibraries()) if (!ExecuteJavascriptLibraries())
return false; return false;
v8::HandleScope handle_scope(isolate()); v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate(), context_); v8::Local<v8::Context>::New(isolate, context_);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
v8::Handle<v8::Value> functionProperty = v8::Handle<v8::Value> functionProperty =
context->Global()->Get(v8::String::NewFromUtf8(isolate(), "runTest")); context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest"));
EXPECT_FALSE(functionProperty.IsEmpty()); EXPECT_FALSE(functionProperty.IsEmpty());
if (::testing::Test::HasNonfatalFailure()) if (::testing::Test::HasNonfatalFailure())
return false; return false;
...@@ -106,20 +108,20 @@ bool V8UnitTest::RunJavascriptTestF(const std::string& testFixture, ...@@ -106,20 +108,20 @@ bool V8UnitTest::RunJavascriptTestF(const std::string& testFixture,
v8::Handle<v8::Function> function = v8::Handle<v8::Function> function =
v8::Handle<v8::Function>::Cast(functionProperty); v8::Handle<v8::Function>::Cast(functionProperty);
v8::Local<v8::Array> params = v8::Array::New(isolate()); v8::Local<v8::Array> params = v8::Array::New(isolate);
params->Set(0, params->Set(0,
v8::String::NewFromUtf8(isolate(), v8::String::NewFromUtf8(isolate,
testFixture.data(), testFixture.data(),
v8::String::kNormalString, v8::String::kNormalString,
testFixture.size())); testFixture.size()));
params->Set(1, params->Set(1,
v8::String::NewFromUtf8(isolate(), v8::String::NewFromUtf8(isolate,
testName.data(), testName.data(),
v8::String::kNormalString, v8::String::kNormalString,
testName.size())); testName.size()));
v8::Handle<v8::Value> args[] = { v8::Handle<v8::Value> args[] = {
v8::Boolean::New(isolate(), false), v8::Boolean::New(isolate, false),
v8::String::NewFromUtf8(isolate(), "RUN_TEST_F"), params}; v8::String::NewFromUtf8(isolate, "RUN_TEST_F"), params};
v8::TryCatch try_catch; v8::TryCatch try_catch;
v8::Handle<v8::Value> result = function->Call(context->Global(), 3, args); v8::Handle<v8::Value> result = function->Call(context->Global(), 3, args);
...@@ -162,57 +164,60 @@ void V8UnitTest::InitPathsAndLibraries() { ...@@ -162,57 +164,60 @@ void V8UnitTest::InitPathsAndLibraries() {
} }
void V8UnitTest::SetUp() { void V8UnitTest::SetUp() {
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate()); v8::Isolate* isolate = blink::mainThreadIsolate();
v8::Handle<v8::String> logString = v8::String::NewFromUtf8(isolate(), "log"); v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
v8::Handle<v8::String> logString = v8::String::NewFromUtf8(isolate, "log");
v8::Handle<v8::FunctionTemplate> logFunction = v8::Handle<v8::FunctionTemplate> logFunction =
v8::FunctionTemplate::New(isolate(), &V8UnitTest::Log); v8::FunctionTemplate::New(isolate, &V8UnitTest::Log);
global->Set(logString, logFunction); global->Set(logString, logFunction);
// Set up chrome object for chrome.send(). // Set up chrome object for chrome.send().
v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(isolate()); v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(isolate);
global->Set(v8::String::NewFromUtf8(isolate(), "chrome"), chrome); global->Set(v8::String::NewFromUtf8(isolate, "chrome"), chrome);
chrome->Set(v8::String::NewFromUtf8(isolate(), "send"), chrome->Set(v8::String::NewFromUtf8(isolate, "send"),
v8::FunctionTemplate::New(isolate(), &V8UnitTest::ChromeSend)); v8::FunctionTemplate::New(isolate, &V8UnitTest::ChromeSend));
// Set up console object for console.log(), etc. // Set up console object for console.log(), etc.
v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(isolate()); v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(isolate);
global->Set(v8::String::NewFromUtf8(isolate(), "console"), console); global->Set(v8::String::NewFromUtf8(isolate, "console"), console);
console->Set(logString, logFunction); console->Set(logString, logFunction);
console->Set(v8::String::NewFromUtf8(isolate(), "info"), logFunction); console->Set(v8::String::NewFromUtf8(isolate, "info"), logFunction);
console->Set(v8::String::NewFromUtf8(isolate(), "warn"), logFunction); console->Set(v8::String::NewFromUtf8(isolate, "warn"), logFunction);
console->Set(v8::String::NewFromUtf8(isolate(), "error"), console->Set(v8::String::NewFromUtf8(isolate, "error"),
v8::FunctionTemplate::New(isolate(), &V8UnitTest::Error)); v8::FunctionTemplate::New(isolate, &V8UnitTest::Error));
context_.Reset(isolate(), v8::Context::New(isolate(), NULL, global)); context_.Reset(isolate, v8::Context::New(isolate, NULL, global));
} }
void V8UnitTest::SetGlobalStringVar(const std::string& var_name, void V8UnitTest::SetGlobalStringVar(const std::string& var_name,
const std::string& value) { const std::string& value) {
v8::Isolate* isolate = blink::mainThreadIsolate();
v8::Local<v8::Context> context = v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate(), context_); v8::Local<v8::Context>::New(isolate, context_);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
context->Global()->Set( context->Global()->Set(
v8::String::NewFromUtf8(isolate(), v8::String::NewFromUtf8(isolate,
var_name.c_str(), var_name.c_str(),
v8::String::kNormalString, v8::String::kNormalString,
var_name.length()), var_name.length()),
v8::String::NewFromUtf8( v8::String::NewFromUtf8(
isolate(), value.c_str(), v8::String::kNormalString, value.length())); isolate, value.c_str(), v8::String::kNormalString, value.length()));
} }
void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source,
const base::StringPiece& script_name) { const base::StringPiece& script_name) {
v8::HandleScope handle_scope(isolate()); v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate(), context_); v8::Local<v8::Context>::New(isolate, context_);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
v8::Handle<v8::String> source = v8::Handle<v8::String> source =
v8::String::NewFromUtf8(isolate(), v8::String::NewFromUtf8(isolate,
script_source.data(), script_source.data(),
v8::String::kNormalString, v8::String::kNormalString,
script_source.size()); script_source.size());
v8::Handle<v8::String> name = v8::Handle<v8::String> name =
v8::String::NewFromUtf8(isolate(), v8::String::NewFromUtf8(isolate,
script_name.data(), script_name.data(),
v8::String::kNormalString, v8::String::kNormalString,
script_name.size()); script_name.size());
...@@ -249,13 +254,14 @@ std::string V8UnitTest::ExceptionToString(const v8::TryCatch& try_catch) { ...@@ -249,13 +254,14 @@ std::string V8UnitTest::ExceptionToString(const v8::TryCatch& try_catch) {
} }
void V8UnitTest::TestFunction(const std::string& function_name) { void V8UnitTest::TestFunction(const std::string& function_name) {
v8::HandleScope handle_scope(isolate()); v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate(), context_); v8::Local<v8::Context>::New(isolate, context_);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
v8::Handle<v8::Value> functionProperty = context->Global()->Get( v8::Handle<v8::Value> functionProperty = context->Global()->Get(
v8::String::NewFromUtf8(isolate(), function_name.c_str())); v8::String::NewFromUtf8(isolate, function_name.c_str()));
ASSERT_FALSE(functionProperty.IsEmpty()); ASSERT_FALSE(functionProperty.IsEmpty());
ASSERT_TRUE(functionProperty->IsFunction()); ASSERT_TRUE(functionProperty->IsFunction());
v8::Handle<v8::Function> function = v8::Handle<v8::Function> function =
......
...@@ -68,36 +68,12 @@ class V8UnitTest : public testing::Test { ...@@ -68,36 +68,12 @@ class V8UnitTest : public testing::Test {
static void ChromeSend(const v8::FunctionCallbackInfo<v8::Value>& args); static void ChromeSend(const v8::FunctionCallbackInfo<v8::Value>& args);
private: private:
// A helper class to ensure that the lifetimes of the Isolate and the
// HandleScope are correctly nested.
class IsolateScope {
public:
IsolateScope() : isolate_(v8::Isolate::New()) { isolate_->Enter(); }
~IsolateScope() {
isolate_->Exit();
isolate_->Dispose();
}
v8::Isolate* isolate() const { return isolate_; }
private:
v8::Isolate* isolate_;
DISALLOW_COPY_AND_ASSIGN(IsolateScope);
};
// A handy shortcut.
v8::Isolate* isolate() const { return isolate_scope_.isolate(); }
// Executes all added javascript libraries. Returns true if no errors. // Executes all added javascript libraries. Returns true if no errors.
bool ExecuteJavascriptLibraries(); bool ExecuteJavascriptLibraries();
// Initializes paths and libraries. // Initializes paths and libraries.
void InitPathsAndLibraries(); void InitPathsAndLibraries();
IsolateScope isolate_scope_;
// Handle scope that is used throughout the life of this class. // Handle scope that is used throughout the life of this class.
v8::HandleScope handle_scope_; v8::HandleScope handle_scope_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment