| OLD | NEW | 
|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "skia/ext/skia_utils_mac.mm" | 5 #include "skia/ext/skia_utils_mac.mm" | 
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" | 
| 7 | 7 | 
| 8 namespace { | 8 namespace { | 
| 9 | 9 | 
| 10 class SkiaUtilsMacTest : public testing::Test { | 10 class SkiaUtilsMacTest : public testing::Test { | 
| 11  public: | 11  public: | 
|  | 12   // Creates a red or blue bitmap. | 
|  | 13   SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit); | 
|  | 14 | 
|  | 15   // Creates a red or blue image. | 
|  | 16   NSImage* CreateNSImage(int width, int height, bool isred); | 
|  | 17 | 
|  | 18   // Checks that the given bitmap rep is actually red or blue. | 
|  | 19   void TestImageRep(NSBitmapImageRep* imageRep, bool isred); | 
|  | 20 | 
|  | 21   // Checks that the given bitmap is actually red or blue. | 
|  | 22   void TestSkBitmap(const SkBitmap& bitmap, bool isred); | 
|  | 23 | 
| 12   // If not red, is blue. | 24   // If not red, is blue. | 
| 13   // If not tfbit (twenty-four-bit), is 444. | 25   // If not tfbit (twenty-four-bit), is 444. | 
| 14   void ShapeHelper(int width, int height, bool isred, bool tfbit); | 26   void ShapeHelper(int width, int height, bool isred, bool tfbit); | 
| 15 }; | 27 }; | 
| 16 | 28 | 
| 17 void SkiaUtilsMacTest::ShapeHelper(int width, int height, | 29 SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height, | 
| 18                                    bool isred, bool tfbit) { | 30                                           bool isred, bool tfbit) { | 
| 19   SkBitmap thing; | 31   SkBitmap bitmap; | 
| 20 | 32 | 
| 21   if (tfbit) | 33   if (tfbit) | 
| 22     thing.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 34     bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 
| 23   else | 35   else | 
| 24     thing.setConfig(SkBitmap::kARGB_4444_Config, width, height); | 36     bitmap.setConfig(SkBitmap::kARGB_4444_Config, width, height); | 
| 25   thing.allocPixels(); | 37   bitmap.allocPixels(); | 
| 26 | 38 | 
| 27   if (isred) | 39   if (isred) | 
| 28     thing.eraseRGB(0xff, 0, 0); | 40     bitmap.eraseRGB(0xff, 0, 0); | 
| 29   else | 41   else | 
| 30     thing.eraseRGB(0, 0, 0xff); | 42     bitmap.eraseRGB(0, 0, 0xff); | 
| 31 | 43 | 
| 32   // Confirm size | 44   return bitmap; | 
| 33   NSImage* image = gfx::SkBitmapToNSImage(thing); | 45 } | 
| 34   EXPECT_DOUBLE_EQ([image size].width, (double)width); |  | 
| 35   EXPECT_DOUBLE_EQ([image size].height, (double)height); |  | 
| 36 | 46 | 
|  | 47 NSImage* SkiaUtilsMacTest::CreateNSImage(int width, int height, bool isred) { | 
|  | 48   NSImage* image = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] | 
|  | 49       autorelease]; | 
|  | 50   [image lockFocus]; | 
|  | 51   if (isred) | 
|  | 52     [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set]; | 
|  | 53   else | 
|  | 54     [[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:1.0] set]; | 
|  | 55   NSRectFill(NSMakeRect(0, 0, width, height)); | 
|  | 56   [image unlockFocus]; | 
|  | 57   return image; | 
|  | 58 } | 
|  | 59 | 
|  | 60 void SkiaUtilsMacTest::TestImageRep(NSBitmapImageRep* imageRep, bool isred) { | 
| 37   // Get the color of a pixel and make sure it looks fine | 61   // Get the color of a pixel and make sure it looks fine | 
| 38   [image lockFocus]; | 62   int x = [imageRep size].width > 17 ? 17 : 0; | 
| 39 | 63   int y = [imageRep size].height > 17 ? 17 : 0; | 
| 40   int x = width > 17 ? 17 : 0; | 64   NSColor* color = [imageRep colorAtX:x y:y]; | 
| 41   int y = height > 17 ? 17 : 0; |  | 
| 42   NSColor* color = NSReadPixel(NSMakePoint(x, y)); |  | 
| 43   CGFloat red = 0, green = 0, blue = 0, alpha = 0; | 65   CGFloat red = 0, green = 0, blue = 0, alpha = 0; | 
| 44   [image unlockFocus]; |  | 
| 45 | 66 | 
| 46   // SkBitmapToNSImage returns a bitmap in the calibrated color space (sRGB), | 67   // SkBitmapToNSImage returns a bitmap in the calibrated color space (sRGB), | 
| 47   // while NSReadPixel returns a color in the device color space. Convert back | 68   // while NSReadPixel returns a color in the device color space. Convert back | 
| 48   // to the calibrated color space before testing. | 69   // to the calibrated color space before testing. | 
| 49   color = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; | 70   color = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; | 
| 50 | 71 | 
| 51   [color getRed:&red green:&green blue:&blue alpha:&alpha]; | 72   [color getRed:&red green:&green blue:&blue alpha:&alpha]; | 
| 52 | 73 | 
| 53   // Be tolerant of floating point rounding and lossy color space conversions. | 74   // Be tolerant of floating point rounding and lossy color space conversions. | 
| 54   if (isred) { | 75   if (isred) { | 
| 55     EXPECT_GT(red, 0.95); | 76     EXPECT_NEAR(red, 1.0, 0.025); | 
| 56     EXPECT_LT(blue, 0.05); | 77     EXPECT_NEAR(blue, 0.0, 0.025); | 
| 57   } else { | 78   } else { | 
| 58     EXPECT_LT(red, 0.05); | 79     EXPECT_NEAR(red, 0.0, 0.025); | 
| 59     EXPECT_GT(blue, 0.95); | 80     EXPECT_NEAR(blue, 1.0, 0.025); | 
| 60   } | 81   } | 
| 61   EXPECT_LT(green, 0.05); | 82   EXPECT_NEAR(green, 0.0, 0.025); | 
| 62   EXPECT_GT(alpha, 0.95); | 83   EXPECT_NEAR(alpha, 1.0, 0.025); | 
|  | 84 } | 
|  | 85 | 
|  | 86 void SkiaUtilsMacTest::TestSkBitmap(const SkBitmap& bitmap, bool isred) { | 
|  | 87   int x = bitmap.width() > 17 ? 17 : 0; | 
|  | 88   int y = bitmap.height() > 17 ? 17 : 0; | 
|  | 89   SkColor color = bitmap.getColor(x, y); | 
|  | 90 | 
|  | 91   if (isred) { | 
|  | 92     EXPECT_EQ(255u, SkColorGetR(color)); | 
|  | 93     EXPECT_EQ(0u, SkColorGetB(color)); | 
|  | 94   } else { | 
|  | 95     EXPECT_EQ(0u, SkColorGetR(color)); | 
|  | 96     EXPECT_EQ(255u, SkColorGetB(color)); | 
|  | 97   } | 
|  | 98   EXPECT_EQ(0u, SkColorGetG(color)); | 
|  | 99   EXPECT_EQ(255u, SkColorGetA(color)); | 
|  | 100 } | 
|  | 101 | 
|  | 102 void SkiaUtilsMacTest::ShapeHelper(int width, int height, | 
|  | 103                                    bool isred, bool tfbit) { | 
|  | 104   SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit)); | 
|  | 105 | 
|  | 106   // Confirm size | 
|  | 107   NSImage* image = gfx::SkBitmapToNSImage(thing); | 
|  | 108   EXPECT_DOUBLE_EQ([image size].width, (double)width); | 
|  | 109   EXPECT_DOUBLE_EQ([image size].height, (double)height); | 
|  | 110 | 
|  | 111   EXPECT_TRUE([[image representations] count] == 1); | 
|  | 112   EXPECT_TRUE([[[image representations] lastObject] | 
|  | 113       isKindOfClass:[NSBitmapImageRep class]]); | 
|  | 114   TestImageRep([[image representations] lastObject], isred); | 
| 63 } | 115 } | 
| 64 | 116 | 
| 65 TEST_F(SkiaUtilsMacTest, BitmapToNSImage_RedSquare64x64) { | 117 TEST_F(SkiaUtilsMacTest, BitmapToNSImage_RedSquare64x64) { | 
| 66   ShapeHelper(64, 64, true, true); | 118   ShapeHelper(64, 64, true, true); | 
| 67 } | 119 } | 
| 68 | 120 | 
| 69 TEST_F(SkiaUtilsMacTest, BitmapToNSImage_BlueRectangle199x19) { | 121 TEST_F(SkiaUtilsMacTest, BitmapToNSImage_BlueRectangle199x19) { | 
| 70   ShapeHelper(199, 19, false, true); | 122   ShapeHelper(199, 19, false, true); | 
| 71 } | 123 } | 
| 72 | 124 | 
| 73 TEST_F(SkiaUtilsMacTest, BitmapToNSImage_BlueRectangle444) { | 125 TEST_F(SkiaUtilsMacTest, BitmapToNSImage_BlueRectangle444) { | 
| 74   ShapeHelper(200, 200, false, false); | 126   ShapeHelper(200, 200, false, false); | 
| 75 } | 127 } | 
| 76 | 128 | 
|  | 129 TEST_F(SkiaUtilsMacTest, MultipleBitmapsToNSImage) { | 
|  | 130   int redWidth = 10; | 
|  | 131   int redHeight = 15; | 
|  | 132   int blueWidth = 20; | 
|  | 133   int blueHeight = 30; | 
|  | 134 | 
|  | 135   SkBitmap redBitmap(CreateSkBitmap(redWidth, redHeight, true, true)); | 
|  | 136   SkBitmap blueBitmap(CreateSkBitmap(blueWidth, blueHeight, false, true)); | 
|  | 137   std::vector<const SkBitmap*> bitmaps; | 
|  | 138   bitmaps.push_back(&redBitmap); | 
|  | 139   bitmaps.push_back(&blueBitmap); | 
|  | 140 | 
|  | 141   NSImage* image = gfx::SkBitmapsToNSImage(bitmaps); | 
|  | 142 | 
|  | 143   // Image size should be the same as the smallest bitmap. | 
|  | 144   EXPECT_DOUBLE_EQ(redWidth, [image size].width); | 
|  | 145   EXPECT_DOUBLE_EQ(redHeight, [image size].height); | 
|  | 146 | 
|  | 147   EXPECT_EQ(2u, [[image representations] count]); | 
|  | 148 | 
|  | 149   for (NSBitmapImageRep* imageRep in [image representations]) { | 
|  | 150     NSBitmapImageRep* imageRep = [[image representations] objectAtIndex:0]; | 
|  | 151     bool isred = [imageRep size].width == redWidth; | 
|  | 152     if (isred) { | 
|  | 153       EXPECT_DOUBLE_EQ(redHeight, [imageRep size].height); | 
|  | 154     } else { | 
|  | 155       EXPECT_DOUBLE_EQ(blueWidth, [imageRep size].width); | 
|  | 156       EXPECT_DOUBLE_EQ(blueHeight, [imageRep size].height); | 
|  | 157     } | 
|  | 158     TestImageRep(imageRep, isred); | 
|  | 159   } | 
|  | 160 } | 
|  | 161 | 
|  | 162 TEST_F(SkiaUtilsMacTest, NSImageRepToSkBitmap) { | 
|  | 163   int width = 10; | 
|  | 164   int height = 15; | 
|  | 165   bool isred = true; | 
|  | 166 | 
|  | 167   NSImage* image = CreateNSImage(width, height, isred); | 
|  | 168   EXPECT_EQ(1u, [[image representations] count]); | 
|  | 169   NSBitmapImageRep* imageRep = [[image representations] lastObject]; | 
|  | 170   SkBitmap bitmap(gfx::NSImageRepToSkBitmap(imageRep, [image size], false)); | 
|  | 171   TestSkBitmap(bitmap, isred); | 
|  | 172 } | 
|  | 173 | 
| 77 }  // namespace | 174 }  // namespace | 
| OLD | NEW | 
|---|