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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 ExpectPass(binding_object, "obj.optionalIntAndCallback(function() {})", | 272 ExpectPass(binding_object, "obj.optionalIntAndCallback(function() {})", |
273 "[null]"); | 273 "[null]"); |
274 ExpectFailure(binding_object, "obj.optionalIntAndCallback(1)", kError); | 274 ExpectFailure(binding_object, "obj.optionalIntAndCallback(1)", kError); |
275 | 275 |
276 ExpectPass(binding_object, "obj.optionalCallback(function() {})", "[]"); | 276 ExpectPass(binding_object, "obj.optionalCallback(function() {})", "[]"); |
277 ExpectPass(binding_object, "obj.optionalCallback()", "[]"); | 277 ExpectPass(binding_object, "obj.optionalCallback()", "[]"); |
278 ExpectPass(binding_object, "obj.optionalCallback(undefined)", "[]"); | 278 ExpectPass(binding_object, "obj.optionalCallback(undefined)", "[]"); |
279 ExpectFailure(binding_object, "obj.optionalCallback(0)", kError); | 279 ExpectFailure(binding_object, "obj.optionalCallback(0)", kError); |
280 } | 280 } |
281 | 281 |
| 282 // Test that enum values are properly exposed on the binding object. |
| 283 TEST_F(APIBindingUnittest, EnumValues) { |
| 284 const char kTypes[] = |
| 285 "[{" |
| 286 " 'id': 'first'," |
| 287 " 'type': 'string'," |
| 288 " 'enum': ['alpha', 'camelCase', 'Hyphen-ated'," |
| 289 " 'SCREAMING', 'nums123', '42nums']" |
| 290 "}, {" |
| 291 " 'id': 'last'," |
| 292 " 'type': 'string'," |
| 293 " 'enum': [{'name': 'omega'}]" |
| 294 "}]"; |
| 295 |
| 296 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); |
| 297 ASSERT_TRUE(types); |
| 298 ArgumentSpec::RefMap refs; |
| 299 APIBinding binding( |
| 300 "test", nullptr, types.get(), nullptr, |
| 301 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 302 base::MakeUnique<APIBindingHooks>(binding::RunJSFunction()), &refs); |
| 303 |
| 304 v8::HandleScope handle_scope(isolate()); |
| 305 v8::Local<v8::Context> context = ContextLocal(); |
| 306 |
| 307 APIEventHandler event_handler( |
| 308 base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 309 v8::Local<v8::Object> binding_object = binding.CreateInstance( |
| 310 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
| 311 |
| 312 const char kExpected[] = |
| 313 "{'ALPHA':'alpha','CAMEL_CASE':'camelCase','HYPHEN_ATED':'Hyphen-ated'," |
| 314 "'NUMS123':'nums123','SCREAMING':'SCREAMING','_42NUMS':'42nums'}"; |
| 315 EXPECT_EQ(ReplaceSingleQuotes(kExpected), |
| 316 GetStringPropertyFromObject(binding_object, context, "first")); |
| 317 EXPECT_EQ(ReplaceSingleQuotes("{'OMEGA':'omega'}"), |
| 318 GetStringPropertyFromObject(binding_object, context, "last")); |
| 319 } |
| 320 |
282 TEST_F(APIBindingUnittest, TypeRefsTest) { | 321 TEST_F(APIBindingUnittest, TypeRefsTest) { |
283 const char kTypes[] = | 322 const char kTypes[] = |
284 "[{" | 323 "[{" |
285 " 'id': 'refObj'," | 324 " 'id': 'refObj'," |
286 " 'type': 'object'," | 325 " 'type': 'object'," |
287 " 'properties': {" | 326 " 'properties': {" |
288 " 'prop1': {'type': 'string'}," | 327 " 'prop1': {'type': 'string'}," |
289 " 'prop2': {'type': 'integer', 'optional': true}" | 328 " 'prop2': {'type': 'integer', 'optional': true}" |
290 " }" | 329 " }" |
291 "}, {" | 330 "}, {" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); | 370 context, isolate(), &event_handler, base::Bind(&AllowAllAPIs)); |
332 | 371 |
333 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})", | 372 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})", |
334 "[{'prop1':'foo'}]"); | 373 "[{'prop1':'foo'}]"); |
335 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", | 374 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", |
336 "[{'prop1':'foo','prop2':2}]"); | 375 "[{'prop1':'foo','prop2':2}]"); |
337 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", | 376 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", |
338 kError); | 377 kError); |
339 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); | 378 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); |
340 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); | 379 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); |
| 380 ExpectPass(binding_object, "obj.takesRefEnum(obj.refEnum.BETA)", "['beta']"); |
341 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); | 381 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); |
342 } | 382 } |
343 | 383 |
344 TEST_F(APIBindingUnittest, RestrictedAPIs) { | 384 TEST_F(APIBindingUnittest, RestrictedAPIs) { |
345 const char kRestrictedFunctions[] = | 385 const char kRestrictedFunctions[] = |
346 "[{" | 386 "[{" |
347 " 'name': 'allowedOne'," | 387 " 'name': 'allowedOne'," |
348 " 'parameters': []" | 388 " 'parameters': []" |
349 "}, {" | 389 "}, {" |
350 " 'name': 'allowedTwo'," | 390 " 'name': 'allowedTwo'," |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 GetBaseValuePropertyFromObject(context->Global(), context, | 606 GetBaseValuePropertyFromObject(context->Global(), context, |
567 "requestArguments"); | 607 "requestArguments"); |
568 ASSERT_TRUE(response_args); | 608 ASSERT_TRUE(response_args); |
569 EXPECT_EQ("[\"foo\"]", ValueToString(*response_args)); | 609 EXPECT_EQ("[\"foo\"]", ValueToString(*response_args)); |
570 | 610 |
571 // Other methods, like stringAndInt(), should behave normally. | 611 // Other methods, like stringAndInt(), should behave normally. |
572 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); | 612 ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]"); |
573 } | 613 } |
574 | 614 |
575 } // namespace extensions | 615 } // namespace extensions |
OLD | NEW |