| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 class V8ValueConverterImplTest : public testing::Test { | 47 class V8ValueConverterImplTest : public testing::Test { |
| 48 public: | 48 public: |
| 49 V8ValueConverterImplTest() | 49 V8ValueConverterImplTest() |
| 50 : isolate_(v8::Isolate::GetCurrent()) { | 50 : isolate_(v8::Isolate::GetCurrent()) { |
| 51 } | 51 } |
| 52 | 52 |
| 53 protected: | 53 protected: |
| 54 void SetUp() override { | 54 void SetUp() override { |
| 55 v8::HandleScope handle_scope(isolate_); | 55 v8::HandleScope handle_scope(isolate_); |
| 56 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_); | 56 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_); |
| 57 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); | 57 context_.Reset(isolate_, v8::Context::New(isolate_, NULL, global)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void TearDown() override { context_.Reset(); } | 60 void TearDown() override { context_.Reset(); } |
| 61 | 61 |
| 62 std::string GetString(base::DictionaryValue* value, const std::string& key) { | 62 std::string GetString(base::DictionaryValue* value, const std::string& key) { |
| 63 std::string temp; | 63 std::string temp; |
| 64 if (!value->GetString(key, &temp)) { | 64 if (!value->GetString(key, &temp)) { |
| 65 ADD_FAILURE(); | 65 ADD_FAILURE(); |
| 66 return std::string(); | 66 return std::string(); |
| 67 } | 67 } |
| 68 return temp; | 68 return temp; |
| 69 } | 69 } |
| 70 | 70 |
| 71 std::string GetString(v8::Handle<v8::Object> value, const std::string& key) { | 71 std::string GetString(v8::Local<v8::Object> value, const std::string& key) { |
| 72 v8::Handle<v8::String> temp = | 72 v8::Local<v8::String> temp = |
| 73 value->Get(v8::String::NewFromUtf8(isolate_, key.c_str())) | 73 value->Get(v8::String::NewFromUtf8(isolate_, key.c_str())) |
| 74 .As<v8::String>(); | 74 .As<v8::String>(); |
| 75 if (temp.IsEmpty()) { | 75 if (temp.IsEmpty()) { |
| 76 ADD_FAILURE(); | 76 ADD_FAILURE(); |
| 77 return std::string(); | 77 return std::string(); |
| 78 } | 78 } |
| 79 v8::String::Utf8Value utf8(temp); | 79 v8::String::Utf8Value utf8(temp); |
| 80 return std::string(*utf8, utf8.length()); | 80 return std::string(*utf8, utf8.length()); |
| 81 } | 81 } |
| 82 | 82 |
| 83 std::string GetString(base::ListValue* value, uint32 index) { | 83 std::string GetString(base::ListValue* value, uint32 index) { |
| 84 std::string temp; | 84 std::string temp; |
| 85 if (!value->GetString(static_cast<size_t>(index), &temp)) { | 85 if (!value->GetString(static_cast<size_t>(index), &temp)) { |
| 86 ADD_FAILURE(); | 86 ADD_FAILURE(); |
| 87 return std::string(); | 87 return std::string(); |
| 88 } | 88 } |
| 89 return temp; | 89 return temp; |
| 90 } | 90 } |
| 91 | 91 |
| 92 std::string GetString(v8::Handle<v8::Array> value, uint32 index) { | 92 std::string GetString(v8::Local<v8::Array> value, uint32 index) { |
| 93 v8::Handle<v8::String> temp = value->Get(index).As<v8::String>(); | 93 v8::Local<v8::String> temp = value->Get(index).As<v8::String>(); |
| 94 if (temp.IsEmpty()) { | 94 if (temp.IsEmpty()) { |
| 95 ADD_FAILURE(); | 95 ADD_FAILURE(); |
| 96 return std::string(); | 96 return std::string(); |
| 97 } | 97 } |
| 98 v8::String::Utf8Value utf8(temp); | 98 v8::String::Utf8Value utf8(temp); |
| 99 return std::string(*utf8, utf8.length()); | 99 return std::string(*utf8, utf8.length()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool IsNull(base::DictionaryValue* value, const std::string& key) { | 102 bool IsNull(base::DictionaryValue* value, const std::string& key) { |
| 103 base::Value* child = NULL; | 103 base::Value* child = NULL; |
| 104 if (!value->Get(key, &child)) { | 104 if (!value->Get(key, &child)) { |
| 105 ADD_FAILURE(); | 105 ADD_FAILURE(); |
| 106 return false; | 106 return false; |
| 107 } | 107 } |
| 108 return child->GetType() == base::Value::TYPE_NULL; | 108 return child->GetType() == base::Value::TYPE_NULL; |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool IsNull(v8::Handle<v8::Object> value, const std::string& key) { | 111 bool IsNull(v8::Local<v8::Object> value, const std::string& key) { |
| 112 v8::Handle<v8::Value> child = | 112 v8::Local<v8::Value> child = |
| 113 value->Get(v8::String::NewFromUtf8(isolate_, key.c_str())); | 113 value->Get(v8::String::NewFromUtf8(isolate_, key.c_str())); |
| 114 if (child.IsEmpty()) { | 114 if (child.IsEmpty()) { |
| 115 ADD_FAILURE(); | 115 ADD_FAILURE(); |
| 116 return false; | 116 return false; |
| 117 } | 117 } |
| 118 return child->IsNull(); | 118 return child->IsNull(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool IsNull(base::ListValue* value, uint32 index) { | 121 bool IsNull(base::ListValue* value, uint32 index) { |
| 122 base::Value* child = NULL; | 122 base::Value* child = NULL; |
| 123 if (!value->Get(static_cast<size_t>(index), &child)) { | 123 if (!value->Get(static_cast<size_t>(index), &child)) { |
| 124 ADD_FAILURE(); | 124 ADD_FAILURE(); |
| 125 return false; | 125 return false; |
| 126 } | 126 } |
| 127 return child->GetType() == base::Value::TYPE_NULL; | 127 return child->GetType() == base::Value::TYPE_NULL; |
| 128 } | 128 } |
| 129 | 129 |
| 130 bool IsNull(v8::Handle<v8::Array> value, uint32 index) { | 130 bool IsNull(v8::Local<v8::Array> value, uint32 index) { |
| 131 v8::Handle<v8::Value> child = value->Get(index); | 131 v8::Local<v8::Value> child = value->Get(index); |
| 132 if (child.IsEmpty()) { | 132 if (child.IsEmpty()) { |
| 133 ADD_FAILURE(); | 133 ADD_FAILURE(); |
| 134 return false; | 134 return false; |
| 135 } | 135 } |
| 136 return child->IsNull(); | 136 return child->IsNull(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void TestWeirdType(const V8ValueConverterImpl& converter, | 139 void TestWeirdType(const V8ValueConverterImpl& converter, |
| 140 v8::Handle<v8::Value> val, | 140 v8::Local<v8::Value> val, |
| 141 base::Value::Type expected_type, | 141 base::Value::Type expected_type, |
| 142 scoped_ptr<base::Value> expected_value) { | 142 scoped_ptr<base::Value> expected_value) { |
| 143 v8::Local<v8::Context> context = | 143 v8::Local<v8::Context> context = |
| 144 v8::Local<v8::Context>::New(isolate_, context_); | 144 v8::Local<v8::Context>::New(isolate_, context_); |
| 145 scoped_ptr<base::Value> raw(converter.FromV8Value(val, context)); | 145 scoped_ptr<base::Value> raw(converter.FromV8Value(val, context)); |
| 146 | 146 |
| 147 if (expected_value) { | 147 if (expected_value) { |
| 148 ASSERT_TRUE(raw.get()); | 148 ASSERT_TRUE(raw.get()); |
| 149 EXPECT_TRUE(expected_value->Equals(raw.get())); | 149 EXPECT_TRUE(expected_value->Equals(raw.get())); |
| 150 EXPECT_EQ(expected_type, raw->GetType()); | 150 EXPECT_EQ(expected_type, raw->GetType()); |
| 151 } else { | 151 } else { |
| 152 EXPECT_FALSE(raw.get()); | 152 EXPECT_FALSE(raw.get()); |
| 153 } | 153 } |
| 154 | 154 |
| 155 v8::Handle<v8::Object> object(v8::Object::New(isolate_)); | 155 v8::Local<v8::Object> object(v8::Object::New(isolate_)); |
| 156 object->Set(v8::String::NewFromUtf8(isolate_, "test"), val); | 156 object->Set(v8::String::NewFromUtf8(isolate_, "test"), val); |
| 157 scoped_ptr<base::DictionaryValue> dictionary( | 157 scoped_ptr<base::DictionaryValue> dictionary( |
| 158 static_cast<base::DictionaryValue*>( | 158 static_cast<base::DictionaryValue*>( |
| 159 converter.FromV8Value(object, context))); | 159 converter.FromV8Value(object, context))); |
| 160 ASSERT_TRUE(dictionary.get()); | 160 ASSERT_TRUE(dictionary.get()); |
| 161 | 161 |
| 162 if (expected_value) { | 162 if (expected_value) { |
| 163 base::Value* temp = NULL; | 163 base::Value* temp = NULL; |
| 164 ASSERT_TRUE(dictionary->Get("test", &temp)); | 164 ASSERT_TRUE(dictionary->Get("test", &temp)); |
| 165 EXPECT_EQ(expected_type, temp->GetType()); | 165 EXPECT_EQ(expected_type, temp->GetType()); |
| 166 EXPECT_TRUE(expected_value->Equals(temp)); | 166 EXPECT_TRUE(expected_value->Equals(temp)); |
| 167 } else { | 167 } else { |
| 168 EXPECT_FALSE(dictionary->HasKey("test")); | 168 EXPECT_FALSE(dictionary->HasKey("test")); |
| 169 } | 169 } |
| 170 | 170 |
| 171 v8::Handle<v8::Array> array(v8::Array::New(isolate_)); | 171 v8::Local<v8::Array> array(v8::Array::New(isolate_)); |
| 172 array->Set(0, val); | 172 array->Set(0, val); |
| 173 scoped_ptr<base::ListValue> list( | 173 scoped_ptr<base::ListValue> list( |
| 174 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 174 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); |
| 175 ASSERT_TRUE(list.get()); | 175 ASSERT_TRUE(list.get()); |
| 176 if (expected_value) { | 176 if (expected_value) { |
| 177 base::Value* temp = NULL; | 177 base::Value* temp = NULL; |
| 178 ASSERT_TRUE(list->Get(0, &temp)); | 178 ASSERT_TRUE(list->Get(0, &temp)); |
| 179 EXPECT_EQ(expected_type, temp->GetType()); | 179 EXPECT_EQ(expected_type, temp->GetType()); |
| 180 EXPECT_TRUE(expected_value->Equals(temp)); | 180 EXPECT_TRUE(expected_value->Equals(temp)); |
| 181 } else { | 181 } else { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 " \"list\": [ \"monkey\", \"balls\" ], \n" | 214 " \"list\": [ \"monkey\", \"balls\" ], \n" |
| 215 " \"empty-list\": [], \n" | 215 " \"empty-list\": [], \n" |
| 216 "}"); | 216 "}"); |
| 217 | 217 |
| 218 v8::HandleScope handle_scope(isolate_); | 218 v8::HandleScope handle_scope(isolate_); |
| 219 v8::Local<v8::Context> context = | 219 v8::Local<v8::Context> context = |
| 220 v8::Local<v8::Context>::New(isolate_, context_); | 220 v8::Local<v8::Context>::New(isolate_, context_); |
| 221 v8::Context::Scope context_scope(context); | 221 v8::Context::Scope context_scope(context); |
| 222 | 222 |
| 223 V8ValueConverterImpl converter; | 223 V8ValueConverterImpl converter; |
| 224 v8::Handle<v8::Object> v8_object = | 224 v8::Local<v8::Object> v8_object = |
| 225 converter.ToV8Value(original_root.get(), context).As<v8::Object>(); | 225 converter.ToV8Value(original_root.get(), context).As<v8::Object>(); |
| 226 ASSERT_FALSE(v8_object.IsEmpty()); | 226 ASSERT_FALSE(v8_object.IsEmpty()); |
| 227 | 227 |
| 228 EXPECT_EQ(static_cast<const base::DictionaryValue&>(*original_root).size(), | 228 EXPECT_EQ(static_cast<const base::DictionaryValue&>(*original_root).size(), |
| 229 v8_object->GetPropertyNames()->Length()); | 229 v8_object->GetPropertyNames()->Length()); |
| 230 EXPECT_TRUE( | 230 EXPECT_TRUE( |
| 231 v8_object->Get(v8::String::NewFromUtf8(isolate_, "null"))->IsNull()); | 231 v8_object->Get(v8::String::NewFromUtf8(isolate_, "null"))->IsNull()); |
| 232 EXPECT_TRUE( | 232 EXPECT_TRUE( |
| 233 v8_object->Get(v8::String::NewFromUtf8(isolate_, "true"))->IsTrue()); | 233 v8_object->Get(v8::String::NewFromUtf8(isolate_, "true"))->IsTrue()); |
| 234 EXPECT_TRUE( | 234 EXPECT_TRUE( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 v8::Local<v8::Context>::New(isolate_, context_); | 284 v8::Local<v8::Context>::New(isolate_, context_); |
| 285 v8::Context::Scope context_scope(context); | 285 v8::Context::Scope context_scope(context); |
| 286 | 286 |
| 287 // Set up objects to throw when reading or writing 'foo'. | 287 // Set up objects to throw when reading or writing 'foo'. |
| 288 const char* source = | 288 const char* source = |
| 289 "Object.prototype.__defineSetter__('foo', " | 289 "Object.prototype.__defineSetter__('foo', " |
| 290 " function() { throw new Error('muah!'); });" | 290 " function() { throw new Error('muah!'); });" |
| 291 "Object.prototype.__defineGetter__('foo', " | 291 "Object.prototype.__defineGetter__('foo', " |
| 292 " function() { throw new Error('muah!'); });"; | 292 " function() { throw new Error('muah!'); });"; |
| 293 | 293 |
| 294 v8::Handle<v8::Script> script( | 294 v8::Local<v8::Script> script( |
| 295 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 295 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 296 script->Run(); | 296 script->Run(); |
| 297 | 297 |
| 298 v8::Handle<v8::Object> object(v8::Object::New(isolate_)); | 298 v8::Local<v8::Object> object(v8::Object::New(isolate_)); |
| 299 object->Set(v8::String::NewFromUtf8(isolate_, "bar"), | 299 object->Set(v8::String::NewFromUtf8(isolate_, "bar"), |
| 300 v8::String::NewFromUtf8(isolate_, "bar")); | 300 v8::String::NewFromUtf8(isolate_, "bar")); |
| 301 | 301 |
| 302 // Converting from v8 value should replace the foo property with null. | 302 // Converting from v8 value should replace the foo property with null. |
| 303 V8ValueConverterImpl converter; | 303 V8ValueConverterImpl converter; |
| 304 scoped_ptr<base::DictionaryValue> converted( | 304 scoped_ptr<base::DictionaryValue> converted( |
| 305 static_cast<base::DictionaryValue*>( | 305 static_cast<base::DictionaryValue*>( |
| 306 converter.FromV8Value(object, context))); | 306 converter.FromV8Value(object, context))); |
| 307 EXPECT_TRUE(converted.get()); | 307 EXPECT_TRUE(converted.get()); |
| 308 // http://code.google.com/p/v8/issues/detail?id=1342 | 308 // http://code.google.com/p/v8/issues/detail?id=1342 |
| 309 // EXPECT_EQ(2u, converted->size()); | 309 // EXPECT_EQ(2u, converted->size()); |
| 310 // EXPECT_TRUE(IsNull(converted.get(), "foo")); | 310 // EXPECT_TRUE(IsNull(converted.get(), "foo")); |
| 311 EXPECT_EQ(1u, converted->size()); | 311 EXPECT_EQ(1u, converted->size()); |
| 312 EXPECT_EQ("bar", GetString(converted.get(), "bar")); | 312 EXPECT_EQ("bar", GetString(converted.get(), "bar")); |
| 313 | 313 |
| 314 // Converting to v8 value should drop the foo property. | 314 // Converting to v8 value should drop the foo property. |
| 315 converted->SetString("foo", "foo"); | 315 converted->SetString("foo", "foo"); |
| 316 v8::Handle<v8::Object> copy = | 316 v8::Local<v8::Object> copy = |
| 317 converter.ToV8Value(converted.get(), context).As<v8::Object>(); | 317 converter.ToV8Value(converted.get(), context).As<v8::Object>(); |
| 318 EXPECT_FALSE(copy.IsEmpty()); | 318 EXPECT_FALSE(copy.IsEmpty()); |
| 319 EXPECT_EQ(2u, copy->GetPropertyNames()->Length()); | 319 EXPECT_EQ(2u, copy->GetPropertyNames()->Length()); |
| 320 EXPECT_EQ("bar", GetString(copy, "bar")); | 320 EXPECT_EQ("bar", GetString(copy, "bar")); |
| 321 } | 321 } |
| 322 | 322 |
| 323 TEST_F(V8ValueConverterImplTest, ArrayExceptions) { | 323 TEST_F(V8ValueConverterImplTest, ArrayExceptions) { |
| 324 v8::HandleScope handle_scope(isolate_); | 324 v8::HandleScope handle_scope(isolate_); |
| 325 v8::Local<v8::Context> context = | 325 v8::Local<v8::Context> context = |
| 326 v8::Local<v8::Context>::New(isolate_, context_); | 326 v8::Local<v8::Context>::New(isolate_, context_); |
| 327 v8::Context::Scope context_scope(context); | 327 v8::Context::Scope context_scope(context); |
| 328 | 328 |
| 329 const char* source = "(function() {" | 329 const char* source = "(function() {" |
| 330 "var arr = [];" | 330 "var arr = [];" |
| 331 "arr.__defineSetter__(0, " | 331 "arr.__defineSetter__(0, " |
| 332 " function() { throw new Error('muah!'); });" | 332 " function() { throw new Error('muah!'); });" |
| 333 "arr.__defineGetter__(0, " | 333 "arr.__defineGetter__(0, " |
| 334 " function() { throw new Error('muah!'); });" | 334 " function() { throw new Error('muah!'); });" |
| 335 "arr[1] = 'bar';" | 335 "arr[1] = 'bar';" |
| 336 "return arr;" | 336 "return arr;" |
| 337 "})();"; | 337 "})();"; |
| 338 | 338 |
| 339 v8::Handle<v8::Script> script( | 339 v8::Local<v8::Script> script( |
| 340 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 340 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 341 v8::Handle<v8::Array> array = script->Run().As<v8::Array>(); | 341 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
| 342 ASSERT_FALSE(array.IsEmpty()); | 342 ASSERT_FALSE(array.IsEmpty()); |
| 343 | 343 |
| 344 // Converting from v8 value should replace the first item with null. | 344 // Converting from v8 value should replace the first item with null. |
| 345 V8ValueConverterImpl converter; | 345 V8ValueConverterImpl converter; |
| 346 scoped_ptr<base::ListValue> converted(static_cast<base::ListValue*>( | 346 scoped_ptr<base::ListValue> converted(static_cast<base::ListValue*>( |
| 347 converter.FromV8Value(array, context))); | 347 converter.FromV8Value(array, context))); |
| 348 ASSERT_TRUE(converted.get()); | 348 ASSERT_TRUE(converted.get()); |
| 349 // http://code.google.com/p/v8/issues/detail?id=1342 | 349 // http://code.google.com/p/v8/issues/detail?id=1342 |
| 350 EXPECT_EQ(2u, converted->GetSize()); | 350 EXPECT_EQ(2u, converted->GetSize()); |
| 351 EXPECT_TRUE(IsNull(converted.get(), 0)); | 351 EXPECT_TRUE(IsNull(converted.get(), 0)); |
| 352 | 352 |
| 353 // Converting to v8 value should drop the first item and leave a hole. | 353 // Converting to v8 value should drop the first item and leave a hole. |
| 354 converted.reset(static_cast<base::ListValue*>( | 354 converted.reset(static_cast<base::ListValue*>( |
| 355 base::test::ParseJson("[ \"foo\", \"bar\" ]").release())); | 355 base::test::ParseJson("[ \"foo\", \"bar\" ]").release())); |
| 356 v8::Handle<v8::Array> copy = | 356 v8::Local<v8::Array> copy = |
| 357 converter.ToV8Value(converted.get(), context).As<v8::Array>(); | 357 converter.ToV8Value(converted.get(), context).As<v8::Array>(); |
| 358 ASSERT_FALSE(copy.IsEmpty()); | 358 ASSERT_FALSE(copy.IsEmpty()); |
| 359 EXPECT_EQ(2u, copy->Length()); | 359 EXPECT_EQ(2u, copy->Length()); |
| 360 EXPECT_EQ("bar", GetString(copy, 1)); | 360 EXPECT_EQ("bar", GetString(copy, 1)); |
| 361 } | 361 } |
| 362 | 362 |
| 363 TEST_F(V8ValueConverterImplTest, WeirdTypes) { | 363 TEST_F(V8ValueConverterImplTest, WeirdTypes) { |
| 364 v8::HandleScope handle_scope(isolate_); | 364 v8::HandleScope handle_scope(isolate_); |
| 365 v8::Local<v8::Context> context = | 365 v8::Local<v8::Context> context = |
| 366 v8::Local<v8::Context>::New(isolate_, context_); | 366 v8::Local<v8::Context>::New(isolate_, context_); |
| 367 v8::Context::Scope context_scope(context); | 367 v8::Context::Scope context_scope(context); |
| 368 | 368 |
| 369 v8::Handle<v8::RegExp> regex(v8::RegExp::New( | 369 v8::Local<v8::RegExp> regex(v8::RegExp::New( |
| 370 v8::String::NewFromUtf8(isolate_, "."), v8::RegExp::kNone)); | 370 v8::String::NewFromUtf8(isolate_, "."), v8::RegExp::kNone)); |
| 371 | 371 |
| 372 V8ValueConverterImpl converter; | 372 V8ValueConverterImpl converter; |
| 373 TestWeirdType(converter, | 373 TestWeirdType(converter, |
| 374 v8::Undefined(isolate_), | 374 v8::Undefined(isolate_), |
| 375 base::Value::TYPE_NULL, // Arbitrary type, result is NULL. | 375 base::Value::TYPE_NULL, // Arbitrary type, result is NULL. |
| 376 scoped_ptr<base::Value>()); | 376 scoped_ptr<base::Value>()); |
| 377 TestWeirdType(converter, | 377 TestWeirdType(converter, |
| 378 v8::Date::New(isolate_, 1000), | 378 v8::Date::New(isolate_, 1000), |
| 379 base::Value::TYPE_DICTIONARY, | 379 base::Value::TYPE_DICTIONARY, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 400 v8::HandleScope handle_scope(isolate_); | 400 v8::HandleScope handle_scope(isolate_); |
| 401 v8::Local<v8::Context> context = | 401 v8::Local<v8::Context> context = |
| 402 v8::Local<v8::Context>::New(isolate_, context_); | 402 v8::Local<v8::Context>::New(isolate_, context_); |
| 403 v8::Context::Scope context_scope(context); | 403 v8::Context::Scope context_scope(context); |
| 404 | 404 |
| 405 const char* source = "(function() {" | 405 const char* source = "(function() {" |
| 406 "Object.prototype.foo = 'foo';" | 406 "Object.prototype.foo = 'foo';" |
| 407 "return {};" | 407 "return {};" |
| 408 "})();"; | 408 "})();"; |
| 409 | 409 |
| 410 v8::Handle<v8::Script> script( | 410 v8::Local<v8::Script> script( |
| 411 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 411 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 412 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); | 412 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 413 ASSERT_FALSE(object.IsEmpty()); | 413 ASSERT_FALSE(object.IsEmpty()); |
| 414 | 414 |
| 415 V8ValueConverterImpl converter; | 415 V8ValueConverterImpl converter; |
| 416 scoped_ptr<base::DictionaryValue> result( | 416 scoped_ptr<base::DictionaryValue> result( |
| 417 static_cast<base::DictionaryValue*>( | 417 static_cast<base::DictionaryValue*>( |
| 418 converter.FromV8Value(object, context))); | 418 converter.FromV8Value(object, context))); |
| 419 ASSERT_TRUE(result.get()); | 419 ASSERT_TRUE(result.get()); |
| 420 EXPECT_EQ(0u, result->size()); | 420 EXPECT_EQ(0u, result->size()); |
| 421 } | 421 } |
| 422 | 422 |
| 423 TEST_F(V8ValueConverterImplTest, StripNullFromObjects) { | 423 TEST_F(V8ValueConverterImplTest, StripNullFromObjects) { |
| 424 v8::HandleScope handle_scope(isolate_); | 424 v8::HandleScope handle_scope(isolate_); |
| 425 v8::Local<v8::Context> context = | 425 v8::Local<v8::Context> context = |
| 426 v8::Local<v8::Context>::New(isolate_, context_); | 426 v8::Local<v8::Context>::New(isolate_, context_); |
| 427 v8::Context::Scope context_scope(context); | 427 v8::Context::Scope context_scope(context); |
| 428 | 428 |
| 429 const char* source = "(function() {" | 429 const char* source = "(function() {" |
| 430 "return { foo: undefined, bar: null };" | 430 "return { foo: undefined, bar: null };" |
| 431 "})();"; | 431 "})();"; |
| 432 | 432 |
| 433 v8::Handle<v8::Script> script( | 433 v8::Local<v8::Script> script( |
| 434 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 434 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 435 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); | 435 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 436 ASSERT_FALSE(object.IsEmpty()); | 436 ASSERT_FALSE(object.IsEmpty()); |
| 437 | 437 |
| 438 V8ValueConverterImpl converter; | 438 V8ValueConverterImpl converter; |
| 439 converter.SetStripNullFromObjects(true); | 439 converter.SetStripNullFromObjects(true); |
| 440 | 440 |
| 441 scoped_ptr<base::DictionaryValue> result( | 441 scoped_ptr<base::DictionaryValue> result( |
| 442 static_cast<base::DictionaryValue*>( | 442 static_cast<base::DictionaryValue*>( |
| 443 converter.FromV8Value(object, context))); | 443 converter.FromV8Value(object, context))); |
| 444 ASSERT_TRUE(result.get()); | 444 ASSERT_TRUE(result.get()); |
| 445 EXPECT_EQ(0u, result->size()); | 445 EXPECT_EQ(0u, result->size()); |
| 446 } | 446 } |
| 447 | 447 |
| 448 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { | 448 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { |
| 449 v8::HandleScope handle_scope(isolate_); | 449 v8::HandleScope handle_scope(isolate_); |
| 450 v8::Local<v8::Context> context = | 450 v8::Local<v8::Context> context = |
| 451 v8::Local<v8::Context>::New(isolate_, context_); | 451 v8::Local<v8::Context>::New(isolate_, context_); |
| 452 v8::Context::Scope context_scope(context); | 452 v8::Context::Scope context_scope(context); |
| 453 | 453 |
| 454 V8ValueConverterImpl converter; | 454 V8ValueConverterImpl converter; |
| 455 | 455 |
| 456 v8::Handle<v8::Object> object = v8::Object::New(isolate_).As<v8::Object>(); | 456 v8::Local<v8::Object> object = v8::Object::New(isolate_).As<v8::Object>(); |
| 457 ASSERT_FALSE(object.IsEmpty()); | 457 ASSERT_FALSE(object.IsEmpty()); |
| 458 object->Set(v8::String::NewFromUtf8(isolate_, "foo"), | 458 object->Set(v8::String::NewFromUtf8(isolate_, "foo"), |
| 459 v8::String::NewFromUtf8(isolate_, "bar")); | 459 v8::String::NewFromUtf8(isolate_, "bar")); |
| 460 object->Set(v8::String::NewFromUtf8(isolate_, "obj"), object); | 460 object->Set(v8::String::NewFromUtf8(isolate_, "obj"), object); |
| 461 | 461 |
| 462 scoped_ptr<base::DictionaryValue> object_result( | 462 scoped_ptr<base::DictionaryValue> object_result( |
| 463 static_cast<base::DictionaryValue*>( | 463 static_cast<base::DictionaryValue*>( |
| 464 converter.FromV8Value(object, context))); | 464 converter.FromV8Value(object, context))); |
| 465 ASSERT_TRUE(object_result.get()); | 465 ASSERT_TRUE(object_result.get()); |
| 466 EXPECT_EQ(2u, object_result->size()); | 466 EXPECT_EQ(2u, object_result->size()); |
| 467 EXPECT_TRUE(IsNull(object_result.get(), "obj")); | 467 EXPECT_TRUE(IsNull(object_result.get(), "obj")); |
| 468 | 468 |
| 469 v8::Handle<v8::Array> array = v8::Array::New(isolate_).As<v8::Array>(); | 469 v8::Local<v8::Array> array = v8::Array::New(isolate_).As<v8::Array>(); |
| 470 ASSERT_FALSE(array.IsEmpty()); | 470 ASSERT_FALSE(array.IsEmpty()); |
| 471 array->Set(0, v8::String::NewFromUtf8(isolate_, "1")); | 471 array->Set(0, v8::String::NewFromUtf8(isolate_, "1")); |
| 472 array->Set(1, array); | 472 array->Set(1, array); |
| 473 | 473 |
| 474 scoped_ptr<base::ListValue> list_result( | 474 scoped_ptr<base::ListValue> list_result( |
| 475 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 475 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); |
| 476 ASSERT_TRUE(list_result.get()); | 476 ASSERT_TRUE(list_result.get()); |
| 477 EXPECT_EQ(2u, list_result->GetSize()); | 477 EXPECT_EQ(2u, list_result->GetSize()); |
| 478 EXPECT_TRUE(IsNull(list_result.get(), 1)); | 478 EXPECT_TRUE(IsNull(list_result.get(), 1)); |
| 479 } | 479 } |
| 480 | 480 |
| 481 TEST_F(V8ValueConverterImplTest, WeirdProperties) { | 481 TEST_F(V8ValueConverterImplTest, WeirdProperties) { |
| 482 v8::HandleScope handle_scope(isolate_); | 482 v8::HandleScope handle_scope(isolate_); |
| 483 v8::Local<v8::Context> context = | 483 v8::Local<v8::Context> context = |
| 484 v8::Local<v8::Context>::New(isolate_, context_); | 484 v8::Local<v8::Context>::New(isolate_, context_); |
| 485 v8::Context::Scope context_scope(context); | 485 v8::Context::Scope context_scope(context); |
| 486 | 486 |
| 487 const char* source = "(function() {" | 487 const char* source = "(function() {" |
| 488 "return {" | 488 "return {" |
| 489 "1: 'foo'," | 489 "1: 'foo'," |
| 490 "'2': 'bar'," | 490 "'2': 'bar'," |
| 491 "true: 'baz'," | 491 "true: 'baz'," |
| 492 "false: 'qux'," | 492 "false: 'qux'," |
| 493 "null: 'quux'," | 493 "null: 'quux'," |
| 494 "undefined: 'oops'" | 494 "undefined: 'oops'" |
| 495 "};" | 495 "};" |
| 496 "})();"; | 496 "})();"; |
| 497 | 497 |
| 498 v8::Handle<v8::Script> script( | 498 v8::Local<v8::Script> script( |
| 499 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 499 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 500 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); | 500 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 501 ASSERT_FALSE(object.IsEmpty()); | 501 ASSERT_FALSE(object.IsEmpty()); |
| 502 | 502 |
| 503 V8ValueConverterImpl converter; | 503 V8ValueConverterImpl converter; |
| 504 scoped_ptr<base::Value> actual(converter.FromV8Value(object, context)); | 504 scoped_ptr<base::Value> actual(converter.FromV8Value(object, context)); |
| 505 | 505 |
| 506 scoped_ptr<base::Value> expected = base::test::ParseJson( | 506 scoped_ptr<base::Value> expected = base::test::ParseJson( |
| 507 "{ \n" | 507 "{ \n" |
| 508 " \"1\": \"foo\", \n" | 508 " \"1\": \"foo\", \n" |
| 509 " \"2\": \"bar\", \n" | 509 " \"2\": \"bar\", \n" |
| 510 " \"true\": \"baz\", \n" | 510 " \"true\": \"baz\", \n" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 521 v8::Local<v8::Context> context = | 521 v8::Local<v8::Context> context = |
| 522 v8::Local<v8::Context>::New(isolate_, context_); | 522 v8::Local<v8::Context>::New(isolate_, context_); |
| 523 v8::Context::Scope context_scope(context); | 523 v8::Context::Scope context_scope(context); |
| 524 | 524 |
| 525 const char* source = "(function() {" | 525 const char* source = "(function() {" |
| 526 "var a = [0];" | 526 "var a = [0];" |
| 527 "a.__defineGetter__(1, function() { return 'bar'; });" | 527 "a.__defineGetter__(1, function() { return 'bar'; });" |
| 528 "return a;" | 528 "return a;" |
| 529 "})();"; | 529 "})();"; |
| 530 | 530 |
| 531 v8::Handle<v8::Script> script( | 531 v8::Local<v8::Script> script( |
| 532 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 532 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 533 v8::Handle<v8::Array> array = script->Run().As<v8::Array>(); | 533 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
| 534 ASSERT_FALSE(array.IsEmpty()); | 534 ASSERT_FALSE(array.IsEmpty()); |
| 535 | 535 |
| 536 V8ValueConverterImpl converter; | 536 V8ValueConverterImpl converter; |
| 537 scoped_ptr<base::ListValue> result( | 537 scoped_ptr<base::ListValue> result( |
| 538 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 538 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); |
| 539 ASSERT_TRUE(result.get()); | 539 ASSERT_TRUE(result.get()); |
| 540 EXPECT_EQ(2u, result->GetSize()); | 540 EXPECT_EQ(2u, result->GetSize()); |
| 541 } | 541 } |
| 542 | 542 |
| 543 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { | 543 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { |
| 544 v8::HandleScope handle_scope(isolate_); | 544 v8::HandleScope handle_scope(isolate_); |
| 545 v8::Local<v8::Context> context = | 545 v8::Local<v8::Context> context = |
| 546 v8::Local<v8::Context>::New(isolate_, context_); | 546 v8::Local<v8::Context>::New(isolate_, context_); |
| 547 v8::Context::Scope context_scope(context); | 547 v8::Context::Scope context_scope(context); |
| 548 | 548 |
| 549 v8::Handle<v8::Object> object; | 549 v8::Local<v8::Object> object; |
| 550 { | 550 { |
| 551 const char* source = "(function() {" | 551 const char* source = "(function() {" |
| 552 "return { foo: undefined, bar: null, baz: function(){} };" | 552 "return { foo: undefined, bar: null, baz: function(){} };" |
| 553 "})();"; | 553 "})();"; |
| 554 v8::Handle<v8::Script> script( | 554 v8::Local<v8::Script> script( |
| 555 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 555 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 556 object = script->Run().As<v8::Object>(); | 556 object = script->Run().As<v8::Object>(); |
| 557 ASSERT_FALSE(object.IsEmpty()); | 557 ASSERT_FALSE(object.IsEmpty()); |
| 558 } | 558 } |
| 559 | 559 |
| 560 v8::Handle<v8::Array> array; | 560 v8::Local<v8::Array> array; |
| 561 { | 561 { |
| 562 const char* source = "(function() {" | 562 const char* source = "(function() {" |
| 563 "return [ undefined, null, function(){} ];" | 563 "return [ undefined, null, function(){} ];" |
| 564 "})();"; | 564 "})();"; |
| 565 v8::Handle<v8::Script> script( | 565 v8::Local<v8::Script> script( |
| 566 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 566 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 567 array = script->Run().As<v8::Array>(); | 567 array = script->Run().As<v8::Array>(); |
| 568 ASSERT_FALSE(array.IsEmpty()); | 568 ASSERT_FALSE(array.IsEmpty()); |
| 569 } | 569 } |
| 570 | 570 |
| 571 v8::Handle<v8::Array> sparse_array; | 571 v8::Local<v8::Array> sparse_array; |
| 572 { | 572 { |
| 573 const char* source = "(function() {" | 573 const char* source = "(function() {" |
| 574 "return new Array(3);" | 574 "return new Array(3);" |
| 575 "})();"; | 575 "})();"; |
| 576 v8::Handle<v8::Script> script( | 576 v8::Local<v8::Script> script( |
| 577 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 577 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 578 sparse_array = script->Run().As<v8::Array>(); | 578 sparse_array = script->Run().As<v8::Array>(); |
| 579 ASSERT_FALSE(sparse_array.IsEmpty()); | 579 ASSERT_FALSE(sparse_array.IsEmpty()); |
| 580 } | 580 } |
| 581 | 581 |
| 582 V8ValueConverterImpl converter; | 582 V8ValueConverterImpl converter; |
| 583 | 583 |
| 584 scoped_ptr<base::Value> actual_object( | 584 scoped_ptr<base::Value> actual_object( |
| 585 converter.FromV8Value(object, context)); | 585 converter.FromV8Value(object, context)); |
| 586 EXPECT_TRUE(base::Value::Equals( | 586 EXPECT_TRUE(base::Value::Equals( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 603 v8::Local<v8::Context> context = | 603 v8::Local<v8::Context> context = |
| 604 v8::Local<v8::Context>::New(isolate_, context_); | 604 v8::Local<v8::Context>::New(isolate_, context_); |
| 605 v8::Context::Scope context_scope(context); | 605 v8::Context::Scope context_scope(context); |
| 606 V8ValueConverterImpl converter; | 606 V8ValueConverterImpl converter; |
| 607 | 607 |
| 608 // We check that the converter checks identity correctly by disabling the | 608 // We check that the converter checks identity correctly by disabling the |
| 609 // optimization of using identity hashes. | 609 // optimization of using identity hashes. |
| 610 ScopedAvoidIdentityHashForTesting scoped_hash_avoider(&converter); | 610 ScopedAvoidIdentityHashForTesting scoped_hash_avoider(&converter); |
| 611 | 611 |
| 612 // Create the v8::Object to be converted. | 612 // Create the v8::Object to be converted. |
| 613 v8::Handle<v8::Array> root(v8::Array::New(isolate_, 4)); | 613 v8::Local<v8::Array> root(v8::Array::New(isolate_, 4)); |
| 614 root->Set(0, v8::Handle<v8::Object>(v8::Object::New(isolate_))); | 614 root->Set(0, v8::Local<v8::Object>(v8::Object::New(isolate_))); |
| 615 root->Set(1, v8::Handle<v8::Object>(v8::Object::New(isolate_))); | 615 root->Set(1, v8::Local<v8::Object>(v8::Object::New(isolate_))); |
| 616 root->Set(2, v8::Handle<v8::Object>(v8::Array::New(isolate_, 0))); | 616 root->Set(2, v8::Local<v8::Object>(v8::Array::New(isolate_, 0))); |
| 617 root->Set(3, v8::Handle<v8::Object>(v8::Array::New(isolate_, 0))); | 617 root->Set(3, v8::Local<v8::Object>(v8::Array::New(isolate_, 0))); |
| 618 | 618 |
| 619 // The expected base::Value result. | 619 // The expected base::Value result. |
| 620 scoped_ptr<base::Value> expected = base::test::ParseJson("[{},{},[],[]]"); | 620 scoped_ptr<base::Value> expected = base::test::ParseJson("[{},{},[],[]]"); |
| 621 ASSERT_TRUE(expected.get()); | 621 ASSERT_TRUE(expected.get()); |
| 622 | 622 |
| 623 // The actual result. | 623 // The actual result. |
| 624 scoped_ptr<base::Value> value(converter.FromV8Value(root, context)); | 624 scoped_ptr<base::Value> value(converter.FromV8Value(root, context)); |
| 625 ASSERT_TRUE(value.get()); | 625 ASSERT_TRUE(value.get()); |
| 626 | 626 |
| 627 EXPECT_TRUE(expected->Equals(value.get())); | 627 EXPECT_TRUE(expected->Equals(value.get())); |
| 628 } | 628 } |
| 629 | 629 |
| 630 TEST_F(V8ValueConverterImplTest, DetectCycles) { | 630 TEST_F(V8ValueConverterImplTest, DetectCycles) { |
| 631 v8::HandleScope handle_scope(isolate_); | 631 v8::HandleScope handle_scope(isolate_); |
| 632 v8::Local<v8::Context> context = | 632 v8::Local<v8::Context> context = |
| 633 v8::Local<v8::Context>::New(isolate_, context_); | 633 v8::Local<v8::Context>::New(isolate_, context_); |
| 634 v8::Context::Scope context_scope(context); | 634 v8::Context::Scope context_scope(context); |
| 635 V8ValueConverterImpl converter; | 635 V8ValueConverterImpl converter; |
| 636 | 636 |
| 637 // Create a recursive array. | 637 // Create a recursive array. |
| 638 v8::Handle<v8::Array> recursive_array(v8::Array::New(isolate_, 1)); | 638 v8::Local<v8::Array> recursive_array(v8::Array::New(isolate_, 1)); |
| 639 recursive_array->Set(0, recursive_array); | 639 recursive_array->Set(0, recursive_array); |
| 640 | 640 |
| 641 // The first repetition should be trimmed and replaced by a null value. | 641 // The first repetition should be trimmed and replaced by a null value. |
| 642 base::ListValue expected_list; | 642 base::ListValue expected_list; |
| 643 expected_list.Append(base::Value::CreateNullValue()); | 643 expected_list.Append(base::Value::CreateNullValue()); |
| 644 | 644 |
| 645 // The actual result. | 645 // The actual result. |
| 646 scoped_ptr<base::Value> actual_list( | 646 scoped_ptr<base::Value> actual_list( |
| 647 converter.FromV8Value(recursive_array, context)); | 647 converter.FromV8Value(recursive_array, context)); |
| 648 ASSERT_TRUE(actual_list.get()); | 648 ASSERT_TRUE(actual_list.get()); |
| 649 | 649 |
| 650 EXPECT_TRUE(expected_list.Equals(actual_list.get())); | 650 EXPECT_TRUE(expected_list.Equals(actual_list.get())); |
| 651 | 651 |
| 652 // Now create a recursive object | 652 // Now create a recursive object |
| 653 const std::string key("key"); | 653 const std::string key("key"); |
| 654 v8::Handle<v8::Object> recursive_object(v8::Object::New(isolate_)); | 654 v8::Local<v8::Object> recursive_object(v8::Object::New(isolate_)); |
| 655 v8::TryCatch try_catch; | 655 v8::TryCatch try_catch; |
| 656 recursive_object->Set( | 656 recursive_object->Set( |
| 657 v8::String::NewFromUtf8( | 657 v8::String::NewFromUtf8( |
| 658 isolate_, key.c_str(), v8::String::kNormalString, key.length()), | 658 isolate_, key.c_str(), v8::String::kNormalString, key.length()), |
| 659 recursive_object); | 659 recursive_object); |
| 660 ASSERT_FALSE(try_catch.HasCaught()); | 660 ASSERT_FALSE(try_catch.HasCaught()); |
| 661 | 661 |
| 662 // The first repetition should be trimmed and replaced by a null value. | 662 // The first repetition should be trimmed and replaced by a null value. |
| 663 base::DictionaryValue expected_dictionary; | 663 base::DictionaryValue expected_dictionary; |
| 664 expected_dictionary.Set(key, base::Value::CreateNullValue()); | 664 expected_dictionary.Set(key, base::Value::CreateNullValue()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 // The leaf node shouldn't have any properties. | 707 // The leaf node shouldn't have any properties. |
| 708 base::DictionaryValue empty; | 708 base::DictionaryValue empty; |
| 709 EXPECT_TRUE(base::Value::Equals(&empty, current)) << *current; | 709 EXPECT_TRUE(base::Value::Equals(&empty, current)) << *current; |
| 710 } | 710 } |
| 711 | 711 |
| 712 class V8ValueConverterOverridingStrategyForTesting | 712 class V8ValueConverterOverridingStrategyForTesting |
| 713 : public V8ValueConverter::Strategy { | 713 : public V8ValueConverter::Strategy { |
| 714 public: | 714 public: |
| 715 V8ValueConverterOverridingStrategyForTesting() | 715 V8ValueConverterOverridingStrategyForTesting() |
| 716 : reference_value_(NewReferenceValue()) {} | 716 : reference_value_(NewReferenceValue()) {} |
| 717 bool FromV8Object(v8::Handle<v8::Object> value, | 717 bool FromV8Object(v8::Local<v8::Object> value, |
| 718 base::Value** out, | 718 base::Value** out, |
| 719 v8::Isolate* isolate, | 719 v8::Isolate* isolate, |
| 720 const FromV8ValueCallback& callback) const override { | 720 const FromV8ValueCallback& callback) const override { |
| 721 *out = NewReferenceValue(); | 721 *out = NewReferenceValue(); |
| 722 return true; | 722 return true; |
| 723 } | 723 } |
| 724 bool FromV8Array(v8::Handle<v8::Array> value, | 724 bool FromV8Array(v8::Local<v8::Array> value, |
| 725 base::Value** out, | 725 base::Value** out, |
| 726 v8::Isolate* isolate, | 726 v8::Isolate* isolate, |
| 727 const FromV8ValueCallback& callback) const override { | 727 const FromV8ValueCallback& callback) const override { |
| 728 *out = NewReferenceValue(); | 728 *out = NewReferenceValue(); |
| 729 return true; | 729 return true; |
| 730 } | 730 } |
| 731 bool FromV8ArrayBuffer(v8::Handle<v8::Object> value, | 731 bool FromV8ArrayBuffer(v8::Local<v8::Object> value, |
| 732 base::Value** out, | 732 base::Value** out, |
| 733 v8::Isolate* isolate) const override { | 733 v8::Isolate* isolate) const override { |
| 734 *out = NewReferenceValue(); | 734 *out = NewReferenceValue(); |
| 735 return true; | 735 return true; |
| 736 } | 736 } |
| 737 bool FromV8Number(v8::Handle<v8::Number> value, | 737 bool FromV8Number(v8::Local<v8::Number> value, |
| 738 base::Value** out) const override { | 738 base::Value** out) const override { |
| 739 *out = NewReferenceValue(); | 739 *out = NewReferenceValue(); |
| 740 return true; | 740 return true; |
| 741 } | 741 } |
| 742 bool FromV8Undefined(base::Value** out) const override { | 742 bool FromV8Undefined(base::Value** out) const override { |
| 743 *out = NewReferenceValue(); | 743 *out = NewReferenceValue(); |
| 744 return true; | 744 return true; |
| 745 } | 745 } |
| 746 base::Value* reference_value() const { return reference_value_.get(); } | 746 base::Value* reference_value() const { return reference_value_.get(); } |
| 747 | 747 |
| 748 private: | 748 private: |
| 749 static base::Value* NewReferenceValue() { | 749 static base::Value* NewReferenceValue() { |
| 750 return new base::StringValue("strategy"); | 750 return new base::StringValue("strategy"); |
| 751 } | 751 } |
| 752 scoped_ptr<base::Value> reference_value_; | 752 scoped_ptr<base::Value> reference_value_; |
| 753 }; | 753 }; |
| 754 | 754 |
| 755 TEST_F(V8ValueConverterImplTest, StrategyOverrides) { | 755 TEST_F(V8ValueConverterImplTest, StrategyOverrides) { |
| 756 v8::HandleScope handle_scope(isolate_); | 756 v8::HandleScope handle_scope(isolate_); |
| 757 v8::Local<v8::Context> context = | 757 v8::Local<v8::Context> context = |
| 758 v8::Local<v8::Context>::New(isolate_, context_); | 758 v8::Local<v8::Context>::New(isolate_, context_); |
| 759 v8::Context::Scope context_scope(context); | 759 v8::Context::Scope context_scope(context); |
| 760 | 760 |
| 761 V8ValueConverterImpl converter; | 761 V8ValueConverterImpl converter; |
| 762 V8ValueConverterOverridingStrategyForTesting strategy; | 762 V8ValueConverterOverridingStrategyForTesting strategy; |
| 763 converter.SetStrategy(&strategy); | 763 converter.SetStrategy(&strategy); |
| 764 | 764 |
| 765 v8::Handle<v8::Object> object(v8::Object::New(isolate_)); | 765 v8::Local<v8::Object> object(v8::Object::New(isolate_)); |
| 766 scoped_ptr<base::Value> object_value(converter.FromV8Value(object, context)); | 766 scoped_ptr<base::Value> object_value(converter.FromV8Value(object, context)); |
| 767 ASSERT_TRUE(object_value); | 767 ASSERT_TRUE(object_value); |
| 768 EXPECT_TRUE( | 768 EXPECT_TRUE( |
| 769 base::Value::Equals(strategy.reference_value(), object_value.get())); | 769 base::Value::Equals(strategy.reference_value(), object_value.get())); |
| 770 | 770 |
| 771 v8::Handle<v8::Array> array(v8::Array::New(isolate_)); | 771 v8::Local<v8::Array> array(v8::Array::New(isolate_)); |
| 772 scoped_ptr<base::Value> array_value(converter.FromV8Value(array, context)); | 772 scoped_ptr<base::Value> array_value(converter.FromV8Value(array, context)); |
| 773 ASSERT_TRUE(array_value); | 773 ASSERT_TRUE(array_value); |
| 774 EXPECT_TRUE( | 774 EXPECT_TRUE( |
| 775 base::Value::Equals(strategy.reference_value(), array_value.get())); | 775 base::Value::Equals(strategy.reference_value(), array_value.get())); |
| 776 | 776 |
| 777 v8::Handle<v8::ArrayBuffer> array_buffer(v8::ArrayBuffer::New(isolate_, 0)); | 777 v8::Local<v8::ArrayBuffer> array_buffer(v8::ArrayBuffer::New(isolate_, 0)); |
| 778 scoped_ptr<base::Value> array_buffer_value( | 778 scoped_ptr<base::Value> array_buffer_value( |
| 779 converter.FromV8Value(array_buffer, context)); | 779 converter.FromV8Value(array_buffer, context)); |
| 780 ASSERT_TRUE(array_buffer_value); | 780 ASSERT_TRUE(array_buffer_value); |
| 781 EXPECT_TRUE(base::Value::Equals(strategy.reference_value(), | 781 EXPECT_TRUE(base::Value::Equals(strategy.reference_value(), |
| 782 array_buffer_value.get())); | 782 array_buffer_value.get())); |
| 783 | 783 |
| 784 v8::Handle<v8::ArrayBufferView> array_buffer_view( | 784 v8::Local<v8::ArrayBufferView> array_buffer_view( |
| 785 v8::Uint8Array::New(array_buffer, 0, 0)); | 785 v8::Uint8Array::New(array_buffer, 0, 0)); |
| 786 scoped_ptr<base::Value> array_buffer_view_value( | 786 scoped_ptr<base::Value> array_buffer_view_value( |
| 787 converter.FromV8Value(array_buffer_view, context)); | 787 converter.FromV8Value(array_buffer_view, context)); |
| 788 ASSERT_TRUE(array_buffer_view_value); | 788 ASSERT_TRUE(array_buffer_view_value); |
| 789 EXPECT_TRUE(base::Value::Equals(strategy.reference_value(), | 789 EXPECT_TRUE(base::Value::Equals(strategy.reference_value(), |
| 790 array_buffer_view_value.get())); | 790 array_buffer_view_value.get())); |
| 791 | 791 |
| 792 v8::Handle<v8::Number> number(v8::Number::New(isolate_, 0.0)); | 792 v8::Local<v8::Number> number(v8::Number::New(isolate_, 0.0)); |
| 793 scoped_ptr<base::Value> number_value(converter.FromV8Value(number, context)); | 793 scoped_ptr<base::Value> number_value(converter.FromV8Value(number, context)); |
| 794 ASSERT_TRUE(number_value); | 794 ASSERT_TRUE(number_value); |
| 795 EXPECT_TRUE( | 795 EXPECT_TRUE( |
| 796 base::Value::Equals(strategy.reference_value(), number_value.get())); | 796 base::Value::Equals(strategy.reference_value(), number_value.get())); |
| 797 | 797 |
| 798 v8::Handle<v8::Primitive> undefined(v8::Undefined(isolate_)); | 798 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); |
| 799 scoped_ptr<base::Value> undefined_value( | 799 scoped_ptr<base::Value> undefined_value( |
| 800 converter.FromV8Value(undefined, context)); | 800 converter.FromV8Value(undefined, context)); |
| 801 ASSERT_TRUE(undefined_value); | 801 ASSERT_TRUE(undefined_value); |
| 802 EXPECT_TRUE( | 802 EXPECT_TRUE( |
| 803 base::Value::Equals(strategy.reference_value(), undefined_value.get())); | 803 base::Value::Equals(strategy.reference_value(), undefined_value.get())); |
| 804 } | 804 } |
| 805 | 805 |
| 806 class V8ValueConverterBypassStrategyForTesting | 806 class V8ValueConverterBypassStrategyForTesting |
| 807 : public V8ValueConverter::Strategy { | 807 : public V8ValueConverter::Strategy { |
| 808 public: | 808 public: |
| 809 bool FromV8Object(v8::Handle<v8::Object> value, | 809 bool FromV8Object(v8::Local<v8::Object> value, |
| 810 base::Value** out, | 810 base::Value** out, |
| 811 v8::Isolate* isolate, | 811 v8::Isolate* isolate, |
| 812 const FromV8ValueCallback& callback) const override { | 812 const FromV8ValueCallback& callback) const override { |
| 813 return false; | 813 return false; |
| 814 } | 814 } |
| 815 bool FromV8Array(v8::Handle<v8::Array> value, | 815 bool FromV8Array(v8::Local<v8::Array> value, |
| 816 base::Value** out, | 816 base::Value** out, |
| 817 v8::Isolate* isolate, | 817 v8::Isolate* isolate, |
| 818 const FromV8ValueCallback& callback) const override { | 818 const FromV8ValueCallback& callback) const override { |
| 819 return false; | 819 return false; |
| 820 } | 820 } |
| 821 bool FromV8ArrayBuffer(v8::Handle<v8::Object> value, | 821 bool FromV8ArrayBuffer(v8::Local<v8::Object> value, |
| 822 base::Value** out, | 822 base::Value** out, |
| 823 v8::Isolate* isolate) const override { | 823 v8::Isolate* isolate) const override { |
| 824 return false; | 824 return false; |
| 825 } | 825 } |
| 826 bool FromV8Number(v8::Handle<v8::Number> value, | 826 bool FromV8Number(v8::Local<v8::Number> value, |
| 827 base::Value** out) const override { | 827 base::Value** out) const override { |
| 828 return false; | 828 return false; |
| 829 } | 829 } |
| 830 bool FromV8Undefined(base::Value** out) const override { return false; } | 830 bool FromV8Undefined(base::Value** out) const override { return false; } |
| 831 }; | 831 }; |
| 832 | 832 |
| 833 // Verify that having a strategy that fallbacks to default behaviour | 833 // Verify that having a strategy that fallbacks to default behaviour |
| 834 // actually preserves it. | 834 // actually preserves it. |
| 835 TEST_F(V8ValueConverterImplTest, StrategyBypass) { | 835 TEST_F(V8ValueConverterImplTest, StrategyBypass) { |
| 836 v8::HandleScope handle_scope(isolate_); | 836 v8::HandleScope handle_scope(isolate_); |
| 837 v8::Local<v8::Context> context = | 837 v8::Local<v8::Context> context = |
| 838 v8::Local<v8::Context>::New(isolate_, context_); | 838 v8::Local<v8::Context>::New(isolate_, context_); |
| 839 v8::Context::Scope context_scope(context); | 839 v8::Context::Scope context_scope(context); |
| 840 | 840 |
| 841 V8ValueConverterImpl converter; | 841 V8ValueConverterImpl converter; |
| 842 V8ValueConverterBypassStrategyForTesting strategy; | 842 V8ValueConverterBypassStrategyForTesting strategy; |
| 843 converter.SetStrategy(&strategy); | 843 converter.SetStrategy(&strategy); |
| 844 | 844 |
| 845 v8::Handle<v8::Object> object(v8::Object::New(isolate_)); | 845 v8::Local<v8::Object> object(v8::Object::New(isolate_)); |
| 846 scoped_ptr<base::Value> object_value(converter.FromV8Value(object, context)); | 846 scoped_ptr<base::Value> object_value(converter.FromV8Value(object, context)); |
| 847 ASSERT_TRUE(object_value); | 847 ASSERT_TRUE(object_value); |
| 848 scoped_ptr<base::Value> reference_object_value(base::test::ParseJson("{}")); | 848 scoped_ptr<base::Value> reference_object_value(base::test::ParseJson("{}")); |
| 849 EXPECT_TRUE( | 849 EXPECT_TRUE( |
| 850 base::Value::Equals(reference_object_value.get(), object_value.get())); | 850 base::Value::Equals(reference_object_value.get(), object_value.get())); |
| 851 | 851 |
| 852 v8::Handle<v8::Array> array(v8::Array::New(isolate_)); | 852 v8::Local<v8::Array> array(v8::Array::New(isolate_)); |
| 853 scoped_ptr<base::Value> array_value(converter.FromV8Value(array, context)); | 853 scoped_ptr<base::Value> array_value(converter.FromV8Value(array, context)); |
| 854 ASSERT_TRUE(array_value); | 854 ASSERT_TRUE(array_value); |
| 855 scoped_ptr<base::Value> reference_array_value(base::test::ParseJson("[]")); | 855 scoped_ptr<base::Value> reference_array_value(base::test::ParseJson("[]")); |
| 856 EXPECT_TRUE( | 856 EXPECT_TRUE( |
| 857 base::Value::Equals(reference_array_value.get(), array_value.get())); | 857 base::Value::Equals(reference_array_value.get(), array_value.get())); |
| 858 | 858 |
| 859 // Not testing ArrayBuffers as V8ValueConverter uses blink helpers and | 859 // Not testing ArrayBuffers as V8ValueConverter uses blink helpers and |
| 860 // this requires having blink to be initialized. | 860 // this requires having blink to be initialized. |
| 861 | 861 |
| 862 v8::Handle<v8::Number> number(v8::Number::New(isolate_, 0.0)); | 862 v8::Local<v8::Number> number(v8::Number::New(isolate_, 0.0)); |
| 863 scoped_ptr<base::Value> number_value(converter.FromV8Value(number, context)); | 863 scoped_ptr<base::Value> number_value(converter.FromV8Value(number, context)); |
| 864 ASSERT_TRUE(number_value); | 864 ASSERT_TRUE(number_value); |
| 865 scoped_ptr<base::Value> reference_number_value(base::test::ParseJson("0")); | 865 scoped_ptr<base::Value> reference_number_value(base::test::ParseJson("0")); |
| 866 EXPECT_TRUE( | 866 EXPECT_TRUE( |
| 867 base::Value::Equals(reference_number_value.get(), number_value.get())); | 867 base::Value::Equals(reference_number_value.get(), number_value.get())); |
| 868 | 868 |
| 869 v8::Handle<v8::Primitive> undefined(v8::Undefined(isolate_)); | 869 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); |
| 870 scoped_ptr<base::Value> undefined_value( | 870 scoped_ptr<base::Value> undefined_value( |
| 871 converter.FromV8Value(undefined, context)); | 871 converter.FromV8Value(undefined, context)); |
| 872 EXPECT_FALSE(undefined_value); | 872 EXPECT_FALSE(undefined_value); |
| 873 } | 873 } |
| 874 | 874 |
| 875 } // namespace content | 875 } // namespace content |
| OLD | NEW |