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

Side by Side Diff: webkit/glue/cpp_variant_unittest.cc

Issue 6254018: Allow chrome.send to pass number, boolean, null and arrays of those (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use malloc+strcpy instead of strdup Created 9 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « webkit/glue/cpp_variant.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <vector>
6
5 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
6 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
8 #include "webkit/glue/cpp_variant.h" 10 #include "webkit/glue/cpp_variant.h"
9 11
10 using WebKit::WebBindings; 12 using WebKit::WebBindings;
11 13
12 // Creates a std::string from an NPVariant of string type. If the NPVariant 14 // Creates a std::string from an NPVariant of string type. If the NPVariant
13 // is not a string, empties the std::string. 15 // is not a string, empties the std::string.
14 void MakeStdString(const NPVariant& np, std::string* std_string) { 16 void MakeStdString(const NPVariant& np, std::string* std_string) {
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 EXPECT_FALSE(cpp.isDouble()); 419 EXPECT_FALSE(cpp.isDouble());
418 EXPECT_FALSE(cpp.isNumber()); 420 EXPECT_FALSE(cpp.isNumber());
419 EXPECT_FALSE(cpp.isString()); 421 EXPECT_FALSE(cpp.isString());
420 EXPECT_FALSE(cpp.isVoid()); 422 EXPECT_FALSE(cpp.isVoid());
421 EXPECT_FALSE(cpp.isNull()); 423 EXPECT_FALSE(cpp.isNull());
422 EXPECT_FALSE(cpp.isEmpty()); 424 EXPECT_FALSE(cpp.isEmpty());
423 EXPECT_TRUE(cpp.isObject()); 425 EXPECT_TRUE(cpp.isObject());
424 WebBindings::releaseObject(obj); 426 WebBindings::releaseObject(obj);
425 CheckObject(cpp); 427 CheckObject(cpp);
426 } 428 }
429
430 bool MockNPHasPropertyFunction(NPObject *npobj, NPIdentifier name) {
431 return true;
432 }
433
434 bool MockNPGetPropertyFunction(NPObject *npobj, NPIdentifier name,
435 NPVariant *result) {
436 if (WebBindings::getStringIdentifier("length") == name) {
437 DOUBLE_TO_NPVARIANT(4, *result);
438 } else if (WebBindings::getIntIdentifier(0) == name) {
439 DOUBLE_TO_NPVARIANT(0, *result);
440 } else if (WebBindings::getIntIdentifier(1) == name) {
441 BOOLEAN_TO_NPVARIANT(true, *result);
442 } else if (WebBindings::getIntIdentifier(2) == name) {
443 NULL_TO_NPVARIANT(*result);
444 } else if (WebBindings::getIntIdentifier(3) == name) {
445 const char* s = "string";
446 size_t length = strlen(s);
447 char* mem = static_cast<char*>(malloc((length + 1) * sizeof(char)));
Evan Martin 2011/01/27 22:12:58 Unless you're copying code from somewhere else, yo
arv (Not doing code reviews) 2011/01/27 23:35:10 Done.
448 strcpy(mem, s);
449 mem[length] = '\0';
450 STRINGZ_TO_NPVARIANT(mem, *result);
451 }
452
453 return true;
454 }
455
456 TEST(CppVariantTest, ToVector) {
457 NPClass array_like_class = {
458 NP_CLASS_STRUCT_VERSION,
459 MockNPAllocate, // NPAllocateFunctionPtr allocate;
460 MockNPDeallocate, // NPDeallocateFunctionPtr deallocate;
Evan Martin 2011/01/27 22:12:58 Do you still need these?
arv (Not doing code reviews) 2011/01/27 23:35:10 Nope. Removing
461 0, // NPInvalidateFunctionPtr invalidate;
462 0, // NPHasMethodFunctionPtr hasMethod;
463 0, // NPInvokeFunctionPtr invoke;
464 0, // NPInvokeDefaultFunctionPtr invokeDefault;
465 MockNPHasPropertyFunction, // NPHasPropertyFunctionPtr hasProperty;
466 MockNPGetPropertyFunction, // NPGetPropertyFunctionPtr getProperty;
467 0, // NPSetPropertyFunctionPtr setProperty;
468 0, // NPRemovePropertyFunctionPtr removeProperty;
469 0, // NPEnumerationFunctionPtr enumerate;
470 0 // NPConstructFunctionPtr construct;
471 };
472
473 NPObject* obj = WebBindings::createObject(NULL, &array_like_class);
474
475 CppVariant cpp;
476 cpp.Set(obj);
477
478 std::vector<CppVariant> cpp_vector = cpp.ToVector();
479 EXPECT_EQ(4u, cpp_vector.size());
480
481 EXPECT_TRUE(cpp_vector[0].isDouble());
482 EXPECT_EQ(0, cpp_vector[0].ToDouble());
483
484 EXPECT_TRUE(cpp_vector[1].isBool());
485 EXPECT_EQ(true, cpp_vector[1].ToBoolean());
486
487 EXPECT_TRUE(cpp_vector[2].isNull());
488
489 EXPECT_TRUE(cpp_vector[3].isString());
490 CheckString("string", cpp_vector[3]);
491
492 WebBindings::releaseObject(obj);
493 }
OLDNEW
« no previous file with comments | « webkit/glue/cpp_variant.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698