OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/test/values_test_util.h" | 13 #include "base/test/values_test_util.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "content/child/v8_value_converter_impl.h" | 15 #include "content/child/v8_value_converter_impl.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
17 #include "v8/include/v8.h" | 18 #include "v8/include/v8.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 // To improve the performance of | 22 // To improve the performance of |
22 // V8ValueConverterImpl::UpdateAndCheckUniqueness, identity hashes of objects | 23 // V8ValueConverterImpl::UpdateAndCheckUniqueness, identity hashes of objects |
23 // are used during checking for duplicates. For testing purposes we need to | 24 // are used during checking for duplicates. For testing purposes we need to |
24 // ignore the hash sometimes. Create this helper object to avoid using identity | 25 // ignore the hash sometimes. Create this helper object to avoid using identity |
25 // hashes for the lifetime of the helper. | 26 // hashes for the lifetime of the helper. |
26 class ScopedAvoidIdentityHashForTesting { | 27 class ScopedAvoidIdentityHashForTesting { |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 converter.ToV8Value(original.get(), context), context)); | 281 converter.ToV8Value(original.get(), context), context)); |
281 | 282 |
282 EXPECT_TRUE(original->Equals(copy.get())); | 283 EXPECT_TRUE(original->Equals(copy.get())); |
283 } | 284 } |
284 | 285 |
285 TEST_F(V8ValueConverterImplTest, ObjectExceptions) { | 286 TEST_F(V8ValueConverterImplTest, ObjectExceptions) { |
286 v8::HandleScope handle_scope(isolate_); | 287 v8::HandleScope handle_scope(isolate_); |
287 v8::Local<v8::Context> context = | 288 v8::Local<v8::Context> context = |
288 v8::Local<v8::Context>::New(isolate_, context_); | 289 v8::Local<v8::Context>::New(isolate_, context_); |
289 v8::Context::Scope context_scope(context); | 290 v8::Context::Scope context_scope(context); |
290 v8::MicrotasksScope microtasks( | 291 blink::WebScopedMicrotaskSuppression microtasks_scope; |
291 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
292 | 292 |
293 // Set up objects to throw when reading or writing 'foo'. | 293 // Set up objects to throw when reading or writing 'foo'. |
294 const char* source = | 294 const char* source = |
295 "Object.prototype.__defineSetter__('foo', " | 295 "Object.prototype.__defineSetter__('foo', " |
296 " function() { throw new Error('muah!'); });" | 296 " function() { throw new Error('muah!'); });" |
297 "Object.prototype.__defineGetter__('foo', " | 297 "Object.prototype.__defineGetter__('foo', " |
298 " function() { throw new Error('muah!'); });"; | 298 " function() { throw new Error('muah!'); });"; |
299 | 299 |
300 v8::Local<v8::Script> script( | 300 v8::Local<v8::Script> script( |
301 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 301 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
(...skipping 22 matching lines...) Expand all Loading... |
324 EXPECT_FALSE(copy.IsEmpty()); | 324 EXPECT_FALSE(copy.IsEmpty()); |
325 EXPECT_EQ(2u, copy->GetPropertyNames()->Length()); | 325 EXPECT_EQ(2u, copy->GetPropertyNames()->Length()); |
326 EXPECT_EQ("bar", GetString(copy, "bar")); | 326 EXPECT_EQ("bar", GetString(copy, "bar")); |
327 } | 327 } |
328 | 328 |
329 TEST_F(V8ValueConverterImplTest, ArrayExceptions) { | 329 TEST_F(V8ValueConverterImplTest, ArrayExceptions) { |
330 v8::HandleScope handle_scope(isolate_); | 330 v8::HandleScope handle_scope(isolate_); |
331 v8::Local<v8::Context> context = | 331 v8::Local<v8::Context> context = |
332 v8::Local<v8::Context>::New(isolate_, context_); | 332 v8::Local<v8::Context>::New(isolate_, context_); |
333 v8::Context::Scope context_scope(context); | 333 v8::Context::Scope context_scope(context); |
334 v8::MicrotasksScope microtasks( | 334 blink::WebScopedMicrotaskSuppression microtasks_scope; |
335 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
336 | 335 |
337 const char* source = "(function() {" | 336 const char* source = "(function() {" |
338 "var arr = [];" | 337 "var arr = [];" |
339 "arr.__defineSetter__(0, " | 338 "arr.__defineSetter__(0, " |
340 " function() { throw new Error('muah!'); });" | 339 " function() { throw new Error('muah!'); });" |
341 "arr.__defineGetter__(0, " | 340 "arr.__defineGetter__(0, " |
342 " function() { throw new Error('muah!'); });" | 341 " function() { throw new Error('muah!'); });" |
343 "arr[1] = 'bar';" | 342 "arr[1] = 'bar';" |
344 "return arr;" | 343 "return arr;" |
345 "})();"; | 344 "})();"; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 regex, | 401 regex, |
403 base::Value::TYPE_STRING, | 402 base::Value::TYPE_STRING, |
404 scoped_ptr<base::Value>(new base::StringValue("/./"))); | 403 scoped_ptr<base::Value>(new base::StringValue("/./"))); |
405 } | 404 } |
406 | 405 |
407 TEST_F(V8ValueConverterImplTest, Prototype) { | 406 TEST_F(V8ValueConverterImplTest, Prototype) { |
408 v8::HandleScope handle_scope(isolate_); | 407 v8::HandleScope handle_scope(isolate_); |
409 v8::Local<v8::Context> context = | 408 v8::Local<v8::Context> context = |
410 v8::Local<v8::Context>::New(isolate_, context_); | 409 v8::Local<v8::Context>::New(isolate_, context_); |
411 v8::Context::Scope context_scope(context); | 410 v8::Context::Scope context_scope(context); |
412 v8::MicrotasksScope microtasks( | 411 blink::WebScopedMicrotaskSuppression microtasks_scope; |
413 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
414 | 412 |
415 const char* source = "(function() {" | 413 const char* source = "(function() {" |
416 "Object.prototype.foo = 'foo';" | 414 "Object.prototype.foo = 'foo';" |
417 "return {};" | 415 "return {};" |
418 "})();"; | 416 "})();"; |
419 | 417 |
420 v8::Local<v8::Script> script( | 418 v8::Local<v8::Script> script( |
421 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 419 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
422 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); | 420 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
423 ASSERT_FALSE(object.IsEmpty()); | 421 ASSERT_FALSE(object.IsEmpty()); |
424 | 422 |
425 V8ValueConverterImpl converter; | 423 V8ValueConverterImpl converter; |
426 scoped_ptr<base::DictionaryValue> result( | 424 scoped_ptr<base::DictionaryValue> result( |
427 static_cast<base::DictionaryValue*>( | 425 static_cast<base::DictionaryValue*>( |
428 converter.FromV8Value(object, context))); | 426 converter.FromV8Value(object, context))); |
429 ASSERT_TRUE(result.get()); | 427 ASSERT_TRUE(result.get()); |
430 EXPECT_EQ(0u, result->size()); | 428 EXPECT_EQ(0u, result->size()); |
431 } | 429 } |
432 | 430 |
433 TEST_F(V8ValueConverterImplTest, StripNullFromObjects) { | 431 TEST_F(V8ValueConverterImplTest, StripNullFromObjects) { |
434 v8::HandleScope handle_scope(isolate_); | 432 v8::HandleScope handle_scope(isolate_); |
435 v8::Local<v8::Context> context = | 433 v8::Local<v8::Context> context = |
436 v8::Local<v8::Context>::New(isolate_, context_); | 434 v8::Local<v8::Context>::New(isolate_, context_); |
437 v8::Context::Scope context_scope(context); | 435 v8::Context::Scope context_scope(context); |
438 v8::MicrotasksScope microtasks( | 436 blink::WebScopedMicrotaskSuppression microtasks_scope; |
439 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
440 | 437 |
441 const char* source = "(function() {" | 438 const char* source = "(function() {" |
442 "return { foo: undefined, bar: null };" | 439 "return { foo: undefined, bar: null };" |
443 "})();"; | 440 "})();"; |
444 | 441 |
445 v8::Local<v8::Script> script( | 442 v8::Local<v8::Script> script( |
446 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 443 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
447 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); | 444 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
448 ASSERT_FALSE(object.IsEmpty()); | 445 ASSERT_FALSE(object.IsEmpty()); |
449 | 446 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 ASSERT_TRUE(list_result.get()); | 485 ASSERT_TRUE(list_result.get()); |
489 EXPECT_EQ(2u, list_result->GetSize()); | 486 EXPECT_EQ(2u, list_result->GetSize()); |
490 EXPECT_TRUE(IsNull(list_result.get(), 1)); | 487 EXPECT_TRUE(IsNull(list_result.get(), 1)); |
491 } | 488 } |
492 | 489 |
493 TEST_F(V8ValueConverterImplTest, WeirdProperties) { | 490 TEST_F(V8ValueConverterImplTest, WeirdProperties) { |
494 v8::HandleScope handle_scope(isolate_); | 491 v8::HandleScope handle_scope(isolate_); |
495 v8::Local<v8::Context> context = | 492 v8::Local<v8::Context> context = |
496 v8::Local<v8::Context>::New(isolate_, context_); | 493 v8::Local<v8::Context>::New(isolate_, context_); |
497 v8::Context::Scope context_scope(context); | 494 v8::Context::Scope context_scope(context); |
498 v8::MicrotasksScope microtasks( | 495 blink::WebScopedMicrotaskSuppression microtasks_scope; |
499 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
500 | 496 |
501 const char* source = "(function() {" | 497 const char* source = "(function() {" |
502 "return {" | 498 "return {" |
503 "1: 'foo'," | 499 "1: 'foo'," |
504 "'2': 'bar'," | 500 "'2': 'bar'," |
505 "true: 'baz'," | 501 "true: 'baz'," |
506 "false: 'qux'," | 502 "false: 'qux'," |
507 "null: 'quux'," | 503 "null: 'quux'," |
508 "undefined: 'oops'" | 504 "undefined: 'oops'" |
509 "};" | 505 "};" |
(...skipping 18 matching lines...) Expand all Loading... |
528 "}"); | 524 "}"); |
529 | 525 |
530 EXPECT_TRUE(expected->Equals(actual.get())); | 526 EXPECT_TRUE(expected->Equals(actual.get())); |
531 } | 527 } |
532 | 528 |
533 TEST_F(V8ValueConverterImplTest, ArrayGetters) { | 529 TEST_F(V8ValueConverterImplTest, ArrayGetters) { |
534 v8::HandleScope handle_scope(isolate_); | 530 v8::HandleScope handle_scope(isolate_); |
535 v8::Local<v8::Context> context = | 531 v8::Local<v8::Context> context = |
536 v8::Local<v8::Context>::New(isolate_, context_); | 532 v8::Local<v8::Context>::New(isolate_, context_); |
537 v8::Context::Scope context_scope(context); | 533 v8::Context::Scope context_scope(context); |
538 v8::MicrotasksScope microtasks( | 534 blink::WebScopedMicrotaskSuppression microtasks_scope; |
539 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
540 | 535 |
541 const char* source = "(function() {" | 536 const char* source = "(function() {" |
542 "var a = [0];" | 537 "var a = [0];" |
543 "a.__defineGetter__(1, function() { return 'bar'; });" | 538 "a.__defineGetter__(1, function() { return 'bar'; });" |
544 "return a;" | 539 "return a;" |
545 "})();"; | 540 "})();"; |
546 | 541 |
547 v8::Local<v8::Script> script( | 542 v8::Local<v8::Script> script( |
548 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 543 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
549 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); | 544 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
550 ASSERT_FALSE(array.IsEmpty()); | 545 ASSERT_FALSE(array.IsEmpty()); |
551 | 546 |
552 V8ValueConverterImpl converter; | 547 V8ValueConverterImpl converter; |
553 scoped_ptr<base::ListValue> result( | 548 scoped_ptr<base::ListValue> result( |
554 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 549 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); |
555 ASSERT_TRUE(result.get()); | 550 ASSERT_TRUE(result.get()); |
556 EXPECT_EQ(2u, result->GetSize()); | 551 EXPECT_EQ(2u, result->GetSize()); |
557 } | 552 } |
558 | 553 |
559 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { | 554 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { |
560 v8::HandleScope handle_scope(isolate_); | 555 v8::HandleScope handle_scope(isolate_); |
561 v8::Local<v8::Context> context = | 556 v8::Local<v8::Context> context = |
562 v8::Local<v8::Context>::New(isolate_, context_); | 557 v8::Local<v8::Context>::New(isolate_, context_); |
563 v8::Context::Scope context_scope(context); | 558 v8::Context::Scope context_scope(context); |
564 v8::MicrotasksScope microtasks( | 559 blink::WebScopedMicrotaskSuppression microtasks_scope; |
565 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); | |
566 | 560 |
567 v8::Local<v8::Object> object; | 561 v8::Local<v8::Object> object; |
568 { | 562 { |
569 const char* source = "(function() {" | 563 const char* source = "(function() {" |
570 "return { foo: undefined, bar: null, baz: function(){} };" | 564 "return { foo: undefined, bar: null, baz: function(){} };" |
571 "})();"; | 565 "})();"; |
572 v8::Local<v8::Script> script( | 566 v8::Local<v8::Script> script( |
573 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 567 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
574 object = script->Run().As<v8::Object>(); | 568 object = script->Run().As<v8::Object>(); |
575 ASSERT_FALSE(object.IsEmpty()); | 569 ASSERT_FALSE(object.IsEmpty()); |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 EXPECT_TRUE( | 878 EXPECT_TRUE( |
885 base::Value::Equals(reference_number_value.get(), number_value.get())); | 879 base::Value::Equals(reference_number_value.get(), number_value.get())); |
886 | 880 |
887 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); | 881 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); |
888 scoped_ptr<base::Value> undefined_value( | 882 scoped_ptr<base::Value> undefined_value( |
889 converter.FromV8Value(undefined, context)); | 883 converter.FromV8Value(undefined, context)); |
890 EXPECT_FALSE(undefined_value); | 884 EXPECT_FALSE(undefined_value); |
891 } | 885 } |
892 | 886 |
893 } // namespace content | 887 } // namespace content |
OLD | NEW |