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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 100 } |
102 | 101 |
103 int image_loaded_count_; | 102 int image_loaded_count_; |
104 bool quit_in_image_loaded_; | 103 bool quit_in_image_loaded_; |
105 MessageLoop ui_loop_; | 104 MessageLoop ui_loop_; |
106 content::TestBrowserThread ui_thread_; | 105 content::TestBrowserThread ui_thread_; |
107 content::TestBrowserThread file_thread_; | 106 content::TestBrowserThread file_thread_; |
108 content::TestBrowserThread io_thread_; | 107 content::TestBrowserThread io_thread_; |
109 }; | 108 }; |
110 | 109 |
111 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. | 110 // Tests loading a single image works. |
112 TEST_F(ImageLoadingTrackerTest, Cache) { | 111 TEST_F(ImageUtilsTest, LoadImageAsync) { |
113 scoped_refptr<Extension> extension(CreateExtension( | 112 scoped_refptr<Extension> extension(CreateExtension( |
114 "image_loading_tracker", Extension::INVALID)); | 113 "image_loading_tracker", Extension::INVALID)); |
115 ASSERT_TRUE(extension.get() != NULL); | 114 ASSERT_TRUE(extension.get() != NULL); |
116 | 115 |
117 ExtensionResource image_resource = | 116 ExtensionResource image_resource = |
118 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, | 117 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
119 ExtensionIconSet::MATCH_EXACTLY); | 118 ExtensionIconSet::MATCH_EXACTLY); |
120 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 119 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
121 extension_misc::EXTENSION_ICON_SMALLISH); | 120 extension_misc::EXTENSION_ICON_SMALLISH); |
122 ImageLoadingTracker loader(this); | 121 |
123 loader.LoadImage(extension.get(), | 122 image_utils::LoadImageAsync(extension.get(), image_resource, |
124 image_resource, | 123 max_size, |
125 max_size, | 124 base::Bind(&ImageUtilsTest::OnImageLoaded, |
126 ImageLoadingTracker::CACHE); | 125 base::Unretained(this))); |
127 | 126 |
128 // 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. |
129 EXPECT_EQ(0, image_loaded_count()); | 128 EXPECT_EQ(0, image_loaded_count()); |
130 | 129 |
131 WaitForImageLoad(); | 130 WaitForImageLoad(); |
132 | 131 |
133 // We should have gotten the image. | 132 // We should have gotten the image. |
134 EXPECT_EQ(1, image_loaded_count()); | 133 EXPECT_EQ(1, image_loaded_count()); |
135 | 134 |
136 // Check that the image was loaded. | 135 // Check that the image was loaded. |
137 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 136 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
138 image_.ToSkBitmap()->width()); | 137 image_.ToSkBitmap()->width()); |
139 | |
140 // The image should be cached in the Extension. | |
141 EXPECT_TRUE(extension->HasCachedImage(image_resource, max_size)); | |
142 | |
143 // Make sure the image is in the extension. | |
144 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | |
145 extension->GetCachedImage(image_resource, max_size).width()); | |
146 | |
147 // Ask the tracker for the image again, this should call us back immediately. | |
148 loader.LoadImage(extension.get(), | |
149 image_resource, | |
150 max_size, | |
151 ImageLoadingTracker::CACHE); | |
152 // We should have gotten the image. | |
153 EXPECT_EQ(1, image_loaded_count()); | |
154 | |
155 // Check that the image was loaded. | |
156 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | |
157 image_.ToSkBitmap()->width()); | |
158 } | 138 } |
159 | 139 |
160 // 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 |
161 // problems. | 141 // problems. |
162 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { | 142 TEST_F(ImageUtilsTest, DeleteExtensionWhileWaitingForCache) { |
163 scoped_refptr<Extension> extension(CreateExtension( | 143 scoped_refptr<Extension> extension(CreateExtension( |
164 "image_loading_tracker", Extension::INVALID)); | 144 "image_loading_tracker", Extension::INVALID)); |
165 ASSERT_TRUE(extension.get() != NULL); | 145 ASSERT_TRUE(extension.get() != NULL); |
166 | 146 |
167 ExtensionResource image_resource = | 147 ExtensionResource image_resource = |
168 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, | 148 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
169 ExtensionIconSet::MATCH_EXACTLY); | 149 ExtensionIconSet::MATCH_EXACTLY); |
170 ImageLoadingTracker loader(this); | 150 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
171 loader.LoadImage(extension.get(), | 151 extension_misc::EXTENSION_ICON_SMALLISH); |
172 image_resource, | 152 image_utils::LoadImageAsync(extension.get(), image_resource, max_size, |
173 gfx::Size(extension_misc::EXTENSION_ICON_SMALLISH, | 153 base::Bind(&ImageUtilsTest::OnImageLoaded, |
174 extension_misc::EXTENSION_ICON_SMALLISH), | 154 base::Unretained(this))); |
175 ImageLoadingTracker::CACHE); | |
176 | 155 |
177 // 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. |
178 EXPECT_EQ(0, image_loaded_count()); | 157 EXPECT_EQ(0, image_loaded_count()); |
179 | 158 |
180 // Send out notification the extension was uninstalled. | 159 // Send out notification the extension was uninstalled. |
181 extensions::UnloadedExtensionInfo details(extension.get(), | 160 extensions::UnloadedExtensionInfo details(extension.get(), |
182 extension_misc::UNLOAD_REASON_UNINSTALL); | 161 extension_misc::UNLOAD_REASON_UNINSTALL); |
183 content::NotificationService::current()->Notify( | 162 content::NotificationService::current()->Notify( |
184 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 163 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
185 content::NotificationService::AllSources(), | 164 content::NotificationService::AllSources(), |
186 content::Details<extensions::UnloadedExtensionInfo>(&details)); | 165 content::Details<extensions::UnloadedExtensionInfo>(&details)); |
187 | 166 |
188 // 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 |
189 // or get valgrind errors. | 168 // or get valgrind errors. |
190 extension = NULL; | 169 extension = NULL; |
191 | 170 |
192 WaitForImageLoad(); | 171 WaitForImageLoad(); |
193 | 172 |
194 // 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. |
195 // We should still have gotten the image. | 174 // We should still have gotten the image. |
196 EXPECT_EQ(1, image_loaded_count()); | 175 EXPECT_EQ(1, image_loaded_count()); |
197 | 176 |
198 // Check that the image was loaded. | 177 // Check that the image was loaded. |
199 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 178 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
200 image_.ToSkBitmap()->width()); | 179 image_.ToSkBitmap()->width()); |
201 } | 180 } |
202 | 181 |
203 // Tests loading multiple dimensions of the same image. | 182 // Tests loading multiple dimensions of the same image. |
204 TEST_F(ImageLoadingTrackerTest, MultipleImages) { | 183 TEST_F(ImageUtilsTest, MultipleImages) { |
205 scoped_refptr<Extension> extension(CreateExtension( | 184 scoped_refptr<Extension> extension(CreateExtension( |
206 "image_loading_tracker", Extension::INVALID)); | 185 "image_loading_tracker", Extension::INVALID)); |
207 ASSERT_TRUE(extension.get() != NULL); | 186 ASSERT_TRUE(extension.get() != NULL); |
208 | 187 |
209 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | 188 std::vector<image_utils::ImageRepresentation> info_list; |
210 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, | 189 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, |
211 extension_misc::EXTENSION_ICON_BITTY}; | 190 extension_misc::EXTENSION_ICON_BITTY}; |
212 for (size_t i = 0; i < arraysize(sizes); ++i) { | 191 for (size_t i = 0; i < arraysize(sizes); ++i) { |
213 ExtensionResource resource = | 192 ExtensionResource resource = |
214 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 193 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
215 info_list.push_back(ImageLoadingTracker::ImageRepresentation( | 194 info_list.push_back(image_utils::ImageRepresentation( |
216 resource, | 195 resource, |
217 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, | 196 image_utils::ImageRepresentation::RESIZE_WHEN_LARGER, |
218 gfx::Size(sizes[i], sizes[i]), | 197 gfx::Size(sizes[i], sizes[i]), |
219 ui::SCALE_FACTOR_NONE)); | 198 ui::SCALE_FACTOR_NONE)); |
220 } | 199 } |
221 | 200 |
222 ImageLoadingTracker loader(this); | 201 image_utils::LoadImagesAsync(extension.get(), info_list, |
223 loader.LoadImages(extension.get(), info_list, ImageLoadingTracker::CACHE); | 202 base::Bind(&ImageUtilsTest::OnImageLoaded, |
| 203 base::Unretained(this))); |
224 | 204 |
225 // 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. |
226 EXPECT_EQ(0, image_loaded_count()); | 206 EXPECT_EQ(0, image_loaded_count()); |
227 | 207 |
228 WaitForImageLoad(); | 208 WaitForImageLoad(); |
229 | 209 |
230 // We should have gotten the image. | 210 // We should have gotten the image. |
231 EXPECT_EQ(1, image_loaded_count()); | 211 EXPECT_EQ(1, image_loaded_count()); |
232 | 212 |
233 // Check that all images were loaded. | 213 // Check that all images were loaded. |
234 std::vector<gfx::ImageSkiaRep> image_reps = | 214 std::vector<gfx::ImageSkiaRep> image_reps = |
235 image_.ToImageSkia()->image_reps(); | 215 image_.ToImageSkia()->image_reps(); |
236 ASSERT_EQ(2u, image_reps.size()); | 216 ASSERT_EQ(2u, image_reps.size()); |
237 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; | 217 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; |
238 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; | 218 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; |
239 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { | 219 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { |
240 std::swap(img_rep1, img_rep2); | 220 std::swap(img_rep1, img_rep2); |
241 } | 221 } |
242 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, | 222 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, |
243 img_rep1->pixel_width()); | 223 img_rep1->pixel_width()); |
244 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, | 224 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
245 img_rep2->pixel_width()); | 225 img_rep2->pixel_width()); |
246 } | 226 } |
247 | 227 |
248 // Tests IsComponentExtensionResource function. | 228 // Tests IsComponentExtensionResource function. |
249 TEST_F(ImageLoadingTrackerTest, IsComponentExtensionResource) { | 229 TEST_F(ImageUtilsTest, IsComponentExtensionResource) { |
250 scoped_refptr<Extension> extension(CreateExtension( | 230 scoped_refptr<Extension> extension(CreateExtension( |
251 "file_manager", Extension::COMPONENT)); | 231 "file_manager", Extension::COMPONENT)); |
252 ASSERT_TRUE(extension.get() != NULL); | 232 ASSERT_TRUE(extension.get() != NULL); |
253 | 233 |
254 ExtensionResource resource = | 234 ExtensionResource resource = |
255 extension->GetIconResource(extension_misc::EXTENSION_ICON_BITTY, | 235 extension->GetIconResource(extension_misc::EXTENSION_ICON_BITTY, |
256 ExtensionIconSet::MATCH_EXACTLY); | 236 ExtensionIconSet::MATCH_EXACTLY); |
257 | 237 |
258 #if defined(FILE_MANAGER_EXTENSION) | 238 #if defined(FILE_MANAGER_EXTENSION) |
259 ImageLoadingTracker loader(this); | |
260 int resource_id; | 239 int resource_id; |
261 ASSERT_EQ(true, | 240 ASSERT_EQ(true, |
262 loader.IsComponentExtensionResource(extension.get(), | 241 image_utils::IsComponentExtensionResource(extension.get(), |
263 resource.relative_path(), | 242 resource.relative_path(), |
264 &resource_id)); | 243 &resource_id)); |
265 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | 244 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); |
266 #endif | 245 #endif |
267 } | 246 } |
OLD | NEW |