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

Side by Side Diff: gin/wrappable_unittest.cc

Issue 105743007: Gin: Make it easier to implement Wrappable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup 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/waiting_callback.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 namespace {
18 17
19 class MyObject : public Wrappable { 18 class MyObject : public Wrappable<MyObject> {
20 public: 19 public:
21 static gin::Handle<MyObject> Create(v8::Isolate* isolate); 20 static gin::Handle<MyObject> Create(v8::Isolate* isolate) {
21 return CreateHandle(isolate, new MyObject());
22 }
22 23
23 int value() const { return value_; } 24 int value() const { return value_; }
24 void set_value(int value) { value_ = value; } 25 void set_value(int value) { value_ = value; }
25 26
26 static WrapperInfo kWrapperInfo;
27 virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
28
29 private: 27 private:
30 MyObject() : value_(0) {} 28 MyObject() : value_(0) {}
31 virtual ~MyObject() {} 29 ~MyObject() {}
32 30
33 int value_; 31 int value_;
34 }; 32 };
35 33
36 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin }; 34 class MyObject2 : public Wrappable<MyObject2> {
37
38 gin::Handle<MyObject> MyObject::Create(v8::Isolate* isolate) {
39 return CreateHandle(isolate, new MyObject());
40 }
41
42 WrapperInfo* MyObject::GetWrapperInfo() {
43 return &kWrapperInfo;
44 }
45
46
47 class MyObject2 : public Wrappable {
48 public:
49 MyObject2() {
50 }
51 static WrapperInfo kWrapperInfo;
52 virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
53 }; 35 };
54 36
55 WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin }; 37 class MyObjectBlink : public Wrappable<MyObjectBlink> {
56
57 WrapperInfo* MyObject2::GetWrapperInfo() {
58 return &kWrapperInfo;
59 }
60
61
62 class MyObjectBlink : public Wrappable {
63 public:
64 MyObjectBlink() {
65 }
66 static WrapperInfo kWrapperInfo;
67 virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
68 }; 38 };
69 39
70 WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderBlink }; 40 INIT_WRAPPABLE(gin::MyObject);
71 41 INIT_WRAPPABLE(gin::MyObject2);
72 WrapperInfo* MyObjectBlink::GetWrapperInfo() { 42 INIT_WRAPPABLE(gin::MyObjectBlink);
73 return &kWrapperInfo;
74 }
75
76 43
77 void RegisterTemplates(v8::Isolate* isolate) { 44 void RegisterTemplates(v8::Isolate* isolate) {
78 PerIsolateData* data = PerIsolateData::From(isolate); 45 PerIsolateData* data = PerIsolateData::From(isolate);
79 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty()); 46 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty());
80 47
81 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate) 48 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate)
82 .SetProperty("value", &MyObject::value, &MyObject::set_value) 49 .SetProperty("value", &MyObject::value, &MyObject::set_value)
83 .Build(); 50 .Build();
84 templ->SetInternalFieldCount(kNumberOfInternalFields); 51 templ->SetInternalFieldCount(kNumberOfInternalFields);
85 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ); 52 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 v8::Handle<v8::Value> argv[] = { 133 v8::Handle<v8::Value> argv[] = {
167 ConvertToV8(isolate, obj.get()), 134 ConvertToV8(isolate, obj.get()),
168 }; 135 };
169 func->Call(v8::Undefined(isolate), 1, argv); 136 func->Call(v8::Undefined(isolate), 1, argv);
170 EXPECT_FALSE(try_catch.HasCaught()); 137 EXPECT_FALSE(try_catch.HasCaught());
171 EXPECT_EQ("", try_catch.GetStackTrace()); 138 EXPECT_EQ("", try_catch.GetStackTrace());
172 139
173 EXPECT_EQ(191, obj->value()); 140 EXPECT_EQ(191, obj->value());
174 } 141 }
175 142
176 } // namespace
177 } // namespace gin 143 } // namespace gin
OLDNEW
« no previous file with comments | « gin/wrappable.cc ('k') | mojo/apps/js/bindings/waiting_callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698