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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "third_party/skia/include/core/SkCanvas.h" | 6 #include "third_party/skia/include/core/SkCanvas.h" |
7 #include "third_party/skia/include/core/SkPaint.h" | 7 #include "third_party/skia/include/core/SkPaint.h" |
8 #include "ui/base/layout.h" | |
9 #include "ui/gfx/image/image.h" | 8 #include "ui/gfx/image/image.h" |
10 #include "ui/gfx/image/image_png_rep.h" | 9 #include "ui/gfx/image/image_png_rep.h" |
11 #include "ui/gfx/image/image_skia.h" | 10 #include "ui/gfx/image/image_skia.h" |
12 #include "ui/gfx/image/image_unittest_util.h" | 11 #include "ui/gfx/image/image_unittest_util.h" |
13 | 12 |
14 #if defined(TOOLKIT_GTK) | 13 #if defined(TOOLKIT_GTK) |
15 #include <gtk/gtk.h> | 14 #include <gtk/gtk.h> |
16 #include "ui/gfx/gtk_util.h" | 15 #include "ui/gfx/gtk_util.h" |
17 #elif defined(OS_IOS) | 16 #elif defined(OS_IOS) |
18 #include "base/mac/foundation_util.h" | 17 #include "base/mac/foundation_util.h" |
19 #include "skia/ext/skia_utils_ios.h" | 18 #include "skia/ext/skia_utils_ios.h" |
20 #elif defined(OS_MACOSX) | 19 #elif defined(OS_MACOSX) |
21 #include "base/mac/mac_util.h" | 20 #include "base/mac/mac_util.h" |
22 #include "skia/ext/skia_utils_mac.h" | 21 #include "skia/ext/skia_utils_mac.h" |
23 #endif | 22 #endif |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 #if defined(TOOLKIT_VIEWS) || defined(OS_ANDROID) | 26 #if defined(TOOLKIT_VIEWS) || defined(OS_ANDROID) |
28 const bool kUsesSkiaNatively = true; | 27 const bool kUsesSkiaNatively = true; |
29 #else | 28 #else |
30 const bool kUsesSkiaNatively = false; | 29 const bool kUsesSkiaNatively = false; |
31 #endif | 30 #endif |
32 | 31 |
33 class ImageTest : public testing::Test { | 32 class ImageTest : public testing::Test { |
| 33 public: |
| 34 ImageTest() { |
| 35 std::vector<float> scales; |
| 36 scales.push_back(1.0f); |
| 37 #if !defined(OS_IOS) |
| 38 scales.push_back(2.0f); |
| 39 #endif |
| 40 gfx::ImageSkia::SetSupportedScales(scales); |
| 41 } |
34 }; | 42 }; |
35 | 43 |
36 namespace gt = gfx::test; | 44 namespace gt = gfx::test; |
37 | 45 |
38 TEST_F(ImageTest, EmptyImage) { | 46 TEST_F(ImageTest, EmptyImage) { |
39 // Test the default constructor. | 47 // Test the default constructor. |
40 gfx::Image image; | 48 gfx::Image image; |
41 EXPECT_EQ(0U, image.RepresentationCount()); | 49 EXPECT_EQ(0U, image.RepresentationCount()); |
42 EXPECT_TRUE(image.IsEmpty()); | 50 EXPECT_TRUE(image.IsEmpty()); |
43 EXPECT_EQ(0, image.Width()); | 51 EXPECT_EQ(0, image.Width()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 EXPECT_TRUE(image3.IsEmpty()); | 96 EXPECT_TRUE(image3.IsEmpty()); |
89 EXPECT_EQ(0, image3.Width()); | 97 EXPECT_EQ(0, image3.Width()); |
90 EXPECT_EQ(0, image3.Height()); | 98 EXPECT_EQ(0, image3.Height()); |
91 EXPECT_EQ(0U, image3.RepresentationCount()); | 99 EXPECT_EQ(0U, image3.RepresentationCount()); |
92 } | 100 } |
93 | 101 |
94 // The resulting Image should be empty when it is created using obviously | 102 // The resulting Image should be empty when it is created using obviously |
95 // invalid data. | 103 // invalid data. |
96 TEST_F(ImageTest, EmptyImageFromObviouslyInvalidPNGImage) { | 104 TEST_F(ImageTest, EmptyImageFromObviouslyInvalidPNGImage) { |
97 std::vector<gfx::ImagePNGRep> image_png_reps1; | 105 std::vector<gfx::ImagePNGRep> image_png_reps1; |
98 image_png_reps1.push_back(gfx::ImagePNGRep(NULL, ui::SCALE_FACTOR_100P)); | 106 image_png_reps1.push_back(gfx::ImagePNGRep(NULL, 1.0f)); |
99 gfx::Image image1(image_png_reps1); | 107 gfx::Image image1(image_png_reps1); |
100 EXPECT_TRUE(image1.IsEmpty()); | 108 EXPECT_TRUE(image1.IsEmpty()); |
101 EXPECT_EQ(0U, image1.RepresentationCount()); | 109 EXPECT_EQ(0U, image1.RepresentationCount()); |
102 | 110 |
103 std::vector<gfx::ImagePNGRep> image_png_reps2; | 111 std::vector<gfx::ImagePNGRep> image_png_reps2; |
104 image_png_reps2.push_back(gfx::ImagePNGRep( | 112 image_png_reps2.push_back(gfx::ImagePNGRep( |
105 new base::RefCountedBytes(), ui::SCALE_FACTOR_100P)); | 113 new base::RefCountedBytes(), 1.0f)); |
106 gfx::Image image2(image_png_reps2); | 114 gfx::Image image2(image_png_reps2); |
107 EXPECT_TRUE(image2.IsEmpty()); | 115 EXPECT_TRUE(image2.IsEmpty()); |
108 EXPECT_EQ(0U, image2.RepresentationCount()); | 116 EXPECT_EQ(0U, image2.RepresentationCount()); |
109 } | 117 } |
110 | 118 |
111 // Test the Width, Height and Size of an empty and non-empty image. | 119 // Test the Width, Height and Size of an empty and non-empty image. |
112 TEST_F(ImageTest, ImageSize) { | 120 TEST_F(ImageTest, ImageSize) { |
113 gfx::Image image; | 121 gfx::Image image; |
114 EXPECT_EQ(0, image.Width()); | 122 EXPECT_EQ(0, image.Width()); |
115 EXPECT_EQ(0, image.Height()); | 123 EXPECT_EQ(0, image.Height()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 EXPECT_FALSE(png_bytes->size()); | 167 EXPECT_FALSE(png_bytes->size()); |
160 } | 168 } |
161 | 169 |
162 // Check that getting the 1x PNG bytes from images which do not have a 1x | 170 // Check that getting the 1x PNG bytes from images which do not have a 1x |
163 // representation returns NULL. | 171 // representation returns NULL. |
164 TEST_F(ImageTest, ImageNo1xToPNG) { | 172 TEST_F(ImageTest, ImageNo1xToPNG) { |
165 // Image with 2x only. | 173 // Image with 2x only. |
166 const int kSize2x = 50; | 174 const int kSize2x = 50; |
167 gfx::ImageSkia image_skia; | 175 gfx::ImageSkia image_skia; |
168 image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( | 176 image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( |
169 kSize2x, kSize2x), ui::SCALE_FACTOR_200P)); | 177 kSize2x, kSize2x), 2.0f)); |
170 gfx::Image image1(image_skia); | 178 gfx::Image image1(image_skia); |
171 scoped_refptr<base::RefCountedMemory> png_bytes1 = image1.As1xPNGBytes(); | 179 scoped_refptr<base::RefCountedMemory> png_bytes1 = image1.As1xPNGBytes(); |
172 EXPECT_TRUE(png_bytes1.get()); | 180 EXPECT_TRUE(png_bytes1.get()); |
173 EXPECT_FALSE(png_bytes1->size()); | 181 EXPECT_FALSE(png_bytes1->size()); |
174 | 182 |
175 std::vector<gfx::ImagePNGRep> image_png_reps; | 183 std::vector<gfx::ImagePNGRep> image_png_reps; |
176 image_png_reps.push_back(gfx::ImagePNGRep( | 184 image_png_reps.push_back(gfx::ImagePNGRep( |
177 gt::CreatePNGBytes(kSize2x), ui::SCALE_FACTOR_200P)); | 185 gt::CreatePNGBytes(kSize2x), 2.0f)); |
178 gfx::Image image2(image_png_reps); | 186 gfx::Image image2(image_png_reps); |
179 EXPECT_FALSE(image2.IsEmpty()); | 187 EXPECT_FALSE(image2.IsEmpty()); |
180 EXPECT_EQ(0, image2.Width()); | 188 EXPECT_EQ(0, image2.Width()); |
181 EXPECT_EQ(0, image2.Height()); | 189 EXPECT_EQ(0, image2.Height()); |
182 scoped_refptr<base::RefCountedMemory> png_bytes2 = image2.As1xPNGBytes(); | 190 scoped_refptr<base::RefCountedMemory> png_bytes2 = image2.As1xPNGBytes(); |
183 EXPECT_TRUE(png_bytes2.get()); | 191 EXPECT_TRUE(png_bytes2.get()); |
184 EXPECT_FALSE(png_bytes2->size()); | 192 EXPECT_FALSE(png_bytes2->size()); |
185 } | 193 } |
186 | 194 |
187 // Check that for an image initialized with multi resolution PNG data, | 195 // Check that for an image initialized with multi resolution PNG data, |
188 // As1xPNGBytes() returns the 1x bytes. | 196 // As1xPNGBytes() returns the 1x bytes. |
189 TEST_F(ImageTest, CreateExtractPNGBytes) { | 197 TEST_F(ImageTest, CreateExtractPNGBytes) { |
190 const int kSize1x = 25; | 198 const int kSize1x = 25; |
191 const int kSize2x = 50; | 199 const int kSize2x = 50; |
192 | 200 |
193 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); | 201 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); |
194 std::vector<gfx::ImagePNGRep> image_png_reps; | 202 std::vector<gfx::ImagePNGRep> image_png_reps; |
195 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); | 203 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, 1.0f)); |
196 image_png_reps.push_back(gfx::ImagePNGRep( | 204 image_png_reps.push_back(gfx::ImagePNGRep( |
197 gt::CreatePNGBytes(kSize2x), ui::SCALE_FACTOR_200P)); | 205 gt::CreatePNGBytes(kSize2x), 2.0f)); |
198 | 206 |
199 gfx::Image image(image_png_reps); | 207 gfx::Image image(image_png_reps); |
200 EXPECT_FALSE(image.IsEmpty()); | 208 EXPECT_FALSE(image.IsEmpty()); |
201 EXPECT_EQ(25, image.Width()); | 209 EXPECT_EQ(25, image.Width()); |
202 EXPECT_EQ(25, image.Height()); | 210 EXPECT_EQ(25, image.Height()); |
203 | 211 |
204 EXPECT_TRUE(std::equal(bytes1x->front(), bytes1x->front() + bytes1x->size(), | 212 EXPECT_TRUE(std::equal(bytes1x->front(), bytes1x->front() + bytes1x->size(), |
205 image.As1xPNGBytes()->front())); | 213 image.As1xPNGBytes()->front())); |
206 } | 214 } |
207 | 215 |
208 TEST_F(ImageTest, MultiResolutionImageSkiaToPNG) { | 216 TEST_F(ImageTest, MultiResolutionImageSkiaToPNG) { |
209 const int kSize1x = 25; | 217 const int kSize1x = 25; |
210 const int kSize2x = 50; | 218 const int kSize2x = 50; |
211 | 219 |
212 SkBitmap bitmap_1x = gt::CreateBitmap(kSize1x, kSize1x); | 220 SkBitmap bitmap_1x = gt::CreateBitmap(kSize1x, kSize1x); |
213 gfx::ImageSkia image_skia; | 221 gfx::ImageSkia image_skia; |
214 image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap_1x, | 222 image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap_1x, |
215 ui::SCALE_FACTOR_100P)); | 223 1.0f)); |
216 image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( | 224 image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( |
217 kSize2x, kSize2x), ui::SCALE_FACTOR_200P)); | 225 kSize2x, kSize2x), 2.0f)); |
218 gfx::Image image(image_skia); | 226 gfx::Image image(image_skia); |
219 | 227 |
220 EXPECT_TRUE(gt::IsEqual(image.As1xPNGBytes(), bitmap_1x)); | 228 EXPECT_TRUE(gt::IsEqual(image.As1xPNGBytes(), bitmap_1x)); |
221 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); | 229 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); |
222 } | 230 } |
223 | 231 |
224 TEST_F(ImageTest, MultiResolutionPNGToImageSkia) { | 232 TEST_F(ImageTest, MultiResolutionPNGToImageSkia) { |
225 const int kSize1x = 25; | 233 const int kSize1x = 25; |
226 const int kSize2x = 50; | 234 const int kSize2x = 50; |
227 | 235 |
228 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); | 236 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); |
229 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); | 237 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); |
230 | 238 |
231 std::vector<gfx::ImagePNGRep> image_png_reps; | 239 std::vector<gfx::ImagePNGRep> image_png_reps; |
232 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); | 240 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, 1.0f)); |
233 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); | 241 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, 2.0f)); |
234 gfx::Image image(image_png_reps); | 242 gfx::Image image(image_png_reps); |
235 | 243 |
236 std::vector<ui::ScaleFactor> scale_factors; | 244 std::vector<float> scales; |
237 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 245 scales.push_back(1.0f); |
238 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 246 scales.push_back(2.0f); |
239 gfx::ImageSkia image_skia = image.AsImageSkia(); | 247 gfx::ImageSkia image_skia = image.AsImageSkia(); |
240 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kSize1x, kSize1x, | 248 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kSize1x, kSize1x, |
241 scale_factors)); | 249 scales)); |
242 EXPECT_TRUE(gt::IsEqual(bytes1x, | 250 EXPECT_TRUE(gt::IsEqual(bytes1x, |
243 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); | 251 image_skia.GetRepresentation(1.0f).sk_bitmap())); |
244 EXPECT_TRUE(gt::IsEqual(bytes2x, | 252 EXPECT_TRUE(gt::IsEqual(bytes2x, |
245 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P).sk_bitmap())); | 253 image_skia.GetRepresentation(2.0f).sk_bitmap())); |
246 } | 254 } |
247 | 255 |
248 TEST_F(ImageTest, MultiResolutionPNGToPlatform) { | 256 TEST_F(ImageTest, MultiResolutionPNGToPlatform) { |
249 const int kSize1x = 25; | 257 const int kSize1x = 25; |
250 const int kSize2x = 50; | 258 const int kSize2x = 50; |
251 | 259 |
252 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); | 260 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); |
253 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); | 261 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); |
254 std::vector<gfx::ImagePNGRep> image_png_reps; | 262 std::vector<gfx::ImagePNGRep> image_png_reps; |
255 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); | 263 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, 1.0f)); |
256 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); | 264 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, 2.0f)); |
257 | 265 |
258 gfx::Image from_png(image_png_reps); | 266 gfx::Image from_png(image_png_reps); |
259 gfx::Image from_platform(gt::CopyPlatformType(from_png)); | 267 gfx::Image from_platform(gt::CopyPlatformType(from_png)); |
260 #if defined(OS_IOS) | 268 #if defined(OS_IOS) |
261 // On iOS the platform type (UIImage) only supports one resolution. | 269 // On iOS the platform type (UIImage) only supports one resolution. |
262 std::vector<ui::ScaleFactor> scale_factors = ui::GetSupportedScaleFactors(); | 270 std::vector<float> scales = gfx::ImageSkia::GetSupportedScales(); |
263 EXPECT_EQ(scale_factors.size(), 1U); | 271 EXPECT_EQ(scales.size(), 1U); |
264 if (scale_factors[0] == ui::SCALE_FACTOR_100P) | 272 if (scales[0] == 1.0f) |
265 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap())); | 273 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap())); |
266 else if (scale_factors[0] == ui::SCALE_FACTOR_200P) | 274 else if (scales[0] == 2.0f) |
267 EXPECT_TRUE(gt::IsEqual(bytes2x, from_platform.AsBitmap())); | 275 EXPECT_TRUE(gt::IsEqual(bytes2x, from_platform.AsBitmap())); |
268 else | 276 else |
269 ADD_FAILURE() << "Unexpected platform scale factor."; | 277 ADD_FAILURE() << "Unexpected platform scale factor."; |
270 #else | 278 #else |
271 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap())); | 279 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap())); |
272 #endif // defined(OS_IOS) | 280 #endif // defined(OS_IOS) |
273 } | 281 } |
274 | 282 |
275 | 283 |
276 TEST_F(ImageTest, PlatformToPNGEncodeAndDecode) { | 284 TEST_F(ImageTest, PlatformToPNGEncodeAndDecode) { |
277 gfx::Image image(gt::CreatePlatformImage()); | 285 gfx::Image image(gt::CreatePlatformImage()); |
278 scoped_refptr<base::RefCountedMemory> png_data = image.As1xPNGBytes(); | 286 scoped_refptr<base::RefCountedMemory> png_data = image.As1xPNGBytes(); |
279 EXPECT_TRUE(png_data.get()); | 287 EXPECT_TRUE(png_data.get()); |
280 EXPECT_TRUE(png_data->size()); | 288 EXPECT_TRUE(png_data->size()); |
281 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); | 289 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); |
282 | 290 |
283 std::vector<gfx::ImagePNGRep> image_png_reps; | 291 std::vector<gfx::ImagePNGRep> image_png_reps; |
284 image_png_reps.push_back(gfx::ImagePNGRep(png_data, ui::SCALE_FACTOR_100P)); | 292 image_png_reps.push_back(gfx::ImagePNGRep(png_data, 1.0f)); |
285 gfx::Image from_png(image_png_reps); | 293 gfx::Image from_png(image_png_reps); |
286 | 294 |
287 EXPECT_TRUE(from_png.HasRepresentation(gfx::Image::kImageRepPNG)); | 295 EXPECT_TRUE(from_png.HasRepresentation(gfx::Image::kImageRepPNG)); |
288 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png))); | 296 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png))); |
289 } | 297 } |
290 | 298 |
291 // The platform types use the platform provided encoding/decoding of PNGs. Make | 299 // The platform types use the platform provided encoding/decoding of PNGs. Make |
292 // sure these work with the Skia Encode/Decode. | 300 // sure these work with the Skia Encode/Decode. |
293 TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) { | 301 TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) { |
294 // Force the conversion sequence skia to png to platform_type. | 302 // Force the conversion sequence skia to png to platform_type. |
295 ui::ScaleFactor ideal_scale_factor = ui::GetScaleFactorFromScale(1.0f); | |
296 | |
297 gfx::Image from_bitmap = gfx::Image::CreateFrom1xBitmap( | 303 gfx::Image from_bitmap = gfx::Image::CreateFrom1xBitmap( |
298 gt::CreateBitmap(25, 25)); | 304 gt::CreateBitmap(25, 25)); |
299 scoped_refptr<base::RefCountedMemory> png_bytes = | 305 scoped_refptr<base::RefCountedMemory> png_bytes = |
300 from_bitmap.As1xPNGBytes(); | 306 from_bitmap.As1xPNGBytes(); |
301 | 307 |
302 std::vector<gfx::ImagePNGRep> image_png_reps; | 308 std::vector<gfx::ImagePNGRep> image_png_reps; |
303 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, ideal_scale_factor)); | 309 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, 1.0f)); |
304 gfx::Image from_png(image_png_reps); | 310 gfx::Image from_png(image_png_reps); |
305 | 311 |
306 gfx::Image from_platform(gt::CopyPlatformType(from_png)); | 312 gfx::Image from_platform(gt::CopyPlatformType(from_png)); |
307 | 313 |
308 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform))); | 314 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform))); |
309 EXPECT_TRUE(gt::IsEqual(png_bytes, from_platform.AsBitmap())); | 315 EXPECT_TRUE(gt::IsEqual(png_bytes, from_platform.AsBitmap())); |
310 } | 316 } |
311 | 317 |
312 TEST_F(ImageTest, PNGEncodeFromPlatformDecodeToSkia) { | 318 TEST_F(ImageTest, PNGEncodeFromPlatformDecodeToSkia) { |
313 // Force the conversion sequence platform_type to png to skia. | 319 // Force the conversion sequence platform_type to png to skia. |
314 gfx::Image from_platform(gt::CreatePlatformImage()); | 320 gfx::Image from_platform(gt::CreatePlatformImage()); |
315 scoped_refptr<base::RefCountedMemory> png_bytes = | 321 scoped_refptr<base::RefCountedMemory> png_bytes = |
316 from_platform.As1xPNGBytes(); | 322 from_platform.As1xPNGBytes(); |
317 std::vector<gfx::ImagePNGRep> image_png_reps; | 323 std::vector<gfx::ImagePNGRep> image_png_reps; |
318 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, ui::SCALE_FACTOR_100P)); | 324 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, 1.0f)); |
319 gfx::Image from_png(image_png_reps); | 325 gfx::Image from_png(image_png_reps); |
320 | 326 |
321 EXPECT_TRUE(gt::IsEqual(from_platform.AsBitmap(), from_png.AsBitmap())); | 327 EXPECT_TRUE(gt::IsEqual(from_platform.AsBitmap(), from_png.AsBitmap())); |
322 } | 328 } |
323 | 329 |
324 TEST_F(ImageTest, PNGDecodeToSkiaFailure) { | 330 TEST_F(ImageTest, PNGDecodeToSkiaFailure) { |
325 scoped_refptr<base::RefCountedBytes> invalid_bytes( | 331 scoped_refptr<base::RefCountedBytes> invalid_bytes( |
326 new base::RefCountedBytes()); | 332 new base::RefCountedBytes()); |
327 invalid_bytes->data().push_back('0'); | 333 invalid_bytes->data().push_back('0'); |
328 std::vector<gfx::ImagePNGRep> image_png_reps; | 334 std::vector<gfx::ImagePNGRep> image_png_reps; |
329 image_png_reps.push_back(gfx::ImagePNGRep( | 335 image_png_reps.push_back(gfx::ImagePNGRep( |
330 invalid_bytes, ui::SCALE_FACTOR_100P)); | 336 invalid_bytes, 1.0f)); |
331 gfx::Image image(image_png_reps); | 337 gfx::Image image(image_png_reps); |
332 gt::CheckImageIndicatesPNGDecodeFailure(image); | 338 gt::CheckImageIndicatesPNGDecodeFailure(image); |
333 } | 339 } |
334 | 340 |
335 TEST_F(ImageTest, PNGDecodeToPlatformFailure) { | 341 TEST_F(ImageTest, PNGDecodeToPlatformFailure) { |
336 scoped_refptr<base::RefCountedBytes> invalid_bytes( | 342 scoped_refptr<base::RefCountedBytes> invalid_bytes( |
337 new base::RefCountedBytes()); | 343 new base::RefCountedBytes()); |
338 invalid_bytes->data().push_back('0'); | 344 invalid_bytes->data().push_back('0'); |
339 std::vector<gfx::ImagePNGRep> image_png_reps; | 345 std::vector<gfx::ImagePNGRep> image_png_reps; |
340 image_png_reps.push_back(gfx::ImagePNGRep( | 346 image_png_reps.push_back(gfx::ImagePNGRep( |
341 invalid_bytes, ui::SCALE_FACTOR_100P)); | 347 invalid_bytes, 1.0f)); |
342 gfx::Image from_png(image_png_reps); | 348 gfx::Image from_png(image_png_reps); |
343 gfx::Image from_platform(gt::CopyPlatformType(from_png)); | 349 gfx::Image from_platform(gt::CopyPlatformType(from_png)); |
344 gt::CheckImageIndicatesPNGDecodeFailure(from_platform); | 350 gt::CheckImageIndicatesPNGDecodeFailure(from_platform); |
345 } | 351 } |
346 | 352 |
347 TEST_F(ImageTest, SkiaToPlatform) { | 353 TEST_F(ImageTest, SkiaToPlatform) { |
348 gfx::Image image(gt::CreateImageSkia(25, 25)); | 354 gfx::Image image(gt::CreateImageSkia(25, 25)); |
349 EXPECT_EQ(25, image.Width()); | 355 EXPECT_EQ(25, image.Width()); |
350 EXPECT_EQ(25, image.Height()); | 356 EXPECT_EQ(25, image.Height()); |
351 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; | 357 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 | 635 |
630 TEST_F(ImageTest, MultiResolutionImageSkia) { | 636 TEST_F(ImageTest, MultiResolutionImageSkia) { |
631 const int kWidth1x = 10; | 637 const int kWidth1x = 10; |
632 const int kHeight1x = 12; | 638 const int kHeight1x = 12; |
633 const int kWidth2x = 20; | 639 const int kWidth2x = 20; |
634 const int kHeight2x = 24; | 640 const int kHeight2x = 24; |
635 | 641 |
636 gfx::ImageSkia image_skia; | 642 gfx::ImageSkia image_skia; |
637 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 643 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
638 gt::CreateBitmap(kWidth1x, kHeight1x), | 644 gt::CreateBitmap(kWidth1x, kHeight1x), |
639 ui::SCALE_FACTOR_100P)); | 645 1.0f)); |
640 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 646 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
641 gt::CreateBitmap(kWidth2x, kHeight2x), | 647 gt::CreateBitmap(kWidth2x, kHeight2x), |
642 ui::SCALE_FACTOR_200P)); | 648 2.0f)); |
643 | 649 |
644 std::vector<ui::ScaleFactor> scale_factors; | 650 std::vector<float> scales; |
645 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 651 scales.push_back(1.0f); |
646 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 652 scales.push_back(2.0f); |
647 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kWidth1x, kHeight1x, | 653 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kWidth1x, kHeight1x, |
648 scale_factors)); | 654 scales)); |
649 | 655 |
650 // Check that the image has a single representation. | 656 // Check that the image has a single representation. |
651 gfx::Image image(image_skia); | 657 gfx::Image image(image_skia); |
652 EXPECT_EQ(1u, image.RepresentationCount()); | 658 EXPECT_EQ(1u, image.RepresentationCount()); |
653 EXPECT_EQ(kWidth1x, image.Width()); | 659 EXPECT_EQ(kWidth1x, image.Width()); |
654 EXPECT_EQ(kHeight1x, image.Height()); | 660 EXPECT_EQ(kHeight1x, image.Height()); |
655 } | 661 } |
656 | 662 |
657 TEST_F(ImageTest, RemoveFromMultiResolutionImageSkia) { | 663 TEST_F(ImageTest, RemoveFromMultiResolutionImageSkia) { |
658 const int kWidth2x = 20; | 664 const int kWidth2x = 20; |
659 const int kHeight2x = 24; | 665 const int kHeight2x = 24; |
660 | 666 |
661 gfx::ImageSkia image_skia; | 667 gfx::ImageSkia image_skia; |
662 | 668 |
663 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 669 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
664 gt::CreateBitmap(kWidth2x, kHeight2x), ui::SCALE_FACTOR_200P)); | 670 gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f)); |
665 EXPECT_EQ(1u, image_skia.image_reps().size()); | 671 EXPECT_EQ(1u, image_skia.image_reps().size()); |
666 | 672 |
667 image_skia.RemoveRepresentation(ui::SCALE_FACTOR_100P); | 673 image_skia.RemoveRepresentation(1.0f); |
668 EXPECT_EQ(1u, image_skia.image_reps().size()); | 674 EXPECT_EQ(1u, image_skia.image_reps().size()); |
669 | 675 |
670 image_skia.RemoveRepresentation(ui::SCALE_FACTOR_200P); | 676 image_skia.RemoveRepresentation(2.0f); |
671 EXPECT_EQ(0u, image_skia.image_reps().size()); | 677 EXPECT_EQ(0u, image_skia.image_reps().size()); |
672 } | 678 } |
673 | 679 |
674 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is | 680 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is |
675 // passed. | 681 // passed. |
676 TEST_F(ImageTest, OwnershipTest) { | 682 TEST_F(ImageTest, OwnershipTest) { |
677 gfx::Image image; | 683 gfx::Image image; |
678 { | 684 { |
679 SkBitmap bitmap(gt::CreateBitmap(10, 10)); | 685 SkBitmap bitmap(gt::CreateBitmap(10, 10)); |
680 EXPECT_TRUE(!bitmap.isNull()); | 686 EXPECT_TRUE(!bitmap.isNull()); |
681 image = gfx::Image(gfx::ImageSkia( | 687 image = gfx::Image(gfx::ImageSkia( |
682 gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P))); | 688 gfx::ImageSkiaRep(bitmap, 1.0f))); |
683 } | 689 } |
684 EXPECT_TRUE(!image.ToSkBitmap()->isNull()); | 690 EXPECT_TRUE(!image.ToSkBitmap()->isNull()); |
685 } | 691 } |
686 | 692 |
687 // Integration tests with UI toolkit frameworks require linking against the | 693 // Integration tests with UI toolkit frameworks require linking against the |
688 // Views library and cannot be here (ui_unittests doesn't include it). They | 694 // Views library and cannot be here (ui_unittests doesn't include it). They |
689 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. | 695 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. |
690 | 696 |
691 } // namespace | 697 } // namespace |
OLD | NEW |