| 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 <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/test/values_test_util.h" | 9 #include "base/test/values_test_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 scoped_ptr<base::Value> raw(converter.FromV8Value(val, context)); | 147 scoped_ptr<base::Value> raw(converter.FromV8Value(val, context)); |
| 148 | 148 |
| 149 if (expected_value) { | 149 if (expected_value) { |
| 150 ASSERT_TRUE(raw.get()); | 150 ASSERT_TRUE(raw.get()); |
| 151 EXPECT_TRUE(expected_value->Equals(raw.get())); | 151 EXPECT_TRUE(expected_value->Equals(raw.get())); |
| 152 EXPECT_EQ(expected_type, raw->GetType()); | 152 EXPECT_EQ(expected_type, raw->GetType()); |
| 153 } else { | 153 } else { |
| 154 EXPECT_FALSE(raw.get()); | 154 EXPECT_FALSE(raw.get()); |
| 155 } | 155 } |
| 156 | 156 |
| 157 v8::Handle<v8::Object> object(v8::Object::New()); | 157 v8::Handle<v8::Object> object(v8::Object::New(isolate_)); |
| 158 object->Set(v8::String::NewFromUtf8(isolate_, "test"), val); | 158 object->Set(v8::String::NewFromUtf8(isolate_, "test"), val); |
| 159 scoped_ptr<base::DictionaryValue> dictionary( | 159 scoped_ptr<base::DictionaryValue> dictionary( |
| 160 static_cast<base::DictionaryValue*>( | 160 static_cast<base::DictionaryValue*>( |
| 161 converter.FromV8Value(object, context))); | 161 converter.FromV8Value(object, context))); |
| 162 ASSERT_TRUE(dictionary.get()); | 162 ASSERT_TRUE(dictionary.get()); |
| 163 | 163 |
| 164 if (expected_value) { | 164 if (expected_value) { |
| 165 base::Value* temp = NULL; | 165 base::Value* temp = NULL; |
| 166 ASSERT_TRUE(dictionary->Get("test", &temp)); | 166 ASSERT_TRUE(dictionary->Get("test", &temp)); |
| 167 EXPECT_EQ(expected_type, temp->GetType()); | 167 EXPECT_EQ(expected_type, temp->GetType()); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 } | 448 } |
| 449 | 449 |
| 450 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { | 450 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { |
| 451 v8::HandleScope handle_scope(isolate_); | 451 v8::HandleScope handle_scope(isolate_); |
| 452 v8::Local<v8::Context> context = | 452 v8::Local<v8::Context> context = |
| 453 v8::Local<v8::Context>::New(isolate_, context_); | 453 v8::Local<v8::Context>::New(isolate_, context_); |
| 454 v8::Context::Scope context_scope(context); | 454 v8::Context::Scope context_scope(context); |
| 455 | 455 |
| 456 V8ValueConverterImpl converter; | 456 V8ValueConverterImpl converter; |
| 457 | 457 |
| 458 v8::Handle<v8::Object> object = v8::Object::New().As<v8::Object>(); | 458 v8::Handle<v8::Object> object = v8::Object::New(isolate_).As<v8::Object>(); |
| 459 ASSERT_FALSE(object.IsEmpty()); | 459 ASSERT_FALSE(object.IsEmpty()); |
| 460 object->Set(v8::String::NewFromUtf8(isolate_, "foo"), | 460 object->Set(v8::String::NewFromUtf8(isolate_, "foo"), |
| 461 v8::String::NewFromUtf8(isolate_, "bar")); | 461 v8::String::NewFromUtf8(isolate_, "bar")); |
| 462 object->Set(v8::String::NewFromUtf8(isolate_, "obj"), object); | 462 object->Set(v8::String::NewFromUtf8(isolate_, "obj"), object); |
| 463 | 463 |
| 464 scoped_ptr<base::DictionaryValue> object_result( | 464 scoped_ptr<base::DictionaryValue> object_result( |
| 465 static_cast<base::DictionaryValue*>( | 465 static_cast<base::DictionaryValue*>( |
| 466 converter.FromV8Value(object, context))); | 466 converter.FromV8Value(object, context))); |
| 467 ASSERT_TRUE(object_result.get()); | 467 ASSERT_TRUE(object_result.get()); |
| 468 EXPECT_EQ(2u, object_result->size()); | 468 EXPECT_EQ(2u, object_result->size()); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 | 629 |
| 630 // The actual result. | 630 // The actual result. |
| 631 scoped_ptr<base::Value> actual_list( | 631 scoped_ptr<base::Value> actual_list( |
| 632 converter.FromV8Value(recursive_array, context)); | 632 converter.FromV8Value(recursive_array, context)); |
| 633 ASSERT_TRUE(actual_list.get()); | 633 ASSERT_TRUE(actual_list.get()); |
| 634 | 634 |
| 635 EXPECT_TRUE(expected_list.Equals(actual_list.get())); | 635 EXPECT_TRUE(expected_list.Equals(actual_list.get())); |
| 636 | 636 |
| 637 // Now create a recursive object | 637 // Now create a recursive object |
| 638 const std::string key("key"); | 638 const std::string key("key"); |
| 639 v8::Handle<v8::Object> recursive_object(v8::Object::New()); | 639 v8::Handle<v8::Object> recursive_object(v8::Object::New(isolate_)); |
| 640 v8::TryCatch try_catch; | 640 v8::TryCatch try_catch; |
| 641 recursive_object->Set( | 641 recursive_object->Set( |
| 642 v8::String::NewFromUtf8( | 642 v8::String::NewFromUtf8( |
| 643 isolate_, key.c_str(), v8::String::kNormalString, key.length()), | 643 isolate_, key.c_str(), v8::String::kNormalString, key.length()), |
| 644 recursive_object); | 644 recursive_object); |
| 645 ASSERT_FALSE(try_catch.HasCaught()); | 645 ASSERT_FALSE(try_catch.HasCaught()); |
| 646 | 646 |
| 647 // The first repetition should be trimmed and replaced by a null value. | 647 // The first repetition should be trimmed and replaced by a null value. |
| 648 base::DictionaryValue expected_dictionary; | 648 base::DictionaryValue expected_dictionary; |
| 649 expected_dictionary.Set(key, base::Value::CreateNullValue()); | 649 expected_dictionary.Set(key, base::Value::CreateNullValue()); |
| 650 | 650 |
| 651 // The actual result. | 651 // The actual result. |
| 652 scoped_ptr<base::Value> actual_dictionary( | 652 scoped_ptr<base::Value> actual_dictionary( |
| 653 converter.FromV8Value(recursive_object, context)); | 653 converter.FromV8Value(recursive_object, context)); |
| 654 ASSERT_TRUE(actual_dictionary.get()); | 654 ASSERT_TRUE(actual_dictionary.get()); |
| 655 | 655 |
| 656 EXPECT_TRUE(expected_dictionary.Equals(actual_dictionary.get())); | 656 EXPECT_TRUE(expected_dictionary.Equals(actual_dictionary.get())); |
| 657 } | 657 } |
| 658 | 658 |
| 659 TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) { | 659 TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) { |
| 660 v8::HandleScope handle_scope(isolate_); | 660 v8::HandleScope handle_scope(isolate_); |
| 661 v8::Local<v8::Context> context = | 661 v8::Local<v8::Context> context = |
| 662 v8::Local<v8::Context>::New(isolate_, context_); | 662 v8::Local<v8::Context>::New(isolate_, context_); |
| 663 v8::Context::Scope context_scope(context); | 663 v8::Context::Scope context_scope(context); |
| 664 | 664 |
| 665 // Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc. | 665 // Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc. |
| 666 int kDepth = 1000; | 666 int kDepth = 1000; |
| 667 const char kKey[] = "key"; | 667 const char kKey[] = "key"; |
| 668 | 668 |
| 669 v8::Local<v8::Object> deep_object = v8::Object::New(); | 669 v8::Local<v8::Object> deep_object = v8::Object::New(isolate_); |
| 670 | 670 |
| 671 v8::Local<v8::Object> leaf = deep_object; | 671 v8::Local<v8::Object> leaf = deep_object; |
| 672 for (int i = 0; i < kDepth; ++i) { | 672 for (int i = 0; i < kDepth; ++i) { |
| 673 v8::Local<v8::Object> new_object = v8::Object::New(); | 673 v8::Local<v8::Object> new_object = v8::Object::New(isolate_); |
| 674 leaf->Set(v8::String::NewFromUtf8(isolate_, kKey), new_object); | 674 leaf->Set(v8::String::NewFromUtf8(isolate_, kKey), new_object); |
| 675 leaf = new_object; | 675 leaf = new_object; |
| 676 } | 676 } |
| 677 | 677 |
| 678 V8ValueConverterImpl converter; | 678 V8ValueConverterImpl converter; |
| 679 scoped_ptr<base::Value> value(converter.FromV8Value(deep_object, context)); | 679 scoped_ptr<base::Value> value(converter.FromV8Value(deep_object, context)); |
| 680 ASSERT_TRUE(value); | 680 ASSERT_TRUE(value); |
| 681 | 681 |
| 682 // Expected depth is kMaxRecursionDepth in v8_value_converter_impl.cc. | 682 // Expected depth is kMaxRecursionDepth in v8_value_converter_impl.cc. |
| 683 int kExpectedDepth = 100; | 683 int kExpectedDepth = 100; |
| 684 | 684 |
| 685 base::Value* current = value.get(); | 685 base::Value* current = value.get(); |
| 686 for (int i = 1; i < kExpectedDepth; ++i) { | 686 for (int i = 1; i < kExpectedDepth; ++i) { |
| 687 base::DictionaryValue* current_as_object = NULL; | 687 base::DictionaryValue* current_as_object = NULL; |
| 688 ASSERT_TRUE(current->GetAsDictionary(¤t_as_object)) << i; | 688 ASSERT_TRUE(current->GetAsDictionary(¤t_as_object)) << i; |
| 689 ASSERT_TRUE(current_as_object->Get(kKey, ¤t)) << i; | 689 ASSERT_TRUE(current_as_object->Get(kKey, ¤t)) << i; |
| 690 } | 690 } |
| 691 | 691 |
| 692 // The leaf node shouldn't have any properties. | 692 // The leaf node shouldn't have any properties. |
| 693 base::DictionaryValue empty; | 693 base::DictionaryValue empty; |
| 694 EXPECT_TRUE(base::Value::Equals(&empty, current)) << *current; | 694 EXPECT_TRUE(base::Value::Equals(&empty, current)) << *current; |
| 695 } | 695 } |
| 696 | 696 |
| 697 } // namespace content | 697 } // namespace content |
| OLD | NEW |