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

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

Issue 10861034: Supply default icon to extension icon image. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 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 | Annotate | Revision Log
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/extension_icon_image.h" 5 #include "chrome/browser/extensions/extension_icon_image.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/browser/extensions/image_loading_tracker.h"
10 #include "chrome/common/chrome_paths.h" 11 #include "chrome/common/chrome_paths.h"
11 #include "chrome/common/extensions/extension.h" 12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_constants.h"
12 #include "content/public/test/test_browser_thread.h" 14 #include "content/public/test/test_browser_thread.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/skia_util.h"
13 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
14 19
15 using content::BrowserThread; 20 using content::BrowserThread;
16 using extensions::Extension; 21 using extensions::Extension;
17 using extensions::IconImage; 22 using extensions::IconImage;
18 23
19 namespace { 24 namespace {
20 25
26 bool ImageRepsAreEqual(const gfx::ImageSkiaRep& image_rep1,
27 const gfx::ImageSkiaRep& image_rep2) {
28 return gfx::BitmapsAreEqual(image_rep1.sk_bitmap(), image_rep2.sk_bitmap());
Jeffrey Yasskin 2012/09/01 01:10:27 Sorry for missing this. This isn't actually a corr
tbarzic 2012/09/04 21:07:09 Done.
29 }
30
31 gfx::ImageSkiaRep CreateBlankRep(int size_dip, ui::ScaleFactor scale_factor) {
32 SkBitmap bitmap;
33 const float scale = ui::GetScaleFactorScale(scale_factor);
34 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
35 static_cast<int>(size_dip * scale),
36 static_cast<int>(size_dip * scale));
37 bitmap.allocPixels();
38 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0));
39 return gfx::ImageSkiaRep(bitmap, scale_factor);
40 }
41
42 // Helper class for synchronously loading extension image resource.
43 class TestImageLoader : public ImageLoadingTracker::Observer {
44 public:
45 explicit TestImageLoader(const Extension* extension)
46 : extension_(extension),
47 waiting_(false),
48 image_loaded_(false),
49 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) {
50 }
51 virtual ~TestImageLoader() {}
52
53 // ImageLoadingTracker::Observer override.
54 virtual void OnImageLoaded(const gfx::Image& image,
55 const std::string& extension_id,
56 int index) OVERRIDE {
57 image_ = image;
58 image_loaded_ = true;
59 if (waiting_)
60 MessageLoop::current()->Quit();
61 }
62
63 gfx::ImageSkia LoadImage(const std::string& path, int size, bool cache) {
Jeffrey Yasskin 2012/09/01 01:10:27 Just use ImageLoadingTracker::CacheParam instead o
tbarzic 2012/09/04 21:07:09 Done.
64 image_loaded_ = false;
65
66 ImageLoadingTracker::CacheParam cache_param =
67 cache ? ImageLoadingTracker::CACHE : ImageLoadingTracker::DONT_CACHE;
68
69 tracker_.LoadImage(extension_,
70 extension_->GetResource(path),
71 gfx::Size(size, size),
72 cache_param);
73
74 // If |image_| still hasn't been loaded (i.e. it is being loaded
75 // asynchronously), wait for it.
76 if (!image_loaded_) {
77 waiting_ = true;
78 MessageLoop::current()->Run();
79 waiting_ = false;
80 }
81
82 EXPECT_TRUE(image_loaded_);
83
84 return image_.IsEmpty() ? gfx::ImageSkia() : *image_.ToImageSkia();
85 }
86
87 private:
88 const Extension* extension_;
89 bool waiting_;
90 bool image_loaded_;
91 gfx::Image image_;
92 ImageLoadingTracker tracker_;
93
94 DISALLOW_COPY_AND_ASSIGN(TestImageLoader);
95 };
96
21 class ExtensionIconImageTest : public testing::Test, 97 class ExtensionIconImageTest : public testing::Test,
22 public IconImage::Observer { 98 public IconImage::Observer {
23 public: 99 public:
24 ExtensionIconImageTest() 100 ExtensionIconImageTest()
25 : image_loaded_count_(0), 101 : image_loaded_count_(0),
26 image_load_failure_count_(0),
27 quit_in_image_loaded_(false), 102 quit_in_image_loaded_(false),
28 ui_thread_(BrowserThread::UI, &ui_loop_), 103 ui_thread_(BrowserThread::UI, &ui_loop_),
29 file_thread_(BrowserThread::FILE), 104 file_thread_(BrowserThread::FILE),
30 io_thread_(BrowserThread::IO) { 105 io_thread_(BrowserThread::IO) {
31 } 106 }
32 107
33 virtual ~ExtensionIconImageTest() {} 108 virtual ~ExtensionIconImageTest() {}
34 109
35 void WaitForImageLoad() { 110 void WaitForImageLoad() {
36 // ExtensionIconImage may return synchronously, in which case there's
37 // nothing to wait for.
38 if (image_loaded_count_ > 0 || image_load_failure_count_ > 0)
39 return;
40 quit_in_image_loaded_ = true; 111 quit_in_image_loaded_ = true;
41 MessageLoop::current()->Run(); 112 MessageLoop::current()->Run();
42 quit_in_image_loaded_ = false; 113 quit_in_image_loaded_ = false;
43 } 114 }
44 115
45 int ImageLoadedCount() { 116 int ImageLoadedCount() {
46 int result = image_loaded_count_; 117 int result = image_loaded_count_;
47 image_loaded_count_ = 0; 118 image_loaded_count_ = 0;
48 return result; 119 return result;
49 } 120 }
50 121
51 int ImageLoadFailureCount() {
52 int result = image_load_failure_count_;
53 image_load_failure_count_ = 0;
54 return result;
55 }
56
57 scoped_refptr<Extension> CreateExtension(const char* name, 122 scoped_refptr<Extension> CreateExtension(const char* name,
58 Extension::Location location) { 123 Extension::Location location) {
59 // Create and load an extension. 124 // Create and load an extension.
60 FilePath test_file; 125 FilePath test_file;
61 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { 126 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) {
62 EXPECT_FALSE(true); 127 EXPECT_FALSE(true);
63 return NULL; 128 return NULL;
64 } 129 }
65 test_file = test_file.AppendASCII("extensions").AppendASCII(name); 130 test_file = test_file.AppendASCII("extensions").AppendASCII(name);
66 int error_code = 0; 131 int error_code = 0;
(...skipping 20 matching lines...) Expand all
87 io_thread_.Start(); 152 io_thread_.Start();
88 } 153 }
89 154
90 // IconImage::Delegate overrides: 155 // IconImage::Delegate overrides:
91 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE { 156 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE {
92 image_loaded_count_++; 157 image_loaded_count_++;
93 if (quit_in_image_loaded_) 158 if (quit_in_image_loaded_)
94 MessageLoop::current()->Quit(); 159 MessageLoop::current()->Quit();
95 } 160 }
96 161
97 virtual void OnIconImageLoadFailed(IconImage* image, 162 gfx::ImageSkia GetDefaultIcon() {
98 ui::ScaleFactor scale_factor) OVERRIDE { 163 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
99 image_load_failure_count_++; 164 IDR_EXTENSIONS_FAVICON);
100 if (quit_in_image_loaded_)
101 MessageLoop::current()->Quit();
102 } 165 }
103 166
104 private: 167 private:
105 int image_loaded_count_; 168 int image_loaded_count_;
106 int image_load_failure_count_;
107 bool quit_in_image_loaded_; 169 bool quit_in_image_loaded_;
108 MessageLoop ui_loop_; 170 MessageLoop ui_loop_;
109 content::TestBrowserThread ui_thread_; 171 content::TestBrowserThread ui_thread_;
110 content::TestBrowserThread file_thread_; 172 content::TestBrowserThread file_thread_;
111 content::TestBrowserThread io_thread_; 173 content::TestBrowserThread io_thread_;
112 174
113 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest); 175 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest);
114 }; 176 };
115 177
116 } // namespace 178 } // namespace
117 179
118 TEST_F(ExtensionIconImageTest, Basic) { 180 TEST_F(ExtensionIconImageTest, Basic) {
119 scoped_refptr<Extension> extension(CreateExtension( 181 scoped_refptr<Extension> extension(CreateExtension(
120 "extension_icon_image", Extension::INVALID)); 182 "extension_icon_image", Extension::INVALID));
121 ASSERT_TRUE(extension.get() != NULL); 183 ASSERT_TRUE(extension.get() != NULL);
122 184
123 IconImage image( 185 gfx::ImageSkia default_icon = GetDefaultIcon();
124 extension, 186
125 extension->icons(), 187 // Load images we expect to find as representations in icon_image, so we
126 extension_misc::EXTENSION_ICON_BITTY, 188 // can later use them to validate icon_image.
127 this); 189 TestImageLoader test_image_loader(extension.get());
190 gfx::ImageSkia bitty_image =
191 test_image_loader.LoadImage("16.png",
192 extension_misc::EXTENSION_ICON_BITTY,
193 false);
194 ASSERT_TRUE(bitty_image.HasRepresentation(ui::SCALE_FACTOR_100P));
195
196 // There is no image of size 32 defined in the extension manifest, so we
197 // should expect manifest image of size 48 resized to size 32.
198 gfx::ImageSkia small_image =
199 test_image_loader.LoadImage("48.png",
200 2 * extension_misc::EXTENSION_ICON_BITTY,
201 false);
202 ASSERT_TRUE(small_image.HasRepresentation(ui::SCALE_FACTOR_100P));
203
204 IconImage image(extension,
205 extension->icons(),
206 extension_misc::EXTENSION_ICON_BITTY,
207 default_icon,
208 this);
128 209
129 // No representations in |image_| yet. 210 // No representations in |image_| yet.
130 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps(); 211 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps();
131 ASSERT_EQ(0u, image_reps.size()); 212 ASSERT_EQ(0u, image_reps.size());
132 213
133 // Gets representation for a scale factor. 214 // Gets representation for a scale factor.
134 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); 215 gfx::ImageSkiaRep representation =
216 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
217 // Before the image representation is loaded, image should contain blank
218 // image representation.
219 EXPECT_TRUE(ImageRepsAreEqual(
220 representation,
221 CreateBlankRep(extension_misc::EXTENSION_ICON_BITTY,
222 ui::SCALE_FACTOR_100P)));
223
135 WaitForImageLoad(); 224 WaitForImageLoad();
136 EXPECT_EQ(1, ImageLoadedCount()); 225 EXPECT_EQ(1, ImageLoadedCount());
137 EXPECT_EQ(0, ImageLoadFailureCount()); 226 ASSERT_EQ(1u, image.image_skia().image_reps().size());
227
228 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
229 // We should get the right representation now.
230 EXPECT_TRUE(ImageRepsAreEqual(
231 representation,
232 bitty_image.GetRepresentation(ui::SCALE_FACTOR_100P)));
233 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
234 representation.pixel_width());
138 235
139 // Gets representation for an additional scale factor. 236 // Gets representation for an additional scale factor.
140 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); 237 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
238
239 EXPECT_TRUE(ImageRepsAreEqual(
240 representation,
241 CreateBlankRep(extension_misc::EXTENSION_ICON_BITTY,
242 ui::SCALE_FACTOR_200P)));
243
141 WaitForImageLoad(); 244 WaitForImageLoad();
142 EXPECT_EQ(1, ImageLoadedCount()); 245 EXPECT_EQ(1, ImageLoadedCount());
143 EXPECT_EQ(0, ImageLoadFailureCount()); 246 ASSERT_EQ(2u, image.image_skia().image_reps().size());
144 247
145 gfx::ImageSkiaRep image_rep = 248 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
146 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); 249 EXPECT_TRUE(ImageRepsAreEqual(
147 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, 250 representation,
148 image_rep.pixel_width()); 251 small_image.GetRepresentation(ui::SCALE_FACTOR_100P)));
149 252 // Image should have been resized.
150 image_rep = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
151 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALL, 253 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALL,
152 image_rep.pixel_width()); 254 representation.pixel_width());
153 } 255 }
154 256
155 // If we can't load icon with the exact size, but a bigger resource is 257 // If we can't load icon with the exact size, but a bigger resource is
Jeffrey Yasskin 2012/09/01 01:10:27 Hasn't this already been tested in the SCALE_FACTO
tbarzic 2012/09/04 21:07:09 Done.
156 // available. 258 // available.
157 TEST_F(ExtensionIconImageTest, FallbackToBigger) { 259 TEST_F(ExtensionIconImageTest, FallbackToBigger) {
158 scoped_refptr<Extension> extension(CreateExtension( 260 scoped_refptr<Extension> extension(CreateExtension(
159 "extension_icon_image", Extension::INVALID)); 261 "extension_icon_image", Extension::INVALID));
160 ASSERT_TRUE(extension.get() != NULL); 262 ASSERT_TRUE(extension.get() != NULL);
161 263
162 IconImage image( 264 gfx::ImageSkia default_icon = GetDefaultIcon();
163 extension, 265
164 extension->icons(), 266 // Load images we expect to find as representations in icon_image, so we
165 extension_misc::EXTENSION_ICON_BITTY, 267 // can later use them to validate icon_image.
166 this); 268 TestImageLoader test_image_loader(extension.get());
269 gfx::ImageSkia small_image =
270 test_image_loader.LoadImage("48.png",
271 2 * extension_misc::EXTENSION_ICON_BITTY,
272 false);
273 ASSERT_TRUE(small_image.HasRepresentation(ui::SCALE_FACTOR_100P));
274
275 IconImage image(extension,
276 extension->icons(),
277 extension_misc::EXTENSION_ICON_BITTY,
278 default_icon,
279 this);
167 280
168 // Get representation for 2x. 281 // Get representation for 2x.
169 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); 282 gfx::ImageSkiaRep representation =
283 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
170 284
171 WaitForImageLoad(); 285 WaitForImageLoad();
172 EXPECT_EQ(1, ImageLoadedCount()); 286 EXPECT_EQ(1, ImageLoadedCount());
173 EXPECT_EQ(0, ImageLoadFailureCount()); 287 ASSERT_EQ(1u, image.image_skia().image_reps().size());
174 288
175 gfx::ImageSkiaRep image_rep = 289 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
176 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); 290 EXPECT_TRUE(ImageRepsAreEqual(
291 representation,
292 small_image.GetRepresentation(ui::SCALE_FACTOR_100P)));
177 293
178 // We should have found a bigger resource and it should have been resized. 294 // We should have found a bigger resource and it should have been resized.
179 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); 295 EXPECT_EQ(ui::SCALE_FACTOR_200P, representation.scale_factor());
180 EXPECT_EQ(2 * extension_misc::EXTENSION_ICON_BITTY, 296 EXPECT_EQ(2 * extension_misc::EXTENSION_ICON_BITTY,
181 image_rep.pixel_width()); 297 representation.pixel_width());
182 } 298 }
183 299
184 // There is no resource with either exact or bigger size, but there is a smaller 300 // There is no resource with either exact or bigger size, but there is a smaller
185 // resource. 301 // resource.
186 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) { 302 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) {
187 scoped_refptr<Extension> extension(CreateExtension( 303 scoped_refptr<Extension> extension(CreateExtension(
188 "extension_icon_image", Extension::INVALID)); 304 "extension_icon_image", Extension::INVALID));
189 ASSERT_TRUE(extension.get() != NULL); 305 ASSERT_TRUE(extension.get() != NULL);
190 306
191 IconImage image( 307 gfx::ImageSkia default_icon = GetDefaultIcon();
192 extension, 308
193 extension->icons(), 309 // Load images we expect to find as representations in icon_image, so we
194 extension_misc::EXTENSION_ICON_SMALL, 310 // can later use them to validate icon_image.
195 this); 311 // The image should not be resized.
Jeffrey Yasskin 2012/09/01 01:10:27 "should not be" makes me nervous in a test. Either
tbarzic 2012/09/04 21:07:09 yeah, that comment is a bit misplaced.. removed it
312 TestImageLoader test_image_loader(extension.get());
313 gfx::ImageSkia expected_image =
314 test_image_loader.LoadImage("48.png",
315 extension_misc::EXTENSION_ICON_MEDIUM,
316 false);
317 ASSERT_TRUE(expected_image.HasRepresentation(ui::SCALE_FACTOR_100P));
318
319 IconImage image(extension,
320 extension->icons(),
321 extension_misc::EXTENSION_ICON_SMALL,
322 default_icon,
323 this);
196 324
197 // Attempt to get representation for 2x. 325 // Attempt to get representation for 2x.
198 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); 326 gfx::ImageSkiaRep representation =
327 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
199 328
200 WaitForImageLoad(); 329 WaitForImageLoad();
201 EXPECT_EQ(1, ImageLoadedCount()); 330 EXPECT_EQ(1, ImageLoadedCount());
202 EXPECT_EQ(0, ImageLoadFailureCount()); 331 ASSERT_EQ(1u, image.image_skia().image_reps().size());
203 332
204 gfx::ImageSkiaRep image_rep = 333 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
205 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); 334 EXPECT_TRUE(ImageRepsAreEqual(
335 representation,
336 expected_image.GetRepresentation(ui::SCALE_FACTOR_100P)));
206 337
207 // We should have loaded the biggest smaller resource. In this case the 338 // We should have loaded the biggest smaller resource. In this case the
208 // loaded resource should not be resized. 339 // loaded resource should not be resized.
209 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor()); 340 EXPECT_EQ(ui::SCALE_FACTOR_200P, representation.scale_factor());
210 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM, 341 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM,
211 image_rep.pixel_width()); 342 representation.pixel_width());
212 } 343 }
213 344
214 // There is no resource with exact size, but there is a smaller and a bigger 345 // There is no resource with exact size, but there is a smaller and a bigger
215 // one. Requested size is smaller than 32 though, so the smaller resource should 346 // one. Requested size is smaller than 32 though, so the smaller resource should
216 // be loaded. 347 // be loaded.
217 TEST_F(ExtensionIconImageTest, FallbackToSmaller) { 348 TEST_F(ExtensionIconImageTest, FallbackToSmaller) {
218 scoped_refptr<Extension> extension(CreateExtension( 349 scoped_refptr<Extension> extension(CreateExtension(
219 "extension_icon_image", Extension::INVALID)); 350 "extension_icon_image", Extension::INVALID));
220 ASSERT_TRUE(extension.get() != NULL); 351 ASSERT_TRUE(extension.get() != NULL);
221 352
222 IconImage image( 353 gfx::ImageSkia default_icon = GetDefaultIcon();
223 extension, 354
224 extension->icons(), 355 // Load images we expect to find as representations in icon_image, so we
225 17, 356 // can later use them to validate icon_image.
226 this); 357 TestImageLoader test_image_loader(extension.get());
358 gfx::ImageSkia expected_image =
359 test_image_loader.LoadImage("16.png",
Jeffrey Yasskin 2012/09/01 01:10:27 Please extract all of these image loads into a hel
tbarzic 2012/09/04 21:07:09 Done.
360 extension_misc::EXTENSION_ICON_BITTY,
361 false);
362 ASSERT_TRUE(expected_image.HasRepresentation(ui::SCALE_FACTOR_100P));
363
364 IconImage image(extension, extension->icons(), 17, default_icon, this);
227 365
228 // Attempt to get representation for 1x. 366 // Attempt to get representation for 1x.
229 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); 367 gfx::ImageSkiaRep representation =
368 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
230 369
231 WaitForImageLoad(); 370 WaitForImageLoad();
232 EXPECT_EQ(1, ImageLoadedCount()); 371 EXPECT_EQ(1, ImageLoadedCount());
233 EXPECT_EQ(0, ImageLoadFailureCount()); 372 ASSERT_EQ(1u, image.image_skia().image_reps().size());
234 373
235 gfx::ImageSkiaRep image_rep = 374 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
236 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); 375 EXPECT_TRUE(ImageRepsAreEqual(
376 representation,
377 expected_image.GetRepresentation(ui::SCALE_FACTOR_100P)));
237 378
238 // We should have loaded smaller (not resized) resource. 379 // We should have loaded smaller (not resized) resource.
239 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); 380 EXPECT_EQ(ui::SCALE_FACTOR_100P, representation.scale_factor());
240 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, 381 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
241 image_rep.pixel_width()); 382 representation.pixel_width());
242 } 383 }
243 384
244 // If resource set is empty, failure should be reported. 385 // If resource set is empty, failure should be reported.
Jeffrey Yasskin 2012/09/01 01:10:27 "failure should be reported" is no longer true.
tbarzic 2012/09/04 21:07:09 Done.
245 TEST_F(ExtensionIconImageTest, NoResources) { 386 TEST_F(ExtensionIconImageTest, NoResources) {
246 scoped_refptr<Extension> extension(CreateExtension( 387 scoped_refptr<Extension> extension(CreateExtension(
247 "extension_icon_image", Extension::INVALID)); 388 "extension_icon_image", Extension::INVALID));
248 ASSERT_TRUE(extension.get() != NULL); 389 ASSERT_TRUE(extension.get() != NULL);
249 390
250 ExtensionIconSet empty_icon_set; 391 ExtensionIconSet empty_icon_set;
392 gfx::ImageSkia default_icon = GetDefaultIcon();
251 393
252 IconImage image( 394 IconImage image(extension,
253 extension, 395 empty_icon_set,
254 empty_icon_set, 396 extension_misc::EXTENSION_ICON_SMALLISH,
255 extension_misc::EXTENSION_ICON_SMALLISH, 397 default_icon,
256 this); 398 this);
257 399
258 // Attempt to get representation for 2x. 400 // Attempt to get representation for 2x.
259 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); 401 gfx::ImageSkiaRep representation =
402 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
403 EXPECT_TRUE(ImageRepsAreEqual(
404 representation,
405 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
406
407 EXPECT_EQ(0, ImageLoadedCount());
408 // We should still have default icon representation.
409 ASSERT_EQ(1u, image.image_skia().image_reps().size());
410
411 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
412 EXPECT_TRUE(ImageRepsAreEqual(
413 representation,
414 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
415 }
416
417 // If resource set is empty, failure should be reported.
Jeffrey Yasskin 2012/09/01 01:10:27 Please update comments when you copy them.
tbarzic 2012/09/04 21:07:09 Done.
418 TEST_F(ExtensionIconImageTest, InvalidResource) {
419 scoped_refptr<Extension> extension(CreateExtension(
420 "extension_icon_image", Extension::INVALID));
421 ASSERT_TRUE(extension.get() != NULL);
422
423 ExtensionIconSet invalid_icon_set;
424 invalid_icon_set.Add(extension_misc::EXTENSION_ICON_SMALLISH, "invalid.png");
425 gfx::ImageSkia default_icon = GetDefaultIcon();
426
427 IconImage image(extension,
428 invalid_icon_set,
429 extension_misc::EXTENSION_ICON_SMALLISH,
430 default_icon,
431 this);
432
433 // Attempt to get representation for 2x.
434 gfx::ImageSkiaRep representation =
435 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
436 EXPECT_TRUE(ImageRepsAreEqual(
437 representation,
438 CreateBlankRep(extension_misc::EXTENSION_ICON_SMALLISH,
439 ui::SCALE_FACTOR_100P)));
260 440
261 WaitForImageLoad(); 441 WaitForImageLoad();
442 EXPECT_EQ(1, ImageLoadedCount());
443 // We should still have default icon representation.
Jeffrey Yasskin 2012/09/01 01:10:27 What do you mean "still"? This has changed from th
tbarzic 2012/09/04 21:07:09 Done.
444 ASSERT_EQ(1u, image.image_skia().image_reps().size());
445
446 representation = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
447 EXPECT_TRUE(ImageRepsAreEqual(
448 representation,
449 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
450 }
451
452 TEST_F(ExtensionIconImageTest, LoadPrecachedImage) {
453 scoped_refptr<Extension> extension(CreateExtension(
454 "extension_icon_image", Extension::INVALID));
455 ASSERT_TRUE(extension.get() != NULL);
456
457 gfx::ImageSkia default_icon = GetDefaultIcon();
458
459 TestImageLoader test_image_loader(extension.get());
460 // Load image and cache it.
461 gfx::ImageSkia bitty_image =
462 test_image_loader.LoadImage("16.png",
463 extension_misc::EXTENSION_ICON_BITTY,
464 true);
465 ASSERT_TRUE(bitty_image.HasRepresentation(ui::SCALE_FACTOR_100P));
466
467 IconImage image(extension,
468 extension->icons(),
469 extension_misc::EXTENSION_ICON_BITTY,
470 default_icon,
471 this);
472
473 // No representations in |image_| yet.
474 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps();
475 ASSERT_EQ(0u, image_reps.size());
476
477 // Gets representation for a scale factor.
478 // Since the icon representation is precached, it should be returned right
479 // away. Also, we should not received any notifications.
480 gfx::ImageSkiaRep representation =
481 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
482 EXPECT_TRUE(ImageRepsAreEqual(
483 representation,
484 bitty_image.GetRepresentation(ui::SCALE_FACTOR_100P)));
485
262 EXPECT_EQ(0, ImageLoadedCount()); 486 EXPECT_EQ(0, ImageLoadedCount());
263 EXPECT_EQ(1, ImageLoadFailureCount()); 487 ASSERT_EQ(1u, image.image_skia().image_reps().size());
264 } 488 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698