OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Because the unit tests for gfx::Image are spread across multiple | 5 // Because the unit tests for gfx::Image are spread across multiple |
6 // implementation files, this header contains the reusable components. | 6 // implementation files, this header contains the reusable components. |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "ui/base/layout.h" | 9 #include "ui/base/layout.h" |
10 #include "ui/gfx/image/image_unittest_util.h" | 10 #include "ui/gfx/image/image_unittest_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 | 13 |
14 #if defined(TOOLKIT_GTK) | 14 #if defined(TOOLKIT_GTK) |
15 #include <gtk/gtk.h> | |
16 #include "ui/gfx/gtk_util.h" | 15 #include "ui/gfx/gtk_util.h" |
17 #elif defined(OS_MACOSX) | 16 #elif defined(OS_MACOSX) |
18 #include "base/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
19 #include "skia/ext/skia_utils_mac.h" | 18 #include "skia/ext/skia_utils_mac.h" |
20 #endif | 19 #endif |
21 | 20 |
22 namespace gfx { | 21 namespace gfx { |
23 namespace test { | 22 namespace test { |
24 | 23 |
25 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
26 | 25 |
27 void SetSupportedScaleFactorsTo1xAnd2x() { | 26 void SetSupportedScaleFactorsTo1xAnd2x() { |
28 std::vector<ui::ScaleFactor> supported_scale_factors; | 27 std::vector<ui::ScaleFactor> supported_scale_factors; |
29 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P); | 28 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P); |
30 supported_scale_factors.push_back(ui::SCALE_FACTOR_200P); | 29 supported_scale_factors.push_back(ui::SCALE_FACTOR_200P); |
31 ui::test::SetSupportedScaleFactors(supported_scale_factors); | 30 ui::test::SetSupportedScaleFactors(supported_scale_factors); |
32 } | 31 } |
33 | 32 |
34 #endif // OS_MACOSX | 33 #endif // OS_MACOSX |
35 | 34 |
36 const SkBitmap CreateBitmap(int width, int height) { | 35 const SkBitmap CreateBitmap(int width, int height) { |
37 SkBitmap bitmap; | 36 SkBitmap bitmap; |
38 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 37 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
39 bitmap.allocPixels(); | 38 bitmap.allocPixels(); |
40 bitmap.eraseRGB(0, 255, 0); | 39 bitmap.eraseRGB(255, 0, 0); |
41 return bitmap; | 40 return bitmap; |
42 } | 41 } |
43 | 42 |
44 gfx::Image CreateImage() { | 43 gfx::Image CreateImage() { |
45 return CreateImage(100, 50); | 44 return CreateImage(100, 50); |
46 } | 45 } |
47 | 46 |
48 gfx::Image CreateImage(int width, int height) { | 47 gfx::Image CreateImage(int width, int height) { |
49 return gfx::Image(CreateBitmap(width, height)); | 48 return gfx::Image(CreateBitmap(width, height)); |
50 } | 49 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 PlatformImage ToPlatformType(const gfx::Image& image) { | 106 PlatformImage ToPlatformType(const gfx::Image& image) { |
108 #if defined(OS_MACOSX) | 107 #if defined(OS_MACOSX) |
109 return image.ToNSImage(); | 108 return image.ToNSImage(); |
110 #elif defined(TOOLKIT_GTK) | 109 #elif defined(TOOLKIT_GTK) |
111 return image.ToGdkPixbuf(); | 110 return image.ToGdkPixbuf(); |
112 #else | 111 #else |
113 return *image.ToSkBitmap(); | 112 return *image.ToSkBitmap(); |
114 #endif | 113 #endif |
115 } | 114 } |
116 | 115 |
117 PlatformImage CopyPlatformType(const gfx::Image& image) { | |
118 #if defined(OS_MACOSX) | |
119 return image.CopyNSImage(); | |
120 #elif defined(TOOLKIT_GTK) | |
121 return image.CopyGdkPixbuf(); | |
122 #else | |
123 return *image.ToSkBitmap(); | |
124 #endif | |
125 } | |
126 | |
127 #if defined(OS_MACOSX) | |
128 // Defined in image_unittest_util_mac.mm. | |
129 #elif defined(TOOLKIT_GTK) | |
130 SkColor GetPlatformImageColor(PlatformImage image) { | |
131 guchar* gdk_pixels = gdk_pixbuf_get_pixels(image); | |
132 guchar alpha = gdk_pixbuf_get_has_alpha(image) ? gdk_pixels[3] : 255; | |
133 return SkColorSetARGB(alpha, gdk_pixels[0], gdk_pixels[1], gdk_pixels[2]); | |
134 } | |
135 #else | |
136 SkColor GetPlatformImageColor(PlatformImage image) { | |
137 SkAutoLockPixels auto_lock(image); | |
138 return image.getColor(10, 10); | |
139 } | |
140 #endif | |
141 | |
142 void CheckColor(SkColor color, bool is_red) { | |
143 // Be tolerant of floating point rounding and lossy color space conversions. | |
144 if (is_red) { | |
145 EXPECT_GT(SkColorGetR(color), 0.95); | |
146 EXPECT_LT(SkColorGetG(color), 0.05); | |
147 } else { | |
148 EXPECT_GT(SkColorGetG(color), 0.95); | |
149 EXPECT_LT(SkColorGetR(color), 0.05); | |
150 } | |
151 EXPECT_LT(SkColorGetB(color), 0.05); | |
152 EXPECT_GT(SkColorGetA(color), 0.95); | |
153 } | |
154 | |
155 bool IsPlatformImageValid(PlatformImage image) { | 116 bool IsPlatformImageValid(PlatformImage image) { |
156 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) | 117 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) |
157 return image != NULL; | 118 return image != NULL; |
158 #else | 119 #else |
159 return !image.isNull(); | 120 return !image.isNull(); |
160 #endif | 121 #endif |
161 } | 122 } |
162 | 123 |
163 bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) { | 124 bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) { |
164 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) | 125 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) |
165 return image1 == image2; | 126 return image1 == image2; |
166 #else | 127 #else |
167 return image1.getPixels() == image2.getPixels(); | 128 return image1.getPixels() == image2.getPixels(); |
168 #endif | 129 #endif |
169 } | 130 } |
170 | 131 |
171 } // namespace test | 132 } // namespace test |
172 } // namespace gfx | 133 } // namespace gfx |
OLD | NEW |