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
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "gin/arguments.h"
7 #include "gin/handle.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_isolate_data.h"
10 #include "gin/public/isolate_holder.h"
11 #include "gin/test/v8_test.h"
12 #include "gin/try_catch.h"
13 #include "gin/wrappable.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 BaseClass() : value_(23) {}
21 virtual ~BaseClass() {}
26 DISALLOW_COPY_AND_ASSIGN(BaseClass
);
29 class MyObject
: public BaseClass
,
30 public Wrappable
<MyObject
> {
32 static WrapperInfo kWrapperInfo
;
34 static gin::Handle
<MyObject
> Create(v8::Isolate
* isolate
) {
35 return CreateHandle(isolate
, new MyObject());
38 int value() const { return value_
; }
39 void set_value(int value
) { value_
= value
; }
42 MyObject() : value_(0) {}
43 ObjectTemplateBuilder
GetObjectTemplateBuilder(v8::Isolate
* isolate
) override
;
44 ~MyObject() override
{}
50 class MyObjectSubclass
: public MyObject
{
52 static gin::Handle
<MyObjectSubclass
> Create(v8::Isolate
* isolate
) {
53 return CreateHandle(isolate
, new MyObjectSubclass());
56 void SayHello(const std::string
& name
) {
57 result
= std::string("Hello, ") + name
;
63 ObjectTemplateBuilder
GetObjectTemplateBuilder(
64 v8::Isolate
* isolate
) override
{
65 return MyObject::GetObjectTemplateBuilder(isolate
)
66 .SetMethod("sayHello", &MyObjectSubclass::SayHello
);
72 ~MyObjectSubclass() override
{}
75 class MyCallableObject
: public Wrappable
<MyCallableObject
> {
77 static WrapperInfo kWrapperInfo
;
79 static gin::Handle
<MyCallableObject
> Create(v8::Isolate
* isolate
) {
80 return CreateHandle(isolate
, new MyCallableObject());
83 int result() { return result_
; }
86 ObjectTemplateBuilder
GetObjectTemplateBuilder(
87 v8::Isolate
* isolate
) override
{
88 return Wrappable
<MyCallableObject
>::GetObjectTemplateBuilder(isolate
)
89 .SetCallAsFunctionHandler(&MyCallableObject::Call
);
92 MyCallableObject() : result_(0) {
95 ~MyCallableObject() override
{}
97 void Call(int val1
, int val2
, int val3
, const gin::Arguments
& arguments
) {
98 if (arguments
.IsConstructCall())
99 arguments
.ThrowTypeError("Cannot be called as constructor.");
107 class MyObject2
: public Wrappable
<MyObject2
> {
109 static WrapperInfo kWrapperInfo
;
112 class MyObjectBlink
: public Wrappable
<MyObjectBlink
> {
114 static WrapperInfo kWrapperInfo
;
117 WrapperInfo
MyObject::kWrapperInfo
= { kEmbedderNativeGin
};
118 ObjectTemplateBuilder
MyObject::GetObjectTemplateBuilder(v8::Isolate
* isolate
) {
119 return Wrappable
<MyObject
>::GetObjectTemplateBuilder(isolate
)
120 .SetProperty("value", &MyObject::value
, &MyObject::set_value
);
123 WrapperInfo
MyCallableObject::kWrapperInfo
= { kEmbedderNativeGin
};
124 WrapperInfo
MyObject2::kWrapperInfo
= { kEmbedderNativeGin
};
125 WrapperInfo
MyObjectBlink::kWrapperInfo
= { kEmbedderNativeGin
};
127 typedef V8Test WrappableTest
;
129 TEST_F(WrappableTest
, WrapAndUnwrap
) {
130 v8::Isolate
* isolate
= instance_
->isolate();
131 v8::HandleScope
handle_scope(isolate
);
133 Handle
<MyObject
> obj
= MyObject::Create(isolate
);
135 v8::Local
<v8::Value
> wrapper
= ConvertToV8(isolate
, obj
.get());
136 EXPECT_FALSE(wrapper
.IsEmpty());
138 MyObject
* unwrapped
= NULL
;
139 EXPECT_TRUE(ConvertFromV8(isolate
, wrapper
, &unwrapped
));
140 EXPECT_EQ(obj
.get(), unwrapped
);
143 TEST_F(WrappableTest
, UnwrapFailures
) {
144 v8::Isolate
* isolate
= instance_
->isolate();
145 v8::HandleScope
handle_scope(isolate
);
147 // Something that isn't an object.
148 v8::Local
<v8::Value
> thing
= v8::Number::New(isolate
, 42);
149 MyObject
* unwrapped
= NULL
;
150 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
151 EXPECT_FALSE(unwrapped
);
153 // An object that's not wrapping anything.
154 thing
= v8::Object::New(isolate
);
155 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
156 EXPECT_FALSE(unwrapped
);
158 // An object that's wrapping a C++ object from Blink.
160 thing
= ConvertToV8(isolate
, new MyObjectBlink());
161 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
162 EXPECT_FALSE(unwrapped
);
164 // An object that's wrapping a C++ object of the wrong type.
166 thing
= ConvertToV8(isolate
, new MyObject2());
167 EXPECT_FALSE(ConvertFromV8(isolate
, thing
, &unwrapped
));
168 EXPECT_FALSE(unwrapped
);
171 TEST_F(WrappableTest
, GetAndSetProperty
) {
172 v8::Isolate
* isolate
= instance_
->isolate();
173 v8::HandleScope
handle_scope(isolate
);
175 gin::Handle
<MyObject
> obj
= MyObject::Create(isolate
);
178 EXPECT_EQ(42, obj
->value());
180 v8::Local
<v8::String
> source
= StringToV8(isolate
,
182 " if (obj.value !== 42) throw 'FAIL';"
183 " else obj.value = 191; })");
184 EXPECT_FALSE(source
.IsEmpty());
186 gin::TryCatch try_catch
;
187 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
188 EXPECT_FALSE(script
.IsEmpty());
189 v8::Local
<v8::Value
> val
= script
->Run();
190 EXPECT_FALSE(val
.IsEmpty());
191 v8::Local
<v8::Function
> func
;
192 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
193 v8::Local
<v8::Value
> argv
[] = {
194 ConvertToV8(isolate
, obj
.get()),
196 func
->Call(v8::Undefined(isolate
), 1, argv
);
197 EXPECT_FALSE(try_catch
.HasCaught());
198 EXPECT_EQ("", try_catch
.GetStackTrace());
200 EXPECT_EQ(191, obj
->value());
203 TEST_F(WrappableTest
, WrappableSubclass
) {
204 v8::Isolate
* isolate
= instance_
->isolate();
205 v8::HandleScope
handle_scope(isolate
);
207 gin::Handle
<MyObjectSubclass
> object(MyObjectSubclass::Create(isolate
));
208 v8::Local
<v8::String
> source
= StringToV8(isolate
,
210 "obj.sayHello('Lily');"
212 gin::TryCatch try_catch
;
213 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
214 v8::Local
<v8::Value
> val
= script
->Run();
215 v8::Local
<v8::Function
> func
;
216 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
217 v8::Local
<v8::Value
> argv
[] = {
218 ConvertToV8(isolate
, object
.get())
220 func
->Call(v8::Undefined(isolate
), 1, argv
);
221 EXPECT_FALSE(try_catch
.HasCaught());
222 EXPECT_EQ("Hello, Lily", object
->result
);
225 TEST_F(WrappableTest
, CallAsFunction
) {
226 v8::Isolate
* isolate
= instance_
->isolate();
227 v8::HandleScope
handle_scope(isolate
);
229 gin::Handle
<MyCallableObject
> object(MyCallableObject::Create(isolate
));
230 EXPECT_EQ(0, object
->result());
231 v8::Local
<v8::String
> source
= StringToV8(isolate
,
235 gin::TryCatch try_catch
;
236 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
237 v8::Local
<v8::Value
> val
= script
->Run();
238 v8::Local
<v8::Function
> func
;
239 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
240 v8::Local
<v8::Value
> argv
[] = {
241 ConvertToV8(isolate
, object
.get())
243 func
->Call(v8::Undefined(isolate
), 1, argv
);
244 EXPECT_FALSE(try_catch
.HasCaught());
245 EXPECT_EQ(42, object
->result());
248 TEST_F(WrappableTest
, CallAsConstructor
) {
249 v8::Isolate
* isolate
= instance_
->isolate();
250 v8::HandleScope
handle_scope(isolate
);
252 gin::Handle
<MyCallableObject
> object(MyCallableObject::Create(isolate
));
253 EXPECT_EQ(0, object
->result());
254 v8::Local
<v8::String
> source
= StringToV8(isolate
,
258 gin::TryCatch try_catch
;
259 v8::Local
<v8::Script
> script
= v8::Script::Compile(source
);
260 v8::Local
<v8::Value
> val
= script
->Run();
261 v8::Local
<v8::Function
> func
;
262 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
263 v8::Local
<v8::Value
> argv
[] = {
264 ConvertToV8(isolate
, object
.get())
266 func
->Call(v8::Undefined(isolate
), 1, argv
);
267 EXPECT_TRUE(try_catch
.HasCaught());