Flip this back to true. IdleHandlers should certainly be being scheduled when the...
[chromium-blink-merge.git] / gin / interceptor_unittest.cc
blob965ad6c57f39092d3a9f28da6ff056cd757332da
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 void SetNamedProperty(v8::Isolate* isolate,
46 const std::string& property,
47 v8::Local<v8::Value> value) OVERRIDE {
48 if (property != "value")
49 return;
50 ConvertFromV8(isolate, value, &value_);
52 virtual std::vector<std::string> EnumerateNamedProperties(
53 v8::Isolate* isolate) OVERRIDE {
54 std::vector<std::string> result;
55 result.push_back("func");
56 result.push_back("value");
57 return result;
60 // gin::IndexedPropertyInterceptor
61 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
62 uint32_t index) OVERRIDE {
63 if (index == 0)
64 return ConvertToV8(isolate, value_);
65 return v8::Local<v8::Value>();
67 virtual void SetIndexedProperty(v8::Isolate* isolate,
68 uint32_t index,
69 v8::Local<v8::Value> value) OVERRIDE {
70 if (index != 0)
71 return;
72 ConvertFromV8(isolate, value, &value_);
74 virtual std::vector<uint32_t> EnumerateIndexedProperties(v8::Isolate* isolate)
75 OVERRIDE {
76 std::vector<uint32_t> result;
77 result.push_back(0);
78 return result;
81 private:
82 explicit MyInterceptor(v8::Isolate* isolate)
83 : NamedPropertyInterceptor(isolate, this),
84 IndexedPropertyInterceptor(isolate, this),
85 value_(0),
86 template_cache_(isolate) {}
87 virtual ~MyInterceptor() {}
89 // gin::Wrappable
90 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate)
91 OVERRIDE {
92 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
93 .AddNamedPropertyInterceptor()
94 .AddIndexedPropertyInterceptor();
97 int Call(int value) {
98 int tmp = value_;
99 value_ = value;
100 return tmp;
103 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
104 const std::string& name) {
105 v8::Local<v8::FunctionTemplate> function_template =
106 template_cache_.Get(name);
107 if (!function_template.IsEmpty())
108 return function_template;
109 function_template = CreateFunctionTemplate(
110 isolate, base::Bind(&MyInterceptor::Call), HolderIsFirstArgument);
111 template_cache_.Set(name, function_template);
112 return function_template;
115 int value_;
117 v8::StdPersistentValueMap<std::string, v8::FunctionTemplate> template_cache_;
119 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
122 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
124 class InterceptorTest : public V8Test {
125 public:
126 void RunInterceptorTest(const std::string& script_source) {
127 v8::Isolate* isolate = instance_->isolate();
128 v8::HandleScope handle_scope(isolate);
130 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
132 obj->set_value(42);
133 EXPECT_EQ(42, obj->value());
135 v8::Handle<v8::String> source = StringToV8(isolate, script_source);
136 EXPECT_FALSE(source.IsEmpty());
138 gin::TryCatch try_catch;
139 v8::Handle<v8::Script> script = v8::Script::Compile(source);
140 EXPECT_FALSE(script.IsEmpty());
141 v8::Handle<v8::Value> val = script->Run();
142 EXPECT_FALSE(val.IsEmpty());
143 v8::Handle<v8::Function> func;
144 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
145 v8::Handle<v8::Value> argv[] = {ConvertToV8(isolate, obj.get()), };
146 func->Call(v8::Undefined(isolate), 1, argv);
147 EXPECT_FALSE(try_catch.HasCaught());
148 EXPECT_EQ("", try_catch.GetStackTrace());
150 EXPECT_EQ(191, obj->value());
154 TEST_F(InterceptorTest, NamedInterceptor) {
155 RunInterceptorTest(
156 "(function (obj) {"
157 " if (obj.value !== 42) throw 'FAIL';"
158 " else obj.value = 191; })");
161 TEST_F(InterceptorTest, NamedInterceptorCall) {
162 RunInterceptorTest(
163 "(function (obj) {"
164 " if (obj.func(191) !== 42) throw 'FAIL';"
165 " })");
168 TEST_F(InterceptorTest, IndexedInterceptor) {
169 RunInterceptorTest(
170 "(function (obj) {"
171 " if (obj[0] !== 42) throw 'FAIL';"
172 " else obj[0] = 191; })");
175 } // namespace gin