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

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

Issue 2615773002: [Extensions Bindings] Add enum support (Closed)
Patch Set: nits Created 3 years, 11 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
« no previous file with comments | « extensions/renderer/api_binding.cc ('k') | extensions/renderer/argument_spec.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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::RunJSFunctionSync()), &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
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 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 v8::Local<v8::Value> result = 918 v8::Local<v8::Value> result =
879 RunFunction(function, context, arraysize(args), args); 919 RunFunction(function, context, arraysize(args), args);
880 ASSERT_FALSE(result.IsEmpty()); 920 ASSERT_FALSE(result.IsEmpty());
881 std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context); 921 std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context);
882 ASSERT_TRUE(json_result); 922 ASSERT_TRUE(json_result);
883 EXPECT_EQ("\"ping pong\"", ValueToString(*json_result)); 923 EXPECT_EQ("\"ping pong\"", ValueToString(*json_result));
884 } 924 }
885 } 925 }
886 926
887 } // namespace extensions 927 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_binding.cc ('k') | extensions/renderer/argument_spec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698