| 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/browser/extensions/image_loader.h" | 5 #include "chrome/browser/extensions/image_loader.h" |
| 6 | 6 |
| 7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 using extensions::Extension; | 28 using extensions::Extension; |
| 29 using extensions::ExtensionResource; | 29 using extensions::ExtensionResource; |
| 30 using extensions::ImageLoader; | 30 using extensions::ImageLoader; |
| 31 using extensions::Manifest; | 31 using extensions::Manifest; |
| 32 | 32 |
| 33 class ImageLoaderTest : public testing::Test { | 33 class ImageLoaderTest : public testing::Test { |
| 34 public: | 34 public: |
| 35 ImageLoaderTest() | 35 ImageLoaderTest() |
| 36 : image_loaded_count_(0), | 36 : image_loaded_count_(0), |
| 37 quit_in_image_loaded_(false), | |
| 38 ui_thread_(BrowserThread::UI, &ui_loop_), | 37 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 39 file_thread_(BrowserThread::FILE), | 38 file_thread_(BrowserThread::FILE), |
| 40 io_thread_(BrowserThread::IO) { | 39 io_thread_(BrowserThread::IO) { |
| 41 } | 40 } |
| 42 | 41 |
| 43 void OnImageLoaded(const gfx::Image& image) { | 42 void OnImageLoaded(const gfx::Image& image) { |
| 44 image_loaded_count_++; | 43 image_loaded_count_++; |
| 45 if (quit_in_image_loaded_) | 44 base::MessageLoop::current()->Quit(); |
| 46 base::MessageLoop::current()->Quit(); | |
| 47 image_ = image; | 45 image_ = image; |
| 48 } | 46 } |
| 49 | 47 |
| 48 void OnImageURLLoaded(const GURL& url) { |
| 49 image_loaded_count_++; |
| 50 base::MessageLoop::current()->Quit(); |
| 51 url_ = url; |
| 52 } |
| 53 |
| 50 void WaitForImageLoad() { | 54 void WaitForImageLoad() { |
| 51 quit_in_image_loaded_ = true; | |
| 52 base::MessageLoop::current()->Run(); | 55 base::MessageLoop::current()->Run(); |
| 53 quit_in_image_loaded_ = false; | |
| 54 } | 56 } |
| 55 | 57 |
| 56 int image_loaded_count() { | 58 int image_loaded_count() { |
| 57 int result = image_loaded_count_; | 59 int result = image_loaded_count_; |
| 58 image_loaded_count_ = 0; | 60 image_loaded_count_ = 0; |
| 59 return result; | 61 return result; |
| 60 } | 62 } |
| 61 | 63 |
| 62 scoped_refptr<Extension> CreateExtension(const char* name, | 64 scoped_refptr<Extension> CreateExtension(const char* name, |
| 63 Manifest::Location location) { | 65 Manifest::Location location) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 87 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) { | 89 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) { |
| 88 EXPECT_FALSE(true); | 90 EXPECT_FALSE(true); |
| 89 return NULL; | 91 return NULL; |
| 90 } | 92 } |
| 91 test_file = test_file.AppendASCII(name); | 93 test_file = test_file.AppendASCII(name); |
| 92 } | 94 } |
| 93 return Extension::Create(test_file, location, *valid_value, | 95 return Extension::Create(test_file, location, *valid_value, |
| 94 Extension::NO_FLAGS, &error); | 96 Extension::NO_FLAGS, &error); |
| 95 } | 97 } |
| 96 | 98 |
| 99 // Holds the image loaded by the test. |
| 97 gfx::Image image_; | 100 gfx::Image image_; |
| 101 // Holds the data url retrieved by the test. |
| 102 GURL url_; |
| 98 | 103 |
| 99 private: | 104 private: |
| 100 virtual void SetUp() OVERRIDE { | 105 virtual void SetUp() OVERRIDE { |
| 101 testing::Test::SetUp(); | 106 testing::Test::SetUp(); |
| 102 file_thread_.Start(); | 107 file_thread_.Start(); |
| 103 io_thread_.Start(); | 108 io_thread_.Start(); |
| 104 } | 109 } |
| 105 | 110 |
| 106 int image_loaded_count_; | 111 int image_loaded_count_; |
| 107 bool quit_in_image_loaded_; | |
| 108 base::MessageLoop ui_loop_; | 112 base::MessageLoop ui_loop_; |
| 109 content::TestBrowserThread ui_thread_; | 113 content::TestBrowserThread ui_thread_; |
| 110 content::TestBrowserThread file_thread_; | 114 content::TestBrowserThread file_thread_; |
| 111 content::TestBrowserThread io_thread_; | 115 content::TestBrowserThread io_thread_; |
| 112 }; | 116 }; |
| 113 | 117 |
| 114 // Tests loading an image works correctly. | 118 // Tests loading an image works correctly. |
| 115 TEST_F(ImageLoaderTest, LoadImage) { | 119 TEST_F(ImageLoaderTest, LoadImage) { |
| 116 scoped_refptr<Extension> extension(CreateExtension( | 120 scoped_refptr<Extension> extension(CreateExtension( |
| 117 "image_loading_tracker", Manifest::INVALID_LOCATION)); | 121 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 118 ASSERT_TRUE(extension.get() != NULL); | 122 ASSERT_TRUE(extension.get() != NULL); |
| 119 | 123 |
| 120 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( | 124 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( |
| 121 extension.get(), | 125 extension.get(), |
| 122 extension_misc::EXTENSION_ICON_SMALLISH, | 126 extension_misc::EXTENSION_ICON_SMALLISH, |
| 123 ExtensionIconSet::MATCH_EXACTLY); | 127 ExtensionIconSet::MATCH_EXACTLY); |
| 124 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 128 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 125 extension_misc::EXTENSION_ICON_SMALLISH); | 129 extension_misc::EXTENSION_ICON_SMALLISH); |
| 126 ImageLoader loader; | 130 ImageLoader loader(NULL); |
| 127 loader.LoadImageAsync(extension.get(), | 131 loader.LoadImageAsync(extension.get(), |
| 128 image_resource, | 132 image_resource, |
| 129 max_size, | 133 max_size, |
| 130 base::Bind(&ImageLoaderTest::OnImageLoaded, | 134 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 131 base::Unretained(this))); | 135 base::Unretained(this))); |
| 132 | 136 |
| 133 // The image isn't cached, so we should not have received notification. | 137 // The image isn't cached, so we should not have received notification. |
| 134 EXPECT_EQ(0, image_loaded_count()); | 138 EXPECT_EQ(0, image_loaded_count()); |
| 135 | 139 |
| 136 WaitForImageLoad(); | 140 WaitForImageLoad(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 149 scoped_refptr<Extension> extension(CreateExtension( | 153 scoped_refptr<Extension> extension(CreateExtension( |
| 150 "image_loading_tracker", Manifest::INVALID_LOCATION)); | 154 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 151 ASSERT_TRUE(extension.get() != NULL); | 155 ASSERT_TRUE(extension.get() != NULL); |
| 152 | 156 |
| 153 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( | 157 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( |
| 154 extension.get(), | 158 extension.get(), |
| 155 extension_misc::EXTENSION_ICON_SMALLISH, | 159 extension_misc::EXTENSION_ICON_SMALLISH, |
| 156 ExtensionIconSet::MATCH_EXACTLY); | 160 ExtensionIconSet::MATCH_EXACTLY); |
| 157 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 161 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 158 extension_misc::EXTENSION_ICON_SMALLISH); | 162 extension_misc::EXTENSION_ICON_SMALLISH); |
| 159 ImageLoader loader; | 163 ImageLoader loader(NULL); |
| 160 std::set<int> sizes; | 164 std::set<int> sizes; |
| 161 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); | 165 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); |
| 162 loader.LoadImageAsync(extension.get(), | 166 loader.LoadImageAsync(extension.get(), |
| 163 image_resource, | 167 image_resource, |
| 164 max_size, | 168 max_size, |
| 165 base::Bind(&ImageLoaderTest::OnImageLoaded, | 169 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 166 base::Unretained(this))); | 170 base::Unretained(this))); |
| 167 | 171 |
| 168 // The image isn't cached, so we should not have received notification. | 172 // The image isn't cached, so we should not have received notification. |
| 169 EXPECT_EQ(0, image_loaded_count()); | 173 EXPECT_EQ(0, image_loaded_count()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 for (size_t i = 0; i < arraysize(sizes); ++i) { | 207 for (size_t i = 0; i < arraysize(sizes); ++i) { |
| 204 ExtensionResource resource = extensions::IconsInfo::GetIconResource( | 208 ExtensionResource resource = extensions::IconsInfo::GetIconResource( |
| 205 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 209 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
| 206 info_list.push_back(ImageLoader::ImageRepresentation( | 210 info_list.push_back(ImageLoader::ImageRepresentation( |
| 207 resource, | 211 resource, |
| 208 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, | 212 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, |
| 209 gfx::Size(sizes[i], sizes[i]), | 213 gfx::Size(sizes[i], sizes[i]), |
| 210 ui::SCALE_FACTOR_NONE)); | 214 ui::SCALE_FACTOR_NONE)); |
| 211 } | 215 } |
| 212 | 216 |
| 213 ImageLoader loader; | 217 ImageLoader loader(NULL); |
| 214 loader.LoadImagesAsync(extension.get(), info_list, | 218 loader.LoadImagesAsync(extension.get(), info_list, |
| 215 base::Bind(&ImageLoaderTest::OnImageLoaded, | 219 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 216 base::Unretained(this))); | 220 base::Unretained(this))); |
| 217 | 221 |
| 218 // The image isn't cached, so we should not have received notification. | 222 // The image isn't cached, so we should not have received notification. |
| 219 EXPECT_EQ(0, image_loaded_count()); | 223 EXPECT_EQ(0, image_loaded_count()); |
| 220 | 224 |
| 221 WaitForImageLoad(); | 225 WaitForImageLoad(); |
| 222 | 226 |
| 223 // We should have gotten the image. | 227 // We should have gotten the image. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 251 | 255 |
| 252 #if defined(FILE_MANAGER_EXTENSION) | 256 #if defined(FILE_MANAGER_EXTENSION) |
| 253 int resource_id; | 257 int resource_id; |
| 254 ASSERT_EQ(true, | 258 ASSERT_EQ(true, |
| 255 ImageLoader::IsComponentExtensionResource(extension->path(), | 259 ImageLoader::IsComponentExtensionResource(extension->path(), |
| 256 resource.relative_path(), | 260 resource.relative_path(), |
| 257 &resource_id)); | 261 &resource_id)); |
| 258 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | 262 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); |
| 259 #endif | 263 #endif |
| 260 } | 264 } |
| 265 |
| 266 // Tests loading an image works correctly. |
| 267 TEST_F(ImageLoaderTest, LoadIcon) { |
| 268 scoped_refptr<Extension> extension(CreateExtension( |
| 269 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 270 ASSERT_TRUE(extension.get() != NULL); |
| 271 |
| 272 ImageLoader loader(NULL); |
| 273 loader.LoadExtensionIconAsync(extension.get(), |
| 274 extension_misc::EXTENSION_ICON_SMALLISH, |
| 275 ExtensionIconSet::MATCH_BIGGER, |
| 276 false, |
| 277 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 278 base::Unretained(this))); |
| 279 |
| 280 // The image isn't cached, so we should not have received notification. |
| 281 EXPECT_EQ(0, image_loaded_count()); |
| 282 |
| 283 WaitForImageLoad(); |
| 284 |
| 285 // We should have gotten the image. |
| 286 EXPECT_EQ(1, image_loaded_count()); |
| 287 |
| 288 // Check that the image was loaded. |
| 289 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 290 image_.ToSkBitmap()->width()); |
| 291 } |
| 292 |
| 293 // Tests loading an icon in grayscale works correctly. |
| 294 TEST_F(ImageLoaderTest, LoadIconGrayscale) { |
| 295 scoped_refptr<Extension> extension(CreateExtension( |
| 296 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 297 ASSERT_TRUE(extension.get() != NULL); |
| 298 |
| 299 ImageLoader loader(NULL); |
| 300 loader.LoadExtensionIconAsync(extension.get(), |
| 301 extension_misc::EXTENSION_ICON_SMALLISH, |
| 302 ExtensionIconSet::MATCH_BIGGER, |
| 303 true, |
| 304 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 305 base::Unretained(this))); |
| 306 |
| 307 // The image isn't cached, so we should not have received notification. |
| 308 EXPECT_EQ(0, image_loaded_count()); |
| 309 |
| 310 WaitForImageLoad(); |
| 311 |
| 312 // We should have gotten the image. |
| 313 EXPECT_EQ(1, image_loaded_count()); |
| 314 |
| 315 // Check that the image was loaded. |
| 316 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 317 image_.ToSkBitmap()->width()); |
| 318 } |
| 319 |
| 320 // Tests loading an icon URL works correctly. |
| 321 TEST_F(ImageLoaderTest, LoadIconURL) { |
| 322 scoped_refptr<Extension> extension(CreateExtension( |
| 323 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 324 ASSERT_TRUE(extension.get() != NULL); |
| 325 |
| 326 ImageLoader loader(NULL); |
| 327 loader.LoadExtensionIconDataURLAsync( |
| 328 extension.get(), |
| 329 extension_misc::EXTENSION_ICON_SMALLISH, |
| 330 ExtensionIconSet::MATCH_BIGGER, |
| 331 true, |
| 332 base::Bind(&ImageLoaderTest::OnImageURLLoaded, base::Unretained(this))); |
| 333 |
| 334 // The image isn't cached, so we should not have received notification. |
| 335 EXPECT_EQ(0, image_loaded_count()); |
| 336 |
| 337 WaitForImageLoad(); |
| 338 |
| 339 // We should have gotten the image. |
| 340 EXPECT_EQ(1, image_loaded_count()); |
| 341 } |
| 342 |
| 343 // Tests loading the default icon works correctly. |
| 344 TEST_F(ImageLoaderTest, LoadDefaultIcon) { |
| 345 ImageLoader loader(NULL); |
| 346 loader.LoadExtensionIconAsync( |
| 347 NULL, |
| 348 -1, |
| 349 ExtensionIconSet::MATCH_BIGGER, |
| 350 false, |
| 351 base::Bind(&ImageLoaderTest::OnImageLoaded, base::Unretained(this))); |
| 352 |
| 353 // The image isn't cached, so we should not have received notification. |
| 354 EXPECT_EQ(0, image_loaded_count()); |
| 355 |
| 356 WaitForImageLoad(); |
| 357 |
| 358 // We should have gotten the image. |
| 359 EXPECT_EQ(1, image_loaded_count()); |
| 360 } |
| 361 |
| 362 // Tests loading the icon of an extension without icon works correctly. |
| 363 TEST_F(ImageLoaderTest, LoadExtensionWithoutIcon) { |
| 364 scoped_refptr<Extension> extension(CreateExtension( |
| 365 "default_image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 366 ASSERT_TRUE(extension.get() != NULL); |
| 367 |
| 368 ImageLoader loader(NULL); |
| 369 loader.LoadExtensionIconAsync(extension.get(), |
| 370 extension_misc::EXTENSION_ICON_SMALLISH, |
| 371 ExtensionIconSet::MATCH_BIGGER, |
| 372 false, |
| 373 base::Bind(&ImageLoaderTest::OnImageLoaded, |
| 374 base::Unretained(this))); |
| 375 |
| 376 // The image isn't cached, so we should not have received notification. |
| 377 EXPECT_EQ(0, image_loaded_count()); |
| 378 |
| 379 WaitForImageLoad(); |
| 380 |
| 381 // We should have gotten the image. |
| 382 EXPECT_EQ(1, image_loaded_count()); |
| 383 |
| 384 // Check that the image was loaded. |
| 385 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 386 image_.ToSkBitmap()->width()); |
| 387 } |
| OLD | NEW |