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. 4 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/base/layout.h" 9 #include "ui/gfx/image/image.h" 10 #include "ui/gfx/image/image_png_rep.h" 11 #include "ui/gfx/image/image_skia.h" 12 #include "ui/gfx/image/image_unittest_util.h" 13 14 #if defined(TOOLKIT_GTK) 15 #include <gtk/gtk.h> 16 #include "ui/gfx/gtk_util.h" 17 #elif defined(OS_IOS) 18 #include "base/mac/foundation_util.h" 19 #include "skia/ext/skia_utils_ios.h" 20 #elif defined(OS_MACOSX) 21 #include "base/mac/mac_util.h" 22 #include "skia/ext/skia_utils_mac.h" 23 #endif 24 25 namespace { 26 27 #if defined(TOOLKIT_VIEWS) || defined(OS_ANDROID) 28 const bool kUsesSkiaNatively = true; 29 #else 30 const bool kUsesSkiaNatively = false; 31 #endif 32 33 class ImageTest : public testing::Test { 34 }; 35 36 namespace gt = gfx::test; 37 38 TEST_F(ImageTest, EmptyImage) { 39 // Test the default constructor. 40 gfx::Image image; 41 EXPECT_EQ(0U, image.RepresentationCount()); 42 EXPECT_TRUE(image.IsEmpty()); 43 EXPECT_EQ(0, image.Width()); 44 EXPECT_EQ(0, image.Height()); 45 46 // Test the copy constructor. 47 gfx::Image imageCopy(image); 48 EXPECT_TRUE(imageCopy.IsEmpty()); 49 EXPECT_EQ(0, imageCopy.Width()); 50 EXPECT_EQ(0, imageCopy.Height()); 51 52 // Test calling SwapRepresentations() with an empty image. 53 gfx::Image image2(gt::CreateImageSkia(25, 25)); 54 EXPECT_FALSE(image2.IsEmpty()); 55 EXPECT_EQ(25, image2.Width()); 56 EXPECT_EQ(25, image2.Height()); 57 58 image.SwapRepresentations(&image2); 59 EXPECT_FALSE(image.IsEmpty()); 60 EXPECT_EQ(25, image.Width()); 61 EXPECT_EQ(25, image.Height()); 62 EXPECT_TRUE(image2.IsEmpty()); 63 EXPECT_EQ(0, image2.Width()); 64 EXPECT_EQ(0, image2.Height()); 65 } 66 67 // Test constructing a gfx::Image from an empty PlatformImage. 68 TEST_F(ImageTest, EmptyImageFromEmptyPlatformImage) { 69 #if defined(OS_IOS) || defined(OS_MACOSX) || defined(TOOLKIT_GTK) 70 gfx::Image image1(NULL); 71 EXPECT_TRUE(image1.IsEmpty()); 72 EXPECT_EQ(0, image1.Width()); 73 EXPECT_EQ(0, image1.Height()); 74 EXPECT_EQ(0U, image1.RepresentationCount()); 75 #endif 76 77 // gfx::ImageSkia and gfx::ImagePNGRep are available on all platforms. 78 gfx::ImageSkia image_skia; 79 EXPECT_TRUE(image_skia.isNull()); 80 gfx::Image image2(image_skia); 81 EXPECT_TRUE(image2.IsEmpty()); 82 EXPECT_EQ(0, image2.Width()); 83 EXPECT_EQ(0, image2.Height()); 84 EXPECT_EQ(0U, image2.RepresentationCount()); 85 86 std::vector<gfx::ImagePNGRep> image_png_reps; 87 gfx::Image image3(image_png_reps); 88 EXPECT_TRUE(image3.IsEmpty()); 89 EXPECT_EQ(0, image3.Width()); 90 EXPECT_EQ(0, image3.Height()); 91 EXPECT_EQ(0U, image3.RepresentationCount()); 92 } 93 94 // The resulting Image should be empty when it is created using obviously 95 // invalid data. 96 TEST_F(ImageTest, EmptyImageFromObviouslyInvalidPNGImage) { 97 std::vector<gfx::ImagePNGRep> image_png_reps1; 98 image_png_reps1.push_back(gfx::ImagePNGRep(NULL, ui::SCALE_FACTOR_100P)); 99 gfx::Image image1(image_png_reps1); 100 EXPECT_TRUE(image1.IsEmpty()); 101 EXPECT_EQ(0U, image1.RepresentationCount()); 102 103 std::vector<gfx::ImagePNGRep> image_png_reps2; 104 image_png_reps2.push_back(gfx::ImagePNGRep( 105 new base::RefCountedBytes(), ui::SCALE_FACTOR_100P)); 106 gfx::Image image2(image_png_reps2); 107 EXPECT_TRUE(image2.IsEmpty()); 108 EXPECT_EQ(0U, image2.RepresentationCount()); 109 } 110 111 // Test the Width, Height and Size of an empty and non-empty image. 112 TEST_F(ImageTest, ImageSize) { 113 gfx::Image image; 114 EXPECT_EQ(0, image.Width()); 115 EXPECT_EQ(0, image.Height()); 116 EXPECT_EQ(gfx::Size(0, 0), image.Size()); 117 118 gfx::Image image2(gt::CreateImageSkia(10, 25)); 119 EXPECT_EQ(10, image2.Width()); 120 EXPECT_EQ(25, image2.Height()); 121 EXPECT_EQ(gfx::Size(10, 25), image2.Size()); 122 } 123 124 TEST_F(ImageTest, SkiaToSkia) { 125 gfx::Image image(gt::CreateImageSkia(25, 25)); 126 EXPECT_EQ(25, image.Width()); 127 EXPECT_EQ(25, image.Height()); 128 129 // Test ToImageSkia(). 130 const gfx::ImageSkia* image_skia1 = image.ToImageSkia(); 131 EXPECT_TRUE(image_skia1); 132 EXPECT_FALSE(image_skia1->isNull()); 133 EXPECT_EQ(1U, image.RepresentationCount()); 134 135 // Make sure double conversion doesn't happen. 136 const gfx::ImageSkia* image_skia2 = image.ToImageSkia(); 137 EXPECT_EQ(1U, image.RepresentationCount()); 138 139 // ToImageSkia() should always return the same gfx::ImageSkia. 140 EXPECT_EQ(image_skia1, image_skia2); 141 142 // Test ToSkBitmap(). 143 const SkBitmap* bitmap1 = image.ToSkBitmap(); 144 const SkBitmap* bitmap2 = image.ToSkBitmap(); 145 EXPECT_TRUE(bitmap1); 146 EXPECT_FALSE(bitmap1->isNull()); 147 EXPECT_EQ(bitmap1, bitmap2); 148 149 EXPECT_EQ(1U, image.RepresentationCount()); 150 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 151 if (!kUsesSkiaNatively) 152 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); 153 } 154 155 TEST_F(ImageTest, EmptyImageToPNG) { 156 gfx::Image image; 157 scoped_refptr<base::RefCountedMemory> png_bytes = image.As1xPNGBytes(); 158 EXPECT_TRUE(png_bytes.get()); 159 EXPECT_FALSE(png_bytes->size()); 160 } 161 162 // Check that getting the 1x PNG bytes from images which do not have a 1x 163 // representation returns NULL. 164 TEST_F(ImageTest, ImageNo1xToPNG) { 165 // Image with 2x only. 166 const int kSize2x = 50; 167 gfx::ImageSkia image_skia; 168 image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( 169 kSize2x, kSize2x), ui::SCALE_FACTOR_200P)); 170 gfx::Image image1(image_skia); 171 scoped_refptr<base::RefCountedMemory> png_bytes1 = image1.As1xPNGBytes(); 172 EXPECT_TRUE(png_bytes1.get()); 173 EXPECT_FALSE(png_bytes1->size()); 174 175 std::vector<gfx::ImagePNGRep> image_png_reps; 176 image_png_reps.push_back(gfx::ImagePNGRep( 177 gt::CreatePNGBytes(kSize2x), ui::SCALE_FACTOR_200P)); 178 gfx::Image image2(image_png_reps); 179 EXPECT_FALSE(image2.IsEmpty()); 180 EXPECT_EQ(0, image2.Width()); 181 EXPECT_EQ(0, image2.Height()); 182 scoped_refptr<base::RefCountedMemory> png_bytes2 = image2.As1xPNGBytes(); 183 EXPECT_TRUE(png_bytes2.get()); 184 EXPECT_FALSE(png_bytes2->size()); 185 } 186 187 // Check that for an image initialized with multi resolution PNG data, 188 // As1xPNGBytes() returns the 1x bytes. 189 TEST_F(ImageTest, CreateExtractPNGBytes) { 190 const int kSize1x = 25; 191 const int kSize2x = 50; 192 193 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); 194 std::vector<gfx::ImagePNGRep> image_png_reps; 195 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); 196 image_png_reps.push_back(gfx::ImagePNGRep( 197 gt::CreatePNGBytes(kSize2x), ui::SCALE_FACTOR_200P)); 198 199 gfx::Image image(image_png_reps); 200 EXPECT_FALSE(image.IsEmpty()); 201 EXPECT_EQ(25, image.Width()); 202 EXPECT_EQ(25, image.Height()); 203 204 EXPECT_TRUE(std::equal(bytes1x->front(), bytes1x->front() + bytes1x->size(), 205 image.As1xPNGBytes()->front())); 206 } 207 208 TEST_F(ImageTest, MultiResolutionImageSkiaToPNG) { 209 const int kSize1x = 25; 210 const int kSize2x = 50; 211 212 SkBitmap bitmap_1x = gt::CreateBitmap(kSize1x, kSize1x); 213 gfx::ImageSkia image_skia; 214 image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap_1x, 215 ui::SCALE_FACTOR_100P)); 216 image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( 217 kSize2x, kSize2x), ui::SCALE_FACTOR_200P)); 218 gfx::Image image(image_skia); 219 220 EXPECT_TRUE(gt::IsEqual(image.As1xPNGBytes(), bitmap_1x)); 221 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); 222 } 223 224 TEST_F(ImageTest, MultiResolutionPNGToImageSkia) { 225 const int kSize1x = 25; 226 const int kSize2x = 50; 227 228 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); 229 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); 230 231 std::vector<gfx::ImagePNGRep> image_png_reps; 232 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); 233 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); 234 gfx::Image image(image_png_reps); 235 236 std::vector<ui::ScaleFactor> scale_factors; 237 scale_factors.push_back(ui::SCALE_FACTOR_100P); 238 scale_factors.push_back(ui::SCALE_FACTOR_200P); 239 gfx::ImageSkia image_skia = image.AsImageSkia(); 240 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kSize1x, kSize1x, 241 scale_factors)); 242 EXPECT_TRUE(gt::IsEqual(bytes1x, 243 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); 244 EXPECT_TRUE(gt::IsEqual(bytes2x, 245 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P).sk_bitmap())); 246 } 247 248 TEST_F(ImageTest, MultiResolutionPNGToPlatform) { 249 const int kSize1x = 25; 250 const int kSize2x = 50; 251 252 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); 253 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); 254 std::vector<gfx::ImagePNGRep> image_png_reps; 255 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); 256 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); 257 258 gfx::Image from_png(image_png_reps); 259 gfx::Image from_platform(gt::CopyPlatformType(from_png)); 260 #if defined(OS_IOS) 261 // On iOS the platform type (UIImage) only supports one resolution. 262 std::vector<ui::ScaleFactor> scale_factors = ui::GetSupportedScaleFactors(); 263 EXPECT_EQ(scale_factors.size(), 1U); 264 if (scale_factors[0] == ui::SCALE_FACTOR_100P) 265 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap())); 266 else if (scale_factors[0] == ui::SCALE_FACTOR_200P) 267 EXPECT_TRUE(gt::IsEqual(bytes2x, from_platform.AsBitmap())); 268 else 269 ADD_FAILURE() << "Unexpected platform scale factor."; 270 #else 271 EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap())); 272 #endif // defined(OS_IOS) 273 } 274 275 276 TEST_F(ImageTest, PlatformToPNGEncodeAndDecode) { 277 gfx::Image image(gt::CreatePlatformImage()); 278 scoped_refptr<base::RefCountedMemory> png_data = image.As1xPNGBytes(); 279 EXPECT_TRUE(png_data.get()); 280 EXPECT_TRUE(png_data->size()); 281 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); 282 283 std::vector<gfx::ImagePNGRep> image_png_reps; 284 image_png_reps.push_back(gfx::ImagePNGRep(png_data, ui::SCALE_FACTOR_100P)); 285 gfx::Image from_png(image_png_reps); 286 287 EXPECT_TRUE(from_png.HasRepresentation(gfx::Image::kImageRepPNG)); 288 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png))); 289 } 290 291 // The platform types use the platform provided encoding/decoding of PNGs. Make 292 // sure these work with the Skia Encode/Decode. 293 TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) { 294 // 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( 298 gt::CreateBitmap(25, 25)); 299 scoped_refptr<base::RefCountedMemory> png_bytes = 300 from_bitmap.As1xPNGBytes(); 301 302 std::vector<gfx::ImagePNGRep> image_png_reps; 303 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, ideal_scale_factor)); 304 gfx::Image from_png(image_png_reps); 305 306 gfx::Image from_platform(gt::CopyPlatformType(from_png)); 307 308 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform))); 309 EXPECT_TRUE(gt::IsEqual(png_bytes, from_platform.AsBitmap())); 310 } 311 312 TEST_F(ImageTest, PNGEncodeFromPlatformDecodeToSkia) { 313 // Force the conversion sequence platform_type to png to skia. 314 gfx::Image from_platform(gt::CreatePlatformImage()); 315 scoped_refptr<base::RefCountedMemory> png_bytes = 316 from_platform.As1xPNGBytes(); 317 std::vector<gfx::ImagePNGRep> image_png_reps; 318 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, ui::SCALE_FACTOR_100P)); 319 gfx::Image from_png(image_png_reps); 320 321 EXPECT_TRUE(gt::IsEqual(from_platform.AsBitmap(), from_png.AsBitmap())); 322 } 323 324 TEST_F(ImageTest, PNGDecodeToSkiaFailure) { 325 scoped_refptr<base::RefCountedBytes> invalid_bytes( 326 new base::RefCountedBytes()); 327 invalid_bytes->data().push_back('0'); 328 std::vector<gfx::ImagePNGRep> image_png_reps; 329 image_png_reps.push_back(gfx::ImagePNGRep( 330 invalid_bytes, ui::SCALE_FACTOR_100P)); 331 gfx::Image image(image_png_reps); 332 gt::CheckImageIndicatesPNGDecodeFailure(image); 333 } 334 335 TEST_F(ImageTest, PNGDecodeToPlatformFailure) { 336 scoped_refptr<base::RefCountedBytes> invalid_bytes( 337 new base::RefCountedBytes()); 338 invalid_bytes->data().push_back('0'); 339 std::vector<gfx::ImagePNGRep> image_png_reps; 340 image_png_reps.push_back(gfx::ImagePNGRep( 341 invalid_bytes, ui::SCALE_FACTOR_100P)); 342 gfx::Image from_png(image_png_reps); 343 gfx::Image from_platform(gt::CopyPlatformType(from_png)); 344 gt::CheckImageIndicatesPNGDecodeFailure(from_platform); 345 } 346 347 TEST_F(ImageTest, SkiaToPlatform) { 348 gfx::Image image(gt::CreateImageSkia(25, 25)); 349 EXPECT_EQ(25, image.Width()); 350 EXPECT_EQ(25, image.Height()); 351 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; 352 353 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 354 if (!kUsesSkiaNatively) 355 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); 356 357 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); 358 EXPECT_EQ(kRepCount, image.RepresentationCount()); 359 360 const SkBitmap* bitmap = image.ToSkBitmap(); 361 EXPECT_FALSE(bitmap->isNull()); 362 EXPECT_EQ(kRepCount, image.RepresentationCount()); 363 364 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 365 EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType())); 366 EXPECT_EQ(25, image.Width()); 367 EXPECT_EQ(25, image.Height()); 368 } 369 370 TEST_F(ImageTest, PlatformToSkia) { 371 gfx::Image image(gt::CreatePlatformImage()); 372 EXPECT_EQ(25, image.Width()); 373 EXPECT_EQ(25, image.Height()); 374 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; 375 376 EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType())); 377 if (!kUsesSkiaNatively) 378 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 379 380 const SkBitmap* bitmap = image.ToSkBitmap(); 381 EXPECT_TRUE(bitmap); 382 EXPECT_FALSE(bitmap->isNull()); 383 EXPECT_EQ(kRepCount, image.RepresentationCount()); 384 385 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); 386 EXPECT_EQ(kRepCount, image.RepresentationCount()); 387 388 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 389 EXPECT_EQ(25, image.Width()); 390 EXPECT_EQ(25, image.Height()); 391 } 392 393 TEST_F(ImageTest, PlatformToPlatform) { 394 gfx::Image image(gt::CreatePlatformImage()); 395 EXPECT_EQ(25, image.Width()); 396 EXPECT_EQ(25, image.Height()); 397 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); 398 EXPECT_EQ(1U, image.RepresentationCount()); 399 400 // Make sure double conversion doesn't happen. 401 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); 402 EXPECT_EQ(1U, image.RepresentationCount()); 403 404 EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType())); 405 if (!kUsesSkiaNatively) 406 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 407 EXPECT_EQ(25, image.Width()); 408 EXPECT_EQ(25, image.Height()); 409 } 410 411 TEST_F(ImageTest, PlatformToSkiaToCopy) { 412 const gfx::ImageSkia* image_skia = NULL; 413 { 414 gfx::Image image(gt::CreatePlatformImage()); 415 image_skia = image.CopyImageSkia(); 416 } 417 EXPECT_TRUE(image_skia); 418 EXPECT_FALSE(image_skia->isNull()); 419 delete image_skia; 420 421 const SkBitmap* bitmap = NULL; 422 { 423 gfx::Image image(gt::CreatePlatformImage()); 424 bitmap = image.CopySkBitmap(); 425 } 426 427 EXPECT_TRUE(bitmap); 428 EXPECT_FALSE(bitmap->isNull()); 429 delete bitmap; 430 } 431 432 #if defined(TOOLKIT_GTK) 433 TEST_F(ImageTest, SkiaToGdkCopy) { 434 GdkPixbuf* pixbuf; 435 436 { 437 gfx::Image image(gt::CreateImageSkia(25, 25)); 438 pixbuf = image.CopyGdkPixbuf(); 439 } 440 441 EXPECT_TRUE(pixbuf); 442 g_object_unref(pixbuf); 443 } 444 445 TEST_F(ImageTest, SkiaToCairoCreatesGdk) { 446 gfx::Image image(gt::CreateImageSkia(25, 25)); 447 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepGdk)); 448 EXPECT_TRUE(image.ToCairo()); 449 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepGdk)); 450 } 451 #endif 452 453 #if defined(OS_IOS) 454 TEST_F(ImageTest, SkiaToCocoaTouchCopy) { 455 UIImage* ui_image; 456 457 { 458 gfx::Image image(gt::CreateImageSkia(25, 25)); 459 ui_image = image.CopyUIImage(); 460 } 461 462 EXPECT_TRUE(ui_image); 463 base::mac::NSObjectRelease(ui_image); 464 } 465 #elif defined(OS_MACOSX) 466 TEST_F(ImageTest, SkiaToCocoaCopy) { 467 NSImage* ns_image; 468 469 { 470 gfx::Image image(gt::CreateImageSkia(25, 25)); 471 ns_image = image.CopyNSImage(); 472 } 473 474 EXPECT_TRUE(ns_image); 475 base::mac::NSObjectRelease(ns_image); 476 } 477 #endif 478 479 TEST_F(ImageTest, CheckSkiaColor) { 480 gfx::Image image(gt::CreatePlatformImage()); 481 482 const SkBitmap* bitmap = image.ToSkBitmap(); 483 SkAutoLockPixels auto_lock(*bitmap); 484 gt::CheckColors(bitmap->getColor(10, 10), SK_ColorGREEN); 485 } 486 487 TEST_F(ImageTest, SkBitmapConversionPreservesOrientation) { 488 const int width = 50; 489 const int height = 50; 490 SkBitmap bitmap; 491 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 492 bitmap.allocPixels(); 493 bitmap.eraseRGB(0, 255, 0); 494 495 // Paint the upper half of the image in red (lower half is in green). 496 SkCanvas canvas(bitmap); 497 SkPaint red; 498 red.setColor(SK_ColorRED); 499 canvas.drawRect(SkRect::MakeWH(width, height / 2), red); 500 { 501 SCOPED_TRACE("Checking color of the initial SkBitmap"); 502 gt::CheckColors(bitmap.getColor(10, 10), SK_ColorRED); 503 gt::CheckColors(bitmap.getColor(10, 40), SK_ColorGREEN); 504 } 505 506 // Convert from SkBitmap to a platform representation, then check the upper 507 // half of the platform image to make sure it is red, not green. 508 gfx::Image from_skbitmap = gfx::Image::CreateFrom1xBitmap(bitmap); 509 { 510 SCOPED_TRACE("Checking color of the platform image"); 511 gt::CheckColors( 512 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10), 513 SK_ColorRED); 514 gt::CheckColors( 515 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40), 516 SK_ColorGREEN); 517 } 518 519 // Force a conversion back to SkBitmap and check that the upper half is red. 520 gfx::Image from_platform(gt::CopyPlatformType(from_skbitmap)); 521 const SkBitmap* bitmap2 = from_platform.ToSkBitmap(); 522 SkAutoLockPixels auto_lock(*bitmap2); 523 { 524 SCOPED_TRACE("Checking color after conversion back to SkBitmap"); 525 gt::CheckColors(bitmap2->getColor(10, 10), SK_ColorRED); 526 gt::CheckColors(bitmap2->getColor(10, 40), SK_ColorGREEN); 527 } 528 } 529 530 TEST_F(ImageTest, SkBitmapConversionPreservesTransparency) { 531 const int width = 50; 532 const int height = 50; 533 SkBitmap bitmap; 534 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 535 bitmap.allocPixels(); 536 bitmap.setIsOpaque(false); 537 bitmap.eraseARGB(0, 0, 255, 0); 538 539 // Paint the upper half of the image in red (lower half is transparent). 540 SkCanvas canvas(bitmap); 541 SkPaint red; 542 red.setColor(SK_ColorRED); 543 canvas.drawRect(SkRect::MakeWH(width, height / 2), red); 544 { 545 SCOPED_TRACE("Checking color of the initial SkBitmap"); 546 gt::CheckColors(bitmap.getColor(10, 10), SK_ColorRED); 547 gt::CheckIsTransparent(bitmap.getColor(10, 40)); 548 } 549 550 // Convert from SkBitmap to a platform representation, then check the upper 551 // half of the platform image to make sure it is red, not green. 552 gfx::Image from_skbitmap = gfx::Image::CreateFrom1xBitmap(bitmap); 553 { 554 SCOPED_TRACE("Checking color of the platform image"); 555 gt::CheckColors( 556 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10), 557 SK_ColorRED); 558 gt::CheckIsTransparent( 559 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40)); 560 } 561 562 // Force a conversion back to SkBitmap and check that the upper half is red. 563 gfx::Image from_platform(gt::CopyPlatformType(from_skbitmap)); 564 const SkBitmap* bitmap2 = from_platform.ToSkBitmap(); 565 SkAutoLockPixels auto_lock(*bitmap2); 566 { 567 SCOPED_TRACE("Checking color after conversion back to SkBitmap"); 568 gt::CheckColors(bitmap2->getColor(10, 10), SK_ColorRED); 569 gt::CheckIsTransparent(bitmap.getColor(10, 40)); 570 } 571 } 572 573 TEST_F(ImageTest, SwapRepresentations) { 574 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; 575 576 gfx::Image image1(gt::CreateImageSkia(25, 25)); 577 const gfx::ImageSkia* image_skia1 = image1.ToImageSkia(); 578 EXPECT_EQ(1U, image1.RepresentationCount()); 579 580 gfx::Image image2(gt::CreatePlatformImage()); 581 const gfx::ImageSkia* image_skia2 = image2.ToImageSkia(); 582 gt::PlatformImage platform_image = gt::ToPlatformType(image2); 583 EXPECT_EQ(kRepCount, image2.RepresentationCount()); 584 585 image1.SwapRepresentations(&image2); 586 587 EXPECT_EQ(image_skia2, image1.ToImageSkia()); 588 EXPECT_TRUE(gt::PlatformImagesEqual(platform_image, 589 gt::ToPlatformType(image1))); 590 EXPECT_EQ(image_skia1, image2.ToImageSkia()); 591 EXPECT_EQ(kRepCount, image1.RepresentationCount()); 592 EXPECT_EQ(1U, image2.RepresentationCount()); 593 } 594 595 TEST_F(ImageTest, Copy) { 596 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; 597 598 gfx::Image image1(gt::CreateImageSkia(25, 25)); 599 EXPECT_EQ(25, image1.Width()); 600 EXPECT_EQ(25, image1.Height()); 601 gfx::Image image2(image1); 602 EXPECT_EQ(25, image2.Width()); 603 EXPECT_EQ(25, image2.Height()); 604 605 EXPECT_EQ(1U, image1.RepresentationCount()); 606 EXPECT_EQ(1U, image2.RepresentationCount()); 607 EXPECT_EQ(image1.ToImageSkia(), image2.ToImageSkia()); 608 609 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image2))); 610 EXPECT_EQ(kRepCount, image2.RepresentationCount()); 611 EXPECT_EQ(kRepCount, image1.RepresentationCount()); 612 } 613 614 TEST_F(ImageTest, Assign) { 615 gfx::Image image1(gt::CreatePlatformImage()); 616 EXPECT_EQ(25, image1.Width()); 617 EXPECT_EQ(25, image1.Height()); 618 // Assignment must be on a separate line to the declaration in order to test 619 // assignment operator (instead of copy constructor). 620 gfx::Image image2; 621 image2 = image1; 622 EXPECT_EQ(25, image2.Width()); 623 EXPECT_EQ(25, image2.Height()); 624 625 EXPECT_EQ(1U, image1.RepresentationCount()); 626 EXPECT_EQ(1U, image2.RepresentationCount()); 627 EXPECT_EQ(image1.ToSkBitmap(), image2.ToSkBitmap()); 628 } 629 630 TEST_F(ImageTest, MultiResolutionImageSkia) { 631 const int kWidth1x = 10; 632 const int kHeight1x = 12; 633 const int kWidth2x = 20; 634 const int kHeight2x = 24; 635 636 gfx::ImageSkia image_skia; 637 image_skia.AddRepresentation(gfx::ImageSkiaRep( 638 gt::CreateBitmap(kWidth1x, kHeight1x), 639 ui::SCALE_FACTOR_100P)); 640 image_skia.AddRepresentation(gfx::ImageSkiaRep( 641 gt::CreateBitmap(kWidth2x, kHeight2x), 642 ui::SCALE_FACTOR_200P)); 643 644 std::vector<ui::ScaleFactor> scale_factors; 645 scale_factors.push_back(ui::SCALE_FACTOR_100P); 646 scale_factors.push_back(ui::SCALE_FACTOR_200P); 647 EXPECT_TRUE(gt::ImageSkiaStructureMatches(image_skia, kWidth1x, kHeight1x, 648 scale_factors)); 649 650 // Check that the image has a single representation. 651 gfx::Image image(image_skia); 652 EXPECT_EQ(1u, image.RepresentationCount()); 653 EXPECT_EQ(kWidth1x, image.Width()); 654 EXPECT_EQ(kHeight1x, image.Height()); 655 } 656 657 TEST_F(ImageTest, RemoveFromMultiResolutionImageSkia) { 658 const int kWidth2x = 20; 659 const int kHeight2x = 24; 660 661 gfx::ImageSkia image_skia; 662 663 image_skia.AddRepresentation(gfx::ImageSkiaRep( 664 gt::CreateBitmap(kWidth2x, kHeight2x), ui::SCALE_FACTOR_200P)); 665 EXPECT_EQ(1u, image_skia.image_reps().size()); 666 667 image_skia.RemoveRepresentation(ui::SCALE_FACTOR_100P); 668 EXPECT_EQ(1u, image_skia.image_reps().size()); 669 670 image_skia.RemoveRepresentation(ui::SCALE_FACTOR_200P); 671 EXPECT_EQ(0u, image_skia.image_reps().size()); 672 } 673 674 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is 675 // passed. 676 TEST_F(ImageTest, OwnershipTest) { 677 gfx::Image image; 678 { 679 SkBitmap bitmap(gt::CreateBitmap(10, 10)); 680 EXPECT_TRUE(!bitmap.isNull()); 681 image = gfx::Image(gfx::ImageSkia( 682 gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P))); 683 } 684 EXPECT_TRUE(!image.ToSkBitmap()->isNull()); 685 } 686 687 // Integration tests with UI toolkit frameworks require linking against the 688 // 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. 690 691 } // namespace 692