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::ImageSkiaStructureMatches(image_skia, kSize1x, kSize1x,
248 EXPECT_TRUE(gt::IsEqual(bytes1x,
249 image_skia.GetRepresentation(1.0f).sk_bitmap()));
250 EXPECT_TRUE(gt::IsEqual(bytes2x,
251 image_skia.GetRepresentation(2.0f).sk_bitmap()));
254 TEST_F(ImageTest, MultiResolutionPNGToPlatform) {
255 const int kSize1x = 25;
256 const int kSize2x = 50;
258 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x);
259 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x);
260 std::vector<gfx::ImagePNGRep> image_png_reps;
261 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, 1.0f));
262 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, 2.0f));
264 gfx::Image from_png(image_png_reps);
265 gfx::Image from_platform(gt::CopyPlatformType(from_png));
267 // On iOS the platform type (UIImage) only supports one resolution.
268 std::vector<float> scales = gfx::ImageSkia::GetSupportedScales();
269 EXPECT_EQ(scales.size(), 1U);
270 if (scales[0] == 1.0f)
271 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap()));
272 else if (scales[0] == 2.0f)
273 EXPECT_TRUE(gt::IsEqual(bytes2x, from_platform.AsBitmap()));
275 ADD_FAILURE() << "Unexpected platform scale factor.";
277 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap()));
278 #endif // defined(OS_IOS)
282 TEST_F(ImageTest, PlatformToPNGEncodeAndDecode) {
283 gfx::Image image(gt::CreatePlatformImage());
284 scoped_refptr<base::RefCountedMemory> png_data = image.As1xPNGBytes();
285 EXPECT_TRUE(png_data.get());
286 EXPECT_TRUE(png_data->size());
287 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG));
289 std::vector<gfx::ImagePNGRep> image_png_reps;
290 image_png_reps.push_back(gfx::ImagePNGRep(png_data, 1.0f));
291 gfx::Image from_png(image_png_reps);
293 EXPECT_TRUE(from_png.HasRepresentation(gfx::Image::kImageRepPNG));
294 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png)));
297 // The platform types use the platform provided encoding/decoding of PNGs. Make
298 // sure these work with the Skia Encode/Decode.
299 TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) {
300 // Force the conversion sequence skia to png to platform_type.
301 gfx::Image from_bitmap = gfx::Image::CreateFrom1xBitmap(
302 gt::CreateBitmap(25, 25));
303 scoped_refptr<base::RefCountedMemory> png_bytes =
304 from_bitmap.As1xPNGBytes();
306 std::vector<gfx::ImagePNGRep> image_png_reps;
307 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, 1.0f));
308 gfx::Image from_png(image_png_reps);
310 gfx::Image from_platform(gt::CopyPlatformType(from_png));
312 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform)));
313 EXPECT_TRUE(gt::IsEqual(png_bytes, from_platform.AsBitmap()));
316 TEST_F(ImageTest, PNGEncodeFromPlatformDecodeToSkia) {
317 // Force the conversion sequence platform_type to png to skia.
318 gfx::Image from_platform(gt::CreatePlatformImage());
319 scoped_refptr<base::RefCountedMemory> png_bytes =
320 from_platform.As1xPNGBytes();
321 std::vector<gfx::ImagePNGRep> image_png_reps;
322 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, 1.0f));
323 gfx::Image from_png(image_png_reps);
325 EXPECT_TRUE(gt::IsEqual(from_platform.AsBitmap(), from_png.AsBitmap()));
328 TEST_F(ImageTest, PNGDecodeToSkiaFailure) {
329 scoped_refptr<base::RefCountedBytes> invalid_bytes(
330 new base::RefCountedBytes());
331 invalid_bytes->data().push_back('0');
332 std::vector<gfx::ImagePNGRep> image_png_reps;
333 image_png_reps.push_back(gfx::ImagePNGRep(
334 invalid_bytes, 1.0f));
335 gfx::Image image(image_png_reps);
336 gt::CheckImageIndicatesPNGDecodeFailure(image);
339 TEST_F(ImageTest, PNGDecodeToPlatformFailure) {
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 from_png(image_png_reps);
347 gfx::Image from_platform(gt::CopyPlatformType(from_png));
348 gt::CheckImageIndicatesPNGDecodeFailure(from_platform);
351 TEST_F(ImageTest, SkiaToPlatform) {
352 gfx::Image image(gt::CreateImageSkia(25, 25));
353 EXPECT_EQ(25, image.Width());
354 EXPECT_EQ(25, image.Height());
355 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
357 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
358 if (!kUsesSkiaNatively)
359 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
361 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
362 EXPECT_EQ(kRepCount, image.RepresentationCount());
364 const SkBitmap* bitmap = image.ToSkBitmap();
365 EXPECT_FALSE(bitmap->isNull());
366 EXPECT_EQ(kRepCount, image.RepresentationCount());
368 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
369 EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
370 EXPECT_EQ(25, image.Width());
371 EXPECT_EQ(25, image.Height());
374 TEST_F(ImageTest, PlatformToSkia) {
375 gfx::Image image(gt::CreatePlatformImage());
376 EXPECT_EQ(25, image.Width());
377 EXPECT_EQ(25, image.Height());
378 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
380 EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
381 if (!kUsesSkiaNatively)
382 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia));
384 const SkBitmap* bitmap = image.ToSkBitmap();
386 EXPECT_FALSE(bitmap->isNull());
387 EXPECT_EQ(kRepCount, image.RepresentationCount());
389 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
390 EXPECT_EQ(kRepCount, image.RepresentationCount());
392 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
393 EXPECT_EQ(25, image.Width());
394 EXPECT_EQ(25, image.Height());
397 TEST_F(ImageTest, PlatformToPlatform) {
398 gfx::Image image(gt::CreatePlatformImage());
399 EXPECT_EQ(25, image.Width());
400 EXPECT_EQ(25, image.Height());
401 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
402 EXPECT_EQ(1U, image.RepresentationCount());
404 // Make sure double conversion doesn't happen.
405 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
406 EXPECT_EQ(1U, image.RepresentationCount());
408 EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
409 if (!kUsesSkiaNatively)
410 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia));
411 EXPECT_EQ(25, image.Width());
412 EXPECT_EQ(25, image.Height());
415 TEST_F(ImageTest, PlatformToSkiaToCopy) {
416 const gfx::ImageSkia* image_skia = NULL;
418 gfx::Image image(gt::CreatePlatformImage());
419 image_skia = image.CopyImageSkia();
421 EXPECT_TRUE(image_skia);
422 EXPECT_FALSE(image_skia->isNull());
425 const SkBitmap* bitmap = NULL;
427 gfx::Image image(gt::CreatePlatformImage());
428 bitmap = image.CopySkBitmap();
432 EXPECT_FALSE(bitmap->isNull());
437 TEST_F(ImageTest, SkiaToCocoaTouchCopy) {
441 gfx::Image image(gt::CreateImageSkia(25, 25));
442 ui_image = image.CopyUIImage();
445 EXPECT_TRUE(ui_image);
446 base::mac::NSObjectRelease(ui_image);
448 #elif defined(OS_MACOSX)
449 TEST_F(ImageTest, SkiaToCocoaCopy) {
453 gfx::Image image(gt::CreateImageSkia(25, 25));
454 ns_image = image.CopyNSImage();
457 EXPECT_TRUE(ns_image);
458 base::mac::NSObjectRelease(ns_image);
462 TEST_F(ImageTest, CheckSkiaColor) {
463 gfx::Image image(gt::CreatePlatformImage());
465 const SkBitmap* bitmap = image.ToSkBitmap();
466 SkAutoLockPixels auto_lock(*bitmap);
467 gt::CheckColors(bitmap->getColor(10, 10), SK_ColorGREEN);
470 TEST_F(ImageTest, SkBitmapConversionPreservesOrientation) {
471 const int width = 50;
472 const int height = 50;
474 bitmap.allocN32Pixels(width, height);
475 bitmap.eraseRGB(0, 255, 0);
477 // Paint the upper half of the image in red (lower half is in green).
478 SkCanvas canvas(bitmap);
480 red.setColor(SK_ColorRED);
481 canvas.drawRect(SkRect::MakeWH(width, height / 2), red);
483 SCOPED_TRACE("Checking color of the initial SkBitmap");
484 gt::CheckColors(bitmap.getColor(10, 10), SK_ColorRED);
485 gt::CheckColors(bitmap.getColor(10, 40), SK_ColorGREEN);
488 // Convert from SkBitmap to a platform representation, then check the upper
489 // half of the platform image to make sure it is red, not green.
490 gfx::Image from_skbitmap = gfx::Image::CreateFrom1xBitmap(bitmap);
492 SCOPED_TRACE("Checking color of the platform image");
494 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10),
497 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40),
501 // Force a conversion back to SkBitmap and check that the upper half is red.
502 gfx::Image from_platform(gt::CopyPlatformType(from_skbitmap));
503 const SkBitmap* bitmap2 = from_platform.ToSkBitmap();
504 SkAutoLockPixels auto_lock(*bitmap2);
506 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
507 gt::CheckColors(bitmap2->getColor(10, 10), SK_ColorRED);
508 gt::CheckColors(bitmap2->getColor(10, 40), SK_ColorGREEN);
512 TEST_F(ImageTest, SkBitmapConversionPreservesTransparency) {
513 const int width = 50;
514 const int height = 50;
516 bitmap.allocN32Pixels(width, height);
517 bitmap.eraseARGB(0, 0, 255, 0);
519 // Paint the upper half of the image in red (lower half is transparent).
520 SkCanvas canvas(bitmap);
522 red.setColor(SK_ColorRED);
523 canvas.drawRect(SkRect::MakeWH(width, height / 2), red);
525 SCOPED_TRACE("Checking color of the initial SkBitmap");
526 gt::CheckColors(bitmap.getColor(10, 10), SK_ColorRED);
527 gt::CheckIsTransparent(bitmap.getColor(10, 40));
530 // Convert from SkBitmap to a platform representation, then check the upper
531 // half of the platform image to make sure it is red, not green.
532 gfx::Image from_skbitmap = gfx::Image::CreateFrom1xBitmap(bitmap);
534 SCOPED_TRACE("Checking color of the platform image");
536 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10),
538 gt::CheckIsTransparent(
539 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40));
542 // Force a conversion back to SkBitmap and check that the upper half is red.
543 gfx::Image from_platform(gt::CopyPlatformType(from_skbitmap));
544 const SkBitmap* bitmap2 = from_platform.ToSkBitmap();
545 SkAutoLockPixels auto_lock(*bitmap2);
547 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
548 gt::CheckColors(bitmap2->getColor(10, 10), SK_ColorRED);
549 gt::CheckIsTransparent(bitmap.getColor(10, 40));
553 TEST_F(ImageTest, SwapRepresentations) {
554 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
556 gfx::Image image1(gt::CreateImageSkia(25, 25));
557 const gfx::ImageSkia* image_skia1 = image1.ToImageSkia();
558 EXPECT_EQ(1U, image1.RepresentationCount());
560 gfx::Image image2(gt::CreatePlatformImage());
561 const gfx::ImageSkia* image_skia2 = image2.ToImageSkia();
562 gt::PlatformImage platform_image = gt::ToPlatformType(image2);
563 EXPECT_EQ(kRepCount, image2.RepresentationCount());
565 image1.SwapRepresentations(&image2);
567 EXPECT_EQ(image_skia2, image1.ToImageSkia());
568 EXPECT_TRUE(gt::PlatformImagesEqual(platform_image,
569 gt::ToPlatformType(image1)));
570 EXPECT_EQ(image_skia1, image2.ToImageSkia());
571 EXPECT_EQ(kRepCount, image1.RepresentationCount());
572 EXPECT_EQ(1U, image2.RepresentationCount());
575 TEST_F(ImageTest, Copy) {
576 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
578 gfx::Image image1(gt::CreateImageSkia(25, 25));
579 EXPECT_EQ(25, image1.Width());
580 EXPECT_EQ(25, image1.Height());
581 gfx::Image image2(image1);
582 EXPECT_EQ(25, image2.Width());
583 EXPECT_EQ(25, image2.Height());
585 EXPECT_EQ(1U, image1.RepresentationCount());
586 EXPECT_EQ(1U, image2.RepresentationCount());
587 EXPECT_EQ(image1.ToImageSkia(), image2.ToImageSkia());
589 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image2)));
590 EXPECT_EQ(kRepCount, image2.RepresentationCount());
591 EXPECT_EQ(kRepCount, image1.RepresentationCount());
594 TEST_F(ImageTest, Assign) {
595 gfx::Image image1(gt::CreatePlatformImage());
596 EXPECT_EQ(25, image1.Width());
597 EXPECT_EQ(25, image1.Height());
598 // Assignment must be on a separate line to the declaration in order to test
599 // assignment operator (instead of copy constructor).
602 EXPECT_EQ(25, image2.Width());
603 EXPECT_EQ(25, image2.Height());
605 EXPECT_EQ(1U, image1.RepresentationCount());
606 EXPECT_EQ(1U, image2.RepresentationCount());
607 EXPECT_EQ(image1.ToSkBitmap(), image2.ToSkBitmap());
610 TEST_F(ImageTest, MultiResolutionImageSkia) {
611 const int kWidth1x = 10;
612 const int kHeight1x = 12;
613 const int kWidth2x = 20;
614 const int kHeight2x = 24;
616 gfx::ImageSkia image_skia;
617 image_skia.AddRepresentation(gfx::ImageSkiaRep(
618 gt::CreateBitmap(kWidth1x, kHeight1x),
620 image_skia.AddRepresentation(gfx::ImageSkiaRep(
621 gt::CreateBitmap(kWidth2x, kHeight2x),
624 std::vector<float> scales;
625 scales.push_back(1.0f);
626 scales.push_back(2.0f);
627 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kWidth1x, kHeight1x,
630 // Check that the image has a single representation.
631 gfx::Image image(image_skia);
632 EXPECT_EQ(1u, image.RepresentationCount());
633 EXPECT_EQ(kWidth1x, image.Width());
634 EXPECT_EQ(kHeight1x, image.Height());
637 TEST_F(ImageTest, RemoveFromMultiResolutionImageSkia) {
638 const int kWidth2x = 20;
639 const int kHeight2x = 24;
641 gfx::ImageSkia image_skia;
643 image_skia.AddRepresentation(gfx::ImageSkiaRep(
644 gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f));
645 EXPECT_EQ(1u, image_skia.image_reps().size());
647 image_skia.RemoveRepresentation(1.0f);
648 EXPECT_EQ(1u, image_skia.image_reps().size());
650 image_skia.RemoveRepresentation(2.0f);
651 EXPECT_EQ(0u, image_skia.image_reps().size());
654 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is
656 TEST_F(ImageTest, OwnershipTest) {
659 SkBitmap bitmap(gt::CreateBitmap(10, 10));
660 EXPECT_TRUE(!bitmap.isNull());
661 image = gfx::Image(gfx::ImageSkia(
662 gfx::ImageSkiaRep(bitmap, 1.0f)));
664 EXPECT_TRUE(!image.ToSkBitmap()->isNull());
667 // Integration tests with UI toolkit frameworks require linking against the
668 // Views library and cannot be here (ui_unittests doesn't include it). They
669 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc.