| 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_loading_tracker.h" | 5 #include "chrome/browser/extensions/image_loading_tracker.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.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "chrome/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "chrome/common/extensions/extension_icon_set.h" | 14 #include "chrome/common/extensions/extension_icon_set.h" |
| 15 #include "chrome/common/extensions/extension_resource.h" | 15 #include "chrome/common/extensions/extension_resource.h" |
| 16 #include "chrome/common/extensions/manifest.h" |
| 16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 18 #include "grit/component_extension_resources.h" | 19 #include "grit/component_extension_resources.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
| 22 #include "ui/gfx/image/image_skia.h" | 23 #include "ui/gfx/image/image_skia.h" |
| 23 #include "ui/gfx/size.h" | 24 #include "ui/gfx/size.h" |
| 24 | 25 |
| 25 using content::BrowserThread; | 26 using content::BrowserThread; |
| 26 using extensions::Extension; | 27 using extensions::Extension; |
| 28 using extensions::Manifest; |
| 27 | 29 |
| 28 class ImageLoadingTrackerTest : public testing::Test, | 30 class ImageLoadingTrackerTest : public testing::Test, |
| 29 public ImageLoadingTracker::Observer { | 31 public ImageLoadingTracker::Observer { |
| 30 public: | 32 public: |
| 31 ImageLoadingTrackerTest() | 33 ImageLoadingTrackerTest() |
| 32 : image_loaded_count_(0), | 34 : image_loaded_count_(0), |
| 33 quit_in_image_loaded_(false), | 35 quit_in_image_loaded_(false), |
| 34 ui_thread_(BrowserThread::UI, &ui_loop_), | 36 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 35 file_thread_(BrowserThread::FILE), | 37 file_thread_(BrowserThread::FILE), |
| 36 io_thread_(BrowserThread::IO) { | 38 io_thread_(BrowserThread::IO) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 51 quit_in_image_loaded_ = false; | 53 quit_in_image_loaded_ = false; |
| 52 } | 54 } |
| 53 | 55 |
| 54 int image_loaded_count() { | 56 int image_loaded_count() { |
| 55 int result = image_loaded_count_; | 57 int result = image_loaded_count_; |
| 56 image_loaded_count_ = 0; | 58 image_loaded_count_ = 0; |
| 57 return result; | 59 return result; |
| 58 } | 60 } |
| 59 | 61 |
| 60 scoped_refptr<Extension> CreateExtension(const char* name, | 62 scoped_refptr<Extension> CreateExtension(const char* name, |
| 61 Extension::Location location) { | 63 Manifest::Location location) { |
| 62 // Create and load an extension. | 64 // Create and load an extension. |
| 63 FilePath test_file; | 65 FilePath test_file; |
| 64 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { | 66 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
| 65 EXPECT_FALSE(true); | 67 EXPECT_FALSE(true); |
| 66 return NULL; | 68 return NULL; |
| 67 } | 69 } |
| 68 test_file = test_file.AppendASCII("extensions") | 70 test_file = test_file.AppendASCII("extensions") |
| 69 .AppendASCII(name); | 71 .AppendASCII(name); |
| 70 int error_code = 0; | 72 int error_code = 0; |
| 71 std::string error; | 73 std::string error; |
| 72 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); | 74 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); |
| 73 scoped_ptr<DictionaryValue> valid_value( | 75 scoped_ptr<DictionaryValue> valid_value( |
| 74 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, | 76 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, |
| 75 &error))); | 77 &error))); |
| 76 EXPECT_EQ(0, error_code) << error; | 78 EXPECT_EQ(0, error_code) << error; |
| 77 if (error_code != 0) | 79 if (error_code != 0) |
| 78 return NULL; | 80 return NULL; |
| 79 | 81 |
| 80 EXPECT_TRUE(valid_value.get()); | 82 EXPECT_TRUE(valid_value.get()); |
| 81 if (!valid_value.get()) | 83 if (!valid_value.get()) |
| 82 return NULL; | 84 return NULL; |
| 83 | 85 |
| 84 if (location == Extension::COMPONENT) { | 86 if (location == Manifest::COMPONENT) { |
| 85 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) { | 87 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) { |
| 86 EXPECT_FALSE(true); | 88 EXPECT_FALSE(true); |
| 87 return NULL; | 89 return NULL; |
| 88 } | 90 } |
| 89 test_file = test_file.AppendASCII(name); | 91 test_file = test_file.AppendASCII(name); |
| 90 } | 92 } |
| 91 return Extension::Create(test_file, location, *valid_value, | 93 return Extension::Create(test_file, location, *valid_value, |
| 92 Extension::NO_FLAGS, &error); | 94 Extension::NO_FLAGS, &error); |
| 93 } | 95 } |
| 94 | 96 |
| 95 gfx::Image image_; | 97 gfx::Image image_; |
| 96 | 98 |
| 97 private: | 99 private: |
| 98 virtual void SetUp() { | 100 virtual void SetUp() { |
| 99 file_thread_.Start(); | 101 file_thread_.Start(); |
| 100 io_thread_.Start(); | 102 io_thread_.Start(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 int image_loaded_count_; | 105 int image_loaded_count_; |
| 104 bool quit_in_image_loaded_; | 106 bool quit_in_image_loaded_; |
| 105 MessageLoop ui_loop_; | 107 MessageLoop ui_loop_; |
| 106 content::TestBrowserThread ui_thread_; | 108 content::TestBrowserThread ui_thread_; |
| 107 content::TestBrowserThread file_thread_; | 109 content::TestBrowserThread file_thread_; |
| 108 content::TestBrowserThread io_thread_; | 110 content::TestBrowserThread io_thread_; |
| 109 }; | 111 }; |
| 110 | 112 |
| 111 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. | 113 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. |
| 112 TEST_F(ImageLoadingTrackerTest, Cache) { | 114 TEST_F(ImageLoadingTrackerTest, Cache) { |
| 113 scoped_refptr<Extension> extension(CreateExtension( | 115 scoped_refptr<Extension> extension(CreateExtension( |
| 114 "image_loading_tracker", Extension::INVALID)); | 116 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 115 ASSERT_TRUE(extension.get() != NULL); | 117 ASSERT_TRUE(extension.get() != NULL); |
| 116 | 118 |
| 117 ExtensionResource image_resource = | 119 ExtensionResource image_resource = |
| 118 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, | 120 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
| 119 ExtensionIconSet::MATCH_EXACTLY); | 121 ExtensionIconSet::MATCH_EXACTLY); |
| 120 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 122 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 121 extension_misc::EXTENSION_ICON_SMALLISH); | 123 extension_misc::EXTENSION_ICON_SMALLISH); |
| 122 ImageLoadingTracker loader(this); | 124 ImageLoadingTracker loader(this); |
| 123 loader.LoadImage(extension.get(), | 125 loader.LoadImage(extension.get(), |
| 124 image_resource, | 126 image_resource, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 154 | 156 |
| 155 // Check that the image was loaded. | 157 // Check that the image was loaded. |
| 156 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 158 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 157 image_.ToSkBitmap()->width()); | 159 image_.ToSkBitmap()->width()); |
| 158 } | 160 } |
| 159 | 161 |
| 160 // Tests deleting an extension while waiting for the image to load doesn't cause | 162 // Tests deleting an extension while waiting for the image to load doesn't cause |
| 161 // problems. | 163 // problems. |
| 162 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { | 164 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { |
| 163 scoped_refptr<Extension> extension(CreateExtension( | 165 scoped_refptr<Extension> extension(CreateExtension( |
| 164 "image_loading_tracker", Extension::INVALID)); | 166 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 165 ASSERT_TRUE(extension.get() != NULL); | 167 ASSERT_TRUE(extension.get() != NULL); |
| 166 | 168 |
| 167 ExtensionResource image_resource = | 169 ExtensionResource image_resource = |
| 168 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, | 170 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
| 169 ExtensionIconSet::MATCH_EXACTLY); | 171 ExtensionIconSet::MATCH_EXACTLY); |
| 170 ImageLoadingTracker loader(this); | 172 ImageLoadingTracker loader(this); |
| 171 loader.LoadImage(extension.get(), | 173 loader.LoadImage(extension.get(), |
| 172 image_resource, | 174 image_resource, |
| 173 gfx::Size(extension_misc::EXTENSION_ICON_SMALLISH, | 175 gfx::Size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 174 extension_misc::EXTENSION_ICON_SMALLISH), | 176 extension_misc::EXTENSION_ICON_SMALLISH), |
| (...skipping 21 matching lines...) Expand all Loading... |
| 196 EXPECT_EQ(1, image_loaded_count()); | 198 EXPECT_EQ(1, image_loaded_count()); |
| 197 | 199 |
| 198 // Check that the image was loaded. | 200 // Check that the image was loaded. |
| 199 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 201 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 200 image_.ToSkBitmap()->width()); | 202 image_.ToSkBitmap()->width()); |
| 201 } | 203 } |
| 202 | 204 |
| 203 // Tests loading multiple dimensions of the same image. | 205 // Tests loading multiple dimensions of the same image. |
| 204 TEST_F(ImageLoadingTrackerTest, MultipleImages) { | 206 TEST_F(ImageLoadingTrackerTest, MultipleImages) { |
| 205 scoped_refptr<Extension> extension(CreateExtension( | 207 scoped_refptr<Extension> extension(CreateExtension( |
| 206 "image_loading_tracker", Extension::INVALID)); | 208 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
| 207 ASSERT_TRUE(extension.get() != NULL); | 209 ASSERT_TRUE(extension.get() != NULL); |
| 208 | 210 |
| 209 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | 211 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; |
| 210 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, | 212 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, |
| 211 extension_misc::EXTENSION_ICON_BITTY}; | 213 extension_misc::EXTENSION_ICON_BITTY}; |
| 212 for (size_t i = 0; i < arraysize(sizes); ++i) { | 214 for (size_t i = 0; i < arraysize(sizes); ++i) { |
| 213 ExtensionResource resource = | 215 ExtensionResource resource = |
| 214 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 216 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
| 215 info_list.push_back(ImageLoadingTracker::ImageRepresentation( | 217 info_list.push_back(ImageLoadingTracker::ImageRepresentation( |
| 216 resource, | 218 resource, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 237 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; | 239 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; |
| 238 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; | 240 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; |
| 239 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { | 241 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { |
| 240 std::swap(img_rep1, img_rep2); | 242 std::swap(img_rep1, img_rep2); |
| 241 } | 243 } |
| 242 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, | 244 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, |
| 243 img_rep1->pixel_width()); | 245 img_rep1->pixel_width()); |
| 244 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 246 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 245 img_rep2->pixel_width()); | 247 img_rep2->pixel_width()); |
| 246 } | 248 } |
| OLD | NEW |