OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "extensions/renderer/api_binding.h" | 10 #include "extensions/renderer/api_binding.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 void SetProperties(const char* properties) { | 167 void SetProperties(const char* properties) { |
168 binding_properties_ = DictionaryValueFromString(properties); | 168 binding_properties_ = DictionaryValueFromString(properties); |
169 ASSERT_TRUE(binding_properties_); | 169 ASSERT_TRUE(binding_properties_); |
170 } | 170 } |
171 | 171 |
172 void SetHooks(std::unique_ptr<APIBindingHooks> hooks) { | 172 void SetHooks(std::unique_ptr<APIBindingHooks> hooks) { |
173 binding_hooks_ = std::move(hooks); | 173 binding_hooks_ = std::move(hooks); |
174 ASSERT_TRUE(binding_hooks_); | 174 ASSERT_TRUE(binding_hooks_); |
175 } | 175 } |
176 | 176 |
| 177 void SetCreateCustomType(const APIBinding::CreateCustomType& callback) { |
| 178 create_custom_type_ = callback; |
| 179 } |
| 180 |
177 void InitializeBinding() { | 181 void InitializeBinding() { |
178 if (!binding_hooks_) { | 182 if (!binding_hooks_) { |
179 binding_hooks_ = | 183 binding_hooks_ = |
180 base::MakeUnique<APIBindingHooks>(binding::RunJSFunctionSync()); | 184 base::MakeUnique<APIBindingHooks>(binding::RunJSFunctionSync()); |
181 } | 185 } |
182 event_handler_ = base::MakeUnique<APIEventHandler>( | 186 event_handler_ = base::MakeUnique<APIEventHandler>( |
183 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 187 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
184 base::Bind(&OnEventListenersChanged)); | 188 base::Bind(&OnEventListenersChanged)); |
185 binding_ = base::MakeUnique<APIBinding>( | 189 binding_ = base::MakeUnique<APIBinding>( |
186 "test", binding_functions_.get(), binding_types_.get(), | 190 "test", binding_functions_.get(), binding_types_.get(), |
187 binding_events_.get(), binding_properties_.get(), | 191 binding_events_.get(), binding_properties_.get(), create_custom_type_, |
188 std::move(binding_hooks_), &type_refs_, request_handler_.get(), | 192 std::move(binding_hooks_), &type_refs_, request_handler_.get(), |
189 event_handler_.get()); | 193 event_handler_.get()); |
190 EXPECT_EQ(!binding_types_.get(), type_refs_.empty()); | 194 EXPECT_EQ(!binding_types_.get(), type_refs_.empty()); |
191 } | 195 } |
192 | 196 |
193 void ExpectPass(v8::Local<v8::Object> object, | 197 void ExpectPass(v8::Local<v8::Object> object, |
194 const std::string& script_source, | 198 const std::string& script_source, |
195 const std::string& expected_json_arguments_single_quotes, | 199 const std::string& expected_json_arguments_single_quotes, |
196 bool expect_callback) { | 200 bool expect_callback) { |
197 ExpectPass(ContextLocal(), object, script_source, | 201 ExpectPass(ContextLocal(), object, script_source, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 std::unique_ptr<APIBinding> binding_; | 242 std::unique_ptr<APIBinding> binding_; |
239 std::unique_ptr<APIEventHandler> event_handler_; | 243 std::unique_ptr<APIEventHandler> event_handler_; |
240 std::unique_ptr<APIRequestHandler> request_handler_; | 244 std::unique_ptr<APIRequestHandler> request_handler_; |
241 APITypeReferenceMap type_refs_; | 245 APITypeReferenceMap type_refs_; |
242 | 246 |
243 std::unique_ptr<base::ListValue> binding_functions_; | 247 std::unique_ptr<base::ListValue> binding_functions_; |
244 std::unique_ptr<base::ListValue> binding_events_; | 248 std::unique_ptr<base::ListValue> binding_events_; |
245 std::unique_ptr<base::ListValue> binding_types_; | 249 std::unique_ptr<base::ListValue> binding_types_; |
246 std::unique_ptr<base::DictionaryValue> binding_properties_; | 250 std::unique_ptr<base::DictionaryValue> binding_properties_; |
247 std::unique_ptr<APIBindingHooks> binding_hooks_; | 251 std::unique_ptr<APIBindingHooks> binding_hooks_; |
| 252 APIBinding::CreateCustomType create_custom_type_; |
248 | 253 |
249 DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest); | 254 DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest); |
250 }; | 255 }; |
251 | 256 |
252 void APIBindingUnittest::RunTest(v8::Local<v8::Context> context, | 257 void APIBindingUnittest::RunTest(v8::Local<v8::Context> context, |
253 v8::Local<v8::Object> object, | 258 v8::Local<v8::Object> object, |
254 const std::string& script_source, | 259 const std::string& script_source, |
255 bool should_pass, | 260 bool should_pass, |
256 const std::string& expected_json_arguments, | 261 const std::string& expected_json_arguments, |
257 bool expect_callback, | 262 bool expect_callback, |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 v8::HandleScope handle_scope(isolate()); | 562 v8::HandleScope handle_scope(isolate()); |
558 v8::Local<v8::Context> context = ContextLocal(); | 563 v8::Local<v8::Context> context = ContextLocal(); |
559 v8::Local<v8::Object> binding_object = | 564 v8::Local<v8::Object> binding_object = |
560 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); | 565 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); |
561 EXPECT_EQ("17", | 566 EXPECT_EQ("17", |
562 GetStringPropertyFromObject(binding_object, context, "prop1")); | 567 GetStringPropertyFromObject(binding_object, context, "prop1")); |
563 EXPECT_EQ(R"({"subprop1":"some value","subprop2":true})", | 568 EXPECT_EQ(R"({"subprop1":"some value","subprop2":true})", |
564 GetStringPropertyFromObject(binding_object, context, "prop2")); | 569 GetStringPropertyFromObject(binding_object, context, "prop2")); |
565 } | 570 } |
566 | 571 |
| 572 TEST_F(APIBindingUnittest, TestRefProperties) { |
| 573 SetProperties( |
| 574 "{" |
| 575 " 'alpha': {" |
| 576 " '$ref': 'AlphaRef'" |
| 577 " }," |
| 578 " 'beta': {" |
| 579 " '$ref': 'BetaRef'" |
| 580 " }" |
| 581 "}"); |
| 582 auto create_custom_type = [](v8::Local<v8::Context> context, |
| 583 const std::string& type_name, |
| 584 const std::string& property_name) { |
| 585 v8::Isolate* isolate = context->GetIsolate(); |
| 586 v8::Local<v8::Object> result = v8::Object::New(isolate); |
| 587 if (type_name == "AlphaRef") { |
| 588 EXPECT_EQ("alpha", property_name); |
| 589 result |
| 590 ->Set(context, gin::StringToSymbol(isolate, "alphaProp"), |
| 591 gin::StringToV8(isolate, "alphaVal")) |
| 592 .ToChecked(); |
| 593 } else if (type_name == "BetaRef") { |
| 594 EXPECT_EQ("beta", property_name); |
| 595 result |
| 596 ->Set(context, gin::StringToSymbol(isolate, "betaProp"), |
| 597 gin::StringToV8(isolate, "betaVal")) |
| 598 .ToChecked(); |
| 599 } else { |
| 600 EXPECT_TRUE(false) << type_name; |
| 601 } |
| 602 return result; |
| 603 }; |
| 604 |
| 605 SetCreateCustomType(base::Bind(create_custom_type)); |
| 606 |
| 607 InitializeBinding(); |
| 608 |
| 609 v8::HandleScope handle_scope(isolate()); |
| 610 v8::Local<v8::Context> context = ContextLocal(); |
| 611 v8::Local<v8::Object> binding_object = |
| 612 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); |
| 613 EXPECT_EQ(R"({"alphaProp":"alphaVal"})", |
| 614 GetStringPropertyFromObject(binding_object, context, "alpha")); |
| 615 EXPECT_EQ( |
| 616 R"({"betaProp":"betaVal"})", |
| 617 GetStringPropertyFromObject(binding_object, context, "beta")); |
| 618 } |
| 619 |
567 TEST_F(APIBindingUnittest, TestDisposedContext) { | 620 TEST_F(APIBindingUnittest, TestDisposedContext) { |
568 SetFunctions(kFunctions); | 621 SetFunctions(kFunctions); |
569 InitializeBinding(); | 622 InitializeBinding(); |
570 | 623 |
571 v8::HandleScope handle_scope(isolate()); | 624 v8::HandleScope handle_scope(isolate()); |
572 v8::Local<v8::Context> context = ContextLocal(); | 625 v8::Local<v8::Context> context = ContextLocal(); |
573 | 626 |
574 v8::Local<v8::Object> binding_object = | 627 v8::Local<v8::Object> binding_object = |
575 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); | 628 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); |
576 | 629 |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 | 1098 |
1046 blink::WebScopedUserGesture user_gesture(nullptr); | 1099 blink::WebScopedUserGesture user_gesture(nullptr); |
1047 RunFunction(function, context, arraysize(argv), argv); | 1100 RunFunction(function, context, arraysize(argv), argv); |
1048 ASSERT_TRUE(last_request()); | 1101 ASSERT_TRUE(last_request()); |
1049 EXPECT_TRUE(last_request()->has_user_gesture); | 1102 EXPECT_TRUE(last_request()->has_user_gesture); |
1050 | 1103 |
1051 reset_last_request(); | 1104 reset_last_request(); |
1052 } | 1105 } |
1053 | 1106 |
1054 } // namespace extensions | 1107 } // namespace extensions |
OLD | NEW |