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), |
37 ui_thread_(BrowserThread::UI, &ui_loop_), | 38 ui_thread_(BrowserThread::UI, &ui_loop_), |
38 file_thread_(BrowserThread::FILE), | 39 file_thread_(BrowserThread::FILE), |
39 io_thread_(BrowserThread::IO) { | 40 io_thread_(BrowserThread::IO) { |
40 } | 41 } |
41 | 42 |
42 void OnImageLoaded(const gfx::Image& image) { | 43 void OnImageLoaded(const gfx::Image& image) { |
43 image_loaded_count_++; | 44 image_loaded_count_++; |
44 base::MessageLoop::current()->Quit(); | 45 if (quit_in_image_loaded_) |
| 46 base::MessageLoop::current()->Quit(); |
45 image_ = image; | 47 image_ = image; |
46 } | 48 } |
47 | 49 |
48 void OnImageURLLoaded(const GURL& url) { | |
49 image_loaded_count_++; | |
50 base::MessageLoop::current()->Quit(); | |
51 url_ = url; | |
52 } | |
53 | |
54 void WaitForImageLoad() { | 50 void WaitForImageLoad() { |
| 51 quit_in_image_loaded_ = true; |
55 base::MessageLoop::current()->Run(); | 52 base::MessageLoop::current()->Run(); |
| 53 quit_in_image_loaded_ = false; |
56 } | 54 } |
57 | 55 |
58 int image_loaded_count() { | 56 int image_loaded_count() { |
59 int result = image_loaded_count_; | 57 int result = image_loaded_count_; |
60 image_loaded_count_ = 0; | 58 image_loaded_count_ = 0; |
61 return result; | 59 return result; |
62 } | 60 } |
63 | 61 |
64 scoped_refptr<Extension> CreateExtension(const char* name, | 62 scoped_refptr<Extension> CreateExtension(const char* name, |
65 Manifest::Location location) { | 63 Manifest::Location location) { |
(...skipping 23 matching lines...) Expand all Loading... |
89 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) { | 87 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) { |
90 EXPECT_FALSE(true); | 88 EXPECT_FALSE(true); |
91 return NULL; | 89 return NULL; |
92 } | 90 } |
93 test_file = test_file.AppendASCII(name); | 91 test_file = test_file.AppendASCII(name); |
94 } | 92 } |
95 return Extension::Create(test_file, location, *valid_value, | 93 return Extension::Create(test_file, location, *valid_value, |
96 Extension::NO_FLAGS, &error); | 94 Extension::NO_FLAGS, &error); |
97 } | 95 } |
98 | 96 |
99 // Holds the image loaded by the test. | |
100 gfx::Image image_; | 97 gfx::Image image_; |
101 // Holds the data url retrieved by the test. | |
102 GURL url_; | |
103 | 98 |
104 private: | 99 private: |
105 virtual void SetUp() OVERRIDE { | 100 virtual void SetUp() OVERRIDE { |
106 testing::Test::SetUp(); | 101 testing::Test::SetUp(); |
107 file_thread_.Start(); | 102 file_thread_.Start(); |
108 io_thread_.Start(); | 103 io_thread_.Start(); |
109 } | 104 } |
110 | 105 |
111 int image_loaded_count_; | 106 int image_loaded_count_; |
| 107 bool quit_in_image_loaded_; |
112 base::MessageLoop ui_loop_; | 108 base::MessageLoop ui_loop_; |
113 content::TestBrowserThread ui_thread_; | 109 content::TestBrowserThread ui_thread_; |
114 content::TestBrowserThread file_thread_; | 110 content::TestBrowserThread file_thread_; |
115 content::TestBrowserThread io_thread_; | 111 content::TestBrowserThread io_thread_; |
116 }; | 112 }; |
117 | 113 |
118 // Tests loading an image works correctly. | 114 // Tests loading an image works correctly. |
119 TEST_F(ImageLoaderTest, LoadImage) { | 115 TEST_F(ImageLoaderTest, LoadImage) { |
120 scoped_refptr<Extension> extension(CreateExtension( | 116 scoped_refptr<Extension> extension(CreateExtension( |
121 "image_loading_tracker", Manifest::INVALID_LOCATION)); | 117 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
122 ASSERT_TRUE(extension.get() != NULL); | 118 ASSERT_TRUE(extension.get() != NULL); |
123 | 119 |
124 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( | 120 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( |
125 extension.get(), | 121 extension.get(), |
126 extension_misc::EXTENSION_ICON_SMALLISH, | 122 extension_misc::EXTENSION_ICON_SMALLISH, |
127 ExtensionIconSet::MATCH_EXACTLY); | 123 ExtensionIconSet::MATCH_EXACTLY); |
128 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 124 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
129 extension_misc::EXTENSION_ICON_SMALLISH); | 125 extension_misc::EXTENSION_ICON_SMALLISH); |
130 ImageLoader loader(NULL); | 126 ImageLoader loader; |
131 loader.LoadImageAsync(extension.get(), | 127 loader.LoadImageAsync(extension.get(), |
132 image_resource, | 128 image_resource, |
133 max_size, | 129 max_size, |
134 base::Bind(&ImageLoaderTest::OnImageLoaded, | 130 base::Bind(&ImageLoaderTest::OnImageLoaded, |
135 base::Unretained(this))); | 131 base::Unretained(this))); |
136 | 132 |
137 // The image isn't cached, so we should not have received notification. | 133 // The image isn't cached, so we should not have received notification. |
138 EXPECT_EQ(0, image_loaded_count()); | 134 EXPECT_EQ(0, image_loaded_count()); |
139 | 135 |
140 WaitForImageLoad(); | 136 WaitForImageLoad(); |
(...skipping 12 matching lines...) Expand all Loading... |
153 scoped_refptr<Extension> extension(CreateExtension( | 149 scoped_refptr<Extension> extension(CreateExtension( |
154 "image_loading_tracker", Manifest::INVALID_LOCATION)); | 150 "image_loading_tracker", Manifest::INVALID_LOCATION)); |
155 ASSERT_TRUE(extension.get() != NULL); | 151 ASSERT_TRUE(extension.get() != NULL); |
156 | 152 |
157 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( | 153 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( |
158 extension.get(), | 154 extension.get(), |
159 extension_misc::EXTENSION_ICON_SMALLISH, | 155 extension_misc::EXTENSION_ICON_SMALLISH, |
160 ExtensionIconSet::MATCH_EXACTLY); | 156 ExtensionIconSet::MATCH_EXACTLY); |
161 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, | 157 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
162 extension_misc::EXTENSION_ICON_SMALLISH); | 158 extension_misc::EXTENSION_ICON_SMALLISH); |
163 ImageLoader loader(NULL); | 159 ImageLoader loader; |
164 std::set<int> sizes; | 160 std::set<int> sizes; |
165 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); | 161 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); |
166 loader.LoadImageAsync(extension.get(), | 162 loader.LoadImageAsync(extension.get(), |
167 image_resource, | 163 image_resource, |
168 max_size, | 164 max_size, |
169 base::Bind(&ImageLoaderTest::OnImageLoaded, | 165 base::Bind(&ImageLoaderTest::OnImageLoaded, |
170 base::Unretained(this))); | 166 base::Unretained(this))); |
171 | 167 |
172 // The image isn't cached, so we should not have received notification. | 168 // The image isn't cached, so we should not have received notification. |
173 EXPECT_EQ(0, image_loaded_count()); | 169 EXPECT_EQ(0, image_loaded_count()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 for (size_t i = 0; i < arraysize(sizes); ++i) { | 203 for (size_t i = 0; i < arraysize(sizes); ++i) { |
208 ExtensionResource resource = extensions::IconsInfo::GetIconResource( | 204 ExtensionResource resource = extensions::IconsInfo::GetIconResource( |
209 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 205 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
210 info_list.push_back(ImageLoader::ImageRepresentation( | 206 info_list.push_back(ImageLoader::ImageRepresentation( |
211 resource, | 207 resource, |
212 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, | 208 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, |
213 gfx::Size(sizes[i], sizes[i]), | 209 gfx::Size(sizes[i], sizes[i]), |
214 ui::SCALE_FACTOR_NONE)); | 210 ui::SCALE_FACTOR_NONE)); |
215 } | 211 } |
216 | 212 |
217 ImageLoader loader(NULL); | 213 ImageLoader loader; |
218 loader.LoadImagesAsync(extension.get(), info_list, | 214 loader.LoadImagesAsync(extension.get(), info_list, |
219 base::Bind(&ImageLoaderTest::OnImageLoaded, | 215 base::Bind(&ImageLoaderTest::OnImageLoaded, |
220 base::Unretained(this))); | 216 base::Unretained(this))); |
221 | 217 |
222 // The image isn't cached, so we should not have received notification. | 218 // The image isn't cached, so we should not have received notification. |
223 EXPECT_EQ(0, image_loaded_count()); | 219 EXPECT_EQ(0, image_loaded_count()); |
224 | 220 |
225 WaitForImageLoad(); | 221 WaitForImageLoad(); |
226 | 222 |
227 // We should have gotten the image. | 223 // We should have gotten the image. |
(...skipping 27 matching lines...) Expand all Loading... |
255 | 251 |
256 #if defined(FILE_MANAGER_EXTENSION) | 252 #if defined(FILE_MANAGER_EXTENSION) |
257 int resource_id; | 253 int resource_id; |
258 ASSERT_EQ(true, | 254 ASSERT_EQ(true, |
259 ImageLoader::IsComponentExtensionResource(extension->path(), | 255 ImageLoader::IsComponentExtensionResource(extension->path(), |
260 resource.relative_path(), | 256 resource.relative_path(), |
261 &resource_id)); | 257 &resource_id)); |
262 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | 258 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); |
263 #endif | 259 #endif |
264 } | 260 } |
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 |