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 EXPECT_TRUE(type_name == "AlphaRef" || type_name == "BetaRef") << type_name; | |
jbroman
2017/02/21 16:47:37
nit: might as well just put this in the else branc
Devlin
2017/02/21 18:05:40
Done.
| |
588 if (type_name == "AlphaRef") { | |
589 EXPECT_EQ("alpha", property_name); | |
590 result | |
591 ->Set(context, gin::StringToV8(isolate, "alphaProp"), | |
jbroman
2017/02/21 16:47:37
super-nit: it'll be internalized by V8 when you tr
Devlin
2017/02/21 18:05:40
Done.
| |
592 gin::StringToV8(isolate, "alphaVal")) | |
593 .ToChecked(); | |
594 } else if (type_name == "BetaRef") { | |
595 EXPECT_EQ("beta", property_name); | |
596 result | |
597 ->Set(context, gin::StringToV8(isolate, "betaProp"), | |
598 gin::StringToV8(isolate, "betaVal")) | |
599 .ToChecked(); | |
600 } | |
601 return result; | |
602 }; | |
603 | |
604 SetCreateCustomType(base::Bind(create_custom_type)); | |
605 | |
606 InitializeBinding(); | |
607 | |
608 v8::HandleScope handle_scope(isolate()); | |
609 v8::Local<v8::Context> context = ContextLocal(); | |
610 v8::Local<v8::Object> binding_object = | |
611 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); | |
612 EXPECT_EQ(R"({"alphaProp":"alphaVal"})", | |
613 GetStringPropertyFromObject(binding_object, context, "alpha")); | |
614 EXPECT_EQ( | |
615 R"({"betaProp":"betaVal"})", | |
616 GetStringPropertyFromObject(binding_object, context, "beta")); | |
617 } | |
618 | |
567 TEST_F(APIBindingUnittest, TestDisposedContext) { | 619 TEST_F(APIBindingUnittest, TestDisposedContext) { |
568 SetFunctions(kFunctions); | 620 SetFunctions(kFunctions); |
569 InitializeBinding(); | 621 InitializeBinding(); |
570 | 622 |
571 v8::HandleScope handle_scope(isolate()); | 623 v8::HandleScope handle_scope(isolate()); |
572 v8::Local<v8::Context> context = ContextLocal(); | 624 v8::Local<v8::Context> context = ContextLocal(); |
573 | 625 |
574 v8::Local<v8::Object> binding_object = | 626 v8::Local<v8::Object> binding_object = |
575 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); | 627 binding()->CreateInstance(context, isolate(), base::Bind(&AllowAllAPIs)); |
576 | 628 |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1045 | 1097 |
1046 blink::WebScopedUserGesture user_gesture(nullptr); | 1098 blink::WebScopedUserGesture user_gesture(nullptr); |
1047 RunFunction(function, context, arraysize(argv), argv); | 1099 RunFunction(function, context, arraysize(argv), argv); |
1048 ASSERT_TRUE(last_request()); | 1100 ASSERT_TRUE(last_request()); |
1049 EXPECT_TRUE(last_request()->has_user_gesture); | 1101 EXPECT_TRUE(last_request()->has_user_gesture); |
1050 | 1102 |
1051 reset_last_request(); | 1103 reset_last_request(); |
1052 } | 1104 } |
1053 | 1105 |
1054 } // namespace extensions | 1106 } // namespace extensions |
OLD | NEW |