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" |
| 6 |
| 7 #include <algorithm> |
| 8 |
5 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
6 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
7 #include "base/path_service.h" | 11 #include "base/path_service.h" |
8 #include "chrome/browser/extensions/image_loading_tracker.h" | |
9 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
11 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
12 #include "chrome/common/extensions/extension_icon_set.h" | 15 #include "chrome/common/extensions/extension_icon_set.h" |
13 #include "chrome/common/extensions/extension_resource.h" | 16 #include "chrome/common/extensions/extension_resource.h" |
14 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
15 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
16 #include "grit/component_extension_resources.h" | 19 #include "grit/component_extension_resources.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 193 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
191 image_.ToSkBitmap()->width()); | 194 image_.ToSkBitmap()->width()); |
192 } | 195 } |
193 | 196 |
194 // Tests loading multiple dimensions of the same image. | 197 // Tests loading multiple dimensions of the same image. |
195 TEST_F(ImageLoadingTrackerTest, MultipleImages) { | 198 TEST_F(ImageLoadingTrackerTest, MultipleImages) { |
196 scoped_refptr<Extension> extension(CreateExtension( | 199 scoped_refptr<Extension> extension(CreateExtension( |
197 "image_loading_tracker", Extension::INVALID)); | 200 "image_loading_tracker", Extension::INVALID)); |
198 ASSERT_TRUE(extension.get() != NULL); | 201 ASSERT_TRUE(extension.get() != NULL); |
199 | 202 |
200 std::vector<ImageLoadingTracker::ImageInfo> info_list; | 203 std::vector<ImageLoadingTracker::ImageRepInfo> info_list; |
201 int sizes[] = {ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 204 int sizes[] = {ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
202 ExtensionIconSet::EXTENSION_ICON_BITTY}; | 205 ExtensionIconSet::EXTENSION_ICON_BITTY}; |
203 for (size_t i = 0; i < arraysize(sizes); ++i) { | 206 for (size_t i = 0; i < arraysize(sizes); ++i) { |
204 ExtensionResource resource = | 207 ExtensionResource resource = |
205 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 208 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
206 info_list.push_back(ImageLoadingTracker::ImageInfo( | 209 info_list.push_back(ImageLoadingTracker::ImageRepInfo( |
207 resource, gfx::Size(sizes[i], sizes[i]))); | 210 resource, |
| 211 ImageLoadingTracker::ImageRepInfo::RESIZE_WHEN_LARGER, |
| 212 gfx::Size(sizes[i], sizes[i]), |
| 213 ui::SCALE_FACTOR_NONE)); |
208 } | 214 } |
209 | 215 |
210 ImageLoadingTracker loader(this); | 216 ImageLoadingTracker loader(this); |
211 loader.LoadImages(extension.get(), info_list, ImageLoadingTracker::CACHE); | 217 loader.LoadImages(extension.get(), info_list, ImageLoadingTracker::CACHE); |
212 | 218 |
213 // The image isn't cached, so we should not have received notification. | 219 // The image isn't cached, so we should not have received notification. |
214 EXPECT_EQ(0, image_loaded_count()); | 220 EXPECT_EQ(0, image_loaded_count()); |
215 | 221 |
216 WaitForImageLoad(); | 222 WaitForImageLoad(); |
217 | 223 |
(...skipping 28 matching lines...) Expand all Loading... |
246 #if defined(FILE_MANAGER_EXTENSION) | 252 #if defined(FILE_MANAGER_EXTENSION) |
247 ImageLoadingTracker loader(this); | 253 ImageLoadingTracker loader(this); |
248 int resource_id; | 254 int resource_id; |
249 ASSERT_EQ(true, | 255 ASSERT_EQ(true, |
250 loader.IsComponentExtensionResource(extension.get(), | 256 loader.IsComponentExtensionResource(extension.get(), |
251 resource, | 257 resource, |
252 resource_id)); | 258 resource_id)); |
253 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | 259 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); |
254 #endif | 260 #endif |
255 } | 261 } |
| 262 |
| 263 // Tests loading image that supports DIP. |
| 264 TEST_F(ImageLoadingTrackerTest, LoadImageSkia) { |
| 265 scoped_refptr<Extension> extension(CreateExtension( |
| 266 "image_loading_tracker", Extension::INVALID)); |
| 267 ASSERT_TRUE(extension.get() != NULL); |
| 268 |
| 269 ImageLoadingTracker loader(this); |
| 270 loader.LoadImageSkia(extension, |
| 271 ExtensionIconSet::EXTENSION_ICON_BITTY, |
| 272 ExtensionIconSet::MATCH_SMALLER, |
| 273 gfx::Size(ExtensionIconSet::EXTENSION_ICON_BITTY, |
| 274 ExtensionIconSet::EXTENSION_ICON_BITTY), |
| 275 ImageLoadingTracker::DONT_CACHE); |
| 276 |
| 277 // Observer's OnImageLoaded is called before LoadImageSkia returns. |
| 278 EXPECT_EQ(1, image_loaded_count()); |
| 279 |
| 280 // No representations in |image_| yet. |
| 281 std::vector<gfx::ImageSkiaRep> image_reps = |
| 282 image_.ToImageSkia()->image_reps(); |
| 283 ASSERT_EQ(0u, image_reps.size()); |
| 284 |
| 285 // Gets representation for a scale factor. |
| 286 image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P); |
| 287 WaitForImageLoad(); |
| 288 EXPECT_EQ(1, image_loaded_count()); |
| 289 |
| 290 gfx::ImageSkiaRep image_rep = |
| 291 image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P); |
| 292 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY, |
| 293 image_rep.pixel_width()); |
| 294 |
| 295 // Gets representation for an additional scale factor. |
| 296 image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_200P); |
| 297 WaitForImageLoad(); |
| 298 EXPECT_EQ(1, image_loaded_count()); |
| 299 |
| 300 image_rep = image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_200P); |
| 301 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALL, |
| 302 image_rep.pixel_width()); |
| 303 } |
| 304 |
| 305 // Tests LoadImageSkia with a non-existent resource. |
| 306 TEST_F(ImageLoadingTrackerTest, LoadImageSkiaBadResource) { |
| 307 scoped_refptr<Extension> extension(CreateExtension( |
| 308 "image_loading_tracker", Extension::INVALID)); |
| 309 ASSERT_TRUE(extension.get() != NULL); |
| 310 |
| 311 ImageLoadingTracker loader(this); |
| 312 loader.LoadImageSkia(extension, |
| 313 ExtensionIconSet::EXTENSION_ICON_GIGANTOR, |
| 314 ExtensionIconSet::MATCH_BIGGER, |
| 315 gfx::Size(ExtensionIconSet::EXTENSION_ICON_GIGANTOR, |
| 316 ExtensionIconSet::EXTENSION_ICON_GIGANTOR), |
| 317 ImageLoadingTracker::DONT_CACHE); |
| 318 |
| 319 // Observer's OnImageLoaded is called before LoadImageSkia returns. |
| 320 EXPECT_EQ(1, image_loaded_count()); |
| 321 |
| 322 EXPECT_TRUE(image_.IsEmpty()); |
| 323 } |
| 324 |
| 325 // Tests LoadImageSkia with a resource that does not have 2x. |
| 326 TEST_F(ImageLoadingTrackerTest, LoadImageSkiaMissing2x) { |
| 327 scoped_refptr<Extension> extension(CreateExtension( |
| 328 "image_loading_tracker", Extension::INVALID)); |
| 329 ASSERT_TRUE(extension.get() != NULL); |
| 330 |
| 331 ImageLoadingTracker loader(this); |
| 332 loader.LoadImageSkia(extension, |
| 333 ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 334 ExtensionIconSet::MATCH_BIGGER, |
| 335 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 336 ExtensionIconSet::EXTENSION_ICON_SMALLISH), |
| 337 ImageLoadingTracker::DONT_CACHE); |
| 338 |
| 339 // Observer's OnImageLoaded is called before LoadImageSkia returns. |
| 340 EXPECT_EQ(1, image_loaded_count()); |
| 341 |
| 342 // Gets representation for 1x. |
| 343 image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P); |
| 344 WaitForImageLoad(); |
| 345 EXPECT_EQ(1, image_loaded_count()); |
| 346 |
| 347 gfx::ImageSkiaRep image_rep = |
| 348 image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_100P); |
| 349 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 350 image_rep.pixel_width()); |
| 351 |
| 352 // Get representation for 2x. |
| 353 image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_200P); |
| 354 |
| 355 // Resource does not exist and OnImageLoaded is notified before above |
| 356 // GetRepresentation returns. |
| 357 EXPECT_EQ(1, image_loaded_count()); |
| 358 |
| 359 image_rep = image_.ToImageSkia()->GetRepresentation(ui::SCALE_FACTOR_200P); |
| 360 |
| 361 // 1x representation would be returned since there is no 2x resource. |
| 362 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); |
| 363 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 364 image_rep.pixel_width()); |
| 365 } |
| 366 |
OLD | NEW |