OLD | NEW |
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 Loading... |
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 const char* s = "string"; |
| 447 size_t length = strlen(s); |
| 448 char* mem = static_cast<char*>(malloc(length + 1)); |
| 449 base::strlcpy(mem, s, length + 1); |
| 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 0, // NPAllocateFunctionPtr allocate; |
| 460 0, // NPDeallocateFunctionPtr deallocate; |
| 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 } |
OLD | NEW |