Adds fake hardware video encoder.
[chromium-blink-merge.git] / gin / interceptor_unittest.cc
blob4d2d7fb89df3efab032365d946017943dfb95aa9
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 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
35 const std::string& property) override {
36 if (property == "value") {
37 return ConvertToV8(isolate, value_);
38 } else if (property == "func") {
39 return GetFunctionTemplate(isolate, "func")->GetFunction();
40 } else {
41 return v8::Local<v8::Value>();
44 bool SetNamedProperty(v8::Isolate* isolate,
45 const std::string& property,
46 v8::Local<v8::Value> value) override {
47 if (property == "value") {
48 ConvertFromV8(isolate, value, &value_);
49 return true;
51 return false;
53 std::vector<std::string> EnumerateNamedProperties(
54 v8::Isolate* isolate) override {
55 std::vector<std::string> result;
56 result.push_back("func");
57 result.push_back("value");
58 return result;
61 // gin::IndexedPropertyInterceptor
62 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
63 uint32_t index) override {
64 if (index == 0)
65 return ConvertToV8(isolate, value_);
66 return v8::Local<v8::Value>();
68 bool SetIndexedProperty(v8::Isolate* isolate,
69 uint32_t index,
70 v8::Local<v8::Value> value) override {
71 if (index == 0) {
72 ConvertFromV8(isolate, value, &value_);
73 return true;
75 // Don't allow bypassing the interceptor.
76 return true;
78 std::vector<uint32_t> EnumerateIndexedProperties(
79 v8::Isolate* isolate) override {
80 std::vector<uint32_t> result;
81 result.push_back(0);
82 return result;
85 private:
86 explicit MyInterceptor(v8::Isolate* isolate)
87 : NamedPropertyInterceptor(isolate, this),
88 IndexedPropertyInterceptor(isolate, this),
89 value_(0),
90 template_cache_(isolate) {}
91 ~MyInterceptor() override {}
93 // gin::Wrappable
94 ObjectTemplateBuilder GetObjectTemplateBuilder(
95 v8::Isolate* isolate) override {
96 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
97 .AddNamedPropertyInterceptor()
98 .AddIndexedPropertyInterceptor();
101 int Call(int value) {
102 int tmp = value_;
103 value_ = value;
104 return tmp;
107 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
108 const std::string& name) {
109 v8::Local<v8::FunctionTemplate> function_template =
110 template_cache_.Get(name);
111 if (!function_template.IsEmpty())
112 return function_template;
113 function_template = CreateFunctionTemplate(
114 isolate, base::Bind(&MyInterceptor::Call), HolderIsFirstArgument);
115 template_cache_.Set(name, function_template);
116 return function_template;
119 int value_;
121 v8::StdPersistentValueMap<std::string, v8::FunctionTemplate> template_cache_;
123 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
126 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
128 class InterceptorTest : public V8Test {
129 public:
130 void RunInterceptorTest(const std::string& script_source) {
131 v8::Isolate* isolate = instance_->isolate();
132 v8::HandleScope handle_scope(isolate);
134 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
136 obj->set_value(42);
137 EXPECT_EQ(42, obj->value());
139 v8::Handle<v8::String> source = StringToV8(isolate, script_source);
140 EXPECT_FALSE(source.IsEmpty());
142 gin::TryCatch try_catch;
143 v8::Handle<v8::Script> script = v8::Script::Compile(source);
144 EXPECT_FALSE(script.IsEmpty());
145 v8::Handle<v8::Value> val = script->Run();
146 EXPECT_FALSE(val.IsEmpty());
147 v8::Handle<v8::Function> func;
148 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
149 v8::Handle<v8::Value> argv[] = {ConvertToV8(isolate, obj.get()), };
150 func->Call(v8::Undefined(isolate), 1, argv);
151 EXPECT_FALSE(try_catch.HasCaught());
152 EXPECT_EQ("", try_catch.GetStackTrace());
154 EXPECT_EQ(191, obj->value());
158 TEST_F(InterceptorTest, NamedInterceptor) {
159 RunInterceptorTest(
160 "(function (obj) {"
161 " if (obj.value !== 42) throw 'FAIL';"
162 " else obj.value = 191; })");
165 TEST_F(InterceptorTest, NamedInterceptorCall) {
166 RunInterceptorTest(
167 "(function (obj) {"
168 " if (obj.func(191) !== 42) throw 'FAIL';"
169 " })");
172 TEST_F(InterceptorTest, IndexedInterceptor) {
173 RunInterceptorTest(
174 "(function (obj) {"
175 " if (obj[0] !== 42) throw 'FAIL';"
176 " else obj[0] = 191; })");
179 TEST_F(InterceptorTest, BypassInterceptorAllowed) {
180 RunInterceptorTest(
181 "(function (obj) {"
182 " obj.value = 191 /* make test happy */;"
183 " obj.foo = 23;"
184 " if (obj.foo !== 23) throw 'FAIL'; })");
187 TEST_F(InterceptorTest, BypassInterceptorForbidden) {
188 RunInterceptorTest(
189 "(function (obj) {"
190 " obj.value = 191 /* make test happy */;"
191 " obj[1] = 23;"
192 " if (obj[1] === 23) throw 'FAIL'; })");
195 } // namespace gin