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; | 98 v8::MicrotasksScope microtasks( |
| 99 isolate, v8::MicrotasksScope::kDoNotRunMicrotasks); |
100 | 100 |
101 v8::Local<v8::Value> function_property = | 101 v8::Local<v8::Value> function_property = |
102 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); | 102 context->Global()->Get(v8::String::NewFromUtf8(isolate, "runTest")); |
103 EXPECT_FALSE(function_property.IsEmpty()); | 103 EXPECT_FALSE(function_property.IsEmpty()); |
104 if (::testing::Test::HasNonfatalFailure()) | 104 if (::testing::Test::HasNonfatalFailure()) |
105 return false; | 105 return false; |
106 EXPECT_TRUE(function_property->IsFunction()); | 106 EXPECT_TRUE(function_property->IsFunction()); |
107 if (::testing::Test::HasNonfatalFailure()) | 107 if (::testing::Test::HasNonfatalFailure()) |
108 return false; | 108 return false; |
109 v8::Local<v8::Function> function = | 109 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())); | 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 blink::WebScopedMicrotaskSuppression microtasks_scope; | 214 v8::MicrotasksScope microtasks( |
| 215 isolate, v8::MicrotasksScope::kDoNotRunMicrotasks); |
215 v8::Local<v8::String> source = | 216 v8::Local<v8::String> source = |
216 v8::String::NewFromUtf8(isolate, | 217 v8::String::NewFromUtf8(isolate, |
217 script_source.data(), | 218 script_source.data(), |
218 v8::String::kNormalString, | 219 v8::String::kNormalString, |
219 script_source.size()); | 220 script_source.size()); |
220 v8::Local<v8::String> name = | 221 v8::Local<v8::String> name = |
221 v8::String::NewFromUtf8(isolate, | 222 v8::String::NewFromUtf8(isolate, |
222 script_name.data(), | 223 script_name.data(), |
223 v8::String::kNormalString, | 224 v8::String::kNormalString, |
224 script_name.size()); | 225 script_name.size()); |
(...skipping 28 matching lines...) Expand all Loading... |
253 } | 254 } |
254 return str; | 255 return str; |
255 } | 256 } |
256 | 257 |
257 void V8UnitTest::TestFunction(const std::string& function_name) { | 258 void V8UnitTest::TestFunction(const std::string& function_name) { |
258 v8::Isolate* isolate = blink::mainThreadIsolate(); | 259 v8::Isolate* isolate = blink::mainThreadIsolate(); |
259 v8::HandleScope handle_scope(isolate); | 260 v8::HandleScope handle_scope(isolate); |
260 v8::Local<v8::Context> context = | 261 v8::Local<v8::Context> context = |
261 v8::Local<v8::Context>::New(isolate, context_); | 262 v8::Local<v8::Context>::New(isolate, context_); |
262 v8::Context::Scope context_scope(context); | 263 v8::Context::Scope context_scope(context); |
263 blink::WebScopedMicrotaskSuppression microtasks_scope; | 264 v8::MicrotasksScope microtasks( |
| 265 isolate, v8::MicrotasksScope::kDoNotRunMicrotasks); |
264 | 266 |
265 v8::Local<v8::Value> function_property = context->Global()->Get( | 267 v8::Local<v8::Value> function_property = context->Global()->Get( |
266 v8::String::NewFromUtf8(isolate, function_name.c_str())); | 268 v8::String::NewFromUtf8(isolate, function_name.c_str())); |
267 ASSERT_FALSE(function_property.IsEmpty()); | 269 ASSERT_FALSE(function_property.IsEmpty()); |
268 ASSERT_TRUE(function_property->IsFunction()); | 270 ASSERT_TRUE(function_property->IsFunction()); |
269 v8::Local<v8::Function> function = | 271 v8::Local<v8::Function> function = |
270 v8::Local<v8::Function>::Cast(function_property); | 272 v8::Local<v8::Function>::Cast(function_property); |
271 | 273 |
272 v8::TryCatch try_catch(isolate); | 274 v8::TryCatch try_catch(isolate); |
273 v8::Local<v8::Value> result = function->Call(context->Global(), 0, NULL); | 275 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>()); | 306 v8::Local<v8::Array> test_result(args[1].As<v8::Array>()); |
305 EXPECT_EQ(2U, test_result->Length()); | 307 EXPECT_EQ(2U, test_result->Length()); |
306 if (::testing::Test::HasNonfatalFailure()) | 308 if (::testing::Test::HasNonfatalFailure()) |
307 return; | 309 return; |
308 g_test_result_ok = test_result->Get(0)->BooleanValue(); | 310 g_test_result_ok = test_result->Get(0)->BooleanValue(); |
309 if (!g_test_result_ok) { | 311 if (!g_test_result_ok) { |
310 v8::String::Utf8Value message(test_result->Get(1)); | 312 v8::String::Utf8Value message(test_result->Get(1)); |
311 LOG(ERROR) << *message; | 313 LOG(ERROR) << *message; |
312 } | 314 } |
313 } | 315 } |
OLD | NEW |