Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / image / image_unittest_util.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 // Because the unit tests for gfx::Image are spread across multiple
6 // implementation files, this header contains the reusable components.
7
8 #include "ui/gfx/image/image_unittest_util.h"
9
10 #include <cmath>
11
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"
17
18 #if defined(OS_IOS)
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/mac_util.h"
24 #include "skia/ext/skia_utils_mac.h"
25 #endif
26
27 namespace gfx {
28 namespace test {
29
30 namespace {
31
32 bool ColorComponentsClose(SkColor component1, SkColor component2) {
33   int c1 = static_cast<int>(component1);
34   int c2 = static_cast<int>(component2);
35   return std::abs(c1 - c2) <= 40;
36 }
37
38 bool ColorsClose(SkColor color1, SkColor color2) {
39   // Be tolerant of floating point rounding and lossy color space conversions.
40   return ColorComponentsClose(SkColorGetR(color1), SkColorGetR(color2)) &&
41          ColorComponentsClose(SkColorGetG(color1), SkColorGetG(color2)) &&
42          ColorComponentsClose(SkColorGetB(color1), SkColorGetB(color2)) &&
43          ColorComponentsClose(SkColorGetA(color1), SkColorGetA(color2));
44 }
45
46 }  // namespace
47
48 std::vector<float> Get1xAnd2xScales() {
49   std::vector<float> scales;
50   scales.push_back(1.0f);
51   scales.push_back(2.0f);
52   return scales;
53 }
54
55 const SkBitmap CreateBitmap(int width, int height) {
56   SkBitmap bitmap;
57   bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
58   bitmap.allocPixels();
59   bitmap.eraseRGB(0, 255, 0);
60   return bitmap;
61 }
62
63 gfx::ImageSkia CreateImageSkia(int width, int height) {
64   return gfx::ImageSkia::CreateFrom1xBitmap(CreateBitmap(width, height));
65 }
66
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());
71   return bytes;
72 }
73
74 gfx::Image CreateImage() {
75   return CreateImage(100, 50);
76 }
77
78 gfx::Image CreateImage(int width, int height) {
79   return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height));
80 }
81
82 bool IsEqual(const gfx::Image& img1, const gfx::Image& img2) {
83   std::vector<gfx::ImageSkiaRep> img1_reps = img1.AsImageSkia().image_reps();
84   gfx::ImageSkia image_skia2 = img2.AsImageSkia();
85   if (image_skia2.image_reps().size() != img1_reps.size())
86     return false;
87
88   for (size_t i = 0; i < img1_reps.size(); ++i) {
89     float scale = img1_reps[i].scale();
90     const gfx::ImageSkiaRep& image_rep2 = image_skia2.GetRepresentation(scale);
91     if (image_rep2.scale() != scale ||
92         !IsEqual(img1_reps[i].sk_bitmap(), image_rep2.sk_bitmap())) {
93       return false;
94     }
95   }
96   return true;
97 }
98
99 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
100   if (bmp1.isNull() && bmp2.isNull())
101     return true;
102
103   if (bmp1.width() != bmp2.width() ||
104       bmp1.height() != bmp2.height() ||
105       bmp1.config() != SkBitmap::kARGB_8888_Config ||
106       bmp2.config() != SkBitmap::kARGB_8888_Config) {
107     return false;
108   }
109
110   SkAutoLockPixels lock1(bmp1);
111   SkAutoLockPixels lock2(bmp2);
112   if (!bmp1.getPixels() || !bmp2.getPixels())
113     return false;
114
115   for (int y = 0; y < bmp1.height(); ++y) {
116     for (int x = 0; x < bmp1.width(); ++x) {
117       if (!ColorsClose(bmp1.getColor(x,y), bmp2.getColor(x,y)))
118         return false;
119     }
120   }
121
122   return true;
123 }
124
125 bool IsEqual(const scoped_refptr<base::RefCountedMemory>& bytes,
126              const SkBitmap& bitmap) {
127   SkBitmap decoded;
128   if (!bytes.get() ||
129       !PNGCodec::Decode(bytes->front(), bytes->size(), &decoded)) {
130     return bitmap.isNull();
131   }
132
133   return IsEqual(bitmap, decoded);
134 }
135
136 void CheckImageIndicatesPNGDecodeFailure(const gfx::Image& image) {
137   SkBitmap bitmap = image.AsBitmap();
138   EXPECT_FALSE(bitmap.isNull());
139   EXPECT_LE(16, bitmap.width());
140   EXPECT_LE(16, bitmap.height());
141   SkAutoLockPixels auto_lock(bitmap);
142   CheckColors(bitmap.getColor(10, 10), SK_ColorRED);
143 }
144
145 bool ImageSkiaStructureMatches(
146     const gfx::ImageSkia& image_skia,
147     int width,
148     int height,
149     const std::vector<float>& scales) {
150   if (image_skia.isNull() ||
151       image_skia.width() != width ||
152       image_skia.height() != height ||
153       image_skia.image_reps().size() != scales.size()) {
154     return false;
155   }
156
157   for (size_t i = 0; i < scales.size(); ++i) {
158     gfx::ImageSkiaRep image_rep =
159         image_skia.GetRepresentation(scales[i]);
160     if (image_rep.is_null() || image_rep.scale() != scales[i])
161       return false;
162
163     if (image_rep.pixel_width() != static_cast<int>(width * scales[i]) ||
164         image_rep.pixel_height() != static_cast<int>(height * scales[i])) {
165       return false;
166     }
167   }
168   return true;
169 }
170
171 bool IsEmpty(const gfx::Image& image) {
172   const SkBitmap& bmp = *image.ToSkBitmap();
173   return bmp.isNull() ||
174          (bmp.width() == 0 && bmp.height() == 0);
175 }
176
177 PlatformImage CreatePlatformImage() {
178   const SkBitmap bitmap(CreateBitmap(25, 25));
179 #if defined(OS_IOS)
180   float scale = ImageSkia::GetMaxSupportedScale();
181
182   base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
183       CGColorSpaceCreateDeviceRGB());
184   UIImage* image =
185       gfx::SkBitmapToUIImageWithColorSpace(bitmap, scale, color_space);
186   base::mac::NSObjectRetain(image);
187   return image;
188 #elif defined(OS_MACOSX)
189   NSImage* image = gfx::SkBitmapToNSImage(bitmap);
190   base::mac::NSObjectRetain(image);
191   return image;
192 #else
193   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
194 #endif
195 }
196
197 gfx::Image::RepresentationType GetPlatformRepresentationType() {
198 #if defined(OS_IOS)
199   return gfx::Image::kImageRepCocoaTouch;
200 #elif defined(OS_MACOSX)
201   return gfx::Image::kImageRepCocoa;
202 #else
203   return gfx::Image::kImageRepSkia;
204 #endif
205 }
206
207 PlatformImage ToPlatformType(const gfx::Image& image) {
208 #if defined(OS_IOS)
209   return image.ToUIImage();
210 #elif defined(OS_MACOSX)
211   return image.ToNSImage();
212 #else
213   return image.AsImageSkia();
214 #endif
215 }
216
217 PlatformImage CopyPlatformType(const gfx::Image& image) {
218 #if defined(OS_IOS)
219   return image.CopyUIImage();
220 #elif defined(OS_MACOSX)
221   return image.CopyNSImage();
222 #else
223   return image.AsImageSkia();
224 #endif
225 }
226
227 #if defined(OS_MACOSX)
228 // Defined in image_unittest_util_mac.mm.
229 #else
230 SkColor GetPlatformImageColor(PlatformImage image, int x, int y) {
231   SkBitmap bitmap = *image.bitmap();
232   SkAutoLockPixels auto_lock(bitmap);
233   return bitmap.getColor(x, y);
234 }
235 #endif
236
237 void CheckColors(SkColor color1, SkColor color2) {
238   EXPECT_TRUE(ColorsClose(color1, color2));
239 }
240
241 void CheckIsTransparent(SkColor color) {
242   EXPECT_LT(SkColorGetA(color) / 255.0, 0.05);
243 }
244
245 bool IsPlatformImageValid(PlatformImage image) {
246 #if defined(OS_MACOSX)
247   return image != NULL;
248 #else
249   return !image.isNull();
250 #endif
251 }
252
253 bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) {
254 #if defined(OS_MACOSX)
255   return image1 == image2;
256 #else
257   return image1.BackedBySameObjectAs(image2);
258 #endif
259 }
260
261 }  // namespace test
262 }  // namespace gfx