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/foundation_util.h"
18 #include "skia/ext/skia_utils_mac.h"
23 #if defined(OS_IOS) || defined(OS_MACOSX)
24 const bool kUsesSkiaNatively
= false;
26 const bool kUsesSkiaNatively
= true;
29 class ImageTest
: public testing::Test
{
32 std::vector
<float> scales
;
33 scales
.push_back(1.0f
);
35 scales
.push_back(2.0f
);
37 gfx::ImageSkia::SetSupportedScales(scales
);
41 namespace gt
= gfx::test
;
43 TEST_F(ImageTest
, EmptyImage
) {
44 // Test the default constructor.
46 EXPECT_EQ(0U, image
.RepresentationCount());
47 EXPECT_TRUE(image
.IsEmpty());
48 EXPECT_EQ(0, image
.Width());
49 EXPECT_EQ(0, image
.Height());
51 // Test the copy constructor.
52 gfx::Image
imageCopy(image
);
53 EXPECT_TRUE(imageCopy
.IsEmpty());
54 EXPECT_EQ(0, imageCopy
.Width());
55 EXPECT_EQ(0, imageCopy
.Height());
57 // Test calling SwapRepresentations() with an empty image.
58 gfx::Image
image2(gt::CreateImageSkia(25, 25));
59 EXPECT_FALSE(image2
.IsEmpty());
60 EXPECT_EQ(25, image2
.Width());
61 EXPECT_EQ(25, image2
.Height());
63 image
.SwapRepresentations(&image2
);
64 EXPECT_FALSE(image
.IsEmpty());
65 EXPECT_EQ(25, image
.Width());
66 EXPECT_EQ(25, image
.Height());
67 EXPECT_TRUE(image2
.IsEmpty());
68 EXPECT_EQ(0, image2
.Width());
69 EXPECT_EQ(0, image2
.Height());
72 // Test constructing a gfx::Image from an empty PlatformImage.
73 TEST_F(ImageTest
, EmptyImageFromEmptyPlatformImage
) {
74 #if defined(OS_IOS) || defined(OS_MACOSX)
75 gfx::Image
image1(NULL
);
76 EXPECT_TRUE(image1
.IsEmpty());
77 EXPECT_EQ(0, image1
.Width());
78 EXPECT_EQ(0, image1
.Height());
79 EXPECT_EQ(0U, image1
.RepresentationCount());
82 // gfx::ImageSkia and gfx::ImagePNGRep are available on all platforms.
83 gfx::ImageSkia image_skia
;
84 EXPECT_TRUE(image_skia
.isNull());
85 gfx::Image
image2(image_skia
);
86 EXPECT_TRUE(image2
.IsEmpty());
87 EXPECT_EQ(0, image2
.Width());
88 EXPECT_EQ(0, image2
.Height());
89 EXPECT_EQ(0U, image2
.RepresentationCount());
91 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
92 gfx::Image
image3(image_png_reps
);
93 EXPECT_TRUE(image3
.IsEmpty());
94 EXPECT_EQ(0, image3
.Width());
95 EXPECT_EQ(0, image3
.Height());
96 EXPECT_EQ(0U, image3
.RepresentationCount());
99 // The resulting Image should be empty when it is created using obviously
101 TEST_F(ImageTest
, EmptyImageFromObviouslyInvalidPNGImage
) {
102 std::vector
<gfx::ImagePNGRep
> image_png_reps1
;
103 image_png_reps1
.push_back(gfx::ImagePNGRep(NULL
, 1.0f
));
104 gfx::Image
image1(image_png_reps1
);
105 EXPECT_TRUE(image1
.IsEmpty());
106 EXPECT_EQ(0U, image1
.RepresentationCount());
108 std::vector
<gfx::ImagePNGRep
> image_png_reps2
;
109 image_png_reps2
.push_back(gfx::ImagePNGRep(
110 new base::RefCountedBytes(), 1.0f
));
111 gfx::Image
image2(image_png_reps2
);
112 EXPECT_TRUE(image2
.IsEmpty());
113 EXPECT_EQ(0U, image2
.RepresentationCount());
116 // Test the Width, Height and Size of an empty and non-empty image.
117 TEST_F(ImageTest
, ImageSize
) {
119 EXPECT_EQ(0, image
.Width());
120 EXPECT_EQ(0, image
.Height());
121 EXPECT_EQ(gfx::Size(0, 0), image
.Size());
123 gfx::Image
image2(gt::CreateImageSkia(10, 25));
124 EXPECT_EQ(10, image2
.Width());
125 EXPECT_EQ(25, image2
.Height());
126 EXPECT_EQ(gfx::Size(10, 25), image2
.Size());
129 TEST_F(ImageTest
, SkiaToSkia
) {
130 gfx::Image
image(gt::CreateImageSkia(25, 25));
131 EXPECT_EQ(25, image
.Width());
132 EXPECT_EQ(25, image
.Height());
134 // Test ToImageSkia().
135 const gfx::ImageSkia
* image_skia1
= image
.ToImageSkia();
136 EXPECT_TRUE(image_skia1
);
137 EXPECT_FALSE(image_skia1
->isNull());
138 EXPECT_EQ(1U, image
.RepresentationCount());
140 // Make sure double conversion doesn't happen.
141 const gfx::ImageSkia
* image_skia2
= image
.ToImageSkia();
142 EXPECT_EQ(1U, image
.RepresentationCount());
144 // ToImageSkia() should always return the same gfx::ImageSkia.
145 EXPECT_EQ(image_skia1
, image_skia2
);
147 // Test ToSkBitmap().
148 const SkBitmap
* bitmap1
= image
.ToSkBitmap();
149 const SkBitmap
* bitmap2
= image
.ToSkBitmap();
150 EXPECT_TRUE(bitmap1
);
151 EXPECT_FALSE(bitmap1
->isNull());
152 EXPECT_EQ(bitmap1
, bitmap2
);
154 EXPECT_EQ(1U, image
.RepresentationCount());
155 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
156 if (!kUsesSkiaNatively
)
157 EXPECT_FALSE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
160 TEST_F(ImageTest
, EmptyImageToPNG
) {
162 scoped_refptr
<base::RefCountedMemory
> png_bytes
= image
.As1xPNGBytes();
163 EXPECT_TRUE(png_bytes
.get());
164 EXPECT_FALSE(png_bytes
->size());
167 // Check that getting the 1x PNG bytes from images which do not have a 1x
168 // representation returns NULL.
169 TEST_F(ImageTest
, ImageNo1xToPNG
) {
170 // Image with 2x only.
171 const int kSize2x
= 50;
172 gfx::ImageSkia image_skia
;
173 image_skia
.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap(
174 kSize2x
, kSize2x
), 2.0f
));
175 gfx::Image
image1(image_skia
);
176 scoped_refptr
<base::RefCountedMemory
> png_bytes1
= image1
.As1xPNGBytes();
177 EXPECT_TRUE(png_bytes1
.get());
178 EXPECT_FALSE(png_bytes1
->size());
180 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
181 image_png_reps
.push_back(gfx::ImagePNGRep(
182 gt::CreatePNGBytes(kSize2x
), 2.0f
));
183 gfx::Image
image2(image_png_reps
);
184 EXPECT_FALSE(image2
.IsEmpty());
185 EXPECT_EQ(0, image2
.Width());
186 EXPECT_EQ(0, image2
.Height());
187 scoped_refptr
<base::RefCountedMemory
> png_bytes2
= image2
.As1xPNGBytes();
188 EXPECT_TRUE(png_bytes2
.get());
189 EXPECT_FALSE(png_bytes2
->size());
192 // Check that for an image initialized with multi resolution PNG data,
193 // As1xPNGBytes() returns the 1x bytes.
194 TEST_F(ImageTest
, CreateExtractPNGBytes
) {
195 const int kSize1x
= 25;
196 const int kSize2x
= 50;
198 scoped_refptr
<base::RefCountedMemory
> bytes1x
= gt::CreatePNGBytes(kSize1x
);
199 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
200 image_png_reps
.push_back(gfx::ImagePNGRep(bytes1x
, 1.0f
));
201 image_png_reps
.push_back(gfx::ImagePNGRep(
202 gt::CreatePNGBytes(kSize2x
), 2.0f
));
204 gfx::Image
image(image_png_reps
);
205 EXPECT_FALSE(image
.IsEmpty());
206 EXPECT_EQ(25, image
.Width());
207 EXPECT_EQ(25, image
.Height());
209 EXPECT_TRUE(std::equal(bytes1x
->front(), bytes1x
->front() + bytes1x
->size(),
210 image
.As1xPNGBytes()->front()));
213 TEST_F(ImageTest
, MultiResolutionImageSkiaToPNG
) {
214 const int kSize1x
= 25;
215 const int kSize2x
= 50;
217 SkBitmap bitmap_1x
= gt::CreateBitmap(kSize1x
, kSize1x
);
218 gfx::ImageSkia image_skia
;
219 image_skia
.AddRepresentation(gfx::ImageSkiaRep(bitmap_1x
,
221 image_skia
.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap(
222 kSize2x
, kSize2x
), 2.0f
));
223 gfx::Image
image(image_skia
);
225 EXPECT_TRUE(gt::IsEqual(image
.As1xPNGBytes(), bitmap_1x
));
226 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepPNG
));
229 TEST_F(ImageTest
, MultiResolutionPNGToImageSkia
) {
230 const int kSize1x
= 25;
231 const int kSize2x
= 50;
233 scoped_refptr
<base::RefCountedMemory
> bytes1x
= gt::CreatePNGBytes(kSize1x
);
234 scoped_refptr
<base::RefCountedMemory
> bytes2x
= gt::CreatePNGBytes(kSize2x
);
236 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
237 image_png_reps
.push_back(gfx::ImagePNGRep(bytes1x
, 1.0f
));
238 image_png_reps
.push_back(gfx::ImagePNGRep(bytes2x
, 2.0f
));
239 gfx::Image
image(image_png_reps
);
241 std::vector
<float> scales
;
242 scales
.push_back(1.0f
);
243 scales
.push_back(2.0f
);
244 gfx::ImageSkia image_skia
= image
.AsImageSkia();
245 EXPECT_TRUE(gt::IsEqual(bytes1x
,
246 image_skia
.GetRepresentation(1.0f
).sk_bitmap()));
247 EXPECT_TRUE(gt::IsEqual(bytes2x
,
248 image_skia
.GetRepresentation(2.0f
).sk_bitmap()));
249 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia
, kSize1x
, kSize1x
,
252 // IOS does not support arbitrary scale factors.
253 gfx::ImageSkiaRep rep_1_6x
= image_skia
.GetRepresentation(1.6f
);
254 ASSERT_FALSE(rep_1_6x
.is_null());
255 ASSERT_EQ(1.6f
, rep_1_6x
.scale());
256 EXPECT_EQ("40x40", rep_1_6x
.pixel_size().ToString());
258 gfx::ImageSkiaRep rep_0_8x
= image_skia
.GetRepresentation(0.8f
);
259 ASSERT_FALSE(rep_0_8x
.is_null());
260 ASSERT_EQ(0.8f
, rep_0_8x
.scale());
261 EXPECT_EQ("20x20", rep_0_8x
.pixel_size().ToString());
265 TEST_F(ImageTest
, MultiResolutionPNGToPlatform
) {
266 const int kSize1x
= 25;
267 const int kSize2x
= 50;
269 scoped_refptr
<base::RefCountedMemory
> bytes1x
= gt::CreatePNGBytes(kSize1x
);
270 scoped_refptr
<base::RefCountedMemory
> bytes2x
= gt::CreatePNGBytes(kSize2x
);
271 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
272 image_png_reps
.push_back(gfx::ImagePNGRep(bytes1x
, 1.0f
));
273 image_png_reps
.push_back(gfx::ImagePNGRep(bytes2x
, 2.0f
));
275 gfx::Image
from_png(image_png_reps
);
276 gfx::Image
from_platform(gt::CopyPlatformType(from_png
));
278 // On iOS the platform type (UIImage) only supports one resolution.
279 std::vector
<float> scales
= gfx::ImageSkia::GetSupportedScales();
280 EXPECT_EQ(scales
.size(), 1U);
281 if (scales
[0] == 1.0f
)
282 EXPECT_TRUE(gt::IsEqual(bytes1x
, from_platform
.AsBitmap()));
283 else if (scales
[0] == 2.0f
)
284 EXPECT_TRUE(gt::IsEqual(bytes2x
, from_platform
.AsBitmap()));
286 ADD_FAILURE() << "Unexpected platform scale factor.";
288 EXPECT_TRUE(gt::IsEqual(bytes1x
, from_platform
.AsBitmap()));
289 #endif // defined(OS_IOS)
293 TEST_F(ImageTest
, PlatformToPNGEncodeAndDecode
) {
294 gfx::Image
image(gt::CreatePlatformImage());
295 scoped_refptr
<base::RefCountedMemory
> png_data
= image
.As1xPNGBytes();
296 EXPECT_TRUE(png_data
.get());
297 EXPECT_TRUE(png_data
->size());
298 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepPNG
));
300 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
301 image_png_reps
.push_back(gfx::ImagePNGRep(png_data
, 1.0f
));
302 gfx::Image
from_png(image_png_reps
);
304 EXPECT_TRUE(from_png
.HasRepresentation(gfx::Image::kImageRepPNG
));
305 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png
)));
308 // The platform types use the platform provided encoding/decoding of PNGs. Make
309 // sure these work with the Skia Encode/Decode.
310 TEST_F(ImageTest
, PNGEncodeFromSkiaDecodeToPlatform
) {
311 // Force the conversion sequence skia to png to platform_type.
312 gfx::Image from_bitmap
= gfx::Image::CreateFrom1xBitmap(
313 gt::CreateBitmap(25, 25));
314 scoped_refptr
<base::RefCountedMemory
> png_bytes
=
315 from_bitmap
.As1xPNGBytes();
317 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
318 image_png_reps
.push_back(gfx::ImagePNGRep(png_bytes
, 1.0f
));
319 gfx::Image
from_png(image_png_reps
);
321 gfx::Image
from_platform(gt::CopyPlatformType(from_png
));
323 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform
)));
324 EXPECT_TRUE(gt::IsEqual(png_bytes
, from_platform
.AsBitmap()));
327 TEST_F(ImageTest
, PNGEncodeFromPlatformDecodeToSkia
) {
328 // Force the conversion sequence platform_type to png to skia.
329 gfx::Image
from_platform(gt::CreatePlatformImage());
330 scoped_refptr
<base::RefCountedMemory
> png_bytes
=
331 from_platform
.As1xPNGBytes();
332 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
333 image_png_reps
.push_back(gfx::ImagePNGRep(png_bytes
, 1.0f
));
334 gfx::Image
from_png(image_png_reps
);
336 EXPECT_TRUE(gt::IsEqual(from_platform
.AsBitmap(), from_png
.AsBitmap()));
339 TEST_F(ImageTest
, PNGDecodeToSkiaFailure
) {
340 scoped_refptr
<base::RefCountedBytes
> invalid_bytes(
341 new base::RefCountedBytes());
342 invalid_bytes
->data().push_back('0');
343 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
344 image_png_reps
.push_back(gfx::ImagePNGRep(
345 invalid_bytes
, 1.0f
));
346 gfx::Image
image(image_png_reps
);
347 gt::CheckImageIndicatesPNGDecodeFailure(image
);
350 TEST_F(ImageTest
, PNGDecodeToPlatformFailure
) {
351 scoped_refptr
<base::RefCountedBytes
> invalid_bytes(
352 new base::RefCountedBytes());
353 invalid_bytes
->data().push_back('0');
354 std::vector
<gfx::ImagePNGRep
> image_png_reps
;
355 image_png_reps
.push_back(gfx::ImagePNGRep(
356 invalid_bytes
, 1.0f
));
357 gfx::Image
from_png(image_png_reps
);
358 gfx::Image
from_platform(gt::CopyPlatformType(from_png
));
359 gt::CheckImageIndicatesPNGDecodeFailure(from_platform
);
362 TEST_F(ImageTest
, SkiaToPlatform
) {
363 gfx::Image
image(gt::CreateImageSkia(25, 25));
364 EXPECT_EQ(25, image
.Width());
365 EXPECT_EQ(25, image
.Height());
366 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
368 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
369 if (!kUsesSkiaNatively
)
370 EXPECT_FALSE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
372 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
373 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
375 const SkBitmap
* bitmap
= image
.ToSkBitmap();
376 EXPECT_FALSE(bitmap
->isNull());
377 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
379 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
380 EXPECT_TRUE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
381 EXPECT_EQ(25, image
.Width());
382 EXPECT_EQ(25, image
.Height());
385 TEST_F(ImageTest
, PlatformToSkia
) {
386 gfx::Image
image(gt::CreatePlatformImage());
387 EXPECT_EQ(25, image
.Width());
388 EXPECT_EQ(25, image
.Height());
389 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
391 EXPECT_TRUE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
392 if (!kUsesSkiaNatively
)
393 EXPECT_FALSE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
395 const SkBitmap
* bitmap
= image
.ToSkBitmap();
397 EXPECT_FALSE(bitmap
->isNull());
398 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
400 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
401 EXPECT_EQ(kRepCount
, image
.RepresentationCount());
403 EXPECT_TRUE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
404 EXPECT_EQ(25, image
.Width());
405 EXPECT_EQ(25, image
.Height());
408 TEST_F(ImageTest
, PlatformToPlatform
) {
409 gfx::Image
image(gt::CreatePlatformImage());
410 EXPECT_EQ(25, image
.Width());
411 EXPECT_EQ(25, image
.Height());
412 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
413 EXPECT_EQ(1U, image
.RepresentationCount());
415 // Make sure double conversion doesn't happen.
416 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image
)));
417 EXPECT_EQ(1U, image
.RepresentationCount());
419 EXPECT_TRUE(image
.HasRepresentation(gt::GetPlatformRepresentationType()));
420 if (!kUsesSkiaNatively
)
421 EXPECT_FALSE(image
.HasRepresentation(gfx::Image::kImageRepSkia
));
422 EXPECT_EQ(25, image
.Width());
423 EXPECT_EQ(25, image
.Height());
426 TEST_F(ImageTest
, PlatformToSkiaToCopy
) {
427 const gfx::ImageSkia
* image_skia
= NULL
;
429 gfx::Image
image(gt::CreatePlatformImage());
430 image_skia
= image
.CopyImageSkia();
432 EXPECT_TRUE(image_skia
);
433 EXPECT_FALSE(image_skia
->isNull());
436 const SkBitmap
* bitmap
= NULL
;
438 gfx::Image
image(gt::CreatePlatformImage());
439 bitmap
= image
.CopySkBitmap();
443 EXPECT_FALSE(bitmap
->isNull());
448 TEST_F(ImageTest
, SkiaToCocoaTouchCopy
) {
452 gfx::Image
image(gt::CreateImageSkia(25, 25));
453 ui_image
= image
.CopyUIImage();
456 EXPECT_TRUE(ui_image
);
457 base::mac::NSObjectRelease(ui_image
);
459 #elif defined(OS_MACOSX)
460 TEST_F(ImageTest
, SkiaToCocoaCopy
) {
464 gfx::Image
image(gt::CreateImageSkia(25, 25));
465 ns_image
= image
.CopyNSImage();
468 EXPECT_TRUE(ns_image
);
469 base::mac::NSObjectRelease(ns_image
);
473 TEST_F(ImageTest
, CheckSkiaColor
) {
474 gfx::Image
image(gt::CreatePlatformImage());
476 const SkBitmap
* bitmap
= image
.ToSkBitmap();
477 SkAutoLockPixels
auto_lock(*bitmap
);
478 gt::CheckColors(bitmap
->getColor(10, 10), SK_ColorGREEN
);
481 TEST_F(ImageTest
, SkBitmapConversionPreservesOrientation
) {
482 const int width
= 50;
483 const int height
= 50;
485 bitmap
.allocN32Pixels(width
, height
);
486 bitmap
.eraseARGB(255, 0, 255, 0);
488 // Paint the upper half of the image in red (lower half is in green).
489 SkCanvas
canvas(bitmap
);
491 red
.setColor(SK_ColorRED
);
492 canvas
.drawRect(SkRect::MakeWH(width
, height
/ 2), red
);
494 SCOPED_TRACE("Checking color of the initial SkBitmap");
495 gt::CheckColors(bitmap
.getColor(10, 10), SK_ColorRED
);
496 gt::CheckColors(bitmap
.getColor(10, 40), SK_ColorGREEN
);
499 // Convert from SkBitmap to a platform representation, then check the upper
500 // half of the platform image to make sure it is red, not green.
501 gfx::Image from_skbitmap
= gfx::Image::CreateFrom1xBitmap(bitmap
);
503 SCOPED_TRACE("Checking color of the platform image");
505 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 10),
508 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 40),
512 // Force a conversion back to SkBitmap and check that the upper half is red.
513 gfx::Image
from_platform(gt::CopyPlatformType(from_skbitmap
));
514 const SkBitmap
* bitmap2
= from_platform
.ToSkBitmap();
515 SkAutoLockPixels
auto_lock(*bitmap2
);
517 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
518 gt::CheckColors(bitmap2
->getColor(10, 10), SK_ColorRED
);
519 gt::CheckColors(bitmap2
->getColor(10, 40), SK_ColorGREEN
);
523 TEST_F(ImageTest
, SkBitmapConversionPreservesTransparency
) {
524 const int width
= 50;
525 const int height
= 50;
527 bitmap
.allocN32Pixels(width
, height
);
528 bitmap
.eraseARGB(0, 0, 255, 0);
530 // Paint the upper half of the image in red (lower half is transparent).
531 SkCanvas
canvas(bitmap
);
533 red
.setColor(SK_ColorRED
);
534 canvas
.drawRect(SkRect::MakeWH(width
, height
/ 2), red
);
536 SCOPED_TRACE("Checking color of the initial SkBitmap");
537 gt::CheckColors(bitmap
.getColor(10, 10), SK_ColorRED
);
538 gt::CheckIsTransparent(bitmap
.getColor(10, 40));
541 // Convert from SkBitmap to a platform representation, then check the upper
542 // half of the platform image to make sure it is red, not green.
543 gfx::Image from_skbitmap
= gfx::Image::CreateFrom1xBitmap(bitmap
);
545 SCOPED_TRACE("Checking color of the platform image");
547 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 10),
549 gt::CheckIsTransparent(
550 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap
), 10, 40));
553 // Force a conversion back to SkBitmap and check that the upper half is red.
554 gfx::Image
from_platform(gt::CopyPlatformType(from_skbitmap
));
555 const SkBitmap
* bitmap2
= from_platform
.ToSkBitmap();
556 SkAutoLockPixels
auto_lock(*bitmap2
);
558 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
559 gt::CheckColors(bitmap2
->getColor(10, 10), SK_ColorRED
);
560 gt::CheckIsTransparent(bitmap
.getColor(10, 40));
564 TEST_F(ImageTest
, SwapRepresentations
) {
565 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
567 gfx::Image
image1(gt::CreateImageSkia(25, 25));
568 const gfx::ImageSkia
* image_skia1
= image1
.ToImageSkia();
569 EXPECT_EQ(1U, image1
.RepresentationCount());
571 gfx::Image
image2(gt::CreatePlatformImage());
572 const gfx::ImageSkia
* image_skia2
= image2
.ToImageSkia();
573 gt::PlatformImage platform_image
= gt::ToPlatformType(image2
);
574 EXPECT_EQ(kRepCount
, image2
.RepresentationCount());
576 image1
.SwapRepresentations(&image2
);
578 EXPECT_EQ(image_skia2
, image1
.ToImageSkia());
579 EXPECT_TRUE(gt::PlatformImagesEqual(platform_image
,
580 gt::ToPlatformType(image1
)));
581 EXPECT_EQ(image_skia1
, image2
.ToImageSkia());
582 EXPECT_EQ(kRepCount
, image1
.RepresentationCount());
583 EXPECT_EQ(1U, image2
.RepresentationCount());
586 TEST_F(ImageTest
, Copy
) {
587 const size_t kRepCount
= kUsesSkiaNatively
? 1U : 2U;
589 gfx::Image
image1(gt::CreateImageSkia(25, 25));
590 EXPECT_EQ(25, image1
.Width());
591 EXPECT_EQ(25, image1
.Height());
592 gfx::Image
image2(image1
);
593 EXPECT_EQ(25, image2
.Width());
594 EXPECT_EQ(25, image2
.Height());
596 EXPECT_EQ(1U, image1
.RepresentationCount());
597 EXPECT_EQ(1U, image2
.RepresentationCount());
598 EXPECT_EQ(image1
.ToImageSkia(), image2
.ToImageSkia());
600 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image2
)));
601 EXPECT_EQ(kRepCount
, image2
.RepresentationCount());
602 EXPECT_EQ(kRepCount
, image1
.RepresentationCount());
605 TEST_F(ImageTest
, Assign
) {
606 gfx::Image
image1(gt::CreatePlatformImage());
607 EXPECT_EQ(25, image1
.Width());
608 EXPECT_EQ(25, image1
.Height());
609 // Assignment must be on a separate line to the declaration in order to test
610 // assignment operator (instead of copy constructor).
613 EXPECT_EQ(25, image2
.Width());
614 EXPECT_EQ(25, image2
.Height());
616 EXPECT_EQ(1U, image1
.RepresentationCount());
617 EXPECT_EQ(1U, image2
.RepresentationCount());
618 EXPECT_EQ(image1
.ToSkBitmap(), image2
.ToSkBitmap());
621 TEST_F(ImageTest
, MultiResolutionImageSkia
) {
622 const int kWidth1x
= 10;
623 const int kHeight1x
= 12;
624 const int kWidth2x
= 20;
625 const int kHeight2x
= 24;
627 gfx::ImageSkia image_skia
;
628 image_skia
.AddRepresentation(gfx::ImageSkiaRep(
629 gt::CreateBitmap(kWidth1x
, kHeight1x
),
631 image_skia
.AddRepresentation(gfx::ImageSkiaRep(
632 gt::CreateBitmap(kWidth2x
, kHeight2x
),
635 std::vector
<float> scales
;
636 scales
.push_back(1.0f
);
637 scales
.push_back(2.0f
);
638 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia
, kWidth1x
, kHeight1x
,
641 // Check that the image has a single representation.
642 gfx::Image
image(image_skia
);
643 EXPECT_EQ(1u, image
.RepresentationCount());
644 EXPECT_EQ(kWidth1x
, image
.Width());
645 EXPECT_EQ(kHeight1x
, image
.Height());
648 TEST_F(ImageTest
, RemoveFromMultiResolutionImageSkia
) {
649 const int kWidth2x
= 20;
650 const int kHeight2x
= 24;
652 gfx::ImageSkia image_skia
;
654 image_skia
.AddRepresentation(gfx::ImageSkiaRep(
655 gt::CreateBitmap(kWidth2x
, kHeight2x
), 2.0f
));
656 EXPECT_EQ(1u, image_skia
.image_reps().size());
658 image_skia
.RemoveRepresentation(1.0f
);
659 EXPECT_EQ(1u, image_skia
.image_reps().size());
661 image_skia
.RemoveRepresentation(2.0f
);
662 EXPECT_EQ(0u, image_skia
.image_reps().size());
665 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is
667 TEST_F(ImageTest
, OwnershipTest
) {
670 SkBitmap
bitmap(gt::CreateBitmap(10, 10));
671 EXPECT_TRUE(!bitmap
.isNull());
672 image
= gfx::Image(gfx::ImageSkia(
673 gfx::ImageSkiaRep(bitmap
, 1.0f
)));
675 EXPECT_TRUE(!image
.ToSkBitmap()->isNull());
678 // Integration tests with UI toolkit frameworks require linking against the
679 // Views library and cannot be here (ui_base_unittests doesn't include it). They
680 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc.