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
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "third_party/skia/include/core/SkCanvas.h"
7 #include "third_party/skia/include/core/SkPaint.h"
8 #include "ui/gfx/image/image.h"
9 #include "ui/gfx/image/image_png_rep.h"
10 #include "ui/gfx/image/image_skia.h"
11 #include "ui/gfx/image/image_unittest_util.h"
14 #include "base/mac/foundation_util.h"
15 #include "skia/ext/skia_utils_ios.h"
16 #elif defined(OS_MACOSX)
17 #include "base/mac/mac_util.h"
18 #include "skia/ext/skia_utils_mac.h"
23 #if defined(TOOLKIT_VIEWS) || defined(OS_ANDROID) || \
24 (defined(OS_LINUX) && !defined(USE_CAIRO))
25 const bool kUsesSkiaNatively
= true;
27 const bool kUsesSkiaNatively
= false;
30 class ImageTest
: public testing::Test
{
33 std::vector
<float> scales
;
34 scales
.push_back(1.0f
);
36 scales
.push_back(2.0f
);
38 gfx::ImageSkia::SetSupportedScales(scales
);
42 namespace gt
= gfx::test
;
44 TEST_F(ImageTest
, EmptyImage
) {
45 // Test the default constructor.
47 EXPECT_EQ(0U, image
.RepresentationCount());
48 EXPECT_TRUE(image
.IsEmpty());
49 EXPECT_EQ(0, image
.Width());
50 EXPECT_EQ(0, image
.Height());
52 // Test the copy constructor.
53 gfx::Image
imageCopy(image
);
54 EXPECT_TRUE(imageCopy
.IsEmpty());
55 EXPECT_EQ(0, imageCopy
.Width());
56 EXPECT_EQ(0, imageCopy
.Height());
58 // Test calling SwapRepresentations() with an empty image.
59 gfx::Image
image2(gt::CreateImageSkia(25, 25));
60 EXPECT_FALSE(image2
.IsEmpty());
61 EXPECT_EQ(25, image2
.Width());
62 EXPECT_EQ(25, image2
.Height());
64 image
.SwapRepresentations(&image2
);
65 EXPECT_FALSE(image
.IsEmpty());
66 EXPECT_EQ(25, image
.Width());
67 EXPECT_EQ(25, image
.Height());
68 EXPECT_TRUE(image2
.IsEmpty());
69 EXPECT_EQ(0, image2
.Width());
70 EXPECT_EQ(0, image2
.Height());
73 // Test constructing a gfx::Image from an empty PlatformImage.
74 TEST_F(ImageTest
, EmptyImageFromEmptyPlatformImage
) {
75 #if defined(OS_IOS) || defined(OS_MACOSX)
76 gfx::Image
image1(NULL
);
77 EXPECT_TRUE(image1
.IsEmpty());
78 EXPECT_EQ(0, image1
.Width());
79 EXPECT_EQ(0, image1
.Height());
80 EXPECT_EQ(0U, image1
.RepresentationCount());
83 // gfx::ImageSkia and gfx::ImagePNGRep are available on all platforms.
84 gfx::ImageSkia image_skia
;
85 EXPECT_TRUE(image_skia
.isNull());
86 gfx::Image
image2(image_skia
);
87 EXPECT_TRUE(image2
.IsEmpty());
88 EXPECT_EQ(0, image2
.Width());
89 EXPECT_EQ(0, image2
.Height());
90 EXPECT_EQ(0U, image2
.RepresentationCount());
92 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
93 gfx::Image
image3(image_png_reps
);
94 EXPECT_TRUE(image3
.IsEmpty());
95 EXPECT_EQ(0, image3
.Width());
96 EXPECT_EQ(0, image3
.Height());
97 EXPECT_EQ(0U, image3
.RepresentationCount());
100 // The resulting Image should be empty when it is created using obviously
102 TEST_F(ImageTest
, EmptyImageFromObviouslyInvalidPNGImage
) {
103 std::vector
<gfx::ImagePNGRep
> image_png_reps1
;
104 image_png_reps1
.push_back(gfx::ImagePNGRep(NULL
, 1.0f
));
105 gfx::Image
image1(image_png_reps1
);
106 EXPECT_TRUE(image1
.IsEmpty());
107 EXPECT_EQ(0U, image1
.RepresentationCount());
109 std::vector
<gfx::ImagePNGRep
> image_png_reps2
;
110 image_png_reps2
.push_back(gfx::ImagePNGRep(
111 new base::RefCountedBytes(), 1.0f
));
112 gfx::Image
image2(image_png_reps2
);
113 EXPECT_TRUE(image2
.IsEmpty());
114 EXPECT_EQ(0U, image2
.RepresentationCount());
117 // Test the Width, Height and Size of an empty and non-empty image.
118 TEST_F(ImageTest
, ImageSize
) {
120 EXPECT_EQ(0, image
.Width());
121 EXPECT_EQ(0, image
.Height());
122 EXPECT_EQ(gfx::Size(0, 0), image
.Size());
124 gfx::Image
image2(gt::CreateImageSkia(10, 25));
125 EXPECT_EQ(10, image2
.Width());
126 EXPECT_EQ(25, image2
.Height());
127 EXPECT_EQ(gfx::Size(10, 25), image2
.Size());
130 TEST_F(ImageTest
, SkiaToSkia
) {
131 gfx::Image
image(gt::CreateImageSkia(25, 25));
132 EXPECT_EQ(25, image
.Width());
133 EXPECT_EQ(25, image
.Height());
135 // Test ToImageSkia().
136 const gfx::ImageSkia
* image_skia1
= image
.ToImageSkia();
137 EXPECT_TRUE(image_skia1
);
138 EXPECT_FALSE(image_skia1
->isNull());
139 EXPECT_EQ(1U, image
.RepresentationCount());
141 // Make sure double conversion doesn't happen.
142 const gfx::ImageSkia
* image_skia2
= image
.ToImageSkia();
143 EXPECT_EQ(1U, image
.RepresentationCount());
145 // ToImageSkia() should always return the same gfx::ImageSkia.
146 EXPECT_EQ(image_skia1
, image_skia2
);
148 // Test ToSkBitmap().
149 const SkBitmap
* bitmap1
= image
.ToSkBitmap();
150 const SkBitmap
* bitmap2
= image
.ToSkBitmap();
151 EXPECT_TRUE(bitmap1
);
152 EXPECT_FALSE(bitmap1
->isNull());
153 EXPECT_EQ(bitmap1
, bitmap2
);
155 EXPECT_EQ(1U, image
.RepresentationCount());
156 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
157 if (!kUsesSkiaNatively
)
158 EXPECT_FALSE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
161 TEST_F(ImageTest
, EmptyImageToPNG
) {
163 scoped_refptr
<base::RefCountedMemory
> png_bytes
= image
.As1xPNGBytes();
164 EXPECT_TRUE(png_bytes
.get());
165 EXPECT_FALSE(png_bytes
->size());
168 // Check that getting the 1x PNG bytes from images which do not have a 1x
169 // representation returns NULL.
170 TEST_F(ImageTest
, ImageNo1xToPNG
) {
171 // Image with 2x only.
172 const int kSize2x
= 50;
173 gfx::ImageSkia image_skia
;
174 image_skia
.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap(
175 kSize2x
, kSize2x
), 2.0f
));
176 gfx::Image
image1(image_skia
);
177 scoped_refptr
<base::RefCountedMemory
> png_bytes1
= image1
.As1xPNGBytes();
178 EXPECT_TRUE(png_bytes1
.get());
179 EXPECT_FALSE(png_bytes1
->size());
181 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
182 image_png_reps
.push_back(gfx::ImagePNGRep(
183 gt::CreatePNGBytes(kSize2x
), 2.0f
));
184 gfx::Image
image2(image_png_reps
);
185 EXPECT_FALSE(image2
.IsEmpty());
186 EXPECT_EQ(0, image2
.Width());
187 EXPECT_EQ(0, image2
.Height());
188 scoped_refptr
<base::RefCountedMemory
> png_bytes2
= image2
.As1xPNGBytes();
189 EXPECT_TRUE(png_bytes2
.get());
190 EXPECT_FALSE(png_bytes2
->size());
193 // Check that for an image initialized with multi resolution PNG data,
194 // As1xPNGBytes() returns the 1x bytes.
195 TEST_F(ImageTest
, CreateExtractPNGBytes
) {
196 const int kSize1x
= 25;
197 const int kSize2x
= 50;
199 scoped_refptr
<base::RefCountedMemory
> bytes1x
= gt::CreatePNGBytes(kSize1x
);
200 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
201 image_png_reps
.push_back(gfx::ImagePNGRep(bytes1x
, 1.0f
));
202 image_png_reps
.push_back(gfx::ImagePNGRep(
203 gt::CreatePNGBytes(kSize2x
), 2.0f
));
205 gfx::Image
image(image_png_reps
);
206 EXPECT_FALSE(image
.IsEmpty());
207 EXPECT_EQ(25, image
.Width());
208 EXPECT_EQ(25, image
.Height());
210 EXPECT_TRUE(std::equal(bytes1x
->front(), bytes1x
->front() + bytes1x
->size(),
211 image
.As1xPNGBytes()->front()));
214 TEST_F(ImageTest
, MultiResolutionImageSkiaToPNG
) {
215 const int kSize1x
= 25;
216 const int kSize2x
= 50;
218 SkBitmap bitmap_1x
= gt::CreateBitmap(kSize1x
, kSize1x
);
219 gfx::ImageSkia image_skia
;
220 image_skia
.AddRepresentation(gfx::ImageSkiaRep(bitmap_1x
,
222 image_skia
.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap(
223 kSize2x
, kSize2x
), 2.0f
));
224 gfx::Image
image(image_skia
);
226 EXPECT_TRUE(gt::IsEqual(image
.As1xPNGBytes(), bitmap_1x
));
227 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepPNG
));
230 TEST_F(ImageTest
, MultiResolutionPNGToImageSkia
) {
231 const int kSize1x
= 25;
232 const int kSize2x
= 50;
234 scoped_refptr
<base::RefCountedMemory
> bytes1x
= gt::CreatePNGBytes(kSize1x
);
235 scoped_refptr
<base::RefCountedMemory
> bytes2x
= gt::CreatePNGBytes(kSize2x
);
237 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
238 image_png_reps
.push_back(gfx::ImagePNGRep(bytes1x
, 1.0f
));
239 image_png_reps
.push_back(gfx::ImagePNGRep(bytes2x
, 2.0f
));
240 gfx::Image
image(image_png_reps
);
242 std::vector
<float> scales
;
243 scales
.push_back(1.0f
);
244 scales
.push_back(2.0f
);
245 gfx::ImageSkia image_skia
= image
.AsImageSkia();
246 EXPECT_TRUE(gt::IsEqual(bytes1x
,
247 image_skia
.GetRepresentation(1.0f
).sk_bitmap()));
248 EXPECT_TRUE(gt::IsEqual(bytes2x
,
249 image_skia
.GetRepresentation(2.0f
).sk_bitmap()));
250 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia
, kSize1x
, kSize1x
,
253 // IOS does not support arbitrary scale factors.
254 gfx::ImageSkiaRep rep_1_6x
= image_skia
.GetRepresentation(1.6f
);
255 ASSERT_FALSE(rep_1_6x
.is_null());
256 ASSERT_EQ(1.6f
, rep_1_6x
.scale());
257 EXPECT_EQ("40x40", rep_1_6x
.pixel_size().ToString());
259 gfx::ImageSkiaRep rep_0_8x
= image_skia
.GetRepresentation(0.8f
);
260 ASSERT_FALSE(rep_0_8x
.is_null());
261 ASSERT_EQ(0.8f
, rep_0_8x
.scale());
262 EXPECT_EQ("20x20", rep_0_8x
.pixel_size().ToString());
266 TEST_F(ImageTest
, MultiResolutionPNGToPlatform
) {
267 const int kSize1x
= 25;
268 const int kSize2x
= 50;
270 scoped_refptr
<base::RefCountedMemory
> bytes1x
= gt::CreatePNGBytes(kSize1x
);
271 scoped_refptr
<base::RefCountedMemory
> bytes2x
= gt::CreatePNGBytes(kSize2x
);
272 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
273 image_png_reps
.push_back(gfx::ImagePNGRep(bytes1x
, 1.0f
));
274 image_png_reps
.push_back(gfx::ImagePNGRep(bytes2x
, 2.0f
));
276 gfx::Image
from_png(image_png_reps
);
277 gfx::Image
from_platform(gt::CopyPlatformType(from_png
));
279 // On iOS the platform type (UIImage) only supports one resolution.
280 std::vector
<float> scales
= gfx::ImageSkia::GetSupportedScales();
281 EXPECT_EQ(scales
.size(), 1U);
282 if (scales
[0] == 1.0f
)
283 EXPECT_TRUE(gt::IsEqual(bytes1x
, from_platform
.AsBitmap()));
284 else if (scales
[0] == 2.0f
)
285 EXPECT_TRUE(gt::IsEqual(bytes2x
, from_platform
.AsBitmap()));
287 ADD_FAILURE() << "Unexpected platform scale factor.";
289 EXPECT_TRUE(gt::IsEqual(bytes1x
, from_platform
.AsBitmap()));
290 #endif // defined(OS_IOS)
294 TEST_F(ImageTest
, PlatformToPNGEncodeAndDecode
) {
295 gfx::Image
image(gt::CreatePlatformImage());
296 scoped_refptr
<base::RefCountedMemory
> png_data
= image
.As1xPNGBytes();
297 EXPECT_TRUE(png_data
.get());
298 EXPECT_TRUE(png_data
->size());
299 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepPNG
));
301 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
302 image_png_reps
.push_back(gfx::ImagePNGRep(png_data
, 1.0f
));
303 gfx::Image
from_png(image_png_reps
);
305 EXPECT_TRUE(from_png
.HasRepresentation(gfx::Image::kImageRepPNG
));
306 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png
)));
309 // The platform types use the platform provided encoding/decoding of PNGs. Make
310 // sure these work with the Skia Encode/Decode.
311 TEST_F(ImageTest
, PNGEncodeFromSkiaDecodeToPlatform
) {
312 // Force the conversion sequence skia to png to platform_type.
313 gfx::Image from_bitmap
= gfx::Image::CreateFrom1xBitmap(
314 gt::CreateBitmap(25, 25));
315 scoped_refptr
<base::RefCountedMemory
> png_bytes
=
316 from_bitmap
.As1xPNGBytes();
318 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
319 image_png_reps
.push_back(gfx::ImagePNGRep(png_bytes
, 1.0f
));
320 gfx::Image
from_png(image_png_reps
);
322 gfx::Image
from_platform(gt::CopyPlatformType(from_png
));
324 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform
)));
325 EXPECT_TRUE(gt::IsEqual(png_bytes
, from_platform
.AsBitmap()));
328 TEST_F(ImageTest
, PNGEncodeFromPlatformDecodeToSkia
) {
329 // Force the conversion sequence platform_type to png to skia.
330 gfx::Image
from_platform(gt::CreatePlatformImage());
331 scoped_refptr
<base::RefCountedMemory
> png_bytes
=
332 from_platform
.As1xPNGBytes();
333 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
334 image_png_reps
.push_back(gfx::ImagePNGRep(png_bytes
, 1.0f
));
335 gfx::Image
from_png(image_png_reps
);
337 EXPECT_TRUE(gt::IsEqual(from_platform
.AsBitmap(), from_png
.AsBitmap()));
340 TEST_F(ImageTest
, PNGDecodeToSkiaFailure
) {
341 scoped_refptr
<base::RefCountedBytes
> invalid_bytes(
342 new base::RefCountedBytes());
343 invalid_bytes
->data().push_back('0');
344 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
345 image_png_reps
.push_back(gfx::ImagePNGRep(
346 invalid_bytes
, 1.0f
));
347 gfx::Image
image(image_png_reps
);
348 gt::CheckImageIndicatesPNGDecodeFailure(image
);
351 TEST_F(ImageTest
, PNGDecodeToPlatformFailure
) {
352 scoped_refptr
<base::RefCountedBytes
> invalid_bytes(
353 new base::RefCountedBytes());
354 invalid_bytes
->data().push_back('0');
355 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
356 image_png_reps
.push_back(gfx::ImagePNGRep(
357 invalid_bytes
, 1.0f
));
358 gfx::Image
from_png(image_png_reps
);
359 gfx::Image
from_platform(gt::CopyPlatformType(from_png
));
360 gt::CheckImageIndicatesPNGDecodeFailure(from_platform
);
363 TEST_F(ImageTest
, SkiaToPlatform
) {
364 gfx::Image
image(gt::CreateImageSkia(25, 25));
365 EXPECT_EQ(25, image
.Width());
366 EXPECT_EQ(25, image
.Height());
367 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
369 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
370 if (!kUsesSkiaNatively
)
371 EXPECT_FALSE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
373 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
374 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
376 const SkBitmap
* bitmap
= image
.ToSkBitmap();
377 EXPECT_FALSE(bitmap
->isNull());
378 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
380 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
381 EXPECT_TRUE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
382 EXPECT_EQ(25, image
.Width());
383 EXPECT_EQ(25, image
.Height());
386 TEST_F(ImageTest
, PlatformToSkia
) {
387 gfx::Image
image(gt::CreatePlatformImage());
388 EXPECT_EQ(25, image
.Width());
389 EXPECT_EQ(25, image
.Height());
390 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
392 EXPECT_TRUE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
393 if (!kUsesSkiaNatively
)
394 EXPECT_FALSE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
396 const SkBitmap
* bitmap
= image
.ToSkBitmap();
398 EXPECT_FALSE(bitmap
->isNull());
399 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
401 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
402 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
404 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
405 EXPECT_EQ(25, image
.Width());
406 EXPECT_EQ(25, image
.Height());
409 TEST_F(ImageTest
, PlatformToPlatform
) {
410 gfx::Image
image(gt::CreatePlatformImage());
411 EXPECT_EQ(25, image
.Width());
412 EXPECT_EQ(25, image
.Height());
413 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
414 EXPECT_EQ(1U, image
.RepresentationCount());
416 // Make sure double conversion doesn't happen.
417 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
418 EXPECT_EQ(1U, image
.RepresentationCount());
420 EXPECT_TRUE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
421 if (!kUsesSkiaNatively
)
422 EXPECT_FALSE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
423 EXPECT_EQ(25, image
.Width());
424 EXPECT_EQ(25, image
.Height());
427 TEST_F(ImageTest
, PlatformToSkiaToCopy
) {
428 const gfx::ImageSkia
* image_skia
= NULL
;
430 gfx::Image
image(gt::CreatePlatformImage());
431 image_skia
= image
.CopyImageSkia();
433 EXPECT_TRUE(image_skia
);
434 EXPECT_FALSE(image_skia
->isNull());
437 const SkBitmap
* bitmap
= NULL
;
439 gfx::Image
image(gt::CreatePlatformImage());
440 bitmap
= image
.CopySkBitmap();
444 EXPECT_FALSE(bitmap
->isNull());
449 TEST_F(ImageTest
, SkiaToCocoaTouchCopy
) {
453 gfx::Image
image(gt::CreateImageSkia(25, 25));
454 ui_image
= image
.CopyUIImage();
457 EXPECT_TRUE(ui_image
);
458 base::mac::NSObjectRelease(ui_image
);
460 #elif defined(OS_MACOSX)
461 TEST_F(ImageTest
, SkiaToCocoaCopy
) {
465 gfx::Image
image(gt::CreateImageSkia(25, 25));
466 ns_image
= image
.CopyNSImage();
469 EXPECT_TRUE(ns_image
);
470 base::mac::NSObjectRelease(ns_image
);
474 TEST_F(ImageTest
, CheckSkiaColor
) {
475 gfx::Image
image(gt::CreatePlatformImage());
477 const SkBitmap
* bitmap
= image
.ToSkBitmap();
478 SkAutoLockPixels
auto_lock(*bitmap
);
479 gt::CheckColors(bitmap
->getColor(10, 10), SK_ColorGREEN
);
482 TEST_F(ImageTest
, SkBitmapConversionPreservesOrientation
) {
483 const int width
= 50;
484 const int height
= 50;
486 bitmap
.allocN32Pixels(width
, height
);
487 bitmap
.eraseARGB(255, 0, 255, 0);
489 // Paint the upper half of the image in red (lower half is in green).
490 SkCanvas
canvas(bitmap
);
492 red
.setColor(SK_ColorRED
);
493 canvas
.drawRect(SkRect::MakeWH(width
, height
/ 2), red
);
495 SCOPED_TRACE("Checking color of the initial SkBitmap");
496 gt::CheckColors(bitmap
.getColor(10, 10), SK_ColorRED
);
497 gt::CheckColors(bitmap
.getColor(10, 40), SK_ColorGREEN
);
500 // Convert from SkBitmap to a platform representation, then check the upper
501 // half of the platform image to make sure it is red, not green.
502 gfx::Image from_skbitmap
= gfx::Image::CreateFrom1xBitmap(bitmap
);
504 SCOPED_TRACE("Checking color of the platform image");
506 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 10),
509 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 40),
513 // Force a conversion back to SkBitmap and check that the upper half is red.
514 gfx::Image
from_platform(gt::CopyPlatformType(from_skbitmap
));
515 const SkBitmap
* bitmap2
= from_platform
.ToSkBitmap();
516 SkAutoLockPixels
auto_lock(*bitmap2
);
518 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
519 gt::CheckColors(bitmap2
->getColor(10, 10), SK_ColorRED
);
520 gt::CheckColors(bitmap2
->getColor(10, 40), SK_ColorGREEN
);
524 TEST_F(ImageTest
, SkBitmapConversionPreservesTransparency
) {
525 const int width
= 50;
526 const int height
= 50;
528 bitmap
.allocN32Pixels(width
, height
);
529 bitmap
.eraseARGB(0, 0, 255, 0);
531 // Paint the upper half of the image in red (lower half is transparent).
532 SkCanvas
canvas(bitmap
);
534 red
.setColor(SK_ColorRED
);
535 canvas
.drawRect(SkRect::MakeWH(width
, height
/ 2), red
);
537 SCOPED_TRACE("Checking color of the initial SkBitmap");
538 gt::CheckColors(bitmap
.getColor(10, 10), SK_ColorRED
);
539 gt::CheckIsTransparent(bitmap
.getColor(10, 40));
542 // Convert from SkBitmap to a platform representation, then check the upper
543 // half of the platform image to make sure it is red, not green.
544 gfx::Image from_skbitmap
= gfx::Image::CreateFrom1xBitmap(bitmap
);
546 SCOPED_TRACE("Checking color of the platform image");
548 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 10),
550 gt::CheckIsTransparent(
551 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 40));
554 // Force a conversion back to SkBitmap and check that the upper half is red.
555 gfx::Image
from_platform(gt::CopyPlatformType(from_skbitmap
));
556 const SkBitmap
* bitmap2
= from_platform
.ToSkBitmap();
557 SkAutoLockPixels
auto_lock(*bitmap2
);
559 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
560 gt::CheckColors(bitmap2
->getColor(10, 10), SK_ColorRED
);
561 gt::CheckIsTransparent(bitmap
.getColor(10, 40));
565 TEST_F(ImageTest
, SwapRepresentations
) {
566 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
568 gfx::Image
image1(gt::CreateImageSkia(25, 25));
569 const gfx::ImageSkia
* image_skia1
= image1
.ToImageSkia();
570 EXPECT_EQ(1U, image1
.RepresentationCount());
572 gfx::Image
image2(gt::CreatePlatformImage());
573 const gfx::ImageSkia
* image_skia2
= image2
.ToImageSkia();
574 gt::PlatformImage platform_image
= gt::ToPlatformType(image2
);
575 EXPECT_EQ(kRepCount
, image2
.RepresentationCount());
577 image1
.SwapRepresentations(&image2
);
579 EXPECT_EQ(image_skia2
, image1
.ToImageSkia());
580 EXPECT_TRUE(gt::PlatformImagesEqual(platform_image
,
581 gt::ToPlatformType(image1
)));
582 EXPECT_EQ(image_skia1
, image2
.ToImageSkia());
583 EXPECT_EQ(kRepCount
, image1
.RepresentationCount());
584 EXPECT_EQ(1U, image2
.RepresentationCount());
587 TEST_F(ImageTest
, Copy
) {
588 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
590 gfx::Image
image1(gt::CreateImageSkia(25, 25));
591 EXPECT_EQ(25, image1
.Width());
592 EXPECT_EQ(25, image1
.Height());
593 gfx::Image
image2(image1
);
594 EXPECT_EQ(25, image2
.Width());
595 EXPECT_EQ(25, image2
.Height());
597 EXPECT_EQ(1U, image1
.RepresentationCount());
598 EXPECT_EQ(1U, image2
.RepresentationCount());
599 EXPECT_EQ(image1
.ToImageSkia(), image2
.ToImageSkia());
601 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image2
)));
602 EXPECT_EQ(kRepCount
, image2
.RepresentationCount());
603 EXPECT_EQ(kRepCount
, image1
.RepresentationCount());
606 TEST_F(ImageTest
, Assign
) {
607 gfx::Image
image1(gt::CreatePlatformImage());
608 EXPECT_EQ(25, image1
.Width());
609 EXPECT_EQ(25, image1
.Height());
610 // Assignment must be on a separate line to the declaration in order to test
611 // assignment operator (instead of copy constructor).
614 EXPECT_EQ(25, image2
.Width());
615 EXPECT_EQ(25, image2
.Height());
617 EXPECT_EQ(1U, image1
.RepresentationCount());
618 EXPECT_EQ(1U, image2
.RepresentationCount());
619 EXPECT_EQ(image1
.ToSkBitmap(), image2
.ToSkBitmap());
622 TEST_F(ImageTest
, MultiResolutionImageSkia
) {
623 const int kWidth1x
= 10;
624 const int kHeight1x
= 12;
625 const int kWidth2x
= 20;
626 const int kHeight2x
= 24;
628 gfx::ImageSkia image_skia
;
629 image_skia
.AddRepresentation(gfx::ImageSkiaRep(
630 gt::CreateBitmap(kWidth1x
, kHeight1x
),
632 image_skia
.AddRepresentation(gfx::ImageSkiaRep(
633 gt::CreateBitmap(kWidth2x
, kHeight2x
),
636 std::vector
<float> scales
;
637 scales
.push_back(1.0f
);
638 scales
.push_back(2.0f
);
639 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia
, kWidth1x
, kHeight1x
,
642 // Check that the image has a single representation.
643 gfx::Image
image(image_skia
);
644 EXPECT_EQ(1u, image
.RepresentationCount());
645 EXPECT_EQ(kWidth1x
, image
.Width());
646 EXPECT_EQ(kHeight1x
, image
.Height());
649 TEST_F(ImageTest
, RemoveFromMultiResolutionImageSkia
) {
650 const int kWidth2x
= 20;
651 const int kHeight2x
= 24;
653 gfx::ImageSkia image_skia
;
655 image_skia
.AddRepresentation(gfx::ImageSkiaRep(
656 gt::CreateBitmap(kWidth2x
, kHeight2x
), 2.0f
));
657 EXPECT_EQ(1u, image_skia
.image_reps().size());
659 image_skia
.RemoveRepresentation(1.0f
);
660 EXPECT_EQ(1u, image_skia
.image_reps().size());
662 image_skia
.RemoveRepresentation(2.0f
);
663 EXPECT_EQ(0u, image_skia
.image_reps().size());
666 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is
668 TEST_F(ImageTest
, OwnershipTest
) {
671 SkBitmap
bitmap(gt::CreateBitmap(10, 10));
672 EXPECT_TRUE(!bitmap
.isNull());
673 image
= gfx::Image(gfx::ImageSkia(
674 gfx::ImageSkiaRep(bitmap
, 1.0f
)));
676 EXPECT_TRUE(!image
.ToSkBitmap()->isNull());
679 // Integration tests with UI toolkit frameworks require linking against the
680 // Views library and cannot be here (ui_unittests doesn't include it). They
681 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc.