Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: extensions/renderer/api_signature_unittest.cc

Issue 2915813003: [Extensions Bindings] Accept null callbacks while ignoring schema (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/api_signature.h" 5 #include "extensions/renderer/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/api_binding_test.h" 9 #include "extensions/renderer/api_binding_test.h"
10 #include "extensions/renderer/api_binding_test_util.h" 10 #include "extensions/renderer/api_binding_test_util.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 } 59 }
60 60
61 std::unique_ptr<APISignature> IntAndCallback() { 61 std::unique_ptr<APISignature> IntAndCallback() {
62 SpecVector specs; 62 SpecVector specs;
63 specs.push_back(ArgumentSpecBuilder(ArgumentType::INTEGER, "int").Build()); 63 specs.push_back(ArgumentSpecBuilder(ArgumentType::INTEGER, "int").Build());
64 specs.push_back( 64 specs.push_back(
65 ArgumentSpecBuilder(ArgumentType::FUNCTION, "callback").Build()); 65 ArgumentSpecBuilder(ArgumentType::FUNCTION, "callback").Build());
66 return base::MakeUnique<APISignature>(std::move(specs)); 66 return base::MakeUnique<APISignature>(std::move(specs));
67 } 67 }
68 68
69 std::unique_ptr<APISignature> IntAndOptionalCallback() {
70 SpecVector specs;
71 specs.push_back(ArgumentSpecBuilder(ArgumentType::INTEGER, "int").Build());
72 specs.push_back(ArgumentSpecBuilder(ArgumentType::FUNCTION, "callback")
73 .MakeOptional()
74 .Build());
75 return base::MakeUnique<APISignature>(std::move(specs));
76 }
77
69 std::unique_ptr<APISignature> OptionalIntAndCallback() { 78 std::unique_ptr<APISignature> OptionalIntAndCallback() {
70 SpecVector specs; 79 SpecVector specs;
71 specs.push_back( 80 specs.push_back(
72 ArgumentSpecBuilder(ArgumentType::INTEGER, "int").MakeOptional().Build()); 81 ArgumentSpecBuilder(ArgumentType::INTEGER, "int").MakeOptional().Build());
73 specs.push_back( 82 specs.push_back(
74 ArgumentSpecBuilder(ArgumentType::FUNCTION, "callback").Build()); 83 ArgumentSpecBuilder(ArgumentType::FUNCTION, "callback").Build());
75 return base::MakeUnique<APISignature>(std::move(specs)); 84 return base::MakeUnique<APISignature>(std::move(specs));
76 } 85 }
77 86
78 std::unique_ptr<APISignature> OptionalCallback() { 87 std::unique_ptr<APISignature> OptionalCallback() {
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 OptionalIntAndCallback()->GetExpectedSignature()); 343 OptionalIntAndCallback()->GetExpectedSignature());
335 EXPECT_EQ("optional function callback", 344 EXPECT_EQ("optional function callback",
336 OptionalCallback()->GetExpectedSignature()); 345 OptionalCallback()->GetExpectedSignature());
337 EXPECT_EQ( 346 EXPECT_EQ(
338 "integer int, any any, optional object obj, optional function callback", 347 "integer int, any any, optional object obj, optional function callback",
339 IntAnyOptionalObjectOptionalCallback()->GetExpectedSignature()); 348 IntAnyOptionalObjectOptionalCallback()->GetExpectedSignature());
340 EXPECT_EQ("refObj obj", RefObj()->GetExpectedSignature()); 349 EXPECT_EQ("refObj obj", RefObj()->GetExpectedSignature());
341 EXPECT_EQ("refEnum enum", RefEnum()->GetExpectedSignature()); 350 EXPECT_EQ("refEnum enum", RefEnum()->GetExpectedSignature());
342 } 351 }
343 352
353 TEST_F(APISignatureTest, ParseIgnoringSchema) {
354 v8::HandleScope handle_scope(isolate());
355 v8::Local<v8::Context> context = MainContext();
356
357 auto string_to_v8_vector = [context](base::StringPiece args) {
358 v8::Local<v8::Value> v8_args = V8ValueFromScriptSource(context, args);
359 EXPECT_FALSE(v8_args.IsEmpty());
360 EXPECT_TRUE(v8_args->IsArray());
361 std::vector<v8::Local<v8::Value>> vector_args;
362 EXPECT_TRUE(
363 gin::ConvertFromV8(context->GetIsolate(), v8_args, &vector_args));
364 return vector_args;
365 };
366
367 {
368 // Test with providing an optional callback.
369 auto signature = IntAndOptionalCallback();
370 std::vector<v8::Local<v8::Value>> v8_args =
371 string_to_v8_vector("[1, function() {}]");
372 v8::Local<v8::Function> callback;
373 std::unique_ptr<base::ListValue> parsed;
374 EXPECT_TRUE(signature->ConvertArgumentsIgnoringSchema(context, v8_args,
375 &parsed, &callback));
376 ASSERT_TRUE(parsed);
377 EXPECT_EQ("[1]", ValueToString(*parsed));
378 EXPECT_FALSE(callback.IsEmpty());
379 }
380
381 {
382 // Test with omitting the optional callback.
383 auto signature = IntAndOptionalCallback();
384 std::vector<v8::Local<v8::Value>> v8_args =
385 string_to_v8_vector("[1, null]");
386 v8::Local<v8::Function> callback;
387 std::unique_ptr<base::ListValue> parsed;
388 EXPECT_TRUE(signature->ConvertArgumentsIgnoringSchema(context, v8_args,
389 &parsed, &callback));
390 ASSERT_TRUE(parsed);
391 EXPECT_EQ("[1]", ValueToString(*parsed));
392 EXPECT_TRUE(callback.IsEmpty());
393 }
394
395 {
396 // Test with providing something completely different than the spec, which
397 // is (unfortunately) allowed and used.
398 auto signature = OneString();
399 std::vector<v8::Local<v8::Value>> v8_args =
400 string_to_v8_vector("[{not: 'a string'}]");
401 v8::Local<v8::Function> callback;
402 std::unique_ptr<base::ListValue> parsed;
403 EXPECT_TRUE(signature->ConvertArgumentsIgnoringSchema(context, v8_args,
404 &parsed, &callback));
405 ASSERT_TRUE(parsed);
406 EXPECT_EQ("[{\"not\":\"a string\"}]", ValueToString(*parsed));
jbroman 2017/06/05 18:16:32 or R"([{"not":"a string"}])", if you prefer
Devlin 2017/06/09 20:26:31 Sure, done.
407 EXPECT_TRUE(callback.IsEmpty());
408 }
409 }
410
344 } // namespace extensions 411 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698