Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gin / interceptor_unittest.cc
blobe25005b4e0ef44053601da8b66f54f765d974e1c
1 // Copyright 2014 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.
5 #include "base/logging.h"
6 #include "gin/arguments.h"
7 #include "gin/handle.h"
8 #include "gin/interceptor.h"
9 #include "gin/object_template_builder.h"
10 #include "gin/per_isolate_data.h"
11 #include "gin/public/isolate_holder.h"
12 #include "gin/test/v8_test.h"
13 #include "gin/try_catch.h"
14 #include "gin/wrappable.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "v8/include/v8-util.h"
18 namespace gin {
20 class MyInterceptor : public Wrappable<MyInterceptor>,
21 public NamedPropertyInterceptor,
22 public IndexedPropertyInterceptor {
23 public:
24 static WrapperInfo kWrapperInfo;
26 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
27 return CreateHandle(isolate, new MyInterceptor(isolate));
30 int value() const { return value_; }
31 void set_value(int value) { value_ = value; }
33 // gin::NamedPropertyInterceptor
34 virtual v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
35 const std::string& property)
36 OVERRIDE {
37 if (property == "value") {
38 return ConvertToV8(isolate, value_);
39 } else if (property == "func") {
40 return GetFunctionTemplate(isolate, "func")->GetFunction();
41 } else {
42 return v8::Local<v8::Value>();
45 virtual bool SetNamedProperty(v8::Isolate* isolate,
46 const std::string& property,
47 v8::Local<v8::Value> value) OVERRIDE {
48 if (property == "value") {
49 ConvertFromV8(isolate, value, &value_);
50 return true;
52 return false;
54 virtual std::vector<std::string> EnumerateNamedProperties(
55 v8::Isolate* isolate) OVERRIDE {
56 std::vector<std::string> result;
57 result.push_back("func");
58 result.push_back("value");
59 return result;
62 // gin::IndexedPropertyInterceptor
63 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
64 uint32_t index) OVERRIDE {
65 if (index == 0)
66 return ConvertToV8(isolate, value_);
67 return v8::Local<v8::Value>();
69 virtual bool SetIndexedProperty(v8::Isolate* isolate,
70 uint32_t index,
71 v8::Local<v8::Value> value) OVERRIDE {
72 if (index == 0) {
73 ConvertFromV8(isolate, value, &value_);
74 return true;
76 // Don't allow bypassing the interceptor.
77 return true;
79 virtual std::vector<uint32_t> EnumerateIndexedProperties(v8::Isolate* isolate)
80 OVERRIDE {
81 std::vector<uint32_t> result;
82 result.push_back(0);
83 return result;
86 private:
87 explicit MyInterceptor(v8::Isolate* isolate)
88 : NamedPropertyInterceptor(isolate, this),
89 IndexedPropertyInterceptor(isolate, this),
90 value_(0),
91 template_cache_(isolate) {}
92 virtual ~MyInterceptor() {}
94 // gin::Wrappable
95 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate)
96 OVERRIDE {
97 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
98 .AddNamedPropertyInterceptor()
99 .AddIndexedPropertyInterceptor();
102 int Call(int value) {
103 int tmp = value_;
104 value_ = value;
105 return tmp;
108 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
109 const std::string& name) {
110 v8::Local<v8::FunctionTemplate> function_template =
111 template_cache_.Get(name);
112 if (!function_template.IsEmpty())
113 return function_template;
114 function_template = CreateFunctionTemplate(
115 isolate, base::Bind(&MyInterceptor::Call), HolderIsFirstArgument);
116 template_cache_.Set(name, function_template);
117 return function_template;
120 int value_;
122 v8::StdPersistentValueMap<std::string, v8::FunctionTemplate> template_cache_;
124 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
127 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
129 class InterceptorTest : public V8Test {
130 public:
131 void RunInterceptorTest(const std::string& script_source) {
132 v8::Isolate* isolate = instance_->isolate();
133 v8::HandleScope handle_scope(isolate);
135 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
137 obj->set_value(42);
138 EXPECT_EQ(42, obj->value());
140 v8::Handle<v8::String> source = StringToV8(isolate, script_source);
141 EXPECT_FALSE(source.IsEmpty());
143 gin::TryCatch try_catch;
144 v8::Handle<v8::Script> script = v8::Script::Compile(source);
145 EXPECT_FALSE(script.IsEmpty());
146 v8::Handle<v8::Value> val = script->Run();
147 EXPECT_FALSE(val.IsEmpty());
148 v8::Handle<v8::Function> func;
149 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
150 v8::Handle<v8::Value> argv[] = {ConvertToV8(isolate, obj.get()), };
151 func->Call(v8::Undefined(isolate), 1, argv);
152 EXPECT_FALSE(try_catch.HasCaught());
153 EXPECT_EQ("", try_catch.GetStackTrace());
155 EXPECT_EQ(191, obj->value());
159 TEST_F(InterceptorTest, NamedInterceptor) {
160 RunInterceptorTest(
161 "(function (obj) {"
162 " if (obj.value !== 42) throw 'FAIL';"
163 " else obj.value = 191; })");
166 TEST_F(InterceptorTest, NamedInterceptorCall) {
167 RunInterceptorTest(
168 "(function (obj) {"
169 " if (obj.func(191) !== 42) throw 'FAIL';"
170 " })");
173 TEST_F(InterceptorTest, IndexedInterceptor) {
174 RunInterceptorTest(
175 "(function (obj) {"
176 " if (obj[0] !== 42) throw 'FAIL';"
177 " else obj[0] = 191; })");
180 TEST_F(InterceptorTest, BypassInterceptorAllowed) {
181 RunInterceptorTest(
182 "(function (obj) {"
183 " obj.value = 191 /* make test happy */;"
184 " obj.foo = 23;"
185 " if (obj.foo !== 23) throw 'FAIL'; })");
188 TEST_F(InterceptorTest, BypassInterceptorForbidden) {
189 RunInterceptorTest(
190 "(function (obj) {"
191 " obj.value = 191 /* make test happy */;"
192 " obj[1] = 23;"
193 " if (obj[1] === 23) throw 'FAIL'; })");
196 } // namespace gin