Implement MoveFileLocal (with creating a snapshot).
[chromium-blink-merge.git] / ui / gfx / image / image_unittest_util.cc
blobdaca53f5d0c890c2538e236a13fea632da1e5494
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"
10 #include <cmath>
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"
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/foundation_util.h"
24 #include "base/mac/mac_util.h"
25 #include "skia/ext/skia_utils_mac.h"
26 #endif
28 namespace gfx {
29 namespace test {
31 namespace {
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));
47 } // namespace
49 std::vector<float> Get1xAnd2xScales() {
50 std::vector<float> scales;
51 scales.push_back(1.0f);
52 scales.push_back(2.0f);
53 return scales;
56 const SkBitmap CreateBitmap(int width, int height) {
57 SkBitmap bitmap;
58 bitmap.allocN32Pixels(width, height);
59 bitmap.eraseARGB(255, 0, 255, 0);
60 return bitmap;
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());
71 return bytes;
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())
88 return false;
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())) {
95 return false;
98 return true;
101 bool IsEqual(const SkBitmap& bmp1, const SkBitmap& bmp2) {
102 if (bmp1.isNull() && bmp2.isNull())
103 return true;
105 if (bmp1.width() != bmp2.width() ||
106 bmp1.height() != bmp2.height() ||
107 bmp1.colorType() != kN32_SkColorType ||
108 bmp2.colorType() != kN32_SkColorType) {
109 return false;
112 SkAutoLockPixels lock1(bmp1);
113 SkAutoLockPixels lock2(bmp2);
114 if (!bmp1.getPixels() || !bmp2.getPixels())
115 return false;
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)))
120 return false;
124 return true;
127 bool IsEqual(const scoped_refptr<base::RefCountedMemory>& bytes,
128 const SkBitmap& bitmap) {
129 SkBitmap decoded;
130 if (!bytes.get() ||
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,
149 int width,
150 int height,
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()) {
156 return false;
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])
163 return false;
165 if (image_rep.pixel_width() != static_cast<int>(width * scales[i]) ||
166 image_rep.pixel_height() != static_cast<int>(height * scales[i])) {
167 return false;
170 return true;
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));
181 #if defined(OS_IOS)
182 float scale = ImageSkia::GetMaxSupportedScale();
184 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
185 CGColorSpaceCreateDeviceRGB());
186 UIImage* image =
187 gfx::SkBitmapToUIImageWithColorSpace(bitmap, scale, color_space);
188 base::mac::NSObjectRetain(image);
189 return image;
190 #elif defined(OS_MACOSX)
191 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace(
192 bitmap, base::mac::GetGenericRGBColorSpace());
193 base::mac::NSObjectRetain(image);
194 return image;
195 #else
196 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
197 #endif
200 gfx::Image::RepresentationType GetPlatformRepresentationType() {
201 #if defined(OS_IOS)
202 return gfx::Image::kImageRepCocoaTouch;
203 #elif defined(OS_MACOSX)
204 return gfx::Image::kImageRepCocoa;
205 #else
206 return gfx::Image::kImageRepSkia;
207 #endif
210 PlatformImage ToPlatformType(const gfx::Image& image) {
211 #if defined(OS_IOS)
212 return image.ToUIImage();
213 #elif defined(OS_MACOSX)
214 return image.ToNSImage();
215 #else
216 return image.AsImageSkia();
217 #endif
220 PlatformImage CopyPlatformType(const gfx::Image& image) {
221 #if defined(OS_IOS)
222 return image.CopyUIImage();
223 #elif defined(OS_MACOSX)
224 return image.CopyNSImage();
225 #else
226 return image.AsImageSkia();
227 #endif
230 #if defined(OS_MACOSX)
231 // Defined in image_unittest_util_mac.mm.
232 #else
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);
238 #endif
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;
251 #else
252 return !image.isNull();
253 #endif
256 bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) {
257 #if defined(OS_MACOSX)
258 return image1 == image2;
259 #else
260 return image1.BackedBySameObjectAs(image2);
261 #endif
264 } // namespace test
265 } // namespace gfx