Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / image / image_unittest.cc
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(OS_IOS)
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"
19 #endif
20
21 namespace {
22
23 #if defined(TOOLKIT_VIEWS) || defined(OS_ANDROID) || \
24     (defined(OS_LINUX) && !defined(USE_CAIRO))
25 const bool kUsesSkiaNatively = true;
26 #else
27 const bool kUsesSkiaNatively = false;
28 #endif
29
30 class ImageTest : public testing::Test {
31  public:
32   ImageTest() {
33     std::vector<float> scales;
34     scales.push_back(1.0f);
35 #if !defined(OS_IOS)
36     scales.push_back(2.0f);
37 #endif
38     gfx::ImageSkia::SetSupportedScales(scales);
39   }
40 };
41
42 namespace gt = gfx::test;
43
44 TEST_F(ImageTest, EmptyImage) {
45   // Test the default constructor.
46   gfx::Image image;
47   EXPECT_EQ(0U, image.RepresentationCount());
48   EXPECT_TRUE(image.IsEmpty());
49   EXPECT_EQ(0, image.Width());
50   EXPECT_EQ(0, image.Height());
51
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());
57
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());
63
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());
71 }
72
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());
81 #endif
82
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());
91
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());
98 }
99
100 // The resulting Image should be empty when it is created using obviously
101 // invalid data.
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());
108
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());
115 }
116
117 // Test the Width, Height and Size of an empty and non-empty image.
118 TEST_F(ImageTest, ImageSize) {
119   gfx::Image image;
120   EXPECT_EQ(0, image.Width());
121   EXPECT_EQ(0, image.Height());
122   EXPECT_EQ(gfx::Size(0, 0), image.Size());
123
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());
128 }
129
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());
134
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());
140
141   // Make sure double conversion doesn't happen.
142   const gfx::ImageSkia* image_skia2 = image.ToImageSkia();
143   EXPECT_EQ(1U, image.RepresentationCount());
144
145   // ToImageSkia() should always return the same gfx::ImageSkia.
146   EXPECT_EQ(image_skia1, image_skia2);
147
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);
154
155   EXPECT_EQ(1U, image.RepresentationCount());
156   EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
157   if (!kUsesSkiaNatively)
158     EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
159 }
160
161 TEST_F(ImageTest, EmptyImageToPNG) {
162   gfx::Image image;
163   scoped_refptr<base::RefCountedMemory> png_bytes = image.As1xPNGBytes();
164   EXPECT_TRUE(png_bytes.get());
165   EXPECT_FALSE(png_bytes->size());
166 }
167
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());
180
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());
191 }
192
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;
198
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));
204
205   gfx::Image image(image_png_reps);
206   EXPECT_FALSE(image.IsEmpty());
207   EXPECT_EQ(25, image.Width());
208   EXPECT_EQ(25, image.Height());
209
210   EXPECT_TRUE(std::equal(bytes1x->front(), bytes1x->front() + bytes1x->size(),
211                          image.As1xPNGBytes()->front()));
212 }
213
214 TEST_F(ImageTest, MultiResolutionImageSkiaToPNG) {
215   const int kSize1x = 25;
216   const int kSize2x = 50;
217
218   SkBitmap bitmap_1x = gt::CreateBitmap(kSize1x, kSize1x);
219   gfx::ImageSkia image_skia;
220   image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap_1x,
221                                                  1.0f));
222   image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap(
223       kSize2x, kSize2x), 2.0f));
224   gfx::Image image(image_skia);
225
226   EXPECT_TRUE(gt::IsEqual(image.As1xPNGBytes(), bitmap_1x));
227   EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG));
228 }
229
230 TEST_F(ImageTest, MultiResolutionPNGToImageSkia) {
231   const int kSize1x = 25;
232   const int kSize2x = 50;
233
234   scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x);
235   scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x);
236
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);
241
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,
247                                             scales));
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()));
252 }
253
254 TEST_F(ImageTest, MultiResolutionPNGToPlatform) {
255   const int kSize1x = 25;
256   const int kSize2x = 50;
257
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));
263
264   gfx::Image from_png(image_png_reps);
265   gfx::Image from_platform(gt::CopyPlatformType(from_png));
266 #if defined(OS_IOS)
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()));
274   else
275     ADD_FAILURE() << "Unexpected platform scale factor.";
276 #else
277   EXPECT_TRUE(gt::IsEqual(bytes1x, from_platform.AsBitmap()));
278 #endif  // defined(OS_IOS)
279 }
280
281
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));
288
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);
292
293   EXPECT_TRUE(from_png.HasRepresentation(gfx::Image::kImageRepPNG));
294   EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_png)));
295 }
296
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();
305
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);
309
310   gfx::Image from_platform(gt::CopyPlatformType(from_png));
311
312   EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform)));
313   EXPECT_TRUE(gt::IsEqual(png_bytes, from_platform.AsBitmap()));
314 }
315
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);
324
325   EXPECT_TRUE(gt::IsEqual(from_platform.AsBitmap(), from_png.AsBitmap()));
326 }
327
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);
337 }
338
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);
349 }
350
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;
356
357   EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
358   if (!kUsesSkiaNatively)
359     EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
360
361   EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
362   EXPECT_EQ(kRepCount, image.RepresentationCount());
363
364   const SkBitmap* bitmap = image.ToSkBitmap();
365   EXPECT_FALSE(bitmap->isNull());
366   EXPECT_EQ(kRepCount, image.RepresentationCount());
367
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());
372 }
373
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;
379
380   EXPECT_TRUE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
381   if (!kUsesSkiaNatively)
382     EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia));
383
384   const SkBitmap* bitmap = image.ToSkBitmap();
385   EXPECT_TRUE(bitmap);
386   EXPECT_FALSE(bitmap->isNull());
387   EXPECT_EQ(kRepCount, image.RepresentationCount());
388
389   EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
390   EXPECT_EQ(kRepCount, image.RepresentationCount());
391
392   EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
393   EXPECT_EQ(25, image.Width());
394   EXPECT_EQ(25, image.Height());
395 }
396
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());
403
404   // Make sure double conversion doesn't happen.
405   EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
406   EXPECT_EQ(1U, image.RepresentationCount());
407
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());
413 }
414
415 TEST_F(ImageTest, PlatformToSkiaToCopy) {
416   const gfx::ImageSkia* image_skia = NULL;
417   {
418     gfx::Image image(gt::CreatePlatformImage());
419     image_skia = image.CopyImageSkia();
420   }
421   EXPECT_TRUE(image_skia);
422   EXPECT_FALSE(image_skia->isNull());
423   delete image_skia;
424
425   const SkBitmap* bitmap = NULL;
426   {
427     gfx::Image image(gt::CreatePlatformImage());
428     bitmap = image.CopySkBitmap();
429   }
430
431   EXPECT_TRUE(bitmap);
432   EXPECT_FALSE(bitmap->isNull());
433   delete bitmap;
434 }
435
436 #if defined(OS_IOS)
437 TEST_F(ImageTest, SkiaToCocoaTouchCopy) {
438   UIImage* ui_image;
439
440   {
441     gfx::Image image(gt::CreateImageSkia(25, 25));
442     ui_image = image.CopyUIImage();
443   }
444
445   EXPECT_TRUE(ui_image);
446   base::mac::NSObjectRelease(ui_image);
447 }
448 #elif defined(OS_MACOSX)
449 TEST_F(ImageTest, SkiaToCocoaCopy) {
450   NSImage* ns_image;
451
452   {
453     gfx::Image image(gt::CreateImageSkia(25, 25));
454     ns_image = image.CopyNSImage();
455   }
456
457   EXPECT_TRUE(ns_image);
458   base::mac::NSObjectRelease(ns_image);
459 }
460 #endif
461
462 TEST_F(ImageTest, CheckSkiaColor) {
463   gfx::Image image(gt::CreatePlatformImage());
464
465   const SkBitmap* bitmap = image.ToSkBitmap();
466   SkAutoLockPixels auto_lock(*bitmap);
467   gt::CheckColors(bitmap->getColor(10, 10), SK_ColorGREEN);
468 }
469
470 TEST_F(ImageTest, SkBitmapConversionPreservesOrientation) {
471   const int width = 50;
472   const int height = 50;
473   SkBitmap bitmap;
474   bitmap.allocN32Pixels(width, height);
475   bitmap.eraseRGB(0, 255, 0);
476
477   // Paint the upper half of the image in red (lower half is in green).
478   SkCanvas canvas(bitmap);
479   SkPaint red;
480   red.setColor(SK_ColorRED);
481   canvas.drawRect(SkRect::MakeWH(width, height / 2), red);
482   {
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);
486   }
487
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);
491   {
492     SCOPED_TRACE("Checking color of the platform image");
493     gt::CheckColors(
494         gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10),
495         SK_ColorRED);
496     gt::CheckColors(
497         gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40),
498         SK_ColorGREEN);
499   }
500
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);
505   {
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);
509   }
510 }
511
512 TEST_F(ImageTest, SkBitmapConversionPreservesTransparency) {
513   const int width = 50;
514   const int height = 50;
515   SkBitmap bitmap;
516   bitmap.allocN32Pixels(width, height);
517   bitmap.eraseARGB(0, 0, 255, 0);
518
519   // Paint the upper half of the image in red (lower half is transparent).
520   SkCanvas canvas(bitmap);
521   SkPaint red;
522   red.setColor(SK_ColorRED);
523   canvas.drawRect(SkRect::MakeWH(width, height / 2), red);
524   {
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));
528   }
529
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);
533   {
534     SCOPED_TRACE("Checking color of the platform image");
535     gt::CheckColors(
536         gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10),
537         SK_ColorRED);
538     gt::CheckIsTransparent(
539         gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40));
540   }
541
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);
546   {
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));
550   }
551 }
552
553 TEST_F(ImageTest, SwapRepresentations) {
554   const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
555
556   gfx::Image image1(gt::CreateImageSkia(25, 25));
557   const gfx::ImageSkia* image_skia1 = image1.ToImageSkia();
558   EXPECT_EQ(1U, image1.RepresentationCount());
559
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());
564
565   image1.SwapRepresentations(&image2);
566
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());
573 }
574
575 TEST_F(ImageTest, Copy) {
576   const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
577
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());
584
585   EXPECT_EQ(1U, image1.RepresentationCount());
586   EXPECT_EQ(1U, image2.RepresentationCount());
587   EXPECT_EQ(image1.ToImageSkia(), image2.ToImageSkia());
588
589   EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image2)));
590   EXPECT_EQ(kRepCount, image2.RepresentationCount());
591   EXPECT_EQ(kRepCount, image1.RepresentationCount());
592 }
593
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).
600   gfx::Image image2;
601   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.ToSkBitmap(), image2.ToSkBitmap());
608 }
609
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;
615
616   gfx::ImageSkia image_skia;
617   image_skia.AddRepresentation(gfx::ImageSkiaRep(
618       gt::CreateBitmap(kWidth1x, kHeight1x),
619       1.0f));
620   image_skia.AddRepresentation(gfx::ImageSkiaRep(
621       gt::CreateBitmap(kWidth2x, kHeight2x),
622       2.0f));
623
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,
628                                             scales));
629
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());
635 }
636
637 TEST_F(ImageTest, RemoveFromMultiResolutionImageSkia) {
638   const int kWidth2x = 20;
639   const int kHeight2x = 24;
640
641   gfx::ImageSkia image_skia;
642
643   image_skia.AddRepresentation(gfx::ImageSkiaRep(
644       gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f));
645   EXPECT_EQ(1u, image_skia.image_reps().size());
646
647   image_skia.RemoveRepresentation(1.0f);
648   EXPECT_EQ(1u, image_skia.image_reps().size());
649
650   image_skia.RemoveRepresentation(2.0f);
651   EXPECT_EQ(0u, image_skia.image_reps().size());
652 }
653
654 // Tests that gfx::Image does indeed take ownership of the SkBitmap it is
655 // passed.
656 TEST_F(ImageTest, OwnershipTest) {
657   gfx::Image image;
658   {
659     SkBitmap bitmap(gt::CreateBitmap(10, 10));
660     EXPECT_TRUE(!bitmap.isNull());
661     image = gfx::Image(gfx::ImageSkia(
662         gfx::ImageSkiaRep(bitmap, 1.0f)));
663   }
664   EXPECT_TRUE(!image.ToSkBitmap()->isNull());
665 }
666
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.
670
671 }  // namespace