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 #include "third_party/WebKit/public/web/WebKit.h" |
14 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 // |args| are passed through the various JavaScript logging functions such as | 17 // |args| are passed through the various JavaScript logging functions such as |
19 // console.log. Returns a string appropriate for logging with LOG(severity). | 18 // console.log. Returns a string appropriate for logging with LOG(severity). |
20 std::string LogArgs2String(const v8::FunctionCallbackInfo<v8::Value>& args) { | 19 std::string LogArgs2String(const v8::FunctionCallbackInfo<v8::Value>& args) { |
21 std::string message; | 20 std::string message; |
22 bool first = true; | 21 bool first = true; |
23 for (int i = 0; i < args.Length(); i++) { | 22 for (int i = 0; i < args.Length(); i++) { |
24 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 23 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 g_test_result_ok = false; | 88 g_test_result_ok = false; |
90 std::string test_js; | 89 std::string test_js; |
91 if (!ExecuteJavascriptLibraries()) | 90 if (!ExecuteJavascriptLibraries()) |
92 return false; | 91 return false; |
93 | 92 |
94 v8::Isolate* isolate = blink::mainThreadIsolate(); | 93 v8::Isolate* isolate = blink::mainThreadIsolate(); |
95 v8::HandleScope handle_scope(isolate); | 94 v8::HandleScope handle_scope(isolate); |
96 v8::Local<v8::Context> context = | 95 v8::Local<v8::Context> context = |
97 v8::Local<v8::Context>::New(isolate, context_); | 96 v8::Local<v8::Context>::New(isolate, context_); |
98 v8::Context::Scope context_scope(context); | 97 v8::Context::Scope context_scope(context); |
99 blink::WebScopedMicrotaskSuppression microtasks_scope; | |
100 | 98 |
101 v8::Local<v8::Value> function_property = | 99 v8::Local<v8::Value> function_property = |
102 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); | 100 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); |
103 EXPECT_FALSE(function_property.IsEmpty()); | 101 EXPECT_FALSE(function_property.IsEmpty()); |
104 if (::testing::Test::HasNonfatalFailure()) | 102 if (::testing::Test::HasNonfatalFailure()) |
105 return false; | 103 return false; |
106 EXPECT_TRUE(function_property->IsFunction()); | 104 EXPECT_TRUE(function_property->IsFunction()); |
107 if (::testing::Test::HasNonfatalFailure()) | 105 if (::testing::Test::HasNonfatalFailure()) |
108 return false; | 106 return false; |
109 v8::Local<v8::Function> function = | 107 v8::Local<v8::Function> function = |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 isolate, value.c_str(), v8::String::kNormalString, value.length())); | 202 isolate, value.c_str(), v8::String::kNormalString, value.length())); |
205 } | 203 } |
206 | 204 |
207 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, | 205 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, |
208 const base::StringPiece& script_name) { | 206 const base::StringPiece& script_name) { |
209 v8::Isolate* isolate = blink::mainThreadIsolate(); | 207 v8::Isolate* isolate = blink::mainThreadIsolate(); |
210 v8::HandleScope handle_scope(isolate); | 208 v8::HandleScope handle_scope(isolate); |
211 v8::Local<v8::Context> context = | 209 v8::Local<v8::Context> context = |
212 v8::Local<v8::Context>::New(isolate, context_); | 210 v8::Local<v8::Context>::New(isolate, context_); |
213 v8::Context::Scope context_scope(context); | 211 v8::Context::Scope context_scope(context); |
214 blink::WebScopedMicrotaskSuppression microtasks_scope; | |
215 v8::Local<v8::String> source = | 212 v8::Local<v8::String> source = |
216 v8::String::NewFromUtf8(isolate, | 213 v8::String::NewFromUtf8(isolate, |
217 script_source.data(), | 214 script_source.data(), |
218 v8::String::kNormalString, | 215 v8::String::kNormalString, |
219 script_source.size()); | 216 script_source.size()); |
220 v8::Local<v8::String> name = | 217 v8::Local<v8::String> name = |
221 v8::String::NewFromUtf8(isolate, | 218 v8::String::NewFromUtf8(isolate, |
222 script_name.data(), | 219 script_name.data(), |
223 v8::String::kNormalString, | 220 v8::String::kNormalString, |
224 script_name.size()); | 221 script_name.size()); |
(...skipping 28 matching lines...) Expand all Loading... |
253 } | 250 } |
254 return str; | 251 return str; |
255 } | 252 } |
256 | 253 |
257 void V8UnitTest::TestFunction(const std::string& function_name) { | 254 void V8UnitTest::TestFunction(const std::string& function_name) { |
258 v8::Isolate* isolate = blink::mainThreadIsolate(); | 255 v8::Isolate* isolate = blink::mainThreadIsolate(); |
259 v8::HandleScope handle_scope(isolate); | 256 v8::HandleScope handle_scope(isolate); |
260 v8::Local<v8::Context> context = | 257 v8::Local<v8::Context> context = |
261 v8::Local<v8::Context>::New(isolate, context_); | 258 v8::Local<v8::Context>::New(isolate, context_); |
262 v8::Context::Scope context_scope(context); | 259 v8::Context::Scope context_scope(context); |
263 blink::WebScopedMicrotaskSuppression microtasks_scope; | |
264 | 260 |
265 v8::Local<v8::Value> function_property = context->Global()->Get( | 261 v8::Local<v8::Value> function_property = context->Global()->Get( |
266 v8::String::NewFromUtf8(isolate, function_name.c_str())); | 262 v8::String::NewFromUtf8(isolate, function_name.c_str())); |
267 ASSERT_FALSE(function_property.IsEmpty()); | 263 ASSERT_FALSE(function_property.IsEmpty()); |
268 ASSERT_TRUE(function_property->IsFunction()); | 264 ASSERT_TRUE(function_property->IsFunction()); |
269 v8::Local<v8::Function> function = | 265 v8::Local<v8::Function> function = |
270 v8::Local<v8::Function>::Cast(function_property); | 266 v8::Local<v8::Function>::Cast(function_property); |
271 | 267 |
272 v8::TryCatch try_catch(isolate); | 268 v8::TryCatch try_catch(isolate); |
273 v8::Local<v8::Value> result = function->Call(context->Global(), 0, NULL); | 269 v8::Local<v8::Value> result = function->Call(context->Global(), 0, NULL); |
(...skipping 30 matching lines...) Expand all Loading... |
304 v8::Local<v8::Array> test_result(args[1].As<v8::Array>()); | 300 v8::Local<v8::Array> test_result(args[1].As<v8::Array>()); |
305 EXPECT_EQ(2U, test_result->Length()); | 301 EXPECT_EQ(2U, test_result->Length()); |
306 if (::testing::Test::HasNonfatalFailure()) | 302 if (::testing::Test::HasNonfatalFailure()) |
307 return; | 303 return; |
308 g_test_result_ok = test_result->Get(0)->BooleanValue(); | 304 g_test_result_ok = test_result->Get(0)->BooleanValue(); |
309 if (!g_test_result_ok) { | 305 if (!g_test_result_ok) { |
310 v8::String::Utf8Value message(test_result->Get(1)); | 306 v8::String::Utf8Value message(test_result->Get(1)); |
311 LOG(ERROR) << *message; | 307 LOG(ERROR) << *message; |
312 } | 308 } |
313 } | 309 } |
OLD | NEW |