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/values.h" | 8 #include "base/values.h" |
9 #include "content/renderer/v8_value_converter_impl.h" | 9 #include "content/renderer/v8_value_converter_impl.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
12 | 12 |
| 13 namespace { |
| 14 |
| 15 // A dumb getter for an object's named callback. |
| 16 v8::Handle<v8::Value> NamedCallbackGetter(v8::Local<v8::String> name, |
| 17 const v8::AccessorInfo& info) { |
| 18 return v8::String::New("bar"); |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
13 class V8ValueConverterImplTest : public testing::Test { | 23 class V8ValueConverterImplTest : public testing::Test { |
14 protected: | 24 protected: |
15 virtual void SetUp() { | 25 virtual void SetUp() { |
16 v8::HandleScope handle_scope; | 26 v8::HandleScope handle_scope; |
17 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 27 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
18 context_ = v8::Context::New(NULL, global); | 28 context_ = v8::Context::New(NULL, global); |
19 } | 29 } |
20 | 30 |
21 virtual void TearDown() { | 31 virtual void TearDown() { |
22 context_.Dispose(); | 32 context_.Dispose(); |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); | 401 v8::Handle<v8::Object> object = script->Run().As<v8::Object>(); |
392 ASSERT_FALSE(object.IsEmpty()); | 402 ASSERT_FALSE(object.IsEmpty()); |
393 | 403 |
394 V8ValueConverterImpl converter; | 404 V8ValueConverterImpl converter; |
395 scoped_ptr<DictionaryValue> result( | 405 scoped_ptr<DictionaryValue> result( |
396 static_cast<DictionaryValue*>(converter.FromV8Value(object, context_))); | 406 static_cast<DictionaryValue*>(converter.FromV8Value(object, context_))); |
397 ASSERT_TRUE(result.get()); | 407 ASSERT_TRUE(result.get()); |
398 EXPECT_EQ(1u, result->size()); | 408 EXPECT_EQ(1u, result->size()); |
399 } | 409 } |
400 | 410 |
| 411 // Do not try and convert the named callbacks in objects with internal fields. |
| 412 TEST_F(V8ValueConverterImplTest, ObjectWithInternalFieldsGetters) { |
| 413 v8::Context::Scope context_scope(context_); |
| 414 v8::HandleScope handle_scope; |
| 415 |
| 416 v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); |
| 417 object_template->SetInternalFieldCount(1); |
| 418 object_template->SetAccessor(v8::String::New("foo"), NamedCallbackGetter); |
| 419 v8::Handle<v8::Object> object = object_template->NewInstance(); |
| 420 ASSERT_FALSE(object.IsEmpty()); |
| 421 object->Set(v8::String::New("a"), v8::String::New("b")); |
| 422 |
| 423 V8ValueConverterImpl converter; |
| 424 scoped_ptr<DictionaryValue> result( |
| 425 static_cast<DictionaryValue*>(converter.FromV8Value(object, context_))); |
| 426 ASSERT_TRUE(result.get()); |
| 427 EXPECT_EQ(1u, result->size()); |
| 428 } |
| 429 |
401 TEST_F(V8ValueConverterImplTest, ArrayGetters) { | 430 TEST_F(V8ValueConverterImplTest, ArrayGetters) { |
402 v8::Context::Scope context_scope(context_); | 431 v8::Context::Scope context_scope(context_); |
403 v8::HandleScope handle_scope; | 432 v8::HandleScope handle_scope; |
404 | 433 |
405 const char* source = "(function() {" | 434 const char* source = "(function() {" |
406 "var a = [0];" | 435 "var a = [0];" |
407 "a.__defineGetter__(1, function() { return 'bar'; });" | 436 "a.__defineGetter__(1, function() { return 'bar'; });" |
408 "return a;" | 437 "return a;" |
409 "})();"; | 438 "})();"; |
410 | 439 |
411 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); | 440 v8::Handle<v8::Script> script(v8::Script::New(v8::String::New(source))); |
412 v8::Handle<v8::Array> array = script->Run().As<v8::Array>(); | 441 v8::Handle<v8::Array> array = script->Run().As<v8::Array>(); |
413 ASSERT_FALSE(array.IsEmpty()); | 442 ASSERT_FALSE(array.IsEmpty()); |
414 | 443 |
415 V8ValueConverterImpl converter; | 444 V8ValueConverterImpl converter; |
416 scoped_ptr<ListValue> result( | 445 scoped_ptr<ListValue> result( |
417 static_cast<ListValue*>(converter.FromV8Value(array, context_))); | 446 static_cast<ListValue*>(converter.FromV8Value(array, context_))); |
418 ASSERT_TRUE(result.get()); | 447 ASSERT_TRUE(result.get()); |
419 EXPECT_EQ(2u, result->GetSize()); | 448 EXPECT_EQ(2u, result->GetSize()); |
420 } | 449 } |
OLD | NEW |