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 "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 "[{'prop1':'foo'}]"); | 333 "[{'prop1':'foo'}]"); |
334 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", | 334 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", |
335 "[{'prop1':'foo','prop2':2}]"); | 335 "[{'prop1':'foo','prop2':2}]"); |
336 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", | 336 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", |
337 kError); | 337 kError); |
338 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); | 338 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); |
339 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); | 339 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); |
340 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); | 340 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); |
341 } | 341 } |
342 | 342 |
343 // TODO(devlin): Once we have an object that encompasses all these pieces (the | |
344 // APIBinding, ArgumentSpec::RefMap, and APIRequestHandler), we should move this | |
345 // test. | |
346 TEST_F(APIBindingTest, Callbacks) { | |
347 const char kTestCall[] = | |
348 "obj.functionWithCallback('foo', function() {\n" | |
349 " this.callbackArguments = Array.from(arguments);\n" | |
350 "});"; | |
351 | |
352 const char kFunctionSpec[] = | |
353 "[{" | |
354 " 'name': 'functionWithCallback'," | |
355 " 'parameters': [{" | |
356 " 'name': 'str'," | |
357 " 'type': 'string'" | |
358 " }, {" | |
359 " 'name': 'callback'," | |
360 " 'type': 'function'" | |
361 " }]" | |
362 "}]"; | |
363 | |
364 std::unique_ptr<base::ListValue> functions = | |
365 ListValueFromString(kFunctionSpec); | |
366 ASSERT_TRUE(functions); | |
367 ArgumentSpec::RefMap refs; | |
368 APIBinding binding( | |
369 "test", *functions, base::ListValue(), | |
370 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), | |
371 &refs); | |
372 | |
373 v8::Isolate* isolate = instance_->isolate(); | |
374 | |
375 v8::HandleScope handle_scope(isolate); | |
376 v8::Local<v8::Context> context = | |
377 v8::Local<v8::Context>::New(isolate, context_); | |
378 | |
379 v8::Local<v8::Object> binding_object = | |
380 binding.CreateInstance(context, isolate); | |
381 | |
382 ExpectPass(binding_object, kTestCall, "['foo']"); | |
383 ASSERT_FALSE(last_request_id().empty()); | |
384 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; | |
385 std::unique_ptr<base::ListValue> expected_args = | |
386 ListValueFromString(kResponseArgsJson); | |
387 request_handler()->CompleteRequest(last_request_id(), *expected_args); | |
388 | |
389 v8::Local<v8::Value> res; | |
390 ASSERT_TRUE(context->Global() | |
391 ->Get(context, gin::StringToV8(isolate, "callbackArguments")) | |
392 .ToLocal(&res)); | |
393 | |
394 std::unique_ptr<base::Value> out_val = V8ToBaseValue(res, context); | |
395 ASSERT_TRUE(out_val); | |
396 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*out_val)); | |
397 } | |
398 | |
399 } // namespace extensions | 343 } // namespace extensions |
OLD | NEW |