Index: ui/gfx/image/image_skia_unittest.cc |
diff --git a/ui/gfx/image/image_skia_unittest.cc b/ui/gfx/image/image_skia_unittest.cc |
index 8008f78ad24465fcaff0f3865756910a107d92ad..748afdf0f1e840158e7b9952df4b6c5dca52afb6 100644 |
--- a/ui/gfx/image/image_skia_unittest.cc |
+++ b/ui/gfx/image/image_skia_unittest.cc |
@@ -15,15 +15,6 @@ namespace gfx { |
namespace { |
-ImageSkiaRep CreateImage(const gfx::Size& size, ui::ScaleFactor scale_factor) { |
- SkBitmap bitmap; |
- gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); |
- bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
- pixel_size.width(), pixel_size.height()); |
- bitmap.allocPixels(); |
- return ImageSkiaRep(bitmap, scale_factor); |
-} |
- |
class FixedSource : public ImageSkiaSource { |
public: |
FixedSource(const ImageSkiaRep& image) : image_(image) {} |
@@ -43,7 +34,7 @@ class DynamicSource : public ImageSkiaSource { |
DynamicSource(const gfx::Size& size) : size_(size) {} |
virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
- return CreateImage(size_, scale_factor); |
+ return gfx::ImageSkiaRep(size_, scale_factor); |
} |
private: |
@@ -52,12 +43,28 @@ class DynamicSource : public ImageSkiaSource { |
DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
}; |
+class NullSource: public ImageSkiaSource { |
+ public: |
+ NullSource() { |
+ } |
+ |
+ ~NullSource() { |
+ } |
+ |
+ virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
+ return gfx::ImageSkiaRep(); |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(NullSource); |
+}; |
+ |
} // namespace; |
typedef testing::Test ImageSkiaTest; |
TEST(ImageSkiaTest, FixedSource) { |
- ImageSkiaRep image(CreateImage(Size(100, 200), ui::SCALE_FACTOR_100P)); |
+ ImageSkiaRep image(Size(100, 200), ui::SCALE_FACTOR_100P); |
ImageSkia image_skia(new FixedSource(image), Size(100, 200)); |
EXPECT_EQ(0U, image_skia.image_reps().size()); |
@@ -113,4 +120,51 @@ TEST(ImageSkiaTest, DynamicSource) { |
EXPECT_EQ(2U, image_skia.image_reps().size()); |
} |
+TEST(ImageSkiaTest, GetBitmap) { |
+ ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); |
+ const SkBitmap* bitmap = image_skia.bitmap(); |
+ EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap); |
+ EXPECT_FALSE(bitmap->isNull()); |
+} |
+ |
+// Tests that GetRepresentations returns all of the representations in the |
+// image when there are multiple representations for a scale factor. |
+// This currently is the case with ImageLoadingTracker::LoadImages, to |
+// load the application shortcut icon on Mac in particular. |
+TEST(ImageSkiaTest, GetRepresentationsManyRepsPerScaleFactor) { |
+ const int kSmallIcon1x = 16; |
+ const int kSmallIcon2x = 32; |
+ const int kLargeIcon1x = 32; |
+ |
+ ImageSkia image(new NullSource(), |
+ gfx::Size(kSmallIcon1x, kSmallIcon1x)); |
+ // Simulate a source which loads images on a delay. Upon |
+ // GetImageForScaleFactor, it immediately returns null and starts loading |
+ // image reps slowly. |
+ image.GetRepresentation(ui::SCALE_FACTOR_100P); |
+ image.GetRepresentation(ui::SCALE_FACTOR_200P); |
+ |
+ // After a lengthy amount of simulated time, finally loaded image reps. |
+ image.AddRepresentation(ImageSkiaRep( |
+ gfx::Size(kSmallIcon1x, kSmallIcon1x), ui::SCALE_FACTOR_100P)); |
+ image.AddRepresentation(ImageSkiaRep( |
+ gfx::Size(kSmallIcon2x, kSmallIcon2x), ui::SCALE_FACTOR_200P)); |
+ image.AddRepresentation(ImageSkiaRep( |
+ gfx::Size(kLargeIcon1x, kLargeIcon1x), ui::SCALE_FACTOR_100P)); |
+ |
+ std::vector<ImageSkiaRep> image_reps = image.GetRepresentations(); |
+ EXPECT_EQ(3u, image_reps.size()); |
+ |
+ int num_1x = 0; |
+ int num_2x = 0; |
+ for (size_t i = 0; i < image_reps.size(); ++i) { |
+ if (image_reps[i].scale_factor() == ui::SCALE_FACTOR_100P) |
+ num_1x++; |
+ else if (image_reps[i].scale_factor() == ui::SCALE_FACTOR_200P) |
+ num_2x++; |
+ } |
+ EXPECT_EQ(2, num_1x); |
+ EXPECT_EQ(1, num_2x); |
+} |
+ |
} // namespace gfx |