Chromium Code Reviews| 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_utils.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 "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
| 18 #include "grit/component_extension_resources.h" | 18 #include "grit/component_extension_resources.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
| 22 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
| 23 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
| 24 | 24 |
| 25 using content::BrowserThread; | 25 using content::BrowserThread; |
| 26 using extensions::Extension; | 26 using extensions::Extension; |
| 27 | 27 |
| 28 class ImageLoadingTrackerTest : public testing::Test, | 28 namespace image_utils = extension_image_utils; |
| 29 public ImageLoadingTracker::Observer { | 29 |
| 30 class ImageUtilsTest : public testing::Test { | |
| 30 public: | 31 public: |
| 31 ImageLoadingTrackerTest() | 32 ImageUtilsTest() |
| 32 : image_loaded_count_(0), | 33 : image_loaded_count_(0), |
| 33 quit_in_image_loaded_(false), | 34 quit_in_image_loaded_(false), |
| 34 ui_thread_(BrowserThread::UI, &ui_loop_), | 35 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 35 file_thread_(BrowserThread::FILE), | 36 file_thread_(BrowserThread::FILE), |
| 36 io_thread_(BrowserThread::IO) { | 37 io_thread_(BrowserThread::IO) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 virtual void OnImageLoaded(const gfx::Image& image, | 40 void OnImageLoaded(const gfx::Image& image) { |
| 40 const std::string& extension_id, | |
| 41 int index) OVERRIDE { | |
| 42 image_loaded_count_++; | 41 image_loaded_count_++; |
| 43 if (quit_in_image_loaded_) | 42 if (quit_in_image_loaded_) |
| 44 MessageLoop::current()->Quit(); | 43 MessageLoop::current()->Quit(); |
| 45 image_ = image; | 44 image_ = image; |
| 46 } | 45 } |
| 47 | 46 |
| 48 void WaitForImageLoad() { | 47 void WaitForImageLoad() { |
| 49 quit_in_image_loaded_ = true; | 48 quit_in_image_loaded_ = true; |
| 50 MessageLoop::current()->Run(); | 49 MessageLoop::current()->Run(); |
| 51 quit_in_image_loaded_ = false; | 50 quit_in_image_loaded_ = false; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 } | 93 } |
| 95 | 94 |
| 96 int image_loaded_count_; | 95 int image_loaded_count_; |
| 97 bool quit_in_image_loaded_; | 96 bool quit_in_image_loaded_; |
| 98 MessageLoop ui_loop_; | 97 MessageLoop ui_loop_; |
| 99 content::TestBrowserThread ui_thread_; | 98 content::TestBrowserThread ui_thread_; |
| 100 content::TestBrowserThread file_thread_; | 99 content::TestBrowserThread file_thread_; |
| 101 content::TestBrowserThread io_thread_; | 100 content::TestBrowserThread io_thread_; |
| 102 }; | 101 }; |
| 103 | 102 |
| 104 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. | 103 |
| 105 TEST_F(ImageLoadingTrackerTest, Cache) { | 104 namespace { |
| 105 | |
| 106 | |
| 107 }; | |
|
Finnur
2012/10/05 14:41:33
You writing code in invisible ink now? :)
Marijn Kruisselbrink
2012/10/05 18:00:08
Done.
| |
| 108 | |
| 109 // Tests asking extension_image_utils to cache pushes the result to the | |
| 110 // Extension. | |
| 111 TEST_F(ImageUtilsTest, LoadImageAsync) { | |
| 106 scoped_refptr<Extension> extension(CreateExtension( | 112 scoped_refptr<Extension> extension(CreateExtension( |
| 107 "image_loading_tracker", Extension::INVALID)); | 113 "image_loading_tracker", Extension::INVALID)); |
| 108 ASSERT_TRUE(extension.get() != NULL); | 114 ASSERT_TRUE(extension.get() != NULL); |
| 109 | 115 |
| 110 ExtensionResource image_resource = | 116 ExtensionResource image_resource = |
| 111 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, | 117 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
| 112 ExtensionIconSet::MATCH_EXACTLY); | 118 ExtensionIconSet::MATCH_EXACTLY); |
| 113 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 119 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 114 extension_misc::EXTENSION_ICON_SMALLISH); | 120 extension_misc::EXTENSION_ICON_SMALLISH); |
| 115 ImageLoadingTracker loader(this); | 121 |
| 116 loader.LoadImage(extension.get(), | 122 image_utils::LoadImageAsync(extension.get(), image_resource, |
| 117 image_resource, | 123 max_size, |
| 118 max_size, | 124 base::Bind(&ImageUtilsTest::OnImageLoaded, |
| 119 ImageLoadingTracker::CACHE); | 125 base::Unretained(this))); |
| 120 | 126 |
| 121 // The image isn't cached, so we should not have received notification. | 127 // The image isn't cached, so we should not have received notification. |
| 122 EXPECT_EQ(0, image_loaded_count()); | 128 EXPECT_EQ(0, image_loaded_count()); |
| 123 | 129 |
| 124 WaitForImageLoad(); | 130 WaitForImageLoad(); |
| 125 | 131 |
| 126 // We should have gotten the image. | 132 // We should have gotten the image. |
| 127 EXPECT_EQ(1, image_loaded_count()); | 133 EXPECT_EQ(1, image_loaded_count()); |
| 128 | 134 |
| 129 // Check that the image was loaded. | 135 // Check that the image was loaded. |
| 130 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 136 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 131 image_.ToSkBitmap()->width()); | 137 image_.ToSkBitmap()->width()); |
| 132 | |
| 133 // The image should be cached in the Extension. | |
| 134 EXPECT_TRUE(extension->HasCachedImage(image_resource, max_size)); | |
| 135 | |
| 136 // Make sure the image is in the extension. | |
| 137 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | |
| 138 extension->GetCachedImage(image_resource, max_size).width()); | |
| 139 | |
| 140 // Ask the tracker for the image again, this should call us back immediately. | |
| 141 loader.LoadImage(extension.get(), | |
| 142 image_resource, | |
| 143 max_size, | |
| 144 ImageLoadingTracker::CACHE); | |
| 145 // We should have gotten the image. | |
| 146 EXPECT_EQ(1, image_loaded_count()); | |
|
Finnur
2012/10/05 14:41:33
Why remove this?
Marijn Kruisselbrink
2012/10/05 18:00:08
Because at the moment extension_image_utils doesn'
| |
| 147 | |
| 148 // Check that the image was loaded. | |
| 149 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | |
| 150 image_.ToSkBitmap()->width()); | |
| 151 } | 138 } |
| 152 | 139 |
| 153 // Tests deleting an extension while waiting for the image to load doesn't cause | 140 // Tests deleting an extension while waiting for the image to load doesn't cause |
| 154 // problems. | 141 // problems. |
| 155 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { | 142 TEST_F(ImageUtilsTest, DeleteExtensionWhileWaitingForCache) { |
| 156 scoped_refptr<Extension> extension(CreateExtension( | 143 scoped_refptr<Extension> extension(CreateExtension( |
| 157 "image_loading_tracker", Extension::INVALID)); | 144 "image_loading_tracker", Extension::INVALID)); |
| 158 ASSERT_TRUE(extension.get() != NULL); | 145 ASSERT_TRUE(extension.get() != NULL); |
| 159 | 146 |
| 160 ExtensionResource image_resource = | 147 ExtensionResource image_resource = |
| 161 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, | 148 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
| 162 ExtensionIconSet::MATCH_EXACTLY); | 149 ExtensionIconSet::MATCH_EXACTLY); |
| 163 ImageLoadingTracker loader(this); | 150 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
| 164 loader.LoadImage(extension.get(), | 151 extension_misc::EXTENSION_ICON_SMALLISH); |
| 165 image_resource, | 152 image_utils::LoadImageAsync(extension.get(), image_resource, max_size, |
| 166 gfx::Size(extension_misc::EXTENSION_ICON_SMALLISH, | 153 base::Bind(&ImageUtilsTest::OnImageLoaded, |
| 167 extension_misc::EXTENSION_ICON_SMALLISH), | 154 base::Unretained(this))); |
| 168 ImageLoadingTracker::CACHE); | |
| 169 | 155 |
| 170 // The image isn't cached, so we should not have received notification. | 156 // The image isn't cached, so we should not have received notification. |
| 171 EXPECT_EQ(0, image_loaded_count()); | 157 EXPECT_EQ(0, image_loaded_count()); |
| 172 | 158 |
| 173 // Send out notification the extension was uninstalled. | 159 // Send out notification the extension was uninstalled. |
| 174 extensions::UnloadedExtensionInfo details(extension.get(), | 160 extensions::UnloadedExtensionInfo details(extension.get(), |
| 175 extension_misc::UNLOAD_REASON_UNINSTALL); | 161 extension_misc::UNLOAD_REASON_UNINSTALL); |
| 176 content::NotificationService::current()->Notify( | 162 content::NotificationService::current()->Notify( |
| 177 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 163 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 178 content::NotificationService::AllSources(), | 164 content::NotificationService::AllSources(), |
| 179 content::Details<extensions::UnloadedExtensionInfo>(&details)); | 165 content::Details<extensions::UnloadedExtensionInfo>(&details)); |
| 180 | 166 |
| 181 // Chuck the extension, that way if anyone tries to access it we should crash | 167 // Chuck the extension, that way if anyone tries to access it we should crash |
| 182 // or get valgrind errors. | 168 // or get valgrind errors. |
| 183 extension = NULL; | 169 extension = NULL; |
| 184 | 170 |
| 185 WaitForImageLoad(); | 171 WaitForImageLoad(); |
| 186 | 172 |
| 187 // Even though we deleted the extension, we should still get the image. | 173 // Even though we deleted the extension, we should still get the image. |
| 188 // We should still have gotten the image. | 174 // We should still have gotten the image. |
| 189 EXPECT_EQ(1, image_loaded_count()); | 175 EXPECT_EQ(1, image_loaded_count()); |
| 190 | 176 |
| 191 // Check that the image was loaded. | 177 // Check that the image was loaded. |
| 192 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 178 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 193 image_.ToSkBitmap()->width()); | 179 image_.ToSkBitmap()->width()); |
| 194 } | 180 } |
| 195 | 181 |
| 196 // Tests loading multiple dimensions of the same image. | 182 // Tests loading multiple dimensions of the same image. |
| 197 TEST_F(ImageLoadingTrackerTest, MultipleImages) { | 183 TEST_F(ImageUtilsTest, MultipleImages) { |
| 198 scoped_refptr<Extension> extension(CreateExtension( | 184 scoped_refptr<Extension> extension(CreateExtension( |
| 199 "image_loading_tracker", Extension::INVALID)); | 185 "image_loading_tracker", Extension::INVALID)); |
| 200 ASSERT_TRUE(extension.get() != NULL); | 186 ASSERT_TRUE(extension.get() != NULL); |
| 201 | 187 |
| 202 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | 188 std::vector<image_utils::ImageRepresentation> info_list; |
| 203 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, | 189 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, |
| 204 extension_misc::EXTENSION_ICON_BITTY}; | 190 extension_misc::EXTENSION_ICON_BITTY}; |
| 205 for (size_t i = 0; i < arraysize(sizes); ++i) { | 191 for (size_t i = 0; i < arraysize(sizes); ++i) { |
| 206 ExtensionResource resource = | 192 ExtensionResource resource = |
| 207 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 193 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
| 208 info_list.push_back(ImageLoadingTracker::ImageRepresentation( | 194 info_list.push_back(image_utils::ImageRepresentation( |
| 209 resource, | 195 resource, |
| 210 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, | 196 image_utils::ImageRepresentation::RESIZE_WHEN_LARGER, |
| 211 gfx::Size(sizes[i], sizes[i]), | 197 gfx::Size(sizes[i], sizes[i]), |
| 212 ui::SCALE_FACTOR_NONE)); | 198 ui::SCALE_FACTOR_NONE)); |
| 213 } | 199 } |
| 214 | 200 |
| 215 ImageLoadingTracker loader(this); | 201 image_utils::LoadImagesAsync(extension.get(), info_list, |
| 216 loader.LoadImages(extension.get(), info_list, ImageLoadingTracker::CACHE); | 202 base::Bind(&ImageUtilsTest::OnImageLoaded, |
| 203 base::Unretained(this))); | |
| 217 | 204 |
| 218 // The image isn't cached, so we should not have received notification. | 205 // The image isn't cached, so we should not have received notification. |
| 219 EXPECT_EQ(0, image_loaded_count()); | 206 EXPECT_EQ(0, image_loaded_count()); |
| 220 | 207 |
| 221 WaitForImageLoad(); | 208 WaitForImageLoad(); |
| 222 | 209 |
| 223 // We should have gotten the image. | 210 // We should have gotten the image. |
| 224 EXPECT_EQ(1, image_loaded_count()); | 211 EXPECT_EQ(1, image_loaded_count()); |
| 225 | 212 |
| 226 // Check that all images were loaded. | 213 // Check that all images were loaded. |
| 227 std::vector<gfx::ImageSkiaRep> image_reps = | 214 std::vector<gfx::ImageSkiaRep> image_reps = |
| 228 image_.ToImageSkia()->image_reps(); | 215 image_.ToImageSkia()->image_reps(); |
| 229 ASSERT_EQ(2u, image_reps.size()); | 216 ASSERT_EQ(2u, image_reps.size()); |
| 230 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; | 217 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; |
| 231 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; | 218 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; |
| 232 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { | 219 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { |
| 233 std::swap(img_rep1, img_rep2); | 220 std::swap(img_rep1, img_rep2); |
| 234 } | 221 } |
| 235 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, | 222 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, |
| 236 img_rep1->pixel_width()); | 223 img_rep1->pixel_width()); |
| 237 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 224 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
| 238 img_rep2->pixel_width()); | 225 img_rep2->pixel_width()); |
| 239 } | 226 } |
| 240 | |
| 241 // Tests IsComponentExtensionResource function. | |
| 242 TEST_F(ImageLoadingTrackerTest, IsComponentExtensionResource) { | |
| 243 scoped_refptr<Extension> extension(CreateExtension( | |
| 244 "file_manager", Extension::COMPONENT)); | |
| 245 ASSERT_TRUE(extension.get() != NULL); | |
| 246 | |
| 247 ExtensionResource resource = | |
| 248 extension->GetIconResource(extension_misc::EXTENSION_ICON_BITTY, | |
| 249 ExtensionIconSet::MATCH_EXACTLY); | |
| 250 | |
| 251 #if defined(FILE_MANAGER_EXTENSION) | |
| 252 ImageLoadingTracker loader(this); | |
| 253 int resource_id; | |
| 254 ASSERT_EQ(true, | |
| 255 loader.IsComponentExtensionResource(extension.get(), | |
| 256 resource.relative_path(), | |
| 257 &resource_id)); | |
| 258 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | |
| 259 #endif | |
| 260 } | |
|
Finnur
2012/10/05 14:41:33
Same question here...
| |
| OLD | NEW |