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/files/file_util.h" | 7 #include "base/files/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/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "third_party/WebKit/public/web/WebKit.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // |args| are passed through the various JavaScript logging functions such as | 17 // |args| are passed through the various JavaScript logging functions such as |
17 // console.log. Returns a string appropriate for logging with LOG(severity). | 18 // console.log. Returns a string appropriate for logging with LOG(severity). |
18 std::string LogArgs2String(const v8::FunctionCallbackInfo<v8::Value>& args) { | 19 std::string LogArgs2String(const v8::FunctionCallbackInfo<v8::Value>& args) { |
19 std::string message; | 20 std::string message; |
20 bool first = true; | 21 bool first = true; |
21 for (int i = 0; i < args.Length(); i++) { | 22 for (int i = 0; i < args.Length(); i++) { |
22 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 23 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
(...skipping 15 matching lines...) Expand all Loading... |
38 bool testResult_ok = false; | 39 bool testResult_ok = false; |
39 | 40 |
40 // Location of test data (currently test/data/webui). | 41 // Location of test data (currently test/data/webui). |
41 base::FilePath test_data_directory; | 42 base::FilePath test_data_directory; |
42 | 43 |
43 // Location of generated test data (<(PROGRAM_DIR)/test_data). | 44 // Location of generated test data (<(PROGRAM_DIR)/test_data). |
44 base::FilePath gen_test_data_directory; | 45 base::FilePath gen_test_data_directory; |
45 | 46 |
46 } // namespace | 47 } // namespace |
47 | 48 |
48 V8UnitTest::V8UnitTest() : handle_scope_(isolate_scope_.isolate()) { | 49 V8UnitTest::V8UnitTest() : handle_scope_(blink::mainThreadIsolate()) { |
49 InitPathsAndLibraries(); | 50 InitPathsAndLibraries(); |
50 } | 51 } |
51 | 52 |
52 V8UnitTest::~V8UnitTest() { | 53 V8UnitTest::~V8UnitTest() { |
53 } | 54 } |
54 | 55 |
55 void V8UnitTest::AddLibrary(const base::FilePath& library_path) { | 56 void V8UnitTest::AddLibrary(const base::FilePath& library_path) { |
56 user_libraries_.push_back(library_path); | 57 user_libraries_.push_back(library_path); |
57 } | 58 } |
58 | 59 |
(...skipping 24 matching lines...) Expand all Loading... |
83 } | 84 } |
84 | 85 |
85 bool V8UnitTest::RunJavascriptTestF(const std::string& testFixture, | 86 bool V8UnitTest::RunJavascriptTestF(const std::string& testFixture, |
86 const std::string& testName) { | 87 const std::string& testName) { |
87 had_errors = false; | 88 had_errors = false; |
88 testResult_ok = false; | 89 testResult_ok = false; |
89 std::string test_js; | 90 std::string test_js; |
90 if (!ExecuteJavascriptLibraries()) | 91 if (!ExecuteJavascriptLibraries()) |
91 return false; | 92 return false; |
92 | 93 |
93 v8::HandleScope handle_scope(isolate()); | 94 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 95 v8::HandleScope handle_scope(isolate); |
94 v8::Local<v8::Context> context = | 96 v8::Local<v8::Context> context = |
95 v8::Local<v8::Context>::New(isolate(), context_); | 97 v8::Local<v8::Context>::New(isolate, context_); |
96 v8::Context::Scope context_scope(context); | 98 v8::Context::Scope context_scope(context); |
97 | 99 |
98 v8::Handle<v8::Value> functionProperty = | 100 v8::Handle<v8::Value> functionProperty = |
99 context->Global()->Get(v8::String::NewFromUtf8(isolate(), "runTest")); | 101 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); |
100 EXPECT_FALSE(functionProperty.IsEmpty()); | 102 EXPECT_FALSE(functionProperty.IsEmpty()); |
101 if (::testing::Test::HasNonfatalFailure()) | 103 if (::testing::Test::HasNonfatalFailure()) |
102 return false; | 104 return false; |
103 EXPECT_TRUE(functionProperty->IsFunction()); | 105 EXPECT_TRUE(functionProperty->IsFunction()); |
104 if (::testing::Test::HasNonfatalFailure()) | 106 if (::testing::Test::HasNonfatalFailure()) |
105 return false; | 107 return false; |
106 v8::Handle<v8::Function> function = | 108 v8::Handle<v8::Function> function = |
107 v8::Handle<v8::Function>::Cast(functionProperty); | 109 v8::Handle<v8::Function>::Cast(functionProperty); |
108 | 110 |
109 v8::Local<v8::Array> params = v8::Array::New(isolate()); | 111 v8::Local<v8::Array> params = v8::Array::New(isolate); |
110 params->Set(0, | 112 params->Set(0, |
111 v8::String::NewFromUtf8(isolate(), | 113 v8::String::NewFromUtf8(isolate, |
112 testFixture.data(), | 114 testFixture.data(), |
113 v8::String::kNormalString, | 115 v8::String::kNormalString, |
114 testFixture.size())); | 116 testFixture.size())); |
115 params->Set(1, | 117 params->Set(1, |
116 v8::String::NewFromUtf8(isolate(), | 118 v8::String::NewFromUtf8(isolate, |
117 testName.data(), | 119 testName.data(), |
118 v8::String::kNormalString, | 120 v8::String::kNormalString, |
119 testName.size())); | 121 testName.size())); |
120 v8::Handle<v8::Value> args[] = { | 122 v8::Handle<v8::Value> args[] = { |
121 v8::Boolean::New(isolate(), false), | 123 v8::Boolean::New(isolate, false), |
122 v8::String::NewFromUtf8(isolate(), "RUN_TEST_F"), params}; | 124 v8::String::NewFromUtf8(isolate, "RUN_TEST_F"), params}; |
123 | 125 |
124 v8::TryCatch try_catch; | 126 v8::TryCatch try_catch; |
125 v8::Handle<v8::Value> result = function->Call(context->Global(), 3, args); | 127 v8::Handle<v8::Value> result = function->Call(context->Global(), 3, args); |
126 // The test fails if an exception was thrown. | 128 // The test fails if an exception was thrown. |
127 EXPECT_FALSE(result.IsEmpty()); | 129 EXPECT_FALSE(result.IsEmpty()); |
128 if (::testing::Test::HasNonfatalFailure()) | 130 if (::testing::Test::HasNonfatalFailure()) |
129 return false; | 131 return false; |
130 | 132 |
131 // Ok if ran successfully, passed tests, and didn't have console errors. | 133 // Ok if ran successfully, passed tests, and didn't have console errors. |
132 return result->BooleanValue() && testResult_ok && !had_errors; | 134 return result->BooleanValue() && testResult_ok && !had_errors; |
(...skipping 22 matching lines...) Expand all Loading... |
155 AddLibrary(accessibilityAuditPath); | 157 AddLibrary(accessibilityAuditPath); |
156 | 158 |
157 base::FilePath testApiPath; | 159 base::FilePath testApiPath; |
158 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath)); | 160 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath)); |
159 testApiPath = testApiPath.AppendASCII("webui"); | 161 testApiPath = testApiPath.AppendASCII("webui"); |
160 testApiPath = testApiPath.AppendASCII("test_api.js"); | 162 testApiPath = testApiPath.AppendASCII("test_api.js"); |
161 AddLibrary(testApiPath); | 163 AddLibrary(testApiPath); |
162 } | 164 } |
163 | 165 |
164 void V8UnitTest::SetUp() { | 166 void V8UnitTest::SetUp() { |
165 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate()); | 167 v8::Isolate* isolate = blink::mainThreadIsolate(); |
166 v8::Handle<v8::String> logString = v8::String::NewFromUtf8(isolate(), "log"); | 168 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
| 169 v8::Handle<v8::String> logString = v8::String::NewFromUtf8(isolate, "log"); |
167 v8::Handle<v8::FunctionTemplate> logFunction = | 170 v8::Handle<v8::FunctionTemplate> logFunction = |
168 v8::FunctionTemplate::New(isolate(), &V8UnitTest::Log); | 171 v8::FunctionTemplate::New(isolate, &V8UnitTest::Log); |
169 global->Set(logString, logFunction); | 172 global->Set(logString, logFunction); |
170 | 173 |
171 // Set up chrome object for chrome.send(). | 174 // Set up chrome object for chrome.send(). |
172 v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(isolate()); | 175 v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(isolate); |
173 global->Set(v8::String::NewFromUtf8(isolate(), "chrome"), chrome); | 176 global->Set(v8::String::NewFromUtf8(isolate, "chrome"), chrome); |
174 chrome->Set(v8::String::NewFromUtf8(isolate(), "send"), | 177 chrome->Set(v8::String::NewFromUtf8(isolate, "send"), |
175 v8::FunctionTemplate::New(isolate(), &V8UnitTest::ChromeSend)); | 178 v8::FunctionTemplate::New(isolate, &V8UnitTest::ChromeSend)); |
176 | 179 |
177 // Set up console object for console.log(), etc. | 180 // Set up console object for console.log(), etc. |
178 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(isolate()); | 181 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(isolate); |
179 global->Set(v8::String::NewFromUtf8(isolate(), "console"), console); | 182 global->Set(v8::String::NewFromUtf8(isolate, "console"), console); |
180 console->Set(logString, logFunction); | 183 console->Set(logString, logFunction); |
181 console->Set(v8::String::NewFromUtf8(isolate(), "info"), logFunction); | 184 console->Set(v8::String::NewFromUtf8(isolate, "info"), logFunction); |
182 console->Set(v8::String::NewFromUtf8(isolate(), "warn"), logFunction); | 185 console->Set(v8::String::NewFromUtf8(isolate, "warn"), logFunction); |
183 console->Set(v8::String::NewFromUtf8(isolate(), "error"), | 186 console->Set(v8::String::NewFromUtf8(isolate, "error"), |
184 v8::FunctionTemplate::New(isolate(), &V8UnitTest::Error)); | 187 v8::FunctionTemplate::New(isolate, &V8UnitTest::Error)); |
185 | 188 |
186 context_.Reset(isolate(), v8::Context::New(isolate(), NULL, global)); | 189 context_.Reset(isolate, v8::Context::New(isolate, NULL, global)); |
187 } | 190 } |
188 | 191 |
189 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, | 192 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, |
190 const std::string& value) { | 193 const std::string& value) { |
| 194 v8::Isolate* isolate = blink::mainThreadIsolate(); |
191 v8::Local<v8::Context> context = | 195 v8::Local<v8::Context> context = |
192 v8::Local<v8::Context>::New(isolate(), context_); | 196 v8::Local<v8::Context>::New(isolate, context_); |
193 v8::Context::Scope context_scope(context); | 197 v8::Context::Scope context_scope(context); |
194 context->Global()->Set( | 198 context->Global()->Set( |
195 v8::String::NewFromUtf8(isolate(), | 199 v8::String::NewFromUtf8(isolate, |
196 var_name.c_str(), | 200 var_name.c_str(), |
197 v8::String::kNormalString, | 201 v8::String::kNormalString, |
198 var_name.length()), | 202 var_name.length()), |
199 v8::String::NewFromUtf8( | 203 v8::String::NewFromUtf8( |
200 isolate(), value.c_str(), v8::String::kNormalString, value.length())); | 204 isolate, value.c_str(), v8::String::kNormalString, value.length())); |
201 } | 205 } |
202 | 206 |
203 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, | 207 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, |
204 const base::StringPiece& script_name) { | 208 const base::StringPiece& script_name) { |
205 v8::HandleScope handle_scope(isolate()); | 209 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 210 v8::HandleScope handle_scope(isolate); |
206 v8::Local<v8::Context> context = | 211 v8::Local<v8::Context> context = |
207 v8::Local<v8::Context>::New(isolate(), context_); | 212 v8::Local<v8::Context>::New(isolate, context_); |
208 v8::Context::Scope context_scope(context); | 213 v8::Context::Scope context_scope(context); |
209 v8::Handle<v8::String> source = | 214 v8::Handle<v8::String> source = |
210 v8::String::NewFromUtf8(isolate(), | 215 v8::String::NewFromUtf8(isolate, |
211 script_source.data(), | 216 script_source.data(), |
212 v8::String::kNormalString, | 217 v8::String::kNormalString, |
213 script_source.size()); | 218 script_source.size()); |
214 v8::Handle<v8::String> name = | 219 v8::Handle<v8::String> name = |
215 v8::String::NewFromUtf8(isolate(), | 220 v8::String::NewFromUtf8(isolate, |
216 script_name.data(), | 221 script_name.data(), |
217 v8::String::kNormalString, | 222 v8::String::kNormalString, |
218 script_name.size()); | 223 script_name.size()); |
219 | 224 |
220 v8::TryCatch try_catch; | 225 v8::TryCatch try_catch; |
221 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); | 226 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); |
222 // Ensure the script compiled without errors. | 227 // Ensure the script compiled without errors. |
223 if (script.IsEmpty()) | 228 if (script.IsEmpty()) |
224 FAIL() << ExceptionToString(try_catch); | 229 FAIL() << ExceptionToString(try_catch); |
225 | 230 |
(...skipping 16 matching lines...) Expand all Loading... |
242 int colnum = message->GetStartColumn(); | 247 int colnum = message->GetStartColumn(); |
243 str.append(base::StringPrintf( | 248 str.append(base::StringPrintf( |
244 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); | 249 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); |
245 v8::String::Utf8Value sourceline(message->GetSourceLine()); | 250 v8::String::Utf8Value sourceline(message->GetSourceLine()); |
246 str.append(base::StringPrintf("%s\n", *sourceline)); | 251 str.append(base::StringPrintf("%s\n", *sourceline)); |
247 } | 252 } |
248 return str; | 253 return str; |
249 } | 254 } |
250 | 255 |
251 void V8UnitTest::TestFunction(const std::string& function_name) { | 256 void V8UnitTest::TestFunction(const std::string& function_name) { |
252 v8::HandleScope handle_scope(isolate()); | 257 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 258 v8::HandleScope handle_scope(isolate); |
253 v8::Local<v8::Context> context = | 259 v8::Local<v8::Context> context = |
254 v8::Local<v8::Context>::New(isolate(), context_); | 260 v8::Local<v8::Context>::New(isolate, context_); |
255 v8::Context::Scope context_scope(context); | 261 v8::Context::Scope context_scope(context); |
256 | 262 |
257 v8::Handle<v8::Value> functionProperty = context->Global()->Get( | 263 v8::Handle<v8::Value> functionProperty = context->Global()->Get( |
258 v8::String::NewFromUtf8(isolate(), function_name.c_str())); | 264 v8::String::NewFromUtf8(isolate, function_name.c_str())); |
259 ASSERT_FALSE(functionProperty.IsEmpty()); | 265 ASSERT_FALSE(functionProperty.IsEmpty()); |
260 ASSERT_TRUE(functionProperty->IsFunction()); | 266 ASSERT_TRUE(functionProperty->IsFunction()); |
261 v8::Handle<v8::Function> function = | 267 v8::Handle<v8::Function> function = |
262 v8::Handle<v8::Function>::Cast(functionProperty); | 268 v8::Handle<v8::Function>::Cast(functionProperty); |
263 | 269 |
264 v8::TryCatch try_catch; | 270 v8::TryCatch try_catch; |
265 v8::Handle<v8::Value> result = function->Call(context->Global(), 0, NULL); | 271 v8::Handle<v8::Value> result = function->Call(context->Global(), 0, NULL); |
266 // The test fails if an exception was thrown. | 272 // The test fails if an exception was thrown. |
267 if (result.IsEmpty()) | 273 if (result.IsEmpty()) |
268 FAIL() << ExceptionToString(try_catch); | 274 FAIL() << ExceptionToString(try_catch); |
(...skipping 27 matching lines...) Expand all Loading... |
296 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); | 302 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); |
297 EXPECT_EQ(2U, testResult->Length()); | 303 EXPECT_EQ(2U, testResult->Length()); |
298 if (::testing::Test::HasNonfatalFailure()) | 304 if (::testing::Test::HasNonfatalFailure()) |
299 return; | 305 return; |
300 testResult_ok = testResult->Get(0)->BooleanValue(); | 306 testResult_ok = testResult->Get(0)->BooleanValue(); |
301 if (!testResult_ok) { | 307 if (!testResult_ok) { |
302 v8::String::Utf8Value message(testResult->Get(1)); | 308 v8::String::Utf8Value message(testResult->Get(1)); |
303 LOG(ERROR) << *message; | 309 LOG(ERROR) << *message; |
304 } | 310 } |
305 } | 311 } |
OLD | NEW |