image_unittest_util.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "base/memory/scoped_ptr.h"
9#include "ui/base/layout.h"
10#include "ui/gfx/image/image_unittest_util.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "third_party/skia/include/core/SkBitmap.h"
13
14#if defined(TOOLKIT_GTK)
15#include <gtk/gtk.h>
16#include "ui/gfx/gtk_util.h"
17#elif defined(OS_IOS)
18#include "base/mac/foundation_util.h"
19#include "base/mac/scoped_cftyperef.h"
20#include "skia/ext/skia_utils_ios.h"
21#elif defined(OS_MACOSX)
22#include "base/mac/mac_util.h"
23#include "skia/ext/skia_utils_mac.h"
24#endif
25
26namespace gfx {
27namespace test {
28
29void SetSupportedScaleFactorsTo1xAnd2x() {
30  std::vector<ui::ScaleFactor> supported_scale_factors;
31  supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
32  supported_scale_factors.push_back(ui::SCALE_FACTOR_200P);
33  ui::test::SetSupportedScaleFactors(supported_scale_factors);
34}
35
36const SkBitmap CreateBitmap(int width, int height) {
37  SkBitmap bitmap;
38  bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
39  bitmap.allocPixels();
40  bitmap.eraseRGB(0, 255, 0);
41  return bitmap;
42}
43
44gfx::Image CreateImage() {
45  return CreateImage(100, 50);
46}
47
48gfx::Image CreateImage(int width, int height) {
49  return gfx::Image(CreateBitmap(width, height));
50}
51
52bool IsEqual(const gfx::Image& image1, const gfx::Image& image2) {
53  const SkBitmap& bmp1 = *image1.ToSkBitmap();
54  const SkBitmap& bmp2 = *image2.ToSkBitmap();
55
56  if (bmp1.width() != bmp2.width() ||
57      bmp1.height() != bmp2.height() ||
58      bmp1.config() != SkBitmap::kARGB_8888_Config ||
59      bmp2.config() != SkBitmap::kARGB_8888_Config) {
60    return false;
61  }
62
63  SkAutoLockPixels lock1(bmp1);
64  SkAutoLockPixels lock2(bmp2);
65  if (!bmp1.getPixels() || !bmp2.getPixels())
66    return false;
67
68  for (int y = 0; y < bmp1.height(); ++y) {
69    for (int x = 0; x < bmp1.width(); ++x) {
70      if (*bmp1.getAddr32(x,y) != *bmp2.getAddr32(x,y))
71        return false;
72    }
73  }
74
75  return true;
76}
77
78bool IsEmpty(const gfx::Image& image) {
79  const SkBitmap& bmp = *image.ToSkBitmap();
80  return bmp.isNull() ||
81         (bmp.width() == 0 && bmp.height() == 0);
82}
83
84PlatformImage CreatePlatformImage() {
85  const SkBitmap bitmap(CreateBitmap(25, 25));
86#if defined(OS_IOS)
87  ui::ScaleFactor scale_factor = ui::GetMaxScaleFactor();
88  float scale = ui::GetScaleFactorScale(scale_factor);
89
90  base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space(
91      CGColorSpaceCreateDeviceRGB());
92  UIImage* image =
93      gfx::SkBitmapToUIImageWithColorSpace(bitmap, scale, color_space);
94  base::mac::NSObjectRetain(image);
95  return image;
96#elif defined(OS_MACOSX)
97  NSImage* image = gfx::SkBitmapToNSImage(bitmap);
98  base::mac::NSObjectRetain(image);
99  return image;
100#elif defined(TOOLKIT_GTK)
101  return gfx::GdkPixbufFromSkBitmap(bitmap);
102#else
103  return bitmap;
104#endif
105}
106
107gfx::Image::RepresentationType GetPlatformRepresentationType() {
108#if defined(OS_IOS)
109  return gfx::Image::kImageRepCocoaTouch;
110#elif defined(OS_MACOSX)
111  return gfx::Image::kImageRepCocoa;
112#elif defined(TOOLKIT_GTK)
113  return gfx::Image::kImageRepGdk;
114#else
115  return gfx::Image::kImageRepSkia;
116#endif
117}
118
119PlatformImage ToPlatformType(const gfx::Image& image) {
120#if defined(OS_IOS)
121  return image.ToUIImage();
122#elif defined(OS_MACOSX)
123  return image.ToNSImage();
124#elif defined(TOOLKIT_GTK)
125  return image.ToGdkPixbuf();
126#else
127  return *image.ToSkBitmap();
128#endif
129}
130
131PlatformImage CopyPlatformType(const gfx::Image& image) {
132#if defined(OS_IOS)
133  return image.CopyUIImage();
134#elif defined(OS_MACOSX)
135  return image.CopyNSImage();
136#elif defined(TOOLKIT_GTK)
137  return image.CopyGdkPixbuf();
138#else
139  return *image.ToSkBitmap();
140#endif
141}
142
143#if defined(OS_MACOSX)
144// Defined in image_unittest_util_mac.mm.
145#elif defined(TOOLKIT_GTK)
146SkColor GetPlatformImageColor(PlatformImage image, int x, int y) {
147  int n_channels = gdk_pixbuf_get_n_channels(image);
148  int rowstride = gdk_pixbuf_get_rowstride(image);
149  guchar* gdk_pixels = gdk_pixbuf_get_pixels(image);
150
151  guchar* pixel = gdk_pixels + (y * rowstride) + (x * n_channels);
152  guchar alpha = gdk_pixbuf_get_has_alpha(image) ? pixel[3] : 255;
153  return SkColorSetARGB(alpha, pixel[0], pixel[1], pixel[2]);
154}
155#else
156SkColor GetPlatformImageColor(PlatformImage image, int x, int y) {
157  SkAutoLockPixels auto_lock(image);
158  return image.getColor(x, y);
159}
160#endif
161
162void CheckColor(SkColor color, bool is_red) {
163  // Be tolerant of floating point rounding and lossy color space conversions.
164  if (is_red) {
165    EXPECT_GT(SkColorGetR(color) / 255.0, 0.95);
166    EXPECT_LT(SkColorGetG(color) / 255.0, 0.05);
167  } else {
168    EXPECT_GT(SkColorGetG(color) / 255.0, 0.95);
169    EXPECT_LT(SkColorGetR(color) / 255.0, 0.05);
170  }
171  EXPECT_LT(SkColorGetB(color) / 255.0, 0.05);
172  EXPECT_GT(SkColorGetA(color) / 255.0, 0.95);
173}
174
175bool IsPlatformImageValid(PlatformImage image) {
176#if defined(OS_MACOSX) || defined(TOOLKIT_GTK)
177  return image != NULL;
178#else
179  return !image.isNull();
180#endif
181}
182
183bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) {
184#if defined(OS_MACOSX) || defined(TOOLKIT_GTK)
185  return image1 == image2;
186#else
187  return image1.getPixels() == image2.getPixels();
188#endif
189}
190
191}  // namespace test
192}  // namespace gfx
193