| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "extensions/renderer/bindings/api_signature.h" | 5 #include "extensions/renderer/bindings/api_signature.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "extensions/renderer/bindings/api_binding_test.h" | 9 #include "extensions/renderer/bindings/api_binding_test.h" |
| 10 #include "extensions/renderer/bindings/api_binding_test_util.h" | 10 #include "extensions/renderer/bindings/api_binding_test_util.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 return base::MakeUnique<APISignature>(std::move(specs)); | 116 return base::MakeUnique<APISignature>(std::move(specs)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 std::unique_ptr<APISignature> RefEnum() { | 119 std::unique_ptr<APISignature> RefEnum() { |
| 120 SpecVector specs; | 120 SpecVector specs; |
| 121 specs.push_back( | 121 specs.push_back( |
| 122 ArgumentSpecBuilder(ArgumentType::REF, "enum").SetRef("refEnum").Build()); | 122 ArgumentSpecBuilder(ArgumentType::REF, "enum").SetRef("refEnum").Build()); |
| 123 return base::MakeUnique<APISignature>(std::move(specs)); | 123 return base::MakeUnique<APISignature>(std::move(specs)); |
| 124 } | 124 } |
| 125 | 125 |
| 126 std::unique_ptr<APISignature> OptionalObjectAndCallback() { |
| 127 SpecVector specs; |
| 128 specs.push_back( |
| 129 ArgumentSpecBuilder(ArgumentType::OBJECT, "obj") |
| 130 .AddProperty( |
| 131 "prop1", |
| 132 ArgumentSpecBuilder(ArgumentType::INTEGER).MakeOptional().Build()) |
| 133 .MakeOptional() |
| 134 .Build()); |
| 135 specs.push_back(ArgumentSpecBuilder(ArgumentType::FUNCTION, "callback") |
| 136 .MakeOptional() |
| 137 .Build()); |
| 138 return base::MakeUnique<APISignature>(std::move(specs)); |
| 139 } |
| 140 |
| 126 } // namespace | 141 } // namespace |
| 127 | 142 |
| 128 class APISignatureTest : public APIBindingTest { | 143 class APISignatureTest : public APIBindingTest { |
| 129 public: | 144 public: |
| 130 APISignatureTest() | 145 APISignatureTest() |
| 131 : type_refs_(APITypeReferenceMap::InitializeTypeCallback()) {} | 146 : type_refs_(APITypeReferenceMap::InitializeTypeCallback()) {} |
| 132 ~APISignatureTest() override = default; | 147 ~APISignatureTest() override = default; |
| 133 | 148 |
| 134 void SetUp() override { | 149 void SetUp() override { |
| 135 APIBindingTest::SetUp(); | 150 APIBindingTest::SetUp(); |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 ExpectPass(*signature, "[4, {foo: 'bar'}, function() {}]", | 310 ExpectPass(*signature, "[4, {foo: 'bar'}, function() {}]", |
| 296 "[4,{'foo':'bar'},null]", true); | 311 "[4,{'foo':'bar'},null]", true); |
| 297 ExpectPass(*signature, "[4, {foo: 'bar'}]", "[4,{'foo':'bar'},null]", | 312 ExpectPass(*signature, "[4, {foo: 'bar'}]", "[4,{'foo':'bar'},null]", |
| 298 false); | 313 false); |
| 299 ExpectPass(*signature, "[4, {foo: 'bar'}, {}]", "[4,{'foo':'bar'},{}]", | 314 ExpectPass(*signature, "[4, {foo: 'bar'}, {}]", "[4,{'foo':'bar'},{}]", |
| 300 false); | 315 false); |
| 301 ExpectFailure(*signature, "[4, function() {}]", | 316 ExpectFailure(*signature, "[4, function() {}]", |
| 302 ArgumentError("any", UnserializableValue())); | 317 ArgumentError("any", UnserializableValue())); |
| 303 ExpectFailure(*signature, "[4]", MissingRequiredArgument("any")); | 318 ExpectFailure(*signature, "[4]", MissingRequiredArgument("any")); |
| 304 } | 319 } |
| 320 |
| 321 { |
| 322 auto signature = OptionalObjectAndCallback(); |
| 323 ExpectPass(*signature, "[{prop1: 1}]", "[{'prop1':1}]", false); |
| 324 ExpectPass(*signature, "[]", "[null]", false); |
| 325 ExpectPass(*signature, "[null]", "[null]", false); |
| 326 ExpectFailure( |
| 327 *signature, "[{prop1: 'str'}]", |
| 328 ArgumentError("obj", PropertyError("prop1", InvalidType(kTypeInteger, |
| 329 kTypeString)))); |
| 330 ExpectFailure( |
| 331 *signature, "[{prop1: 'str'}, function() {}]", |
| 332 ArgumentError("obj", PropertyError("prop1", InvalidType(kTypeInteger, |
| 333 kTypeString)))); |
| 334 } |
| 305 } | 335 } |
| 306 | 336 |
| 307 TEST_F(APISignatureTest, TypeRefsTest) { | 337 TEST_F(APISignatureTest, TypeRefsTest) { |
| 308 using namespace api_errors; | 338 using namespace api_errors; |
| 309 | 339 |
| 310 v8::HandleScope handle_scope(isolate()); | 340 v8::HandleScope handle_scope(isolate()); |
| 311 | 341 |
| 312 { | 342 { |
| 313 auto signature = RefObj(); | 343 auto signature = RefObj(); |
| 314 ExpectPass(*signature, "[{prop1: 'foo'}]", "[{'prop1':'foo'}]", false); | 344 ExpectPass(*signature, "[{prop1: 'foo'}]", "[{'prop1':'foo'}]", false); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 std::unique_ptr<base::ListValue> parsed; | 432 std::unique_ptr<base::ListValue> parsed; |
| 403 EXPECT_TRUE(signature->ConvertArgumentsIgnoringSchema(context, v8_args, | 433 EXPECT_TRUE(signature->ConvertArgumentsIgnoringSchema(context, v8_args, |
| 404 &parsed, &callback)); | 434 &parsed, &callback)); |
| 405 ASSERT_TRUE(parsed); | 435 ASSERT_TRUE(parsed); |
| 406 EXPECT_EQ(R"([{"not":"a string"}])", ValueToString(*parsed)); | 436 EXPECT_EQ(R"([{"not":"a string"}])", ValueToString(*parsed)); |
| 407 EXPECT_TRUE(callback.IsEmpty()); | 437 EXPECT_TRUE(callback.IsEmpty()); |
| 408 } | 438 } |
| 409 } | 439 } |
| 410 | 440 |
| 411 } // namespace extensions | 441 } // namespace extensions |
| OLD | NEW |