| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/image_loader.h" | 5 #include "extensions/browser/image_loader.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 image_loaded_count_ = 0; | 67 image_loaded_count_ = 0; |
| 68 return result; | 68 return result; |
| 69 } | 69 } |
| 70 | 70 |
| 71 scoped_refptr<Extension> CreateExtension(const char* dir_name, | 71 scoped_refptr<Extension> CreateExtension(const char* dir_name, |
| 72 Manifest::Location location) { | 72 Manifest::Location location) { |
| 73 // Create and load an extension. | 73 // Create and load an extension. |
| 74 base::FilePath extension_dir; | 74 base::FilePath extension_dir; |
| 75 if (!PathService::Get(DIR_TEST_DATA, &extension_dir)) { | 75 if (!PathService::Get(DIR_TEST_DATA, &extension_dir)) { |
| 76 EXPECT_FALSE(true); | 76 EXPECT_FALSE(true); |
| 77 return NULL; | 77 return nullptr; |
| 78 } | 78 } |
| 79 extension_dir = extension_dir.AppendASCII(dir_name); | 79 extension_dir = extension_dir.AppendASCII(dir_name); |
| 80 int error_code = 0; | 80 int error_code = 0; |
| 81 std::string error; | 81 std::string error; |
| 82 JSONFileValueSerializer serializer( | 82 JSONFileValueSerializer serializer( |
| 83 extension_dir.AppendASCII("manifest.json")); | 83 extension_dir.AppendASCII("manifest.json")); |
| 84 scoped_ptr<base::DictionaryValue> valid_value( | 84 scoped_ptr<base::DictionaryValue> valid_value( |
| 85 static_cast<base::DictionaryValue*>(serializer.Deserialize(&error_code, | 85 static_cast<base::DictionaryValue*>(serializer.Deserialize(&error_code, |
| 86 &error))); | 86 &error))); |
| 87 EXPECT_EQ(0, error_code) << error; | 87 EXPECT_EQ(0, error_code) << error; |
| 88 if (error_code != 0) | 88 if (error_code != 0) |
| 89 return NULL; | 89 return nullptr; |
| 90 | 90 |
| 91 EXPECT_TRUE(valid_value.get()); | 91 EXPECT_TRUE(valid_value.get()); |
| 92 if (!valid_value) | 92 if (!valid_value) |
| 93 return NULL; | 93 return nullptr; |
| 94 | 94 |
| 95 return Extension::Create( | 95 return Extension::Create( |
| 96 extension_dir, location, *valid_value, Extension::NO_FLAGS, &error); | 96 extension_dir, location, *valid_value, Extension::NO_FLAGS, &error); |
| 97 } | 97 } |
| 98 | 98 |
| 99 gfx::Image image_; | 99 gfx::Image image_; |
| 100 gfx::ImageFamily image_family_; | 100 gfx::ImageFamily image_family_; |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 virtual void SetUp() OVERRIDE { | 103 virtual void SetUp() OVERRIDE { |
| 104 testing::Test::SetUp(); | 104 testing::Test::SetUp(); |
| 105 file_thread_.Start(); | 105 file_thread_.Start(); |
| 106 io_thread_.Start(); | 106 io_thread_.Start(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 int image_loaded_count_; | 109 int image_loaded_count_; |
| 110 bool quit_in_image_loaded_; | 110 bool quit_in_image_loaded_; |
| 111 base::MessageLoop ui_loop_; | 111 base::MessageLoop ui_loop_; |
| 112 content::TestBrowserThread ui_thread_; | 112 content::TestBrowserThread ui_thread_; |
| 113 content::TestBrowserThread file_thread_; | 113 content::TestBrowserThread file_thread_; |
| 114 content::TestBrowserThread io_thread_; | 114 content::TestBrowserThread io_thread_; |
| 115 scoped_ptr<NotificationService> notification_service_; | 115 scoped_ptr<NotificationService> notification_service_; |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 // Tests loading an image works correctly. | 118 // Tests loading an image works correctly. |
| 119 TEST_F(ImageLoaderTest, LoadImage) { | 119 TEST_F(ImageLoaderTest, LoadImage) { |
| 120 scoped_refptr<Extension> extension( | 120 scoped_refptr<Extension> extension( |
| 121 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); | 121 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); |
| 122 ASSERT_TRUE(extension.get() != NULL); | 122 ASSERT_TRUE(extension.get() != nullptr); |
| 123 | 123 |
| 124 ExtensionResource image_resource = | 124 ExtensionResource image_resource = |
| 125 IconsInfo::GetIconResource(extension.get(), | 125 IconsInfo::GetIconResource(extension.get(), |
| 126 extension_misc::EXTENSION_ICON_SMALLISH, | 126 extension_misc::EXTENSION_ICON_SMALLISH, |
| 127 ExtensionIconSet::MATCH_EXACTLY); | 127 ExtensionIconSet::MATCH_EXACTLY); |
| 128 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 128 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 129 extension_misc::EXTENSION_ICON_SMALLISH); | 129 extension_misc::EXTENSION_ICON_SMALLISH); |
| 130 ImageLoader loader; | 130 ImageLoader loader; |
| 131 loader.LoadImageAsync(extension.get(), | 131 loader.LoadImageAsync(extension.get(), |
| 132 image_resource, | 132 image_resource, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 146 // Check that the image was loaded. | 146 // Check that the image was loaded. |
| 147 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 147 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 148 image_.ToSkBitmap()->width()); | 148 image_.ToSkBitmap()->width()); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // Tests deleting an extension while waiting for the image to load doesn't cause | 151 // Tests deleting an extension while waiting for the image to load doesn't cause |
| 152 // problems. | 152 // problems. |
| 153 TEST_F(ImageLoaderTest, DeleteExtensionWhileWaitingForCache) { | 153 TEST_F(ImageLoaderTest, DeleteExtensionWhileWaitingForCache) { |
| 154 scoped_refptr<Extension> extension( | 154 scoped_refptr<Extension> extension( |
| 155 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); | 155 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); |
| 156 ASSERT_TRUE(extension.get() != NULL); | 156 ASSERT_TRUE(extension.get() != nullptr); |
| 157 | 157 |
| 158 ExtensionResource image_resource = | 158 ExtensionResource image_resource = |
| 159 IconsInfo::GetIconResource(extension.get(), | 159 IconsInfo::GetIconResource(extension.get(), |
| 160 extension_misc::EXTENSION_ICON_SMALLISH, | 160 extension_misc::EXTENSION_ICON_SMALLISH, |
| 161 ExtensionIconSet::MATCH_EXACTLY); | 161 ExtensionIconSet::MATCH_EXACTLY); |
| 162 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 162 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 163 extension_misc::EXTENSION_ICON_SMALLISH); | 163 extension_misc::EXTENSION_ICON_SMALLISH); |
| 164 ImageLoader loader; | 164 ImageLoader loader; |
| 165 std::set<int> sizes; | 165 std::set<int> sizes; |
| 166 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); | 166 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); |
| 167 loader.LoadImageAsync(extension.get(), | 167 loader.LoadImageAsync(extension.get(), |
| 168 image_resource, | 168 image_resource, |
| 169 max_size, | 169 max_size, |
| 170 base::Bind(&ImageLoaderTest::OnImageLoaded, | 170 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 171 base::Unretained(this))); | 171 base::Unretained(this))); |
| 172 | 172 |
| 173 // The image isn't cached, so we should not have received notification. | 173 // The image isn't cached, so we should not have received notification. |
| 174 EXPECT_EQ(0, image_loaded_count()); | 174 EXPECT_EQ(0, image_loaded_count()); |
| 175 | 175 |
| 176 // Send out notification the extension was uninstalled. | 176 // Send out notification the extension was uninstalled. |
| 177 UnloadedExtensionInfo details(extension.get(), | 177 UnloadedExtensionInfo details(extension.get(), |
| 178 UnloadedExtensionInfo::REASON_UNINSTALL); | 178 UnloadedExtensionInfo::REASON_UNINSTALL); |
| 179 content::NotificationService::current()->Notify( | 179 content::NotificationService::current()->Notify( |
| 180 NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 180 NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 181 content::NotificationService::AllSources(), | 181 content::NotificationService::AllSources(), |
| 182 content::Details<UnloadedExtensionInfo>(&details)); | 182 content::Details<UnloadedExtensionInfo>(&details)); |
| 183 | 183 |
| 184 // Chuck the extension, that way if anyone tries to access it we should crash | 184 // Chuck the extension, that way if anyone tries to access it we should crash |
| 185 // or get valgrind errors. | 185 // or get valgrind errors. |
| 186 extension = NULL; | 186 extension = nullptr; |
| 187 | 187 |
| 188 WaitForImageLoad(); | 188 WaitForImageLoad(); |
| 189 | 189 |
| 190 // Even though we deleted the extension, we should still get the image. | 190 // Even though we deleted the extension, we should still get the image. |
| 191 // We should still have gotten the image. | 191 // We should still have gotten the image. |
| 192 EXPECT_EQ(1, image_loaded_count()); | 192 EXPECT_EQ(1, image_loaded_count()); |
| 193 | 193 |
| 194 // Check that the image was loaded. | 194 // Check that the image was loaded. |
| 195 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 195 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 196 image_.ToSkBitmap()->width()); | 196 image_.ToSkBitmap()->width()); |
| 197 } | 197 } |
| 198 | 198 |
| 199 // Tests loading multiple dimensions of the same image. | 199 // Tests loading multiple dimensions of the same image. |
| 200 TEST_F(ImageLoaderTest, MultipleImages) { | 200 TEST_F(ImageLoaderTest, MultipleImages) { |
| 201 scoped_refptr<Extension> extension( | 201 scoped_refptr<Extension> extension( |
| 202 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); | 202 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); |
| 203 ASSERT_TRUE(extension.get() != NULL); | 203 ASSERT_TRUE(extension.get() != nullptr); |
| 204 | 204 |
| 205 std::vector<ImageLoader::ImageRepresentation> info_list; | 205 std::vector<ImageLoader::ImageRepresentation> info_list; |
| 206 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY, | 206 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY, |
| 207 extension_misc::EXTENSION_ICON_SMALLISH, }; | 207 extension_misc::EXTENSION_ICON_SMALLISH, }; |
| 208 for (size_t i = 0; i < arraysize(sizes); ++i) { | 208 for (size_t i = 0; i < arraysize(sizes); ++i) { |
| 209 ExtensionResource resource = IconsInfo::GetIconResource( | 209 ExtensionResource resource = IconsInfo::GetIconResource( |
| 210 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 210 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
| 211 info_list.push_back(ImageLoader::ImageRepresentation( | 211 info_list.push_back(ImageLoader::ImageRepresentation( |
| 212 resource, | 212 resource, |
| 213 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, | 213 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 238 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, | 238 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, |
| 239 img_rep1->pixel_width()); | 239 img_rep1->pixel_width()); |
| 240 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 240 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 241 img_rep2->pixel_width()); | 241 img_rep2->pixel_width()); |
| 242 } | 242 } |
| 243 | 243 |
| 244 // Tests loading multiple dimensions of the same image into an image family. | 244 // Tests loading multiple dimensions of the same image into an image family. |
| 245 TEST_F(ImageLoaderTest, LoadImageFamily) { | 245 TEST_F(ImageLoaderTest, LoadImageFamily) { |
| 246 scoped_refptr<Extension> extension( | 246 scoped_refptr<Extension> extension( |
| 247 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); | 247 CreateExtension("image_loader", Manifest::INVALID_LOCATION)); |
| 248 ASSERT_TRUE(extension.get() != NULL); | 248 ASSERT_TRUE(extension.get() != nullptr); |
| 249 | 249 |
| 250 std::vector<ImageLoader::ImageRepresentation> info_list; | 250 std::vector<ImageLoader::ImageRepresentation> info_list; |
| 251 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY, | 251 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY, |
| 252 extension_misc::EXTENSION_ICON_SMALLISH, }; | 252 extension_misc::EXTENSION_ICON_SMALLISH, }; |
| 253 for (size_t i = 0; i < arraysize(sizes); ++i) { | 253 for (size_t i = 0; i < arraysize(sizes); ++i) { |
| 254 ExtensionResource resource = IconsInfo::GetIconResource( | 254 ExtensionResource resource = IconsInfo::GetIconResource( |
| 255 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 255 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
| 256 info_list.push_back(ImageLoader::ImageRepresentation( | 256 info_list.push_back(ImageLoader::ImageRepresentation( |
| 257 resource, | 257 resource, |
| 258 ImageLoader::ImageRepresentation::NEVER_RESIZE, | 258 ImageLoader::ImageRepresentation::NEVER_RESIZE, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 304 |
| 305 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; | 305 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; |
| 306 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; | 306 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; |
| 307 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, img_rep1->pixel_width()); | 307 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, img_rep1->pixel_width()); |
| 308 EXPECT_EQ(1.0f, img_rep1->scale()); | 308 EXPECT_EQ(1.0f, img_rep1->scale()); |
| 309 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, img_rep2->pixel_width()); | 309 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, img_rep2->pixel_width()); |
| 310 EXPECT_EQ(2.0f, img_rep2->scale()); | 310 EXPECT_EQ(2.0f, img_rep2->scale()); |
| 311 } | 311 } |
| 312 | 312 |
| 313 } // namespace extensions | 313 } // namespace extensions |
| OLD | NEW |