Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(425)

Side by Side Diff: chrome/browser/extensions/image_loader_unittest.cc

Issue 25050005: Refactored loading of applications / extensions icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 29 matching lines...) Expand all
40 io_thread_(BrowserThread::IO) { 40 io_thread_(BrowserThread::IO) {
41 } 41 }
42 42
43 void OnImageLoaded(const gfx::Image& image) { 43 void OnImageLoaded(const gfx::Image& image) {
44 image_loaded_count_++; 44 image_loaded_count_++;
45 if (quit_in_image_loaded_) 45 if (quit_in_image_loaded_)
46 base::MessageLoop::current()->Quit(); 46 base::MessageLoop::current()->Quit();
47 image_ = image; 47 image_ = image;
48 } 48 }
49 49
50 void OnImageURLLoaded(const GURL& url) {
51 image_loaded_count_++;
52 if (quit_in_image_loaded_)
53 base::MessageLoop::current()->Quit();
54 url_ = url;
55 }
56
50 void WaitForImageLoad() { 57 void WaitForImageLoad() {
51 quit_in_image_loaded_ = true; 58 quit_in_image_loaded_ = true;
52 base::MessageLoop::current()->Run(); 59 base::MessageLoop::current()->Run();
53 quit_in_image_loaded_ = false; 60 quit_in_image_loaded_ = false;
Finnur 2013/10/02 09:52:15 Do you know why it is necessary to set this to fal
dvh-g 2013/10/02 15:08:13 I don't think that the case where the it's really
54 } 61 }
55 62
56 int image_loaded_count() { 63 int image_loaded_count() {
57 int result = image_loaded_count_; 64 int result = image_loaded_count_;
58 image_loaded_count_ = 0; 65 image_loaded_count_ = 0;
59 return result; 66 return result;
60 } 67 }
61 68
62 scoped_refptr<Extension> CreateExtension(const char* name, 69 scoped_refptr<Extension> CreateExtension(const char* name,
63 Manifest::Location location) { 70 Manifest::Location location) {
(...skipping 24 matching lines...) Expand all
88 EXPECT_FALSE(true); 95 EXPECT_FALSE(true);
89 return NULL; 96 return NULL;
90 } 97 }
91 test_file = test_file.AppendASCII(name); 98 test_file = test_file.AppendASCII(name);
92 } 99 }
93 return Extension::Create(test_file, location, *valid_value, 100 return Extension::Create(test_file, location, *valid_value,
94 Extension::NO_FLAGS, &error); 101 Extension::NO_FLAGS, &error);
95 } 102 }
96 103
97 gfx::Image image_; 104 gfx::Image image_;
105 GURL url_;
Finnur 2013/10/02 09:52:15 Document: // Holds the image loaded by the test. a
dvh-g 2013/10/02 15:08:13 Done.
dvh-g 2013/10/02 15:08:13 Done.
98 106
99 private: 107 private:
100 virtual void SetUp() OVERRIDE { 108 virtual void SetUp() OVERRIDE {
101 testing::Test::SetUp(); 109 testing::Test::SetUp();
102 file_thread_.Start(); 110 file_thread_.Start();
103 io_thread_.Start(); 111 io_thread_.Start();
104 } 112 }
105 113
106 int image_loaded_count_; 114 int image_loaded_count_;
107 bool quit_in_image_loaded_; 115 bool quit_in_image_loaded_;
Finnur 2013/10/02 09:52:15 Please rename: quit_when_image_loaded_.
dvh-g 2013/10/02 15:08:13 Done.
108 base::MessageLoop ui_loop_; 116 base::MessageLoop ui_loop_;
109 content::TestBrowserThread ui_thread_; 117 content::TestBrowserThread ui_thread_;
110 content::TestBrowserThread file_thread_; 118 content::TestBrowserThread file_thread_;
111 content::TestBrowserThread io_thread_; 119 content::TestBrowserThread io_thread_;
112 }; 120 };
113 121
114 // Tests loading an image works correctly. 122 // Tests loading an image works correctly.
115 TEST_F(ImageLoaderTest, LoadImage) { 123 TEST_F(ImageLoaderTest, LoadImage) {
116 scoped_refptr<Extension> extension(CreateExtension( 124 scoped_refptr<Extension> extension(CreateExtension(
117 "image_loading_tracker", Manifest::INVALID_LOCATION)); 125 "image_loading_tracker", Manifest::INVALID_LOCATION));
118 ASSERT_TRUE(extension.get() != NULL); 126 ASSERT_TRUE(extension.get() != NULL);
119 127
120 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( 128 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource(
121 extension.get(), 129 extension.get(),
122 extension_misc::EXTENSION_ICON_SMALLISH, 130 extension_misc::EXTENSION_ICON_SMALLISH,
123 ExtensionIconSet::MATCH_EXACTLY); 131 ExtensionIconSet::MATCH_EXACTLY);
124 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, 132 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
125 extension_misc::EXTENSION_ICON_SMALLISH); 133 extension_misc::EXTENSION_ICON_SMALLISH);
126 ImageLoader loader; 134 ImageLoader loader(NULL);
127 loader.LoadImageAsync(extension.get(), 135 loader.LoadImageAsync(extension.get(),
128 image_resource, 136 image_resource,
129 max_size, 137 max_size,
130 base::Bind(&ImageLoaderTest::OnImageLoaded, 138 base::Bind(&ImageLoaderTest::OnImageLoaded,
131 base::Unretained(this))); 139 base::Unretained(this)));
132 140
133 // The image isn't cached, so we should not have received notification. 141 // The image isn't cached, so we should not have received notification.
134 EXPECT_EQ(0, image_loaded_count()); 142 EXPECT_EQ(0, image_loaded_count());
135 143
136 WaitForImageLoad(); 144 WaitForImageLoad();
(...skipping 12 matching lines...) Expand all
149 scoped_refptr<Extension> extension(CreateExtension( 157 scoped_refptr<Extension> extension(CreateExtension(
150 "image_loading_tracker", Manifest::INVALID_LOCATION)); 158 "image_loading_tracker", Manifest::INVALID_LOCATION));
151 ASSERT_TRUE(extension.get() != NULL); 159 ASSERT_TRUE(extension.get() != NULL);
152 160
153 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( 161 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource(
154 extension.get(), 162 extension.get(),
155 extension_misc::EXTENSION_ICON_SMALLISH, 163 extension_misc::EXTENSION_ICON_SMALLISH,
156 ExtensionIconSet::MATCH_EXACTLY); 164 ExtensionIconSet::MATCH_EXACTLY);
157 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, 165 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
158 extension_misc::EXTENSION_ICON_SMALLISH); 166 extension_misc::EXTENSION_ICON_SMALLISH);
159 ImageLoader loader; 167 ImageLoader loader(NULL);
160 std::set<int> sizes; 168 std::set<int> sizes;
161 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH); 169 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH);
162 loader.LoadImageAsync(extension.get(), 170 loader.LoadImageAsync(extension.get(),
163 image_resource, 171 image_resource,
164 max_size, 172 max_size,
165 base::Bind(&ImageLoaderTest::OnImageLoaded, 173 base::Bind(&ImageLoaderTest::OnImageLoaded,
166 base::Unretained(this))); 174 base::Unretained(this)));
167 175
168 // The image isn't cached, so we should not have received notification. 176 // The image isn't cached, so we should not have received notification.
169 EXPECT_EQ(0, image_loaded_count()); 177 EXPECT_EQ(0, image_loaded_count());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 for (size_t i = 0; i < arraysize(sizes); ++i) { 211 for (size_t i = 0; i < arraysize(sizes); ++i) {
204 ExtensionResource resource = extensions::IconsInfo::GetIconResource( 212 ExtensionResource resource = extensions::IconsInfo::GetIconResource(
205 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); 213 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY);
206 info_list.push_back(ImageLoader::ImageRepresentation( 214 info_list.push_back(ImageLoader::ImageRepresentation(
207 resource, 215 resource,
208 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, 216 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER,
209 gfx::Size(sizes[i], sizes[i]), 217 gfx::Size(sizes[i], sizes[i]),
210 ui::SCALE_FACTOR_NONE)); 218 ui::SCALE_FACTOR_NONE));
211 } 219 }
212 220
213 ImageLoader loader; 221 ImageLoader loader(NULL);
214 loader.LoadImagesAsync(extension.get(), info_list, 222 loader.LoadImagesAsync(extension.get(), info_list,
215 base::Bind(&ImageLoaderTest::OnImageLoaded, 223 base::Bind(&ImageLoaderTest::OnImageLoaded,
216 base::Unretained(this))); 224 base::Unretained(this)));
217 225
218 // The image isn't cached, so we should not have received notification. 226 // The image isn't cached, so we should not have received notification.
219 EXPECT_EQ(0, image_loaded_count()); 227 EXPECT_EQ(0, image_loaded_count());
220 228
221 WaitForImageLoad(); 229 WaitForImageLoad();
222 230
223 // We should have gotten the image. 231 // We should have gotten the image.
(...skipping 27 matching lines...) Expand all
251 259
252 #if defined(FILE_MANAGER_EXTENSION) 260 #if defined(FILE_MANAGER_EXTENSION)
253 int resource_id; 261 int resource_id;
254 ASSERT_EQ(true, 262 ASSERT_EQ(true,
255 ImageLoader::IsComponentExtensionResource(extension->path(), 263 ImageLoader::IsComponentExtensionResource(extension->path(),
256 resource.relative_path(), 264 resource.relative_path(),
257 &resource_id)); 265 &resource_id));
258 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); 266 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id);
259 #endif 267 #endif
260 } 268 }
269
270 // Tests loading an image works correctly.
271 TEST_F(ImageLoaderTest, LoadIcon) {
272 scoped_refptr<Extension> extension(CreateExtension(
273 "image_loading_tracker", Manifest::INVALID_LOCATION));
274 ASSERT_TRUE(extension.get() != NULL);
275
276 ImageLoader loader(NULL);
277 loader.LoadExtensionIconAsync(extension.get(),
278 extension_misc::EXTENSION_ICON_SMALLISH,
279 false,
280 base::Bind(&ImageLoaderTest::OnImageLoaded,
281 base::Unretained(this)));
282
283 // The image isn't cached, so we should not have received notification.
284 EXPECT_EQ(0, image_loaded_count());
285
286 WaitForImageLoad();
287
288 // We should have gotten the image.
289 EXPECT_EQ(1, image_loaded_count());
290
291 // Check that the image was loaded.
292 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
293 image_.ToSkBitmap()->width());
294 }
295
296 // Tests loading an icon in grayscale works correctly.
297 TEST_F(ImageLoaderTest, LoadIconGrayscale) {
298 scoped_refptr<Extension> extension(CreateExtension(
299 "image_loading_tracker", Manifest::INVALID_LOCATION));
300 ASSERT_TRUE(extension.get() != NULL);
301
302 ImageLoader loader(NULL);
303 loader.LoadExtensionIconAsync(extension.get(),
304 extension_misc::EXTENSION_ICON_SMALLISH,
305 true,
306 base::Bind(&ImageLoaderTest::OnImageLoaded,
307 base::Unretained(this)));
308
309 // The image isn't cached, so we should not have received notification.
310 EXPECT_EQ(0, image_loaded_count());
311
312 WaitForImageLoad();
313
314 // We should have gotten the image.
315 EXPECT_EQ(1, image_loaded_count());
316
317 // Check that the image was loaded.
318 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
319 image_.ToSkBitmap()->width());
320 }
321
322 // Tests loading an icon URL works correctly.
323 TEST_F(ImageLoaderTest, LoadIconURL) {
324 scoped_refptr<Extension> extension(CreateExtension(
325 "image_loading_tracker", Manifest::INVALID_LOCATION));
326 ASSERT_TRUE(extension.get() != NULL);
327
328 ImageLoader loader(NULL);
329 loader.LoadExtensionIconDataURLAsync(
330 extension.get(),
331 extension_misc::EXTENSION_ICON_SMALLISH,
332 true,
333 base::Bind(&ImageLoaderTest::OnImageURLLoaded, base::Unretained(this)));
334
335 // The image isn't cached, so we should not have received notification.
336 EXPECT_EQ(0, image_loaded_count());
337
338 WaitForImageLoad();
339
340 // We should have gotten the image.
341 EXPECT_EQ(1, image_loaded_count());
Finnur 2013/10/02 09:52:15 We could test against an actual data URL. Or is th
dvh-g 2013/10/02 15:08:13 Even though lossless, I think that PNG compression
Finnur 2013/10/02 15:19:27 Fair enough.
342 }
343
344 // Tests loading the default icon works correctly.
345 TEST_F(ImageLoaderTest, LoadDefaultIcon) {
346 ImageLoader loader(NULL);
347 loader.LoadExtensionIconAsync(
348 NULL,
349 -1,
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 false,
372 base::Bind(&ImageLoaderTest::OnImageLoaded,
373 base::Unretained(this)));
374
375 // The image isn't cached, so we should not have received notification.
376 EXPECT_EQ(0, image_loaded_count());
377
378 WaitForImageLoad();
379
380 // We should have gotten the image.
381 EXPECT_EQ(1, image_loaded_count());
382
383 // Check that the image was loaded.
384 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
385 image_.ToSkBitmap()->width());
386 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698