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 // Because the unit tests for gfx::Image are spread across multiple
6 // implementation files, this header contains the reusable components.
8 #include "ui/gfx/image/image_unittest_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/image/image_skia.h"
19 #include "base/mac/foundation_util.h"
20 #include "base/mac/scoped_cftyperef.h"
21 #include "skia/ext/skia_utils_ios.h"
22 #elif defined(OS_MACOSX)
23 #include "base/mac/foundation_util.h"
24 #include "base/mac/mac_util.h"
25 #include "skia/ext/skia_utils_mac.h"
33 bool ColorComponentsClose(SkColor component1
, SkColor component2
) {
34 int c1
= static_cast<int>(component1
);
35 int c2
= static_cast<int>(component2
);
36 return std::abs(c1
- c2
) <= 40;
39 bool ColorsClose(SkColor color1
, SkColor color2
) {
40 // Be tolerant of floating point rounding and lossy color space conversions.
41 return ColorComponentsClose(SkColorGetR(color1
), SkColorGetR(color2
)) &&
42 ColorComponentsClose(SkColorGetG(color1
), SkColorGetG(color2
)) &&
43 ColorComponentsClose(SkColorGetB(color1
), SkColorGetB(color2
)) &&
44 ColorComponentsClose(SkColorGetA(color1
), SkColorGetA(color2
));
49 std::vector
<float> Get1xAnd2xScales() {
50 std::vector
<float> scales
;
51 scales
.push_back(1.0f
);
52 scales
.push_back(2.0f
);
56 const SkBitmap
CreateBitmap(int width
, int height
) {
58 bitmap
.allocN32Pixels(width
, height
);
59 bitmap
.eraseARGB(255, 0, 255, 0);
63 gfx::ImageSkia
CreateImageSkia(int width
, int height
) {
64 return gfx::ImageSkia::CreateFrom1xBitmap(CreateBitmap(width
, height
));
67 scoped_refptr
<base::RefCountedMemory
> CreatePNGBytes(int edge_size
) {
68 SkBitmap bitmap
= CreateBitmap(edge_size
, edge_size
);
69 scoped_refptr
<base::RefCountedBytes
> bytes(new base::RefCountedBytes());
70 PNGCodec::EncodeBGRASkBitmap(bitmap
, false, &bytes
->data());
74 gfx::Image
CreateImage() {
75 return CreateImage(100, 50);
78 gfx::Image
CreateImage(int width
, int height
) {
79 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width
, height
));
82 bool IsEqual(const gfx::Image
& img1
, const gfx::Image
& img2
) {
83 img1
.AsImageSkia().EnsureRepsForSupportedScales();
84 img2
.AsImageSkia().EnsureRepsForSupportedScales();
85 std::vector
<gfx::ImageSkiaRep
> img1_reps
= img1
.AsImageSkia().image_reps();
86 gfx::ImageSkia image_skia2
= img2
.AsImageSkia();
87 if (image_skia2
.image_reps().size() != img1_reps
.size())
90 for (size_t i
= 0; i
< img1_reps
.size(); ++i
) {
91 float scale
= img1_reps
[i
].scale();
92 const gfx::ImageSkiaRep
& image_rep2
= image_skia2
.GetRepresentation(scale
);
93 if (image_rep2
.scale() != scale
||
94 !IsEqual(img1_reps
[i
].sk_bitmap(), image_rep2
.sk_bitmap())) {
101 bool IsEqual(const SkBitmap
& bmp1
, const SkBitmap
& bmp2
) {
102 if (bmp1
.isNull() && bmp2
.isNull())
105 if (bmp1
.width() != bmp2
.width() ||
106 bmp1
.height() != bmp2
.height() ||
107 bmp1
.colorType() != kN32_SkColorType
||
108 bmp2
.colorType() != kN32_SkColorType
) {
112 SkAutoLockPixels
lock1(bmp1
);
113 SkAutoLockPixels
lock2(bmp2
);
114 if (!bmp1
.getPixels() || !bmp2
.getPixels())
117 for (int y
= 0; y
< bmp1
.height(); ++y
) {
118 for (int x
= 0; x
< bmp1
.width(); ++x
) {
119 if (!ColorsClose(bmp1
.getColor(x
,y
), bmp2
.getColor(x
,y
)))
127 bool IsEqual(const scoped_refptr
<base::RefCountedMemory
>& bytes
,
128 const SkBitmap
& bitmap
) {
131 !PNGCodec::Decode(bytes
->front(), bytes
->size(), &decoded
)) {
132 return bitmap
.isNull();
135 return IsEqual(bitmap
, decoded
);
138 void CheckImageIndicatesPNGDecodeFailure(const gfx::Image
& image
) {
139 SkBitmap bitmap
= image
.AsBitmap();
140 EXPECT_FALSE(bitmap
.isNull());
141 EXPECT_LE(16, bitmap
.width());
142 EXPECT_LE(16, bitmap
.height());
143 SkAutoLockPixels
auto_lock(bitmap
);
144 CheckColors(bitmap
.getColor(10, 10), SK_ColorRED
);
147 bool ImageSkiaStructureMatches(
148 const gfx::ImageSkia
& image_skia
,
151 const std::vector
<float>& scales
) {
152 if (image_skia
.isNull() ||
153 image_skia
.width() != width
||
154 image_skia
.height() != height
||
155 image_skia
.image_reps().size() != scales
.size()) {
159 for (size_t i
= 0; i
< scales
.size(); ++i
) {
160 gfx::ImageSkiaRep image_rep
=
161 image_skia
.GetRepresentation(scales
[i
]);
162 if (image_rep
.is_null() || image_rep
.scale() != scales
[i
])
165 if (image_rep
.pixel_width() != static_cast<int>(width
* scales
[i
]) ||
166 image_rep
.pixel_height() != static_cast<int>(height
* scales
[i
])) {
173 bool IsEmpty(const gfx::Image
& image
) {
174 const SkBitmap
& bmp
= *image
.ToSkBitmap();
175 return bmp
.isNull() ||
176 (bmp
.width() == 0 && bmp
.height() == 0);
179 PlatformImage
CreatePlatformImage() {
180 const SkBitmap
bitmap(CreateBitmap(25, 25));
182 float scale
= ImageSkia::GetMaxSupportedScale();
184 base::ScopedCFTypeRef
<CGColorSpaceRef
> color_space(
185 CGColorSpaceCreateDeviceRGB());
187 gfx::SkBitmapToUIImageWithColorSpace(bitmap
, scale
, color_space
);
188 base::mac::NSObjectRetain(image
);
190 #elif defined(OS_MACOSX)
191 NSImage
* image
= gfx::SkBitmapToNSImageWithColorSpace(
192 bitmap
, base::mac::GetGenericRGBColorSpace());
193 base::mac::NSObjectRetain(image
);
196 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
200 gfx::Image::RepresentationType
GetPlatformRepresentationType() {
202 return gfx::Image::kImageRepCocoaTouch
;
203 #elif defined(OS_MACOSX)
204 return gfx::Image::kImageRepCocoa
;
206 return gfx::Image::kImageRepSkia
;
210 PlatformImage
ToPlatformType(const gfx::Image
& image
) {
212 return image
.ToUIImage();
213 #elif defined(OS_MACOSX)
214 return image
.ToNSImage();
216 return image
.AsImageSkia();
220 PlatformImage
CopyPlatformType(const gfx::Image
& image
) {
222 return image
.CopyUIImage();
223 #elif defined(OS_MACOSX)
224 return image
.CopyNSImage();
226 return image
.AsImageSkia();
230 #if defined(OS_MACOSX)
231 // Defined in image_unittest_util_mac.mm.
233 SkColor
GetPlatformImageColor(PlatformImage image
, int x
, int y
) {
234 SkBitmap bitmap
= *image
.bitmap();
235 SkAutoLockPixels
auto_lock(bitmap
);
236 return bitmap
.getColor(x
, y
);
240 void CheckColors(SkColor color1
, SkColor color2
) {
241 EXPECT_TRUE(ColorsClose(color1
, color2
));
244 void CheckIsTransparent(SkColor color
) {
245 EXPECT_LT(SkColorGetA(color
) / 255.0, 0.05);
248 bool IsPlatformImageValid(PlatformImage image
) {
249 #if defined(OS_MACOSX)
250 return image
!= NULL
;
252 return !image
.isNull();
256 bool PlatformImagesEqual(PlatformImage image1
, PlatformImage image2
) {
257 #if defined(OS_MACOSX)
258 return image1
== image2
;
260 return image1
.BackedBySameObjectAs(image2
);