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

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 base::strdup instead of strdup Created 9 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 | Annotate | Revision Log
« webkit/glue/cpp_variant.cc ('K') | « 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"
8 #include "base/string_util.h"
6 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
8 #include "webkit/glue/cpp_variant.h" 11 #include "webkit/glue/cpp_variant.h"
9 12
10 using WebKit::WebBindings; 13 using WebKit::WebBindings;
11 14
12 // Creates a std::string from an NPVariant of string type. If the NPVariant 15 // Creates a std::string from an NPVariant of string type. If the NPVariant
13 // is not a string, empties the std::string. 16 // is not a string, empties the std::string.
14 void MakeStdString(const NPVariant& np, std::string* std_string) { 17 void MakeStdString(const NPVariant& np, std::string* std_string) {
15 if (np.type == NPVariantType_String) { 18 if (np.type == NPVariantType_String) {
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 EXPECT_FALSE(cpp.isDouble()); 420 EXPECT_FALSE(cpp.isDouble());
418 EXPECT_FALSE(cpp.isNumber()); 421 EXPECT_FALSE(cpp.isNumber());
419 EXPECT_FALSE(cpp.isString()); 422 EXPECT_FALSE(cpp.isString());
420 EXPECT_FALSE(cpp.isVoid()); 423 EXPECT_FALSE(cpp.isVoid());
421 EXPECT_FALSE(cpp.isNull()); 424 EXPECT_FALSE(cpp.isNull());
422 EXPECT_FALSE(cpp.isEmpty()); 425 EXPECT_FALSE(cpp.isEmpty());
423 EXPECT_TRUE(cpp.isObject()); 426 EXPECT_TRUE(cpp.isObject());
424 WebBindings::releaseObject(obj); 427 WebBindings::releaseObject(obj);
425 CheckObject(cpp); 428 CheckObject(cpp);
426 } 429 }
430
431 bool MockNPHasPropertyFunction(NPObject *npobj, NPIdentifier name) {
432 return true;
433 }
434
435 bool MockNPGetPropertyFunction(NPObject *npobj, NPIdentifier name,
436 NPVariant *result) {
437 if (WebBindings::getStringIdentifier("length") == name) {
438 DOUBLE_TO_NPVARIANT(4, *result);
439 } else if (WebBindings::getIntIdentifier(0) == name) {
440 DOUBLE_TO_NPVARIANT(0, *result);
441 } else if (WebBindings::getIntIdentifier(1) == name) {
442 BOOLEAN_TO_NPVARIANT(true, *result);
443 } else if (WebBindings::getIntIdentifier(2) == name) {
444 NULL_TO_NPVARIANT(*result);
445 } else if (WebBindings::getIntIdentifier(3) == name) {
446 STRINGZ_TO_NPVARIANT(base::strdup("string"), *result);
447 }
448
449 return true;
450 }
451
452 TEST(CppVariantTest, ToVector) {
453 NPClass array_like_class = {
454 NP_CLASS_STRUCT_VERSION,
455 0, // NPAllocateFunctionPtr allocate;
456 0, // NPDeallocateFunctionPtr deallocate;
457 0, // NPInvalidateFunctionPtr invalidate;
458 0, // NPHasMethodFunctionPtr hasMethod;
459 0, // NPInvokeFunctionPtr invoke;
460 0, // NPInvokeDefaultFunctionPtr invokeDefault;
461 MockNPHasPropertyFunction, // NPHasPropertyFunctionPtr hasProperty;
462 MockNPGetPropertyFunction, // NPGetPropertyFunctionPtr getProperty;
463 0, // NPSetPropertyFunctionPtr setProperty;
464 0, // NPRemovePropertyFunctionPtr removeProperty;
465 0, // NPEnumerationFunctionPtr enumerate;
466 0 // NPConstructFunctionPtr construct;
467 };
468
469 NPObject* obj = WebBindings::createObject(NULL, &array_like_class);
470
471 CppVariant cpp;
472 cpp.Set(obj);
473
474 std::vector<CppVariant> cpp_vector = cpp.ToVector();
475 EXPECT_EQ(4u, cpp_vector.size());
476
477 EXPECT_TRUE(cpp_vector[0].isDouble());
478 EXPECT_EQ(0, cpp_vector[0].ToDouble());
479
480 EXPECT_TRUE(cpp_vector[1].isBool());
481 EXPECT_EQ(true, cpp_vector[1].ToBoolean());
482
483 EXPECT_TRUE(cpp_vector[2].isNull());
484
485 EXPECT_TRUE(cpp_vector[3].isString());
486 CheckString("string", cpp_vector[3]);
487
488 WebBindings::releaseObject(obj);
489 }
OLDNEW
« webkit/glue/cpp_variant.cc ('K') | « webkit/glue/cpp_variant.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698