| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 extension, SocketPermissionRequest::UDP_BIND, "", 8888)); | 489 extension, SocketPermissionRequest::UDP_BIND, "", 8888)); |
| 490 | 490 |
| 491 EXPECT_FALSE(CheckSocketPermission( | 491 EXPECT_FALSE(CheckSocketPermission( |
| 492 extension, SocketPermissionRequest::UDP_SEND_TO, "example.com", 1900)); | 492 extension, SocketPermissionRequest::UDP_SEND_TO, "example.com", 1900)); |
| 493 EXPECT_TRUE(CheckSocketPermission( | 493 EXPECT_TRUE(CheckSocketPermission( |
| 494 extension, | 494 extension, |
| 495 SocketPermissionRequest::UDP_SEND_TO, | 495 SocketPermissionRequest::UDP_SEND_TO, |
| 496 "239.255.255.250", 1900)); | 496 "239.255.255.250", 1900)); |
| 497 } | 497 } |
| 498 | 498 |
| 499 // Returns a copy of |source| resized to |size| x |size|. | |
| 500 static SkBitmap ResizedCopy(const SkBitmap& source, int size) { | |
| 501 return skia::ImageOperations::Resize(source, | |
| 502 skia::ImageOperations::RESIZE_LANCZOS3, | |
| 503 size, | |
| 504 size); | |
| 505 } | |
| 506 | |
| 507 static bool SizeEquals(const SkBitmap& bitmap, const gfx::Size& size) { | |
| 508 return bitmap.width() == size.width() && bitmap.height() == size.height(); | |
| 509 } | |
| 510 | |
| 511 TEST_F(ExtensionTest, ImageCaching) { | |
| 512 base::FilePath path; | |
| 513 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 514 path = path.AppendASCII("extensions"); | |
| 515 | |
| 516 // Initialize the Extension. | |
| 517 std::string errors; | |
| 518 DictionaryValue values; | |
| 519 values.SetString(keys::kName, "test"); | |
| 520 values.SetString(keys::kVersion, "0.1"); | |
| 521 scoped_refptr<Extension> extension(Extension::Create( | |
| 522 path, Manifest::INVALID_LOCATION, values, Extension::NO_FLAGS, &errors)); | |
| 523 ASSERT_TRUE(extension.get()); | |
| 524 | |
| 525 // Create an ExtensionResource pointing at an icon. | |
| 526 base::FilePath icon_relative_path(FILE_PATH_LITERAL("icon3.png")); | |
| 527 ExtensionResource resource(extension->id(), | |
| 528 extension->path(), | |
| 529 icon_relative_path); | |
| 530 | |
| 531 // Read in the icon file. | |
| 532 base::FilePath icon_absolute_path = | |
| 533 extension->path().Append(icon_relative_path); | |
| 534 std::string raw_png; | |
| 535 ASSERT_TRUE(file_util::ReadFileToString(icon_absolute_path, &raw_png)); | |
| 536 SkBitmap image; | |
| 537 ASSERT_TRUE(gfx::PNGCodec::Decode( | |
| 538 reinterpret_cast<const unsigned char*>(raw_png.data()), | |
| 539 raw_png.length(), | |
| 540 &image)); | |
| 541 | |
| 542 // Make sure the icon file is the size we expect. | |
| 543 gfx::Size original_size(66, 66); | |
| 544 ASSERT_EQ(image.width(), original_size.width()); | |
| 545 ASSERT_EQ(image.height(), original_size.height()); | |
| 546 | |
| 547 // Create two resized versions at size 16x16 and 24x24. | |
| 548 SkBitmap image16 = ResizedCopy(image, 16); | |
| 549 SkBitmap image24 = ResizedCopy(image, 24); | |
| 550 | |
| 551 gfx::Size size16(16, 16); | |
| 552 gfx::Size size24(24, 24); | |
| 553 | |
| 554 // Cache the 16x16 copy. | |
| 555 EXPECT_FALSE(extension->HasCachedImage(resource, size16)); | |
| 556 extension->SetCachedImage(resource, image16, original_size); | |
| 557 EXPECT_TRUE(extension->HasCachedImage(resource, size16)); | |
| 558 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size16), size16)); | |
| 559 EXPECT_FALSE(extension->HasCachedImage(resource, size24)); | |
| 560 EXPECT_FALSE(extension->HasCachedImage(resource, original_size)); | |
| 561 | |
| 562 // Cache the 24x24 copy. | |
| 563 extension->SetCachedImage(resource, image24, original_size); | |
| 564 EXPECT_TRUE(extension->HasCachedImage(resource, size24)); | |
| 565 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size24), size24)); | |
| 566 EXPECT_FALSE(extension->HasCachedImage(resource, original_size)); | |
| 567 | |
| 568 // Cache the original, and verify that it gets returned when we ask for a | |
| 569 // max_size that is larger than the original. | |
| 570 gfx::Size size128(128, 128); | |
| 571 EXPECT_TRUE(image.width() < size128.width() && | |
| 572 image.height() < size128.height()); | |
| 573 extension->SetCachedImage(resource, image, original_size); | |
| 574 EXPECT_TRUE(extension->HasCachedImage(resource, original_size)); | |
| 575 EXPECT_TRUE(extension->HasCachedImage(resource, size128)); | |
| 576 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, original_size), | |
| 577 original_size)); | |
| 578 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size128), | |
| 579 original_size)); | |
| 580 EXPECT_EQ(extension->GetCachedImage(resource, original_size).getPixels(), | |
| 581 extension->GetCachedImage(resource, size128).getPixels()); | |
| 582 } | |
| 583 | |
| 584 // This tests the API permissions with an empty manifest (one that just | 499 // This tests the API permissions with an empty manifest (one that just |
| 585 // specifies a name and a version and nothing else). | 500 // specifies a name and a version and nothing else). |
| 586 TEST_F(ExtensionTest, ApiPermissions) { | 501 TEST_F(ExtensionTest, ApiPermissions) { |
| 587 const struct { | 502 const struct { |
| 588 const char* permission_name; | 503 const char* permission_name; |
| 589 bool expect_success; | 504 bool expect_success; |
| 590 } kTests[] = { | 505 } kTests[] = { |
| 591 // Negative test. | 506 // Negative test. |
| 592 { "non_existing_permission", false }, | 507 { "non_existing_permission", false }, |
| 593 // Test default module/package permission. | 508 // Test default module/package permission. |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1294 scoped_refptr<Extension> extension( | 1209 scoped_refptr<Extension> extension( |
| 1295 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), | 1210 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), |
| 1296 Manifest::INTERNAL, 2, base::FilePath(), | 1211 Manifest::INTERNAL, 2, base::FilePath(), |
| 1297 Extension::NO_FLAGS)); | 1212 Extension::NO_FLAGS)); |
| 1298 if (extension) | 1213 if (extension) |
| 1299 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); | 1214 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); |
| 1300 } | 1215 } |
| 1301 #endif // !defined(OS_CHROMEOS) | 1216 #endif // !defined(OS_CHROMEOS) |
| 1302 | 1217 |
| 1303 } // namespace extensions | 1218 } // namespace extensions |
| OLD | NEW |