Add URL origin checks for Service Worker (un)registration
[chromium-blink-merge.git] / gin / interceptor_unittest.cc
blobee6b7dc50a765a7fd14bbdd5bd6a5768187fb632
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"
17 namespace gin {
19 class MyInterceptor : public Wrappable<MyInterceptor>,
20 public NamedPropertyInterceptor,
21 public IndexedPropertyInterceptor {
22 public:
23 static WrapperInfo kWrapperInfo;
25 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
26 return CreateHandle(isolate, new MyInterceptor(isolate));
29 int value() const { return value_; }
30 void set_value(int value) { value_ = value; }
32 // gin::NamedPropertyInterceptor
33 virtual v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
34 const std::string& property)
35 OVERRIDE {
36 if (property == "value") {
37 return ConvertToV8(isolate, value_);
38 } else if (property == "func") {
39 return CreateFunctionTemplate(isolate,
40 base::Bind(&MyInterceptor::Call),
41 HolderIsFirstArgument)->GetFunction();
42 } else {
43 return v8::Local<v8::Value>();
46 virtual void SetNamedProperty(v8::Isolate* isolate,
47 const std::string& property,
48 v8::Local<v8::Value> value) OVERRIDE {
49 if (property != "value")
50 return;
51 ConvertFromV8(isolate, value, &value_);
53 virtual 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 virtual 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 virtual void SetIndexedProperty(v8::Isolate* isolate,
69 uint32_t index,
70 v8::Local<v8::Value> value) OVERRIDE {
71 if (index != 0)
72 return;
73 ConvertFromV8(isolate, value, &value_);
75 virtual std::vector<uint32_t> EnumerateIndexedProperties(v8::Isolate* isolate)
76 OVERRIDE {
77 std::vector<uint32_t> result;
78 result.push_back(0);
79 return result;
82 private:
83 explicit MyInterceptor(v8::Isolate* isolate)
84 : NamedPropertyInterceptor(isolate, this),
85 IndexedPropertyInterceptor(isolate, this),
86 value_(0) {}
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 int value_;
106 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
108 class InterceptorTest : public V8Test {
109 public:
110 void RunInterceptorTest(const std::string& script_source) {
111 v8::Isolate* isolate = instance_->isolate();
112 v8::HandleScope handle_scope(isolate);
114 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
116 obj->set_value(42);
117 EXPECT_EQ(42, obj->value());
119 v8::Handle<v8::String> source = StringToV8(isolate, script_source);
120 EXPECT_FALSE(source.IsEmpty());
122 gin::TryCatch try_catch;
123 v8::Handle<v8::Script> script = v8::Script::Compile(source);
124 EXPECT_FALSE(script.IsEmpty());
125 v8::Handle<v8::Value> val = script->Run();
126 EXPECT_FALSE(val.IsEmpty());
127 v8::Handle<v8::Function> func;
128 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
129 v8::Handle<v8::Value> argv[] = {ConvertToV8(isolate, obj.get()), };
130 func->Call(v8::Undefined(isolate), 1, argv);
131 EXPECT_FALSE(try_catch.HasCaught());
132 EXPECT_EQ("", try_catch.GetStackTrace());
134 EXPECT_EQ(191, obj->value());
138 TEST_F(InterceptorTest, NamedInterceptor) {
139 RunInterceptorTest(
140 "(function (obj) {"
141 " if (obj.value !== 42) throw 'FAIL';"
142 " else obj.value = 191; })");
145 TEST_F(InterceptorTest, NamedInterceptorCall) {
146 RunInterceptorTest(
147 "(function (obj) {"
148 " if (obj.func(191) !== 42) throw 'FAIL';"
149 " })");
152 TEST_F(InterceptorTest, IndexedInterceptor) {
153 RunInterceptorTest(
154 "(function (obj) {"
155 " if (obj[0] !== 42) throw 'FAIL';"
156 " else obj[0] = 191; })");
159 } // namespace gin