| 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 "ui/gfx/image/image_unittest_util.h" | 8 #include "ui/gfx/image/image_unittest_util.h" |
| 9 | 9 |
| 10 #include <cmath> |
| 11 |
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
| 14 #include "ui/gfx/image/image_skia.h" | 16 #include "ui/gfx/image/image_skia.h" |
| 15 | 17 |
| 16 #if defined(TOOLKIT_GTK) | 18 #if defined(TOOLKIT_GTK) |
| 17 #include <gtk/gtk.h> | 19 #include <gtk/gtk.h> |
| 18 #include "ui/gfx/gtk_util.h" | 20 #include "ui/gfx/gtk_util.h" |
| 19 #elif defined(OS_IOS) | 21 #elif defined(OS_IOS) |
| 20 #include "base/mac/foundation_util.h" | 22 #include "base/mac/foundation_util.h" |
| 21 #include "base/mac/scoped_cftyperef.h" | 23 #include "base/mac/scoped_cftyperef.h" |
| 22 #include "skia/ext/skia_utils_ios.h" | 24 #include "skia/ext/skia_utils_ios.h" |
| 23 #elif defined(OS_MACOSX) | 25 #elif defined(OS_MACOSX) |
| 24 #include "base/mac/mac_util.h" | 26 #include "base/mac/mac_util.h" |
| 25 #include "skia/ext/skia_utils_mac.h" | 27 #include "skia/ext/skia_utils_mac.h" |
| 26 #endif | 28 #endif |
| 27 | 29 |
| 28 namespace gfx { | 30 namespace gfx { |
| 29 namespace test { | 31 namespace test { |
| 30 | 32 |
| 33 namespace { |
| 34 |
| 35 bool ColorComponentsClose(SkColor component1, SkColor component2) { |
| 36 int c1 = static_cast<int>(component1); |
| 37 int c2 = static_cast<int>(component2); |
| 38 return std::abs(c1 - c2) <= 40; |
| 39 } |
| 40 |
| 41 bool ColorsClose(SkColor color1, SkColor color2) { |
| 42 // Be tolerant of floating point rounding and lossy color space conversions. |
| 43 return ColorComponentsClose(SkColorGetR(color1), SkColorGetR(color2)) && |
| 44 ColorComponentsClose(SkColorGetG(color1), SkColorGetG(color2)) && |
| 45 ColorComponentsClose(SkColorGetB(color1), SkColorGetB(color2)) && |
| 46 ColorComponentsClose(SkColorGetA(color1), SkColorGetA(color2)); |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 31 void SetSupportedScaleFactorsTo1xAnd2x() { | 51 void SetSupportedScaleFactorsTo1xAnd2x() { |
| 32 std::vector<ui::ScaleFactor> supported_scale_factors; | 52 std::vector<ui::ScaleFactor> supported_scale_factors; |
| 33 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P); | 53 supported_scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 34 supported_scale_factors.push_back(ui::SCALE_FACTOR_200P); | 54 supported_scale_factors.push_back(ui::SCALE_FACTOR_200P); |
| 35 ui::test::SetSupportedScaleFactors(supported_scale_factors); | 55 ui::test::SetSupportedScaleFactors(supported_scale_factors); |
| 36 } | 56 } |
| 37 | 57 |
| 38 const SkBitmap CreateBitmap(int width, int height) { | 58 const SkBitmap CreateBitmap(int width, int height) { |
| 39 SkBitmap bitmap; | 59 SkBitmap bitmap; |
| 40 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 60 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 return false; | 107 return false; |
| 88 } | 108 } |
| 89 | 109 |
| 90 SkAutoLockPixels lock1(bmp1); | 110 SkAutoLockPixels lock1(bmp1); |
| 91 SkAutoLockPixels lock2(bmp2); | 111 SkAutoLockPixels lock2(bmp2); |
| 92 if (!bmp1.getPixels() || !bmp2.getPixels()) | 112 if (!bmp1.getPixels() || !bmp2.getPixels()) |
| 93 return false; | 113 return false; |
| 94 | 114 |
| 95 for (int y = 0; y < bmp1.height(); ++y) { | 115 for (int y = 0; y < bmp1.height(); ++y) { |
| 96 for (int x = 0; x < bmp1.width(); ++x) { | 116 for (int x = 0; x < bmp1.width(); ++x) { |
| 97 if (*bmp1.getAddr32(x,y) != *bmp2.getAddr32(x,y)) | 117 if (!ColorsClose(bmp1.getColor(x,y), bmp2.getColor(x,y))) |
| 98 return false; | 118 return false; |
| 99 } | 119 } |
| 100 } | 120 } |
| 101 | 121 |
| 102 return true; | 122 return true; |
| 103 } | 123 } |
| 104 | 124 |
| 105 bool IsEqual(const scoped_refptr<base::RefCountedMemory>& bytes, | 125 bool IsEqual(const scoped_refptr<base::RefCountedMemory>& bytes, |
| 106 const SkBitmap& bitmap) { | 126 const SkBitmap& bitmap) { |
| 107 SkBitmap decoded; | 127 SkBitmap decoded; |
| 108 if (!bytes.get() || | 128 if (!bytes.get() || |
| 109 !PNGCodec::Decode(bytes->front(), bytes->size(), &decoded)) { | 129 !PNGCodec::Decode(bytes->front(), bytes->size(), &decoded)) { |
| 110 return bitmap.isNull(); | 130 return bitmap.isNull(); |
| 111 } | 131 } |
| 112 | 132 |
| 113 return IsEqual(bitmap, decoded); | 133 return IsEqual(bitmap, decoded); |
| 114 } | 134 } |
| 115 | 135 |
| 116 void CheckImageIndicatesPNGDecodeFailure(const gfx::Image& image) { | 136 void CheckImageIndicatesPNGDecodeFailure(const gfx::Image& image) { |
| 117 SkBitmap bitmap = image.AsBitmap(); | 137 SkBitmap bitmap = image.AsBitmap(); |
| 118 EXPECT_FALSE(bitmap.isNull()); | 138 EXPECT_FALSE(bitmap.isNull()); |
| 119 EXPECT_LE(16, bitmap.width()); | 139 EXPECT_LE(16, bitmap.width()); |
| 120 EXPECT_LE(16, bitmap.height()); | 140 EXPECT_LE(16, bitmap.height()); |
| 121 SkAutoLockPixels auto_lock(bitmap); | 141 SkAutoLockPixels auto_lock(bitmap); |
| 122 CheckColor(bitmap.getColor(10, 10), true); | 142 CheckColors(bitmap.getColor(10, 10), SK_ColorRED); |
| 123 } | 143 } |
| 124 | 144 |
| 125 bool ImageSkiaStructureMatches( | 145 bool ImageSkiaStructureMatches( |
| 126 const gfx::ImageSkia& image_skia, | 146 const gfx::ImageSkia& image_skia, |
| 127 int width, | 147 int width, |
| 128 int height, | 148 int height, |
| 129 const std::vector<ui::ScaleFactor>& scale_factors) { | 149 const std::vector<ui::ScaleFactor>& scale_factors) { |
| 130 if (image_skia.isNull() || | 150 if (image_skia.isNull() || |
| 131 image_skia.width() != width || | 151 image_skia.width() != width || |
| 132 image_skia.height() != height || | 152 image_skia.height() != height || |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 guchar alpha = gdk_pixbuf_get_has_alpha(image) ? pixel[3] : 255; | 247 guchar alpha = gdk_pixbuf_get_has_alpha(image) ? pixel[3] : 255; |
| 228 return SkColorSetARGB(alpha, pixel[0], pixel[1], pixel[2]); | 248 return SkColorSetARGB(alpha, pixel[0], pixel[1], pixel[2]); |
| 229 } | 249 } |
| 230 #else | 250 #else |
| 231 SkColor GetPlatformImageColor(PlatformImage image, int x, int y) { | 251 SkColor GetPlatformImageColor(PlatformImage image, int x, int y) { |
| 232 SkAutoLockPixels auto_lock(image); | 252 SkAutoLockPixels auto_lock(image); |
| 233 return image.getColor(x, y); | 253 return image.getColor(x, y); |
| 234 } | 254 } |
| 235 #endif | 255 #endif |
| 236 | 256 |
| 237 void CheckColor(SkColor color, bool is_red) { | 257 void CheckColors(SkColor color1, SkColor color2) { |
| 238 // Be tolerant of floating point rounding and lossy color space conversions. | 258 EXPECT_TRUE(ColorsClose(color1, color2)); |
| 239 if (is_red) { | |
| 240 EXPECT_GT(SkColorGetR(color) / 255.0, 0.95); | |
| 241 EXPECT_LT(SkColorGetG(color) / 255.0, 0.05); | |
| 242 } else { | |
| 243 EXPECT_GT(SkColorGetG(color) / 255.0, 0.95); | |
| 244 EXPECT_LT(SkColorGetR(color) / 255.0, 0.05); | |
| 245 } | |
| 246 EXPECT_LT(SkColorGetB(color) / 255.0, 0.05); | |
| 247 EXPECT_GT(SkColorGetA(color) / 255.0, 0.95); | |
| 248 } | 259 } |
| 249 | 260 |
| 250 void CheckIsTransparent(SkColor color) { | 261 void CheckIsTransparent(SkColor color) { |
| 251 EXPECT_LT(SkColorGetA(color) / 255.0, 0.05); | 262 EXPECT_LT(SkColorGetA(color) / 255.0, 0.05); |
| 252 } | 263 } |
| 253 | 264 |
| 254 bool IsPlatformImageValid(PlatformImage image) { | 265 bool IsPlatformImageValid(PlatformImage image) { |
| 255 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) | 266 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) |
| 256 return image != NULL; | 267 return image != NULL; |
| 257 #else | 268 #else |
| 258 return !image.isNull(); | 269 return !image.isNull(); |
| 259 #endif | 270 #endif |
| 260 } | 271 } |
| 261 | 272 |
| 262 bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) { | 273 bool PlatformImagesEqual(PlatformImage image1, PlatformImage image2) { |
| 263 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) | 274 #if defined(OS_MACOSX) || defined(TOOLKIT_GTK) |
| 264 return image1 == image2; | 275 return image1 == image2; |
| 265 #else | 276 #else |
| 266 return image1.getPixels() == image2.getPixels(); | 277 return image1.getPixels() == image2.getPixels(); |
| 267 #endif | 278 #endif |
| 268 } | 279 } |
| 269 | 280 |
| 270 } // namespace test | 281 } // namespace test |
| 271 } // namespace gfx | 282 } // namespace gfx |
| OLD | NEW |