OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/base/v8_unit_test.h" | 5 #include "chrome/test/base/v8_unit_test.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 bool testResult_ok = false; | 38 bool testResult_ok = false; |
39 | 39 |
40 // Location of test data (currently test/data/webui). | 40 // Location of test data (currently test/data/webui). |
41 base::FilePath test_data_directory; | 41 base::FilePath test_data_directory; |
42 | 42 |
43 // Location of generated test data (<(PROGRAM_DIR)/test_data). | 43 // Location of generated test data (<(PROGRAM_DIR)/test_data). |
44 base::FilePath gen_test_data_directory; | 44 base::FilePath gen_test_data_directory; |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 V8UnitTest::V8UnitTest() { | 48 V8UnitTest::V8UnitTest() |
| 49 : isolate_(v8::Isolate::GetCurrent()), |
| 50 handle_scope_(isolate_) { |
49 InitPathsAndLibraries(); | 51 InitPathsAndLibraries(); |
50 } | 52 } |
51 | 53 |
52 V8UnitTest::~V8UnitTest() {} | 54 V8UnitTest::~V8UnitTest() {} |
53 | 55 |
54 void V8UnitTest::AddLibrary(const base::FilePath& library_path) { | 56 void V8UnitTest::AddLibrary(const base::FilePath& library_path) { |
55 user_libraries_.push_back(library_path); | 57 user_libraries_.push_back(library_path); |
56 } | 58 } |
57 | 59 |
58 bool V8UnitTest::ExecuteJavascriptLibraries() { | 60 bool V8UnitTest::ExecuteJavascriptLibraries() { |
(...skipping 22 matching lines...) Expand all Loading... |
81 } | 83 } |
82 | 84 |
83 bool V8UnitTest::RunJavascriptTestF( | 85 bool V8UnitTest::RunJavascriptTestF( |
84 const std::string& testFixture, const std::string& testName) { | 86 const std::string& testFixture, const std::string& testName) { |
85 had_errors = false; | 87 had_errors = false; |
86 testResult_ok = false; | 88 testResult_ok = false; |
87 std::string test_js; | 89 std::string test_js; |
88 if (!ExecuteJavascriptLibraries()) | 90 if (!ExecuteJavascriptLibraries()) |
89 return false; | 91 return false; |
90 | 92 |
91 v8::Context::Scope context_scope(context_); | 93 v8::Context::Scope context_scope(isolate_, context_); |
92 v8::HandleScope handle_scope; | 94 v8::HandleScope handle_scope(isolate_); |
| 95 v8::Local<v8::Context> context = |
| 96 v8::Local<v8::Context>::New(isolate_, context_); |
93 | 97 |
94 v8::Handle<v8::Value> functionProperty = | 98 v8::Handle<v8::Value> functionProperty = |
95 context_->Global()->Get(v8::String::New("runTest")); | 99 context->Global()->Get(v8::String::New("runTest")); |
96 EXPECT_FALSE(functionProperty.IsEmpty()); | 100 EXPECT_FALSE(functionProperty.IsEmpty()); |
97 if (::testing::Test::HasNonfatalFailure()) | 101 if (::testing::Test::HasNonfatalFailure()) |
98 return false; | 102 return false; |
99 EXPECT_TRUE(functionProperty->IsFunction()); | 103 EXPECT_TRUE(functionProperty->IsFunction()); |
100 if (::testing::Test::HasNonfatalFailure()) | 104 if (::testing::Test::HasNonfatalFailure()) |
101 return false; | 105 return false; |
102 v8::Handle<v8::Function> function = | 106 v8::Handle<v8::Function> function = |
103 v8::Handle<v8::Function>::Cast(functionProperty); | 107 v8::Handle<v8::Function>::Cast(functionProperty); |
104 | 108 |
105 v8::Local<v8::Array> params = v8::Array::New(); | 109 v8::Local<v8::Array> params = v8::Array::New(); |
106 params->Set(0, v8::String::New(testFixture.data(), testFixture.size())); | 110 params->Set(0, v8::String::New(testFixture.data(), testFixture.size())); |
107 params->Set(1, v8::String::New(testName.data(), testName.size())); | 111 params->Set(1, v8::String::New(testName.data(), testName.size())); |
108 v8::Handle<v8::Value> args[] = { | 112 v8::Handle<v8::Value> args[] = { |
109 v8::Boolean::New(false), | 113 v8::Boolean::New(false), |
110 v8::String::New("RUN_TEST_F"), | 114 v8::String::New("RUN_TEST_F"), |
111 params | 115 params |
112 }; | 116 }; |
113 | 117 |
114 v8::TryCatch try_catch; | 118 v8::TryCatch try_catch; |
115 v8::Handle<v8::Value> result = function->Call(context_->Global(), 3, args); | 119 v8::Handle<v8::Value> result = function->Call(context->Global(), 3, args); |
116 // The test fails if an exception was thrown. | 120 // The test fails if an exception was thrown. |
117 EXPECT_FALSE(result.IsEmpty()); | 121 EXPECT_FALSE(result.IsEmpty()); |
118 if (::testing::Test::HasNonfatalFailure()) | 122 if (::testing::Test::HasNonfatalFailure()) |
119 return false; | 123 return false; |
120 | 124 |
121 // Ok if ran successfully, passed tests, and didn't have console errors. | 125 // Ok if ran successfully, passed tests, and didn't have console errors. |
122 return result->BooleanValue() && testResult_ok && !had_errors; | 126 return result->BooleanValue() && testResult_ok && !had_errors; |
123 } | 127 } |
124 | 128 |
125 void V8UnitTest::InitPathsAndLibraries() { | 129 void V8UnitTest::InitPathsAndLibraries() { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 171 |
168 // Set up console object for console.log(), etc. | 172 // Set up console object for console.log(), etc. |
169 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(); | 173 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(); |
170 global->Set(v8::String::New("console"), console); | 174 global->Set(v8::String::New("console"), console); |
171 console->Set(logString, logFunction); | 175 console->Set(logString, logFunction); |
172 console->Set(v8::String::New("info"), logFunction); | 176 console->Set(v8::String::New("info"), logFunction); |
173 console->Set(v8::String::New("warn"), logFunction); | 177 console->Set(v8::String::New("warn"), logFunction); |
174 console->Set(v8::String::New("error"), | 178 console->Set(v8::String::New("error"), |
175 v8::FunctionTemplate::New(&V8UnitTest::Error)); | 179 v8::FunctionTemplate::New(&V8UnitTest::Error)); |
176 | 180 |
177 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 181 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); |
178 // TODO(marja): Use v8::Persistent::Reset here. | |
179 context_ = v8::Persistent<v8::Context>( | |
180 isolate, v8::Context::New(isolate, NULL, global)); | |
181 } | 182 } |
182 | 183 |
183 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, | 184 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, |
184 const std::string& value) { | 185 const std::string& value) { |
185 v8::Context::Scope context_scope(context_); | 186 v8::Context::Scope context_scope(isolate_, context_); |
186 context_->Global()->Set(v8::String::New(var_name.c_str(), var_name.length()), | 187 v8::Local<v8::Context>::New(isolate_, context_)->Global() |
187 v8::String::New(value.c_str(), value.length())); | 188 ->Set(v8::String::New(var_name.c_str(), var_name.length()), |
| 189 v8::String::New(value.c_str(), value.length())); |
188 } | 190 } |
189 | 191 |
190 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, | 192 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, |
191 const base::StringPiece& script_name) { | 193 const base::StringPiece& script_name) { |
192 v8::Context::Scope context_scope(context_); | 194 v8::Context::Scope context_scope(isolate_, context_); |
193 v8::HandleScope handle_scope; | 195 v8::HandleScope handle_scope(isolate_); |
194 v8::Handle<v8::String> source = v8::String::New(script_source.data(), | 196 v8::Handle<v8::String> source = v8::String::New(script_source.data(), |
195 script_source.size()); | 197 script_source.size()); |
196 v8::Handle<v8::String> name = v8::String::New(script_name.data(), | 198 v8::Handle<v8::String> name = v8::String::New(script_name.data(), |
197 script_name.size()); | 199 script_name.size()); |
198 | 200 |
199 v8::TryCatch try_catch; | 201 v8::TryCatch try_catch; |
200 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); | 202 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); |
201 // Ensure the script compiled without errors. | 203 // Ensure the script compiled without errors. |
202 if (script.IsEmpty()) | 204 if (script.IsEmpty()) |
203 FAIL() << ExceptionToString(try_catch); | 205 FAIL() << ExceptionToString(try_catch); |
(...skipping 17 matching lines...) Expand all Loading... |
221 int colnum = message->GetStartColumn(); | 223 int colnum = message->GetStartColumn(); |
222 str.append(base::StringPrintf( | 224 str.append(base::StringPrintf( |
223 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); | 225 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); |
224 v8::String::Utf8Value sourceline(message->GetSourceLine()); | 226 v8::String::Utf8Value sourceline(message->GetSourceLine()); |
225 str.append(base::StringPrintf("%s\n", *sourceline)); | 227 str.append(base::StringPrintf("%s\n", *sourceline)); |
226 } | 228 } |
227 return str; | 229 return str; |
228 } | 230 } |
229 | 231 |
230 void V8UnitTest::TestFunction(const std::string& function_name) { | 232 void V8UnitTest::TestFunction(const std::string& function_name) { |
231 v8::Context::Scope context_scope(context_); | 233 v8::Context::Scope context_scope(isolate_, context_); |
232 v8::HandleScope handle_scope; | 234 v8::HandleScope handle_scope(isolate_); |
| 235 v8::Local<v8::Context> context = |
| 236 v8::Local<v8::Context>::New(isolate_, context_); |
233 | 237 |
234 v8::Handle<v8::Value> functionProperty = | 238 v8::Handle<v8::Value> functionProperty = |
235 context_->Global()->Get(v8::String::New(function_name.c_str())); | 239 context->Global()->Get(v8::String::New(function_name.c_str())); |
236 ASSERT_FALSE(functionProperty.IsEmpty()); | 240 ASSERT_FALSE(functionProperty.IsEmpty()); |
237 ASSERT_TRUE(functionProperty->IsFunction()); | 241 ASSERT_TRUE(functionProperty->IsFunction()); |
238 v8::Handle<v8::Function> function = | 242 v8::Handle<v8::Function> function = |
239 v8::Handle<v8::Function>::Cast(functionProperty); | 243 v8::Handle<v8::Function>::Cast(functionProperty); |
240 | 244 |
241 v8::TryCatch try_catch; | 245 v8::TryCatch try_catch; |
242 v8::Handle<v8::Value> result = function->Call(context_->Global(), 0, NULL); | 246 v8::Handle<v8::Value> result = function->Call(context->Global(), 0, NULL); |
243 // The test fails if an exception was thrown. | 247 // The test fails if an exception was thrown. |
244 if (result.IsEmpty()) | 248 if (result.IsEmpty()) |
245 FAIL() << ExceptionToString(try_catch); | 249 FAIL() << ExceptionToString(try_catch); |
246 } | 250 } |
247 | 251 |
248 // static | 252 // static |
249 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { | 253 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { |
250 LOG(INFO) << LogArgs2String(args); | 254 LOG(INFO) << LogArgs2String(args); |
251 return v8::Undefined(); | 255 return v8::Undefined(); |
252 } | 256 } |
(...skipping 23 matching lines...) Expand all Loading... |
276 EXPECT_EQ(2U, testResult->Length()); | 280 EXPECT_EQ(2U, testResult->Length()); |
277 if (::testing::Test::HasNonfatalFailure()) | 281 if (::testing::Test::HasNonfatalFailure()) |
278 return v8::Undefined(); | 282 return v8::Undefined(); |
279 testResult_ok = testResult->Get(0)->BooleanValue(); | 283 testResult_ok = testResult->Get(0)->BooleanValue(); |
280 if (!testResult_ok) { | 284 if (!testResult_ok) { |
281 v8::String::Utf8Value message(testResult->Get(1)); | 285 v8::String::Utf8Value message(testResult->Get(1)); |
282 LOG(ERROR) << *message; | 286 LOG(ERROR) << *message; |
283 } | 287 } |
284 return v8::Undefined(); | 288 return v8::Undefined(); |
285 } | 289 } |
OLD | NEW |