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