| 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 "ui/gfx/image/image_skia.h" | 5 #include "ui/gfx/image/image_skia.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkBitmap.h" | 7 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/image/image_skia_rep.h" | 9 #include "ui/gfx/image/image_skia_rep.h" |
| 10 #include "ui/gfx/image/image_skia_source.h" | 10 #include "ui/gfx/image/image_skia_source.h" |
| 11 #include "ui/gfx/size.h" | 11 #include "ui/gfx/size.h" |
| 12 #include "ui/base/layout.h" | 12 #include "ui/base/layout.h" |
| 13 | 13 |
| 14 namespace gfx { | 14 namespace gfx { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 class FixedSource : public ImageSkiaSource { | 18 class FixedSource : public ImageSkiaSource { |
| 19 public: | 19 public: |
| 20 FixedSource(const ImageSkiaRep& image) : image_(image) {} | 20 FixedSource(const ImageSkiaRep& image) : image_(image) {} |
| 21 | 21 |
| 22 virtual ~FixedSource() { |
| 23 } |
| 24 |
| 22 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 25 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 23 return image_; | 26 return image_; |
| 24 } | 27 } |
| 25 | 28 |
| 26 private: | 29 private: |
| 27 ImageSkiaRep image_; | 30 ImageSkiaRep image_; |
| 28 | 31 |
| 29 DISALLOW_COPY_AND_ASSIGN(FixedSource); | 32 DISALLOW_COPY_AND_ASSIGN(FixedSource); |
| 30 }; | 33 }; |
| 31 | 34 |
| 32 class DynamicSource : public ImageSkiaSource { | 35 class DynamicSource : public ImageSkiaSource { |
| 33 public: | 36 public: |
| 34 DynamicSource(const gfx::Size& size) : size_(size) {} | 37 DynamicSource(const gfx::Size& size) : size_(size) {} |
| 35 | 38 |
| 39 virtual ~DynamicSource() { |
| 40 } |
| 41 |
| 36 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 42 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 37 return gfx::ImageSkiaRep(size_, scale_factor); | 43 return gfx::ImageSkiaRep(size_, scale_factor); |
| 38 } | 44 } |
| 39 | 45 |
| 40 private: | 46 private: |
| 41 gfx::Size size_; | 47 gfx::Size size_; |
| 42 | 48 |
| 43 DISALLOW_COPY_AND_ASSIGN(DynamicSource); | 49 DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
| 44 }; | 50 }; |
| 45 | 51 |
| 52 class NullSource: public ImageSkiaSource { |
| 53 public: |
| 54 NullSource() { |
| 55 } |
| 56 |
| 57 virtual ~NullSource() { |
| 58 } |
| 59 |
| 60 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 61 return gfx::ImageSkiaRep(); |
| 62 } |
| 63 |
| 64 private: |
| 65 DISALLOW_COPY_AND_ASSIGN(NullSource); |
| 66 }; |
| 67 |
| 46 } // namespace; | 68 } // namespace; |
| 47 | 69 |
| 48 typedef testing::Test ImageSkiaTest; | 70 typedef testing::Test ImageSkiaTest; |
| 49 | 71 |
| 50 TEST(ImageSkiaTest, FixedSource) { | 72 TEST(ImageSkiaTest, FixedSource) { |
| 51 ImageSkiaRep image(Size(100, 200), ui::SCALE_FACTOR_100P); | 73 ImageSkiaRep image(Size(100, 200), ui::SCALE_FACTOR_100P); |
| 52 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); | 74 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); |
| 53 EXPECT_EQ(0U, image_skia.image_reps().size()); | 75 EXPECT_EQ(0U, image_skia.image_reps().size()); |
| 54 | 76 |
| 55 const ImageSkiaRep& result_100p = | 77 const ImageSkiaRep& result_100p = |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 EXPECT_EQ(2U, image_skia.image_reps().size()); | 119 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 98 | 120 |
| 99 // Get the representation again and make sure it doesn't | 121 // Get the representation again and make sure it doesn't |
| 100 // generate new image skia rep. | 122 // generate new image skia rep. |
| 101 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); | 123 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 102 EXPECT_EQ(2U, image_skia.image_reps().size()); | 124 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 103 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); | 125 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 104 EXPECT_EQ(2U, image_skia.image_reps().size()); | 126 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 105 } | 127 } |
| 106 | 128 |
| 129 #if defined(OS_MACOSX) |
| 130 |
| 131 // Tests that GetRepresentations returns all of the representations in the |
| 132 // image when there are multiple representations for a scale factor. |
| 133 // This currently is the case with ImageLoadingTracker::LoadImages, to |
| 134 // load the application shortcut icon on Mac in particular. |
| 135 TEST(ImageSkiaTest, GetRepresentationsManyRepsPerScaleFactor) { |
| 136 const int kSmallIcon1x = 16; |
| 137 const int kSmallIcon2x = 32; |
| 138 const int kLargeIcon1x = 32; |
| 139 |
| 140 ImageSkia image(new NullSource(), gfx::Size(kSmallIcon1x, kSmallIcon1x)); |
| 141 // Simulate a source which loads images on a delay. Upon |
| 142 // GetImageForScaleFactor, it immediately returns null and starts loading |
| 143 // image reps slowly. |
| 144 image.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 145 image.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 146 |
| 147 // After a lengthy amount of simulated time, finally loaded image reps. |
| 148 image.AddRepresentation(ImageSkiaRep( |
| 149 gfx::Size(kSmallIcon1x, kSmallIcon1x), ui::SCALE_FACTOR_100P)); |
| 150 image.AddRepresentation(ImageSkiaRep( |
| 151 gfx::Size(kSmallIcon2x, kSmallIcon2x), ui::SCALE_FACTOR_200P)); |
| 152 image.AddRepresentation(ImageSkiaRep( |
| 153 gfx::Size(kLargeIcon1x, kLargeIcon1x), ui::SCALE_FACTOR_100P)); |
| 154 |
| 155 std::vector<ImageSkiaRep> image_reps = image.GetRepresentations(); |
| 156 EXPECT_EQ(3u, image_reps.size()); |
| 157 |
| 158 int num_1x = 0; |
| 159 int num_2x = 0; |
| 160 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 161 if (image_reps[i].scale_factor() == ui::SCALE_FACTOR_100P) |
| 162 num_1x++; |
| 163 else if (image_reps[i].scale_factor() == ui::SCALE_FACTOR_200P) |
| 164 num_2x++; |
| 165 } |
| 166 EXPECT_EQ(2, num_1x); |
| 167 EXPECT_EQ(1, num_2x); |
| 168 } |
| 169 |
| 170 #endif // OS_MACOSX |
| 171 |
| 107 TEST(ImageSkiaTest, GetBitmap) { | 172 TEST(ImageSkiaTest, GetBitmap) { |
| 108 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); | 173 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); |
| 109 const SkBitmap* bitmap = image_skia.bitmap(); | 174 const SkBitmap* bitmap = image_skia.bitmap(); |
| 110 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap); | 175 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap); |
| 111 EXPECT_FALSE(bitmap->isNull()); | 176 EXPECT_FALSE(bitmap->isNull()); |
| 112 } | 177 } |
| 113 | 178 |
| 114 } // namespace gfx | 179 } // namespace gfx |
| OLD | NEW |