OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/logging.h" | 5 #include "base/logging.h" |
6 #include "gin/arguments.h" | 6 #include "gin/arguments.h" |
| 7 #include "gin/handle.h" |
7 #include "gin/per_isolate_data.h" | 8 #include "gin/per_isolate_data.h" |
8 #include "gin/public/isolate_holder.h" | 9 #include "gin/public/isolate_holder.h" |
9 #include "gin/test/v8_test.h" | 10 #include "gin/test/v8_test.h" |
10 #include "gin/wrappable.h" | 11 #include "gin/wrappable.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace gin { | 14 namespace gin { |
14 namespace { | 15 namespace { |
15 | 16 |
16 class MyObject : public Wrappable { | 17 class MyObject : public Wrappable { |
17 public: | 18 public: |
18 static scoped_refptr<MyObject> Create(); | 19 static gin::Handle<MyObject> Create(v8::Isolate* isolate); |
19 | 20 |
20 int value() const { return value_; } | 21 int value() const { return value_; } |
21 void set_value(int value) { value_ = value; } | 22 void set_value(int value) { value_ = value; } |
22 | 23 |
23 static WrapperInfo kWrapperInfo; | 24 static WrapperInfo kWrapperInfo; |
24 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; | 25 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; |
25 | 26 |
26 private: | 27 private: |
27 MyObject() : value_(0) {} | 28 MyObject() : value_(0) {} |
28 virtual ~MyObject() {} | 29 virtual ~MyObject() {} |
29 | 30 |
30 int value_; | 31 int value_; |
31 }; | 32 }; |
32 | 33 |
33 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; | 34 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; |
34 | 35 |
35 scoped_refptr<MyObject> MyObject::Create() { | 36 gin::Handle<MyObject> MyObject::Create(v8::Isolate* isolate) { |
36 return make_scoped_refptr(new MyObject()); | 37 return CreateHandle(isolate, new MyObject()); |
37 } | 38 } |
38 | 39 |
39 WrapperInfo* MyObject::GetWrapperInfo() { | 40 WrapperInfo* MyObject::GetWrapperInfo() { |
40 return &kWrapperInfo; | 41 return &kWrapperInfo; |
41 } | 42 } |
42 | 43 |
43 } // namespace | 44 } // namespace |
44 | 45 |
45 template<> | 46 template<> |
46 struct Converter<MyObject*> : public WrappableConverter<MyObject> {}; | 47 struct Converter<MyObject*> : public WrappableConverter<MyObject> {}; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); | 86 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); |
86 } | 87 } |
87 | 88 |
88 typedef V8Test WrappableTest; | 89 typedef V8Test WrappableTest; |
89 | 90 |
90 TEST_F(WrappableTest, WrapAndUnwrap) { | 91 TEST_F(WrappableTest, WrapAndUnwrap) { |
91 v8::Isolate* isolate = instance_->isolate(); | 92 v8::Isolate* isolate = instance_->isolate(); |
92 v8::HandleScope handle_scope(isolate); | 93 v8::HandleScope handle_scope(isolate); |
93 | 94 |
94 RegisterTemplate(isolate); | 95 RegisterTemplate(isolate); |
95 scoped_refptr<MyObject> obj = MyObject::Create(); | 96 Handle<MyObject> obj = MyObject::Create(isolate); |
96 | 97 |
97 v8::Handle<v8::Value> wrapper = ConvertToV8(isolate, obj.get()); | 98 v8::Handle<v8::Value> wrapper = ConvertToV8(isolate, obj.Get()); |
98 EXPECT_FALSE(wrapper.IsEmpty()); | 99 EXPECT_FALSE(wrapper.IsEmpty()); |
99 | 100 |
100 MyObject* unwrapped = 0; | 101 MyObject* unwrapped = 0; |
101 EXPECT_TRUE(ConvertFromV8(isolate, wrapper, &unwrapped)); | 102 EXPECT_TRUE(ConvertFromV8(isolate, wrapper, &unwrapped)); |
102 EXPECT_EQ(obj, unwrapped); | 103 EXPECT_EQ(obj.Get(), unwrapped); |
103 } | 104 } |
104 | 105 |
105 TEST_F(WrappableTest, GetAndSetProperty) { | 106 TEST_F(WrappableTest, GetAndSetProperty) { |
106 v8::Isolate* isolate = instance_->isolate(); | 107 v8::Isolate* isolate = instance_->isolate(); |
107 v8::HandleScope handle_scope(isolate); | 108 v8::HandleScope handle_scope(isolate); |
108 | 109 |
109 RegisterTemplate(isolate); | 110 RegisterTemplate(isolate); |
110 scoped_refptr<MyObject> obj = MyObject::Create(); | 111 gin::Handle<MyObject> obj = MyObject::Create(isolate); |
111 | 112 |
112 obj->set_value(42); | 113 obj->set_value(42); |
113 EXPECT_EQ(42, obj->value()); | 114 EXPECT_EQ(42, obj->value()); |
114 | 115 |
115 v8::Handle<v8::String> source = StringToV8(isolate, | 116 v8::Handle<v8::String> source = StringToV8(isolate, |
116 "(function (obj) {" | 117 "(function (obj) {" |
117 " if (obj.value !== 42) throw 'FAIL';" | 118 " if (obj.value !== 42) throw 'FAIL';" |
118 " else obj.value = 191; })"); | 119 " else obj.value = 191; })"); |
119 EXPECT_FALSE(source.IsEmpty()); | 120 EXPECT_FALSE(source.IsEmpty()); |
120 | 121 |
121 v8::TryCatch try_catch; | 122 v8::TryCatch try_catch; |
122 v8::Handle<v8::Script> script = v8::Script::New(source); | 123 v8::Handle<v8::Script> script = v8::Script::New(source); |
123 EXPECT_FALSE(script.IsEmpty()); | 124 EXPECT_FALSE(script.IsEmpty()); |
124 v8::Handle<v8::Value> val = script->Run(); | 125 v8::Handle<v8::Value> val = script->Run(); |
125 EXPECT_FALSE(val.IsEmpty()); | 126 EXPECT_FALSE(val.IsEmpty()); |
126 v8::Handle<v8::Function> func; | 127 v8::Handle<v8::Function> func; |
127 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); | 128 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); |
128 v8::Handle<v8::Value> argv[] = { | 129 v8::Handle<v8::Value> argv[] = { |
129 ConvertToV8(isolate, obj.get()), | 130 ConvertToV8(isolate, obj.Get()), |
130 }; | 131 }; |
131 func->Call(v8::Undefined(isolate), 1, argv); | 132 func->Call(v8::Undefined(isolate), 1, argv); |
132 | 133 |
133 EXPECT_EQ(191, obj->value()); | 134 EXPECT_EQ(191, obj->value()); |
134 } | 135 } |
135 | 136 |
136 } // namespace | 137 } // namespace |
137 } // namespace gin | 138 } // namespace gin |
OLD | NEW |