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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 191 } |
192 | 192 |
193 arguments_.reset(); | 193 arguments_.reset(); |
194 } | 194 } |
195 | 195 |
196 TEST_F(APIBindingTest, Test) { | 196 TEST_F(APIBindingTest, Test) { |
197 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); | 197 std::unique_ptr<base::ListValue> functions = ListValueFromString(kFunctions); |
198 ASSERT_TRUE(functions); | 198 ASSERT_TRUE(functions); |
199 ArgumentSpec::RefMap refs; | 199 ArgumentSpec::RefMap refs; |
200 APIBinding binding( | 200 APIBinding binding( |
201 "test", *functions, base::ListValue(), | 201 "test", *functions, nullptr, |
202 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), | 202 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), |
203 &refs); | 203 &refs); |
204 EXPECT_TRUE(refs.empty()); | 204 EXPECT_TRUE(refs.empty()); |
205 | 205 |
206 v8::Isolate* isolate = instance_->isolate(); | 206 v8::Isolate* isolate = instance_->isolate(); |
207 | 207 |
208 v8::HandleScope handle_scope(isolate); | 208 v8::HandleScope handle_scope(isolate); |
209 v8::Local<v8::Context> context = | 209 v8::Local<v8::Context> context = |
210 v8::Local<v8::Context>::New(isolate, context_); | 210 v8::Local<v8::Context>::New(isolate, context_); |
211 | 211 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 " }]" | 293 " }]" |
294 "}]"; | 294 "}]"; |
295 | 295 |
296 std::unique_ptr<base::ListValue> functions = | 296 std::unique_ptr<base::ListValue> functions = |
297 ListValueFromString(kRefFunctions); | 297 ListValueFromString(kRefFunctions); |
298 ASSERT_TRUE(functions); | 298 ASSERT_TRUE(functions); |
299 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); | 299 std::unique_ptr<base::ListValue> types = ListValueFromString(kTypes); |
300 ASSERT_TRUE(types); | 300 ASSERT_TRUE(types); |
301 ArgumentSpec::RefMap refs; | 301 ArgumentSpec::RefMap refs; |
302 APIBinding binding( | 302 APIBinding binding( |
303 "test", *functions, *types, | 303 "test", *functions, types.get(), |
304 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), | 304 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), |
305 &refs); | 305 &refs); |
306 EXPECT_EQ(2u, refs.size()); | 306 EXPECT_EQ(2u, refs.size()); |
307 EXPECT_TRUE(base::ContainsKey(refs, "refObj")); | 307 EXPECT_TRUE(base::ContainsKey(refs, "refObj")); |
308 EXPECT_TRUE(base::ContainsKey(refs, "refEnum")); | 308 EXPECT_TRUE(base::ContainsKey(refs, "refEnum")); |
309 | 309 |
310 v8::Isolate* isolate = instance_->isolate(); | 310 v8::Isolate* isolate = instance_->isolate(); |
311 | 311 |
312 v8::HandleScope handle_scope(isolate); | 312 v8::HandleScope handle_scope(isolate); |
313 v8::Local<v8::Context> context = | 313 v8::Local<v8::Context> context = |
314 v8::Local<v8::Context>::New(isolate, context_); | 314 v8::Local<v8::Context>::New(isolate, context_); |
315 | 315 |
316 v8::Local<v8::Object> binding_object = | 316 v8::Local<v8::Object> binding_object = |
317 binding.CreateInstance(context, isolate); | 317 binding.CreateInstance(context, isolate); |
318 | 318 |
319 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})", | 319 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})", |
320 "[{'prop1':'foo'}]"); | 320 "[{'prop1':'foo'}]"); |
321 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", | 321 ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 2})", |
322 "[{'prop1':'foo','prop2':2}]"); | 322 "[{'prop1':'foo','prop2':2}]"); |
323 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", | 323 ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})", |
324 kError); | 324 kError); |
325 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); | 325 ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']"); |
326 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); | 326 ExpectPass(binding_object, "obj.takesRefEnum('beta')", "['beta']"); |
327 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); | 327 ExpectFailure(binding_object, "obj.takesRefEnum('gamma')", kError); |
328 } | 328 } |
329 | 329 |
330 // TODO(devlin): Once we have an object that encompasses all these pieces (the | |
331 // APIBinding, ArgumentSpec::RefMap, and APIRequestHandler), we should move this | |
332 // test. | |
333 TEST_F(APIBindingTest, Callbacks) { | |
334 const char kTestCall[] = | |
335 "obj.functionWithCallback('foo', function() {\n" | |
336 " this.callbackArguments = Array.from(arguments);\n" | |
337 "});"; | |
338 | |
339 const char kFunctionSpec[] = | |
340 "[{" | |
341 " 'name': 'functionWithCallback'," | |
342 " 'parameters': [{" | |
343 " 'name': 'str'," | |
344 " 'type': 'string'" | |
345 " }, {" | |
346 " 'name': 'callback'," | |
347 " 'type': 'function'" | |
348 " }]" | |
349 "}]"; | |
350 | |
351 std::unique_ptr<base::ListValue> functions = | |
352 ListValueFromString(kFunctionSpec); | |
353 ASSERT_TRUE(functions); | |
354 ArgumentSpec::RefMap refs; | |
355 APIBinding binding( | |
356 "test", *functions, base::ListValue(), | |
357 base::Bind(&APIBindingTest::OnFunctionCall, base::Unretained(this)), | |
358 &refs); | |
359 | |
360 v8::Isolate* isolate = instance_->isolate(); | |
361 | |
362 v8::HandleScope handle_scope(isolate); | |
363 v8::Local<v8::Context> context = | |
364 v8::Local<v8::Context>::New(isolate, context_); | |
365 | |
366 v8::Local<v8::Object> binding_object = | |
367 binding.CreateInstance(context, isolate); | |
368 | |
369 ExpectPass(binding_object, kTestCall, "['foo']"); | |
370 ASSERT_FALSE(last_request_id().empty()); | |
371 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; | |
372 std::unique_ptr<base::ListValue> expected_args = | |
373 ListValueFromString(kResponseArgsJson); | |
374 request_handler()->CompleteRequest(last_request_id(), *expected_args); | |
375 | |
376 v8::Local<v8::Value> res = | |
377 GetPropertyFromObject(context->Global(), context, "callbackArguments"); | |
378 | |
379 std::unique_ptr<base::Value> out_val = V8ToBaseValue(res, context); | |
380 ASSERT_TRUE(out_val); | |
381 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*out_val)); | |
382 } | |
383 | |
384 } // namespace extensions | 330 } // namespace extensions |
OLD | NEW |