OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
20 #include "chrome/common/extensions/extension_action.h" | 20 #include "chrome/common/extensions/extension_action.h" |
21 #include "chrome/common/extensions/extension_constants.h" | 21 #include "chrome/common/extensions/extension_constants.h" |
22 #include "chrome/common/extensions/extension_error_utils.h" | 22 #include "chrome/common/extensions/extension_error_utils.h" |
| 23 #include "chrome/common/extensions/extension_resource.h" |
23 #include "chrome/common/json_value_serializer.h" | 24 #include "chrome/common/json_value_serializer.h" |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 26 #include "gfx/codec/png_codec.h" |
25 #include "net/base/mime_sniffer.h" | 27 #include "net/base/mime_sniffer.h" |
| 28 #include "skia/ext/image_operations.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
27 | 30 |
28 namespace keys = extension_manifest_keys; | 31 namespace keys = extension_manifest_keys; |
29 namespace values = extension_manifest_values; | 32 namespace values = extension_manifest_values; |
30 namespace errors = extension_manifest_errors; | 33 namespace errors = extension_manifest_errors; |
31 | 34 |
32 class ExtensionTest : public testing::Test { | 35 class ExtensionTest : public testing::Test { |
33 }; | 36 }; |
34 | 37 |
35 // We persist location values in the preferences, so this is a sanity test that | 38 // We persist location values in the preferences, so this is a sanity test that |
(...skipping 801 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 scoped_ptr<Extension> new_extension( | 840 scoped_ptr<Extension> new_extension( |
838 LoadManifest("allow_silent_upgrade", | 841 LoadManifest("allow_silent_upgrade", |
839 std::string(kTests[i].base_name) + "_new.json")); | 842 std::string(kTests[i].base_name) + "_new.json")); |
840 | 843 |
841 EXPECT_EQ(kTests[i].expect_success, | 844 EXPECT_EQ(kTests[i].expect_success, |
842 Extension::IsPrivilegeIncrease(old_extension.get(), | 845 Extension::IsPrivilegeIncrease(old_extension.get(), |
843 new_extension.get())) | 846 new_extension.get())) |
844 << kTests[i].base_name; | 847 << kTests[i].base_name; |
845 } | 848 } |
846 } | 849 } |
| 850 |
| 851 // Returns a copy of |source| resized to |size| x |size|. |
| 852 static SkBitmap ResizedCopy(const SkBitmap& source, int size) { |
| 853 return skia::ImageOperations::Resize(source, |
| 854 skia::ImageOperations::RESIZE_LANCZOS3, |
| 855 size, |
| 856 size); |
| 857 } |
| 858 |
| 859 static bool SizeEquals(const SkBitmap& bitmap, const gfx::Size& size) { |
| 860 return bitmap.width() == size.width() && bitmap.height() == size.height(); |
| 861 } |
| 862 |
| 863 TEST(ExtensionTest, ImageCaching) { |
| 864 FilePath path; |
| 865 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 866 path = path.AppendASCII("extensions"); |
| 867 |
| 868 // Initialize the Extension. |
| 869 std::string errors; |
| 870 scoped_ptr<Extension> extension(new Extension(path)); |
| 871 DictionaryValue values; |
| 872 values.SetString(keys::kName, "test"); |
| 873 values.SetString(keys::kVersion, "0.1"); |
| 874 ASSERT_TRUE(extension->InitFromValue(values, false, &errors)); |
| 875 |
| 876 // Create an ExtensionResource pointing at an icon. |
| 877 FilePath icon_relative_path(FILE_PATH_LITERAL("icon3.png")); |
| 878 ExtensionResource resource(extension->id(), |
| 879 extension->path(), |
| 880 icon_relative_path); |
| 881 |
| 882 // Read in the icon file. |
| 883 FilePath icon_absolute_path = extension->path().Append(icon_relative_path); |
| 884 std::string raw_png; |
| 885 ASSERT_TRUE(file_util::ReadFileToString(icon_absolute_path, &raw_png)); |
| 886 SkBitmap image; |
| 887 ASSERT_TRUE(gfx::PNGCodec::Decode( |
| 888 reinterpret_cast<const unsigned char*>(raw_png.data()), |
| 889 raw_png.length(), |
| 890 &image)); |
| 891 |
| 892 // Make sure the icon file is the size we expect. |
| 893 gfx::Size original_size(66, 66); |
| 894 ASSERT_EQ(image.width(), original_size.width()); |
| 895 ASSERT_EQ(image.height(), original_size.height()); |
| 896 |
| 897 // Create two resized versions at size 16x16 and 24x24. |
| 898 SkBitmap image16 = ResizedCopy(image, 16); |
| 899 SkBitmap image24 = ResizedCopy(image, 24); |
| 900 |
| 901 gfx::Size size16(16, 16); |
| 902 gfx::Size size24(24, 24); |
| 903 |
| 904 // Cache the 16x16 copy. |
| 905 EXPECT_FALSE(extension->HasCachedImage(resource, size16)); |
| 906 extension->SetCachedImage(resource, image16, original_size); |
| 907 EXPECT_TRUE(extension->HasCachedImage(resource, size16)); |
| 908 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size16), size16)); |
| 909 EXPECT_FALSE(extension->HasCachedImage(resource, size24)); |
| 910 EXPECT_FALSE(extension->HasCachedImage(resource, original_size)); |
| 911 |
| 912 // Cache the 24x24 copy. |
| 913 extension->SetCachedImage(resource, image24, original_size); |
| 914 EXPECT_TRUE(extension->HasCachedImage(resource, size24)); |
| 915 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size24), size24)); |
| 916 EXPECT_FALSE(extension->HasCachedImage(resource, original_size)); |
| 917 |
| 918 // Cache the original, and verify that it gets returned when we ask for a |
| 919 // max_size that is larger than the original. |
| 920 gfx::Size size128(128, 128); |
| 921 EXPECT_TRUE(image.width() < size128.width() && |
| 922 image.height() < size128.height()); |
| 923 extension->SetCachedImage(resource, image, original_size); |
| 924 EXPECT_TRUE(extension->HasCachedImage(resource, original_size)); |
| 925 EXPECT_TRUE(extension->HasCachedImage(resource, size128)); |
| 926 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, original_size), |
| 927 original_size)); |
| 928 EXPECT_TRUE(SizeEquals(extension->GetCachedImage(resource, size128), |
| 929 original_size)); |
| 930 EXPECT_EQ(extension->GetCachedImage(resource, original_size).getPixels(), |
| 931 extension->GetCachedImage(resource, size128).getPixels()); |
| 932 } |
OLD | NEW |