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 ImageSkiaRep CreateImage(const gfx::Size& size, ui::ScaleFactor scale_factor) { | |
19 SkBitmap bitmap; | |
20 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); | |
21 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
22 pixel_size.width(), pixel_size.height()); | |
23 bitmap.allocPixels(); | |
24 return ImageSkiaRep(bitmap, scale_factor); | |
25 } | |
26 | |
27 class FixedSource : public ImageSkiaSource { | 18 class FixedSource : public ImageSkiaSource { |
28 public: | 19 public: |
29 FixedSource(const ImageSkiaRep& image) : image_(image) {} | 20 FixedSource(const ImageSkiaRep& image) : image_(image) {} |
30 | 21 |
31 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 22 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
32 return image_; | 23 return image_; |
33 } | 24 } |
34 | 25 |
35 private: | 26 private: |
36 ImageSkiaRep image_; | 27 ImageSkiaRep image_; |
37 | 28 |
38 DISALLOW_COPY_AND_ASSIGN(FixedSource); | 29 DISALLOW_COPY_AND_ASSIGN(FixedSource); |
39 }; | 30 }; |
40 | 31 |
41 class DynamicSource : public ImageSkiaSource { | 32 class DynamicSource : public ImageSkiaSource { |
42 public: | 33 public: |
43 DynamicSource(const gfx::Size& size) : size_(size) {} | 34 DynamicSource(const gfx::Size& size) : size_(size) {} |
44 | 35 |
45 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 36 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
46 return CreateImage(size_, scale_factor); | 37 return gfx::ImageSkiaRep(size_, scale_factor); |
47 } | 38 } |
48 | 39 |
49 private: | 40 private: |
50 gfx::Size size_; | 41 gfx::Size size_; |
51 | 42 |
52 DISALLOW_COPY_AND_ASSIGN(DynamicSource); | 43 DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
53 }; | 44 }; |
54 | 45 |
| 46 class NullSource: public ImageSkiaSource { |
| 47 public: |
| 48 NullSource() { |
| 49 } |
| 50 |
| 51 ~NullSource() { |
| 52 } |
| 53 |
| 54 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 55 return gfx::ImageSkiaRep(); |
| 56 } |
| 57 |
| 58 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(NullSource); |
| 60 }; |
| 61 |
55 } // namespace; | 62 } // namespace; |
56 | 63 |
57 typedef testing::Test ImageSkiaTest; | 64 typedef testing::Test ImageSkiaTest; |
58 | 65 |
59 TEST(ImageSkiaTest, FixedSource) { | 66 TEST(ImageSkiaTest, FixedSource) { |
60 ImageSkiaRep image(CreateImage(Size(100, 200), ui::SCALE_FACTOR_100P)); | 67 ImageSkiaRep image(Size(100, 200), ui::SCALE_FACTOR_100P); |
61 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); | 68 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); |
62 EXPECT_EQ(0U, image_skia.image_reps().size()); | 69 EXPECT_EQ(0U, image_skia.image_reps().size()); |
63 | 70 |
64 const ImageSkiaRep& result_100p = | 71 const ImageSkiaRep& result_100p = |
65 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); | 72 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
66 EXPECT_EQ(100, result_100p.GetWidth()); | 73 EXPECT_EQ(100, result_100p.GetWidth()); |
67 EXPECT_EQ(200, result_100p.GetHeight()); | 74 EXPECT_EQ(200, result_100p.GetHeight()); |
68 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); | 75 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
69 EXPECT_EQ(1U, image_skia.image_reps().size()); | 76 EXPECT_EQ(1U, image_skia.image_reps().size()); |
70 | 77 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 EXPECT_EQ(2U, image_skia.image_reps().size()); | 113 EXPECT_EQ(2U, image_skia.image_reps().size()); |
107 | 114 |
108 // Get the representation again and make sure it doesn't | 115 // Get the representation again and make sure it doesn't |
109 // generate new image skia rep. | 116 // generate new image skia rep. |
110 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); | 117 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
111 EXPECT_EQ(2U, image_skia.image_reps().size()); | 118 EXPECT_EQ(2U, image_skia.image_reps().size()); |
112 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); | 119 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
113 EXPECT_EQ(2U, image_skia.image_reps().size()); | 120 EXPECT_EQ(2U, image_skia.image_reps().size()); |
114 } | 121 } |
115 | 122 |
| 123 TEST(ImageSkiaTest, GetBitmap) { |
| 124 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); |
| 125 const SkBitmap* bitmap = image_skia.bitmap(); |
| 126 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap); |
| 127 EXPECT_FALSE(bitmap->isNull()); |
| 128 } |
| 129 |
| 130 // Tests that GetRepresentations returns all of the representations in the |
| 131 // image when there are multiple representations for a scale factor. |
| 132 // This currently is the case with ImageLoadingTracker::LoadImages, to |
| 133 // load the application shortcut icon on Mac in particular. |
| 134 TEST(ImageSkiaTest, GetRepresentationsManyRepsPerScaleFactor) { |
| 135 const int kSmallIcon1x = 16; |
| 136 const int kSmallIcon2x = 32; |
| 137 const int kLargeIcon1x = 32; |
| 138 |
| 139 ImageSkia image(new NullSource(), |
| 140 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 |
116 } // namespace gfx | 170 } // namespace gfx |
OLD | NEW |