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/handle.h" |
8 #include "gin/object_template_builder.h" | 8 #include "gin/object_template_builder.h" |
9 #include "gin/per_isolate_data.h" | 9 #include "gin/per_isolate_data.h" |
10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
11 #include "gin/test/v8_test.h" | 11 #include "gin/test/v8_test.h" |
12 #include "gin/try_catch.h" | 12 #include "gin/try_catch.h" |
13 #include "gin/wrappable.h" | 13 #include "gin/wrappable.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace gin { | 16 namespace gin { |
17 | 17 |
18 class MyObject : public Wrappable<MyObject> { | 18 class MyObject : public Wrappable<MyObject> { |
19 public: | 19 public: |
20 static WrapperInfo kWrapperInfo; | 20 static WrapperInfo kWrapperInfo; |
21 | 21 |
22 static gin::Handle<MyObject> Create(v8::Isolate* isolate) { | 22 static gin::Handle<MyObject> Create(v8::Isolate* isolate) { |
23 return CreateHandle(isolate, new MyObject()); | 23 return CreateHandle(isolate, new MyObject()); |
24 } | 24 } |
25 | 25 |
26 int value() const { return value_; } | 26 int value() const { return value_; } |
27 void set_value(int value) { value_ = value; } | 27 void set_value(int value) { value_ = value; } |
28 | 28 |
29 private: | 29 private: |
| 30 friend class Wrappable<MyObject>; |
| 31 static v8::Local<v8::ObjectTemplate> GetObjectTemplate(v8::Isolate* isolate); |
| 32 |
30 MyObject() : value_(0) {} | 33 MyObject() : value_(0) {} |
31 virtual ~MyObject() {} | 34 virtual ~MyObject() {} |
32 | 35 |
33 int value_; | 36 int value_; |
34 }; | 37 }; |
35 | 38 |
36 class MyObject2 : public Wrappable<MyObject2> { | 39 class MyObject2 : public Wrappable<MyObject2> { |
37 public: | 40 public: |
38 static WrapperInfo kWrapperInfo; | 41 static WrapperInfo kWrapperInfo; |
| 42 |
| 43 private: |
| 44 friend class Wrappable<MyObject2>; |
| 45 static v8::Local<v8::ObjectTemplate> GetObjectTemplate(v8::Isolate* isolate); |
39 }; | 46 }; |
40 | 47 |
41 class MyObjectBlink : public Wrappable<MyObjectBlink> { | 48 class MyObjectBlink : public Wrappable<MyObjectBlink> { |
42 public: | 49 public: |
43 static WrapperInfo kWrapperInfo; | 50 static WrapperInfo kWrapperInfo; |
| 51 |
| 52 private: |
| 53 friend class Wrappable<MyObjectBlink>; |
| 54 static v8::Local<v8::ObjectTemplate> GetObjectTemplate(v8::Isolate* isolate); |
44 }; | 55 }; |
45 | 56 |
46 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; | 57 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; |
47 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; | 58 v8::Local<v8::ObjectTemplate> MyObject::GetObjectTemplate( |
48 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin }; | 59 v8::Isolate* isolate) { |
49 | 60 return ObjectTemplateBuilder(isolate) |
50 void RegisterTemplates(v8::Isolate* isolate) { | |
51 PerIsolateData* data = PerIsolateData::From(isolate); | |
52 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty()); | |
53 | |
54 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate) | |
55 .SetProperty("value", &MyObject::value, &MyObject::set_value) | 61 .SetProperty("value", &MyObject::value, &MyObject::set_value) |
56 .Build(); | 62 .Build(); |
57 templ->SetInternalFieldCount(kNumberOfInternalFields); | 63 } |
58 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); | |
59 | 64 |
60 templ = v8::ObjectTemplate::New(isolate); | 65 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; |
61 templ->SetInternalFieldCount(kNumberOfInternalFields); | 66 v8::Local<v8::ObjectTemplate> MyObject2::GetObjectTemplate( |
62 data->SetObjectTemplate(&MyObject2::kWrapperInfo, templ); | 67 v8::Isolate* isolate) { |
| 68 return ObjectTemplateBuilder(isolate).Build(); |
| 69 } |
63 | 70 |
64 templ = v8::ObjectTemplate::New(isolate); | 71 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin }; |
65 templ->SetInternalFieldCount(kNumberOfInternalFields); | 72 v8::Local<v8::ObjectTemplate> MyObjectBlink::GetObjectTemplate( |
66 data->SetObjectTemplate(&MyObjectBlink::kWrapperInfo, templ); | 73 v8::Isolate* isolate) { |
| 74 return ObjectTemplateBuilder(isolate).Build(); |
67 } | 75 } |
68 | 76 |
69 typedef V8Test WrappableTest; | 77 typedef V8Test WrappableTest; |
70 | 78 |
71 TEST_F(WrappableTest, WrapAndUnwrap) { | 79 TEST_F(WrappableTest, WrapAndUnwrap) { |
72 v8::Isolate* isolate = instance_->isolate(); | 80 v8::Isolate* isolate = instance_->isolate(); |
73 v8::HandleScope handle_scope(isolate); | 81 v8::HandleScope handle_scope(isolate); |
74 | 82 |
75 RegisterTemplates(isolate); | |
76 Handle<MyObject> obj = MyObject::Create(isolate); | 83 Handle<MyObject> obj = MyObject::Create(isolate); |
77 | 84 |
78 v8::Handle<v8::Value> wrapper = ConvertToV8(isolate, obj.get()); | 85 v8::Handle<v8::Value> wrapper = ConvertToV8(isolate, obj.get()); |
79 EXPECT_FALSE(wrapper.IsEmpty()); | 86 EXPECT_FALSE(wrapper.IsEmpty()); |
80 | 87 |
81 MyObject* unwrapped = NULL; | 88 MyObject* unwrapped = NULL; |
82 EXPECT_TRUE(ConvertFromV8(isolate, wrapper, &unwrapped)); | 89 EXPECT_TRUE(ConvertFromV8(isolate, wrapper, &unwrapped)); |
83 EXPECT_EQ(obj.get(), unwrapped); | 90 EXPECT_EQ(obj.get(), unwrapped); |
84 } | 91 } |
85 | 92 |
86 TEST_F(WrappableTest, UnwrapFailures) { | 93 TEST_F(WrappableTest, UnwrapFailures) { |
87 v8::Isolate* isolate = instance_->isolate(); | 94 v8::Isolate* isolate = instance_->isolate(); |
88 v8::HandleScope handle_scope(isolate); | 95 v8::HandleScope handle_scope(isolate); |
89 | 96 |
90 RegisterTemplates(isolate); | |
91 | |
92 // Something that isn't an object. | 97 // Something that isn't an object. |
93 v8::Handle<v8::Value> thing = v8::Number::New(42); | 98 v8::Handle<v8::Value> thing = v8::Number::New(42); |
94 MyObject* unwrapped = NULL; | 99 MyObject* unwrapped = NULL; |
95 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); | 100 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); |
96 EXPECT_FALSE(unwrapped); | 101 EXPECT_FALSE(unwrapped); |
97 | 102 |
98 // An object that's not wrapping anything. | 103 // An object that's not wrapping anything. |
99 thing = v8::Object::New(isolate); | 104 thing = v8::Object::New(isolate); |
100 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); | 105 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); |
101 EXPECT_FALSE(unwrapped); | 106 EXPECT_FALSE(unwrapped); |
102 | 107 |
103 // An object that's wrapping a C++ object from Blink. | 108 // An object that's wrapping a C++ object from Blink. |
104 thing.Clear(); | 109 thing.Clear(); |
105 thing = ConvertToV8(isolate, new MyObjectBlink()); | 110 thing = ConvertToV8(isolate, new MyObjectBlink()); |
106 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); | 111 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); |
107 EXPECT_FALSE(unwrapped); | 112 EXPECT_FALSE(unwrapped); |
108 | 113 |
109 // An object that's wrapping a C++ object of the wrong type. | 114 // An object that's wrapping a C++ object of the wrong type. |
110 thing.Clear(); | 115 thing.Clear(); |
111 thing = ConvertToV8(isolate, new MyObject2()); | 116 thing = ConvertToV8(isolate, new MyObject2()); |
112 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); | 117 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); |
113 EXPECT_FALSE(unwrapped); | 118 EXPECT_FALSE(unwrapped); |
114 } | 119 } |
115 | 120 |
116 TEST_F(WrappableTest, GetAndSetProperty) { | 121 TEST_F(WrappableTest, GetAndSetProperty) { |
117 v8::Isolate* isolate = instance_->isolate(); | 122 v8::Isolate* isolate = instance_->isolate(); |
118 v8::HandleScope handle_scope(isolate); | 123 v8::HandleScope handle_scope(isolate); |
119 | 124 |
120 RegisterTemplates(isolate); | |
121 gin::Handle<MyObject> obj = MyObject::Create(isolate); | 125 gin::Handle<MyObject> obj = MyObject::Create(isolate); |
122 | 126 |
123 obj->set_value(42); | 127 obj->set_value(42); |
124 EXPECT_EQ(42, obj->value()); | 128 EXPECT_EQ(42, obj->value()); |
125 | 129 |
126 v8::Handle<v8::String> source = StringToV8(isolate, | 130 v8::Handle<v8::String> source = StringToV8(isolate, |
127 "(function (obj) {" | 131 "(function (obj) {" |
128 " if (obj.value !== 42) throw 'FAIL';" | 132 " if (obj.value !== 42) throw 'FAIL';" |
129 " else obj.value = 191; })"); | 133 " else obj.value = 191; })"); |
130 EXPECT_FALSE(source.IsEmpty()); | 134 EXPECT_FALSE(source.IsEmpty()); |
131 | 135 |
132 gin::TryCatch try_catch; | 136 gin::TryCatch try_catch; |
133 v8::Handle<v8::Script> script = v8::Script::New(source); | 137 v8::Handle<v8::Script> script = v8::Script::New(source); |
134 EXPECT_FALSE(script.IsEmpty()); | 138 EXPECT_FALSE(script.IsEmpty()); |
135 v8::Handle<v8::Value> val = script->Run(); | 139 v8::Handle<v8::Value> val = script->Run(); |
136 EXPECT_FALSE(val.IsEmpty()); | 140 EXPECT_FALSE(val.IsEmpty()); |
137 v8::Handle<v8::Function> func; | 141 v8::Handle<v8::Function> func; |
138 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); | 142 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); |
139 v8::Handle<v8::Value> argv[] = { | 143 v8::Handle<v8::Value> argv[] = { |
140 ConvertToV8(isolate, obj.get()), | 144 ConvertToV8(isolate, obj.get()), |
141 }; | 145 }; |
142 func->Call(v8::Undefined(isolate), 1, argv); | 146 func->Call(v8::Undefined(isolate), 1, argv); |
143 EXPECT_FALSE(try_catch.HasCaught()); | 147 EXPECT_FALSE(try_catch.HasCaught()); |
144 EXPECT_EQ("", try_catch.GetStackTrace()); | 148 EXPECT_EQ("", try_catch.GetStackTrace()); |
145 | 149 |
146 EXPECT_EQ(191, obj->value()); | 150 EXPECT_EQ(191, obj->value()); |
147 } | 151 } |
148 | 152 |
149 } // namespace gin | 153 } // namespace gin |
OLD | NEW |