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" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 std::string test_js; | 90 std::string test_js; |
91 if (!ExecuteJavascriptLibraries()) | 91 if (!ExecuteJavascriptLibraries()) |
92 return false; | 92 return false; |
93 | 93 |
94 v8::Isolate* isolate = blink::mainThreadIsolate(); | 94 v8::Isolate* isolate = blink::mainThreadIsolate(); |
95 v8::HandleScope handle_scope(isolate); | 95 v8::HandleScope handle_scope(isolate); |
96 v8::Local<v8::Context> context = | 96 v8::Local<v8::Context> context = |
97 v8::Local<v8::Context>::New(isolate, context_); | 97 v8::Local<v8::Context>::New(isolate, context_); |
98 v8::Context::Scope context_scope(context); | 98 v8::Context::Scope context_scope(context); |
99 | 99 |
100 v8::Handle<v8::Value> functionProperty = | 100 v8::Local<v8::Value> functionProperty = |
101 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); | 101 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); |
102 EXPECT_FALSE(functionProperty.IsEmpty()); | 102 EXPECT_FALSE(functionProperty.IsEmpty()); |
103 if (::testing::Test::HasNonfatalFailure()) | 103 if (::testing::Test::HasNonfatalFailure()) |
104 return false; | 104 return false; |
105 EXPECT_TRUE(functionProperty->IsFunction()); | 105 EXPECT_TRUE(functionProperty->IsFunction()); |
106 if (::testing::Test::HasNonfatalFailure()) | 106 if (::testing::Test::HasNonfatalFailure()) |
107 return false; | 107 return false; |
108 v8::Handle<v8::Function> function = | 108 v8::Local<v8::Function> function = |
109 v8::Handle<v8::Function>::Cast(functionProperty); | 109 v8::Local<v8::Function>::Cast(functionProperty); |
110 | 110 |
111 v8::Local<v8::Array> params = v8::Array::New(isolate); | 111 v8::Local<v8::Array> params = v8::Array::New(isolate); |
112 params->Set(0, | 112 params->Set(0, |
113 v8::String::NewFromUtf8(isolate, | 113 v8::String::NewFromUtf8(isolate, |
114 testFixture.data(), | 114 testFixture.data(), |
115 v8::String::kNormalString, | 115 v8::String::kNormalString, |
116 testFixture.size())); | 116 testFixture.size())); |
117 params->Set(1, | 117 params->Set(1, |
118 v8::String::NewFromUtf8(isolate, | 118 v8::String::NewFromUtf8(isolate, |
119 testName.data(), | 119 testName.data(), |
120 v8::String::kNormalString, | 120 v8::String::kNormalString, |
121 testName.size())); | 121 testName.size())); |
122 v8::Handle<v8::Value> args[] = { | 122 v8::Local<v8::Value> args[] = { |
123 v8::Boolean::New(isolate, false), | 123 v8::Boolean::New(isolate, false), |
124 v8::String::NewFromUtf8(isolate, "RUN_TEST_F"), params}; | 124 v8::String::NewFromUtf8(isolate, "RUN_TEST_F"), params}; |
125 | 125 |
126 v8::TryCatch try_catch; | 126 v8::TryCatch try_catch; |
127 v8::Handle<v8::Value> result = function->Call(context->Global(), 3, args); | 127 v8::Local<v8::Value> result = function->Call(context->Global(), 3, args); |
128 // The test fails if an exception was thrown. | 128 // The test fails if an exception was thrown. |
129 EXPECT_FALSE(result.IsEmpty()); | 129 EXPECT_FALSE(result.IsEmpty()); |
130 if (::testing::Test::HasNonfatalFailure()) | 130 if (::testing::Test::HasNonfatalFailure()) |
131 return false; | 131 return false; |
132 | 132 |
133 // 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. |
134 return result->BooleanValue() && testResult_ok && !had_errors; | 134 return result->BooleanValue() && testResult_ok && !had_errors; |
135 } | 135 } |
136 | 136 |
137 void V8UnitTest::InitPathsAndLibraries() { | 137 void V8UnitTest::InitPathsAndLibraries() { |
(...skipping 20 matching lines...) Expand all Loading... |
158 | 158 |
159 base::FilePath testApiPath; | 159 base::FilePath testApiPath; |
160 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath)); | 160 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath)); |
161 testApiPath = testApiPath.AppendASCII("webui"); | 161 testApiPath = testApiPath.AppendASCII("webui"); |
162 testApiPath = testApiPath.AppendASCII("test_api.js"); | 162 testApiPath = testApiPath.AppendASCII("test_api.js"); |
163 AddLibrary(testApiPath); | 163 AddLibrary(testApiPath); |
164 } | 164 } |
165 | 165 |
166 void V8UnitTest::SetUp() { | 166 void V8UnitTest::SetUp() { |
167 v8::Isolate* isolate = blink::mainThreadIsolate(); | 167 v8::Isolate* isolate = blink::mainThreadIsolate(); |
168 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | 168 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
169 v8::Handle<v8::String> logString = v8::String::NewFromUtf8(isolate, "log"); | 169 v8::Local<v8::String> logString = v8::String::NewFromUtf8(isolate, "log"); |
170 v8::Handle<v8::FunctionTemplate> logFunction = | 170 v8::Local<v8::FunctionTemplate> logFunction = |
171 v8::FunctionTemplate::New(isolate, &V8UnitTest::Log); | 171 v8::FunctionTemplate::New(isolate, &V8UnitTest::Log); |
172 global->Set(logString, logFunction); | 172 global->Set(logString, logFunction); |
173 | 173 |
174 // Set up chrome object for chrome.send(). | 174 // Set up chrome object for chrome.send(). |
175 v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(isolate); | 175 v8::Local<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(isolate); |
176 global->Set(v8::String::NewFromUtf8(isolate, "chrome"), chrome); | 176 global->Set(v8::String::NewFromUtf8(isolate, "chrome"), chrome); |
177 chrome->Set(v8::String::NewFromUtf8(isolate, "send"), | 177 chrome->Set(v8::String::NewFromUtf8(isolate, "send"), |
178 v8::FunctionTemplate::New(isolate, &V8UnitTest::ChromeSend)); | 178 v8::FunctionTemplate::New(isolate, &V8UnitTest::ChromeSend)); |
179 | 179 |
180 // Set up console object for console.log(), etc. | 180 // Set up console object for console.log(), etc. |
181 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(isolate); | 181 v8::Local<v8::ObjectTemplate> console = v8::ObjectTemplate::New(isolate); |
182 global->Set(v8::String::NewFromUtf8(isolate, "console"), console); | 182 global->Set(v8::String::NewFromUtf8(isolate, "console"), console); |
183 console->Set(logString, logFunction); | 183 console->Set(logString, logFunction); |
184 console->Set(v8::String::NewFromUtf8(isolate, "info"), logFunction); | 184 console->Set(v8::String::NewFromUtf8(isolate, "info"), logFunction); |
185 console->Set(v8::String::NewFromUtf8(isolate, "warn"), logFunction); | 185 console->Set(v8::String::NewFromUtf8(isolate, "warn"), logFunction); |
186 console->Set(v8::String::NewFromUtf8(isolate, "error"), | 186 console->Set(v8::String::NewFromUtf8(isolate, "error"), |
187 v8::FunctionTemplate::New(isolate, &V8UnitTest::Error)); | 187 v8::FunctionTemplate::New(isolate, &V8UnitTest::Error)); |
188 | 188 |
189 context_.Reset(isolate, v8::Context::New(isolate, NULL, global)); | 189 context_.Reset(isolate, v8::Context::New(isolate, NULL, global)); |
190 } | 190 } |
191 | 191 |
(...skipping 12 matching lines...) Expand all Loading... |
204 isolate, value.c_str(), v8::String::kNormalString, value.length())); | 204 isolate, value.c_str(), v8::String::kNormalString, value.length())); |
205 } | 205 } |
206 | 206 |
207 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, | 207 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, |
208 const base::StringPiece& script_name) { | 208 const base::StringPiece& script_name) { |
209 v8::Isolate* isolate = blink::mainThreadIsolate(); | 209 v8::Isolate* isolate = blink::mainThreadIsolate(); |
210 v8::HandleScope handle_scope(isolate); | 210 v8::HandleScope handle_scope(isolate); |
211 v8::Local<v8::Context> context = | 211 v8::Local<v8::Context> context = |
212 v8::Local<v8::Context>::New(isolate, context_); | 212 v8::Local<v8::Context>::New(isolate, context_); |
213 v8::Context::Scope context_scope(context); | 213 v8::Context::Scope context_scope(context); |
214 v8::Handle<v8::String> source = | 214 v8::Local<v8::String> source = |
215 v8::String::NewFromUtf8(isolate, | 215 v8::String::NewFromUtf8(isolate, |
216 script_source.data(), | 216 script_source.data(), |
217 v8::String::kNormalString, | 217 v8::String::kNormalString, |
218 script_source.size()); | 218 script_source.size()); |
219 v8::Handle<v8::String> name = | 219 v8::Local<v8::String> name = |
220 v8::String::NewFromUtf8(isolate, | 220 v8::String::NewFromUtf8(isolate, |
221 script_name.data(), | 221 script_name.data(), |
222 v8::String::kNormalString, | 222 v8::String::kNormalString, |
223 script_name.size()); | 223 script_name.size()); |
224 | 224 |
225 v8::TryCatch try_catch; | 225 v8::TryCatch try_catch; |
226 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); | 226 v8::Local<v8::Script> script = v8::Script::Compile(source, name); |
227 // Ensure the script compiled without errors. | 227 // Ensure the script compiled without errors. |
228 if (script.IsEmpty()) | 228 if (script.IsEmpty()) |
229 FAIL() << ExceptionToString(try_catch); | 229 FAIL() << ExceptionToString(try_catch); |
230 | 230 |
231 v8::Handle<v8::Value> result = script->Run(); | 231 v8::Local<v8::Value> result = script->Run(); |
232 // Ensure the script ran without errors. | 232 // Ensure the script ran without errors. |
233 if (result.IsEmpty()) | 233 if (result.IsEmpty()) |
234 FAIL() << ExceptionToString(try_catch); | 234 FAIL() << ExceptionToString(try_catch); |
235 } | 235 } |
236 | 236 |
237 std::string V8UnitTest::ExceptionToString(const v8::TryCatch& try_catch) { | 237 std::string V8UnitTest::ExceptionToString(const v8::TryCatch& try_catch) { |
238 std::string str; | 238 std::string str; |
239 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 239 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
240 v8::String::Utf8Value exception(try_catch.Exception()); | 240 v8::String::Utf8Value exception(try_catch.Exception()); |
241 v8::Local<v8::Message> message(try_catch.Message()); | 241 v8::Local<v8::Message> message(try_catch.Message()); |
(...skipping 11 matching lines...) Expand all Loading... |
253 return str; | 253 return str; |
254 } | 254 } |
255 | 255 |
256 void V8UnitTest::TestFunction(const std::string& function_name) { | 256 void V8UnitTest::TestFunction(const std::string& function_name) { |
257 v8::Isolate* isolate = blink::mainThreadIsolate(); | 257 v8::Isolate* isolate = blink::mainThreadIsolate(); |
258 v8::HandleScope handle_scope(isolate); | 258 v8::HandleScope handle_scope(isolate); |
259 v8::Local<v8::Context> context = | 259 v8::Local<v8::Context> context = |
260 v8::Local<v8::Context>::New(isolate, context_); | 260 v8::Local<v8::Context>::New(isolate, context_); |
261 v8::Context::Scope context_scope(context); | 261 v8::Context::Scope context_scope(context); |
262 | 262 |
263 v8::Handle<v8::Value> functionProperty = context->Global()->Get( | 263 v8::Local<v8::Value> functionProperty = context->Global()->Get( |
264 v8::String::NewFromUtf8(isolate, function_name.c_str())); | 264 v8::String::NewFromUtf8(isolate, function_name.c_str())); |
265 ASSERT_FALSE(functionProperty.IsEmpty()); | 265 ASSERT_FALSE(functionProperty.IsEmpty()); |
266 ASSERT_TRUE(functionProperty->IsFunction()); | 266 ASSERT_TRUE(functionProperty->IsFunction()); |
267 v8::Handle<v8::Function> function = | 267 v8::Local<v8::Function> function = |
268 v8::Handle<v8::Function>::Cast(functionProperty); | 268 v8::Local<v8::Function>::Cast(functionProperty); |
269 | 269 |
270 v8::TryCatch try_catch; | 270 v8::TryCatch try_catch; |
271 v8::Handle<v8::Value> result = function->Call(context->Global(), 0, NULL); | 271 v8::Local<v8::Value> result = function->Call(context->Global(), 0, NULL); |
272 // The test fails if an exception was thrown. | 272 // The test fails if an exception was thrown. |
273 if (result.IsEmpty()) | 273 if (result.IsEmpty()) |
274 FAIL() << ExceptionToString(try_catch); | 274 FAIL() << ExceptionToString(try_catch); |
275 } | 275 } |
276 | 276 |
277 // static | 277 // static |
278 void V8UnitTest::Log(const v8::FunctionCallbackInfo<v8::Value>& args) { | 278 void V8UnitTest::Log(const v8::FunctionCallbackInfo<v8::Value>& args) { |
279 LOG(INFO) << LogArgs2String(args); | 279 LOG(INFO) << LogArgs2String(args); |
280 } | 280 } |
281 | 281 |
(...skipping 10 matching lines...) Expand all Loading... |
292 EXPECT_LE(1, args.Length()); | 292 EXPECT_LE(1, args.Length()); |
293 if (::testing::Test::HasNonfatalFailure()) | 293 if (::testing::Test::HasNonfatalFailure()) |
294 return; | 294 return; |
295 v8::String::Utf8Value message(args[0]); | 295 v8::String::Utf8Value message(args[0]); |
296 EXPECT_EQ("testResult", std::string(*message, message.length())); | 296 EXPECT_EQ("testResult", std::string(*message, message.length())); |
297 if (::testing::Test::HasNonfatalFailure()) | 297 if (::testing::Test::HasNonfatalFailure()) |
298 return; | 298 return; |
299 EXPECT_EQ(2, args.Length()); | 299 EXPECT_EQ(2, args.Length()); |
300 if (::testing::Test::HasNonfatalFailure()) | 300 if (::testing::Test::HasNonfatalFailure()) |
301 return; | 301 return; |
302 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); | 302 v8::Local<v8::Array> testResult(args[1].As<v8::Array>()); |
303 EXPECT_EQ(2U, testResult->Length()); | 303 EXPECT_EQ(2U, testResult->Length()); |
304 if (::testing::Test::HasNonfatalFailure()) | 304 if (::testing::Test::HasNonfatalFailure()) |
305 return; | 305 return; |
306 testResult_ok = testResult->Get(0)->BooleanValue(); | 306 testResult_ok = testResult->Get(0)->BooleanValue(); |
307 if (!testResult_ok) { | 307 if (!testResult_ok) { |
308 v8::String::Utf8Value message(testResult->Get(1)); | 308 v8::String::Utf8Value message(testResult->Get(1)); |
309 LOG(ERROR) << *message; | 309 LOG(ERROR) << *message; |
310 } | 310 } |
311 } | 311 } |
OLD | NEW |