Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: gin/wrappable_unittest.cc

Issue 1161923004: Reland: Plugin Placeholders: Refactor for platforms that don't support plugins (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove some enable_plugins that are no longer needed Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gin/wrappable.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 22 matching lines...) Expand all
33 33
34 static gin::Handle<MyObject> Create(v8::Isolate* isolate) { 34 static gin::Handle<MyObject> Create(v8::Isolate* isolate) {
35 return CreateHandle(isolate, new MyObject()); 35 return CreateHandle(isolate, new MyObject());
36 } 36 }
37 37
38 int value() const { return value_; } 38 int value() const { return value_; }
39 void set_value(int value) { value_ = value; } 39 void set_value(int value) { value_ = value; }
40 40
41 protected: 41 protected:
42 MyObject() : value_(0) {} 42 MyObject() : value_(0) {}
43 ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) override; 43 ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) final {
44 return Wrappable<MyObject>::GetObjectTemplateBuilder(isolate)
45 .SetProperty("value", &MyObject::value, &MyObject::set_value);
46 }
44 ~MyObject() override {} 47 ~MyObject() override {}
45 48
46 private: 49 private:
47 int value_; 50 int value_;
48 }; 51 };
49 52
50 class MyObjectSubclass : public MyObject {
51 public:
52 static gin::Handle<MyObjectSubclass> Create(v8::Isolate* isolate) {
53 return CreateHandle(isolate, new MyObjectSubclass());
54 }
55
56 void SayHello(const std::string& name) {
57 result = std::string("Hello, ") + name;
58 }
59
60 std::string result;
61
62 private:
63 ObjectTemplateBuilder GetObjectTemplateBuilder(
64 v8::Isolate* isolate) override {
65 return MyObject::GetObjectTemplateBuilder(isolate)
66 .SetMethod("sayHello", &MyObjectSubclass::SayHello);
67 }
68
69 MyObjectSubclass() {
70 }
71
72 ~MyObjectSubclass() override {}
73 };
74
75 class MyCallableObject : public Wrappable<MyCallableObject> { 53 class MyCallableObject : public Wrappable<MyCallableObject> {
76 public: 54 public:
77 static WrapperInfo kWrapperInfo; 55 static WrapperInfo kWrapperInfo;
78 56
79 static gin::Handle<MyCallableObject> Create(v8::Isolate* isolate) { 57 static gin::Handle<MyCallableObject> Create(v8::Isolate* isolate) {
80 return CreateHandle(isolate, new MyCallableObject()); 58 return CreateHandle(isolate, new MyCallableObject());
81 } 59 }
82 60
83 int result() { return result_; } 61 int result() { return result_; }
84 62
85 private: 63 private:
86 ObjectTemplateBuilder GetObjectTemplateBuilder( 64 ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) final {
87 v8::Isolate* isolate) override {
88 return Wrappable<MyCallableObject>::GetObjectTemplateBuilder(isolate) 65 return Wrappable<MyCallableObject>::GetObjectTemplateBuilder(isolate)
89 .SetCallAsFunctionHandler(&MyCallableObject::Call); 66 .SetCallAsFunctionHandler(&MyCallableObject::Call);
90 } 67 }
91 68
92 MyCallableObject() : result_(0) { 69 MyCallableObject() : result_(0) {
93 } 70 }
94 71
95 ~MyCallableObject() override {} 72 ~MyCallableObject() override {}
96 73
97 void Call(int val1, int val2, int val3, const gin::Arguments& arguments) { 74 void Call(int val1, int val2, int val3, const gin::Arguments& arguments) {
98 if (arguments.IsConstructCall()) 75 if (arguments.IsConstructCall())
99 arguments.ThrowTypeError("Cannot be called as constructor."); 76 arguments.ThrowTypeError("Cannot be called as constructor.");
100 else 77 else
101 result_ = val1; 78 result_ = val1;
102 } 79 }
103 80
104 int result_; 81 int result_;
105 }; 82 };
106 83
107 class MyObject2 : public Wrappable<MyObject2> { 84 class MyObject2 : public Wrappable<MyObject2> {
108 public: 85 public:
109 static WrapperInfo kWrapperInfo; 86 static WrapperInfo kWrapperInfo;
110 }; 87 };
111 88
112 class MyObjectBlink : public Wrappable<MyObjectBlink> {
113 public:
114 static WrapperInfo kWrapperInfo;
115 };
116
117 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; 89 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);
121 }
122
123 WrapperInfo MyCallableObject::kWrapperInfo = { kEmbedderNativeGin }; 90 WrapperInfo MyCallableObject::kWrapperInfo = { kEmbedderNativeGin };
124 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; 91 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin };
125 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin };
126 92
127 typedef V8Test WrappableTest; 93 typedef V8Test WrappableTest;
128 94
129 TEST_F(WrappableTest, WrapAndUnwrap) { 95 TEST_F(WrappableTest, WrapAndUnwrap) {
130 v8::Isolate* isolate = instance_->isolate(); 96 v8::Isolate* isolate = instance_->isolate();
131 v8::HandleScope handle_scope(isolate); 97 v8::HandleScope handle_scope(isolate);
132 98
133 Handle<MyObject> obj = MyObject::Create(isolate); 99 Handle<MyObject> obj = MyObject::Create(isolate);
134 100
135 v8::Local<v8::Value> wrapper = ConvertToV8(isolate, obj.get()); 101 v8::Local<v8::Value> wrapper = ConvertToV8(isolate, obj.get());
(...skipping 12 matching lines...) Expand all
148 v8::Local<v8::Value> thing = v8::Number::New(isolate, 42); 114 v8::Local<v8::Value> thing = v8::Number::New(isolate, 42);
149 MyObject* unwrapped = NULL; 115 MyObject* unwrapped = NULL;
150 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); 116 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
151 EXPECT_FALSE(unwrapped); 117 EXPECT_FALSE(unwrapped);
152 118
153 // An object that's not wrapping anything. 119 // An object that's not wrapping anything.
154 thing = v8::Object::New(isolate); 120 thing = v8::Object::New(isolate);
155 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); 121 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
156 EXPECT_FALSE(unwrapped); 122 EXPECT_FALSE(unwrapped);
157 123
158 // An object that's wrapping a C++ object from Blink.
159 thing.Clear();
160 thing = ConvertToV8(isolate, new MyObjectBlink());
161 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
162 EXPECT_FALSE(unwrapped);
163
164 // An object that's wrapping a C++ object of the wrong type. 124 // An object that's wrapping a C++ object of the wrong type.
165 thing.Clear(); 125 thing.Clear();
166 thing = ConvertToV8(isolate, new MyObject2()); 126 thing = ConvertToV8(isolate, new MyObject2());
167 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped)); 127 EXPECT_FALSE(ConvertFromV8(isolate, thing, &unwrapped));
168 EXPECT_FALSE(unwrapped); 128 EXPECT_FALSE(unwrapped);
169 } 129 }
170 130
171 TEST_F(WrappableTest, GetAndSetProperty) { 131 TEST_F(WrappableTest, GetAndSetProperty) {
172 v8::Isolate* isolate = instance_->isolate(); 132 v8::Isolate* isolate = instance_->isolate();
173 v8::HandleScope handle_scope(isolate); 133 v8::HandleScope handle_scope(isolate);
(...skipping 19 matching lines...) Expand all
193 v8::Local<v8::Value> argv[] = { 153 v8::Local<v8::Value> argv[] = {
194 ConvertToV8(isolate, obj.get()), 154 ConvertToV8(isolate, obj.get()),
195 }; 155 };
196 func->Call(v8::Undefined(isolate), 1, argv); 156 func->Call(v8::Undefined(isolate), 1, argv);
197 EXPECT_FALSE(try_catch.HasCaught()); 157 EXPECT_FALSE(try_catch.HasCaught());
198 EXPECT_EQ("", try_catch.GetStackTrace()); 158 EXPECT_EQ("", try_catch.GetStackTrace());
199 159
200 EXPECT_EQ(191, obj->value()); 160 EXPECT_EQ(191, obj->value());
201 } 161 }
202 162
203 TEST_F(WrappableTest, WrappableSubclass) {
204 v8::Isolate* isolate = instance_->isolate();
205 v8::HandleScope handle_scope(isolate);
206
207 gin::Handle<MyObjectSubclass> object(MyObjectSubclass::Create(isolate));
208 v8::Local<v8::String> source = StringToV8(isolate,
209 "(function(obj) {"
210 "obj.sayHello('Lily');"
211 "})");
212 gin::TryCatch try_catch(isolate);
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())
219 };
220 func->Call(v8::Undefined(isolate), 1, argv);
221 EXPECT_FALSE(try_catch.HasCaught());
222 EXPECT_EQ("Hello, Lily", object->result);
223 }
224
225 TEST_F(WrappableTest, CallAsFunction) { 163 TEST_F(WrappableTest, CallAsFunction) {
226 v8::Isolate* isolate = instance_->isolate(); 164 v8::Isolate* isolate = instance_->isolate();
227 v8::HandleScope handle_scope(isolate); 165 v8::HandleScope handle_scope(isolate);
228 166
229 gin::Handle<MyCallableObject> object(MyCallableObject::Create(isolate)); 167 gin::Handle<MyCallableObject> object(MyCallableObject::Create(isolate));
230 EXPECT_EQ(0, object->result()); 168 EXPECT_EQ(0, object->result());
231 v8::Local<v8::String> source = StringToV8(isolate, 169 v8::Local<v8::String> source = StringToV8(isolate,
232 "(function(obj) {" 170 "(function(obj) {"
233 "obj(42, 2, 5);" 171 "obj(42, 2, 5);"
234 "})"); 172 "})");
(...skipping 26 matching lines...) Expand all
261 v8::Local<v8::Function> func; 199 v8::Local<v8::Function> func;
262 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); 200 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
263 v8::Local<v8::Value> argv[] = { 201 v8::Local<v8::Value> argv[] = {
264 ConvertToV8(isolate, object.get()) 202 ConvertToV8(isolate, object.get())
265 }; 203 };
266 func->Call(v8::Undefined(isolate), 1, argv); 204 func->Call(v8::Undefined(isolate), 1, argv);
267 EXPECT_TRUE(try_catch.HasCaught()); 205 EXPECT_TRUE(try_catch.HasCaught());
268 } 206 }
269 207
270 } // namespace gin 208 } // namespace gin
OLDNEW
« no previous file with comments | « gin/wrappable.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698