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

Side by Side Diff: gin/wrappable_unittest.cc

Issue 118423004: Add support for wrapping classes indirectly inherited from gin::Wrappable<T> (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update StatsCollectionController Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « gin/wrappable.cc ('k') | mojo/apps/js/bindings/gl/context.h » ('j') | 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"
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 v8::Local<v8::ObjectTemplate> GetObjectTemplate(v8::Isolate* isolate);
23
24 static gin::Handle<MyObject> Create(v8::Isolate* isolate) { 22 static gin::Handle<MyObject> Create(v8::Isolate* isolate) {
25 return CreateHandle(isolate, new MyObject()); 23 return CreateHandle(isolate, new MyObject());
26 } 24 }
27 25
28 int value() const { return value_; } 26 int value() const { return value_; }
29 void set_value(int value) { value_ = value; } 27 void set_value(int value) { value_ = value; }
30 28
31 private: 29 protected:
32 MyObject() : value_(0) {} 30 MyObject() : value_(0) {}
31 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(
32 v8::Isolate* isolate) OVERRIDE;
33 virtual ~MyObject() {} 33 virtual ~MyObject() {}
34 34
35 private:
35 int value_; 36 int value_;
36 }; 37 };
37 38
39 class MyObjectSubclass : public MyObject {
40 public:
41 static gin::Handle<MyObjectSubclass> Create(v8::Isolate* isolate) {
42 return CreateHandle(isolate, new MyObjectSubclass());
43 }
44
45 void SayHello(const std::string& name) {
46 result = std::string("Hello, ") + name;
47 }
48
49 std::string result;
50
51 private:
52 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(
53 v8::Isolate* isolate) OVERRIDE {
54 return MyObject::GetObjectTemplateBuilder(isolate)
55 .SetMethod("sayHello", &MyObjectSubclass::SayHello);
56 }
57
58 MyObjectSubclass() {
59 }
60
61 virtual ~MyObjectSubclass() {
62 }
63 };
64
38 class MyObject2 : public Wrappable<MyObject2> { 65 class MyObject2 : public Wrappable<MyObject2> {
39 public: 66 public:
40 static WrapperInfo kWrapperInfo; 67 static WrapperInfo kWrapperInfo;
41 }; 68 };
42 69
43 class MyObjectBlink : public Wrappable<MyObjectBlink> { 70 class MyObjectBlink : public Wrappable<MyObjectBlink> {
44 public: 71 public:
45 static WrapperInfo kWrapperInfo; 72 static WrapperInfo kWrapperInfo;
46 }; 73 };
47 74
48 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; 75 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin };
49 v8::Local<v8::ObjectTemplate> MyObject::GetObjectTemplate( 76 ObjectTemplateBuilder MyObject::GetObjectTemplateBuilder(v8::Isolate* isolate) {
50 v8::Isolate* isolate) { 77 return Wrappable<MyObject>::GetObjectTemplateBuilder(isolate)
51 return ObjectTemplateBuilder(isolate) 78 .SetProperty("value", &MyObject::value, &MyObject::set_value);
52 .SetProperty("value", &MyObject::value, &MyObject::set_value)
53 .Build();
54 } 79 }
55 80
56 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; 81 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin };
57 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin }; 82 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin };
58 83
59 typedef V8Test WrappableTest; 84 typedef V8Test WrappableTest;
60 85
61 TEST_F(WrappableTest, WrapAndUnwrap) { 86 TEST_F(WrappableTest, WrapAndUnwrap) {
62 v8::Isolate* isolate = instance_->isolate(); 87 v8::Isolate* isolate = instance_->isolate();
63 v8::HandleScope handle_scope(isolate); 88 v8::HandleScope handle_scope(isolate);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 v8::Handle<v8::Value> argv[] = { 150 v8::Handle<v8::Value> argv[] = {
126 ConvertToV8(isolate, obj.get()), 151 ConvertToV8(isolate, obj.get()),
127 }; 152 };
128 func->Call(v8::Undefined(isolate), 1, argv); 153 func->Call(v8::Undefined(isolate), 1, argv);
129 EXPECT_FALSE(try_catch.HasCaught()); 154 EXPECT_FALSE(try_catch.HasCaught());
130 EXPECT_EQ("", try_catch.GetStackTrace()); 155 EXPECT_EQ("", try_catch.GetStackTrace());
131 156
132 EXPECT_EQ(191, obj->value()); 157 EXPECT_EQ(191, obj->value());
133 } 158 }
134 159
160 TEST_F(WrappableTest, WrappableSubclass) {
161 v8::Isolate* isolate = instance_->isolate();
162 v8::HandleScope handle_scope(isolate);
163
164 gin::Handle<MyObjectSubclass> object(MyObjectSubclass::Create(isolate));
165 v8::Handle<v8::String> source = StringToV8(isolate,
166 "(function(obj) {"
167 "obj.sayHello('Lily');"
168 "})");
169 gin::TryCatch try_catch;
170 v8::Handle<v8::Script> script = v8::Script::New(source);
171 v8::Handle<v8::Value> val = script->Run();
172 v8::Handle<v8::Function> func;
173 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
174 v8::Handle<v8::Value> argv[] = {
175 ConvertToV8(isolate, object.get())
176 };
177 func->Call(v8::Undefined(isolate), 1, argv);
178 EXPECT_FALSE(try_catch.HasCaught());
179 EXPECT_EQ("Hello, Lily", object->result);
180 }
181
135 } // namespace gin 182 } // namespace gin
OLDNEW
« no previous file with comments | « gin/wrappable.cc ('k') | mojo/apps/js/bindings/gl/context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698