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/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 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // |args| are passed through the various JavaScript logging functions such as | 16 // |args| are passed through the various JavaScript logging functions such as |
17 // console.log. Returns a string appropriate for logging with LOG(severity). | 17 // console.log. Returns a string appropriate for logging with LOG(severity). |
18 std::string LogArgs2String(const v8::FunctionCallbackInfo<v8::Value>& args) { | 18 std::string LogArgs2String(const v8::FunctionCallbackInfo<v8::Value>& args) { |
19 std::string message; | 19 std::string message; |
20 bool first = true; | 20 bool first = true; |
21 for (int i = 0; i < args.Length(); i++) { | 21 for (int i = 0; i < args.Length(); i++) { |
22 v8::HandleScope handle_scope; | 22 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
23 if (first) | 23 if (first) |
24 first = false; | 24 first = false; |
25 else | 25 else |
26 message += " "; | 26 message += " "; |
27 | 27 |
28 v8::String::Utf8Value str(args[i]); | 28 v8::String::Utf8Value str(args[i]); |
29 message += *str; | 29 message += *str; |
30 } | 30 } |
31 return message; | 31 return message; |
32 } | 32 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 FAIL() << ExceptionToString(try_catch); | 205 FAIL() << ExceptionToString(try_catch); |
206 | 206 |
207 v8::Handle<v8::Value> result = script->Run(); | 207 v8::Handle<v8::Value> result = script->Run(); |
208 // Ensure the script ran without errors. | 208 // Ensure the script ran without errors. |
209 if (result.IsEmpty()) | 209 if (result.IsEmpty()) |
210 FAIL() << ExceptionToString(try_catch); | 210 FAIL() << ExceptionToString(try_catch); |
211 } | 211 } |
212 | 212 |
213 std::string V8UnitTest::ExceptionToString(const v8::TryCatch& try_catch) { | 213 std::string V8UnitTest::ExceptionToString(const v8::TryCatch& try_catch) { |
214 std::string str; | 214 std::string str; |
215 v8::HandleScope handle_scope; | 215 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
216 v8::String::Utf8Value exception(try_catch.Exception()); | 216 v8::String::Utf8Value exception(try_catch.Exception()); |
217 v8::Local<v8::Message> message(try_catch.Message()); | 217 v8::Local<v8::Message> message(try_catch.Message()); |
218 if (message.IsEmpty()) { | 218 if (message.IsEmpty()) { |
219 str.append(base::StringPrintf("%s\n", *exception)); | 219 str.append(base::StringPrintf("%s\n", *exception)); |
220 } else { | 220 } else { |
221 v8::String::Utf8Value filename(message->GetScriptResourceName()); | 221 v8::String::Utf8Value filename(message->GetScriptResourceName()); |
222 int linenum = message->GetLineNumber(); | 222 int linenum = message->GetLineNumber(); |
223 int colnum = message->GetStartColumn(); | 223 int colnum = message->GetStartColumn(); |
224 str.append(base::StringPrintf( | 224 str.append(base::StringPrintf( |
225 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); | 225 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); |
(...skipping 27 matching lines...) Expand all Loading... |
253 void V8UnitTest::Log(const v8::FunctionCallbackInfo<v8::Value>& args) { | 253 void V8UnitTest::Log(const v8::FunctionCallbackInfo<v8::Value>& args) { |
254 LOG(INFO) << LogArgs2String(args); | 254 LOG(INFO) << LogArgs2String(args); |
255 } | 255 } |
256 | 256 |
257 void V8UnitTest::Error(const v8::FunctionCallbackInfo<v8::Value>& args) { | 257 void V8UnitTest::Error(const v8::FunctionCallbackInfo<v8::Value>& args) { |
258 had_errors = true; | 258 had_errors = true; |
259 LOG(ERROR) << LogArgs2String(args); | 259 LOG(ERROR) << LogArgs2String(args); |
260 } | 260 } |
261 | 261 |
262 void V8UnitTest::ChromeSend(const v8::FunctionCallbackInfo<v8::Value>& args) { | 262 void V8UnitTest::ChromeSend(const v8::FunctionCallbackInfo<v8::Value>& args) { |
263 v8::HandleScope handle_scope; | 263 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
264 // We expect to receive 2 args: ("testResult", [ok, message]). However, | 264 // We expect to receive 2 args: ("testResult", [ok, message]). However, |
265 // chrome.send may pass only one. Therefore we need to ensure we have at least | 265 // chrome.send may pass only one. Therefore we need to ensure we have at least |
266 // 1, then ensure that the first is "testResult" before checking again for 2. | 266 // 1, then ensure that the first is "testResult" before checking again for 2. |
267 EXPECT_LE(1, args.Length()); | 267 EXPECT_LE(1, args.Length()); |
268 if (::testing::Test::HasNonfatalFailure()) | 268 if (::testing::Test::HasNonfatalFailure()) |
269 return; | 269 return; |
270 v8::String::Utf8Value message(args[0]); | 270 v8::String::Utf8Value message(args[0]); |
271 EXPECT_EQ("testResult", std::string(*message, message.length())); | 271 EXPECT_EQ("testResult", std::string(*message, message.length())); |
272 if (::testing::Test::HasNonfatalFailure()) | 272 if (::testing::Test::HasNonfatalFailure()) |
273 return; | 273 return; |
274 EXPECT_EQ(2, args.Length()); | 274 EXPECT_EQ(2, args.Length()); |
275 if (::testing::Test::HasNonfatalFailure()) | 275 if (::testing::Test::HasNonfatalFailure()) |
276 return; | 276 return; |
277 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); | 277 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); |
278 EXPECT_EQ(2U, testResult->Length()); | 278 EXPECT_EQ(2U, testResult->Length()); |
279 if (::testing::Test::HasNonfatalFailure()) | 279 if (::testing::Test::HasNonfatalFailure()) |
280 return; | 280 return; |
281 testResult_ok = testResult->Get(0)->BooleanValue(); | 281 testResult_ok = testResult->Get(0)->BooleanValue(); |
282 if (!testResult_ok) { | 282 if (!testResult_ok) { |
283 v8::String::Utf8Value message(testResult->Get(1)); | 283 v8::String::Utf8Value message(testResult->Get(1)); |
284 LOG(ERROR) << *message; | 284 LOG(ERROR) << *message; |
285 } | 285 } |
286 } | 286 } |
OLD | NEW |