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

Side by Side Diff: gin/wrappable_unittest.cc

Issue 79203004: [Gin] Add a mechanism for wrapping C++ object (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Disallow copy and assign 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') | 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
(Empty)
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.
4
5 #include "base/logging.h"
6 #include "gin/arguments.h"
7 #include "gin/gin.h"
8 #include "gin/per_isolate_data.h"
9 #include "gin/test/v8_test.h"
10 #include "gin/wrappable.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace gin {
14 namespace {
15
16 class MyObject : public Wrappable {
17 public:
18 static scoped_refptr<MyObject> Create();
19
20 int value() const { return value_; }
21 void set_value(int value) { value_ = value; }
22
23 static WrapperInfo kWrapperInfo;
24 virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
25
26 private:
27 MyObject() : value_(0) {}
28 virtual ~MyObject() {}
29
30 int value_;
31 };
32
33 WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin };
34
35 scoped_refptr<MyObject> MyObject::Create() {
36 return make_scoped_refptr(new MyObject());
37 }
38
39 WrapperInfo* MyObject::GetWrapperInfo() {
40 return &kWrapperInfo;
41 }
42
43 } // namespace
44
45 template<>
46 struct Converter<MyObject*> : public WrappableConverter<MyObject> {};
47
48 namespace {
49
50 // TODO(abarth): This is too much typing.
51
52 void MyObjectGetValue(const v8::FunctionCallbackInfo<v8::Value>& info) {
53 Arguments args(info);
54
55 MyObject* obj = 0;
56 CHECK(args.Holder(&obj));
57
58 args.Return(obj->value());
59 }
60
61 void MyObjectSetValue(const v8::FunctionCallbackInfo<v8::Value>& info) {
62 Arguments args(info);
63
64 MyObject* obj = 0;
65 CHECK(args.Holder(&obj));
66
67 int val = 0;
68 if (!args.GetNext(&val))
69 return args.ThrowError();
70
71 obj->set_value(val);
72 }
73
74 void RegisterTemplate(v8::Isolate* isolate) {
75 PerIsolateData* data = PerIsolateData::From(isolate);
76 DCHECK(data->GetObjectTemplate(&MyObject::kWrapperInfo).IsEmpty());
77
78 v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New();
79 templ->SetInternalFieldCount(kNumberOfInternalFields);
80 templ->SetAccessorProperty(
81 StringToSymbol(isolate, "value"),
82 v8::FunctionTemplate::New(MyObjectGetValue),
83 v8::FunctionTemplate::New(MyObjectSetValue));
84
85 data->SetObjectTemplate(&MyObject::kWrapperInfo, templ);
86 }
87
88 typedef V8Test WrappableTest;
89
90 TEST_F(WrappableTest, WrapAndUnwrap) {
91 v8::Isolate* isolate = instance_->isolate();
92 v8::HandleScope handle_scope(isolate);
93
94 RegisterTemplate(isolate);
95 scoped_refptr<MyObject> obj = MyObject::Create();
96
97 v8::Handle<v8::Value> wrapper = ConvertToV8(isolate, obj.get());
98 EXPECT_FALSE(wrapper.IsEmpty());
99
100 MyObject* unwrapped = 0;
101 EXPECT_TRUE(ConvertFromV8(wrapper, &unwrapped));
102 EXPECT_EQ(obj, unwrapped);
103 }
104
105 TEST_F(WrappableTest, GetAndSetProperty) {
106 v8::Isolate* isolate = instance_->isolate();
107 v8::HandleScope handle_scope(isolate);
108
109 RegisterTemplate(isolate);
110 scoped_refptr<MyObject> obj = MyObject::Create();
111
112 obj->set_value(42);
113 EXPECT_EQ(42, obj->value());
114
115 v8::Handle<v8::String> source = StringToV8(isolate,
116 "(function (obj) {"
117 " if (obj.value !== 42) throw 'FAIL';"
118 " else obj.value = 191; })");
119 EXPECT_FALSE(source.IsEmpty());
120
121 v8::TryCatch try_catch;
122 v8::Handle<v8::Script> script = v8::Script::New(source);
123 EXPECT_FALSE(script.IsEmpty());
124 v8::Handle<v8::Value> val = script->Run();
125 EXPECT_FALSE(val.IsEmpty());
126 v8::Handle<v8::Function> func;
127 EXPECT_TRUE(ConvertFromV8(val, &func));
128 v8::Handle<v8::Value> argv[] = {
129 ConvertToV8(isolate, obj.get()),
130 };
131 func->Call(v8::Undefined(isolate), 1, argv);
132
133 EXPECT_EQ(191, obj->value());
134 }
135
136 } // namespace
137 } // 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