diff options
author | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-12 04:59:25 +0000 |
---|---|---|
committer | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-12 04:59:25 +0000 |
commit | 29df6d2697c0aa6972932d2ca71fca6e48b5541f (patch) | |
tree | 17c9e2549a93701be1ca251a2d7a122fe0159c94 /ui | |
parent | af9ecaed4b90ff549309aa5e52b9e248b20c5202 (diff) | |
download | chromium_src-29df6d2697c0aa6972932d2ca71fca6e48b5541f.zip chromium_src-29df6d2697c0aa6972932d2ca71fca6e48b5541f.tar.gz chromium_src-29df6d2697c0aa6972932d2ca71fca6e48b5541f.tar.bz2 |
NSImageRep can have a size which is not it's pixel size.
Fix conversion between NSImage to ImageSkia for when this is the case.
Also fix the unittest so that it passes on HiDPI mac and changed unittest constants to be kWidth1x, kHeight1x etc
Test=ImageMacTest.NSImageWithResizedNSImageRepToImageSkia
Bug=None
Review URL: https://chromiumcodereview.appspot.com/10545069
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141616 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/image/image_mac.mm | 3 | ||||
-rw-r--r-- | ui/gfx/image/image_mac_unittest.mm | 112 |
2 files changed, 73 insertions, 42 deletions
diff --git a/ui/gfx/image/image_mac.mm b/ui/gfx/image/image_mac.mm index eeed857..68e541c 100644 --- a/ui/gfx/image/image_mac.mm +++ b/ui/gfx/image/image_mac.mm @@ -17,7 +17,8 @@ namespace internal { gfx::ImageSkia NSImageToImageSkia(NSImage* image) { gfx::ImageSkia image_skia; for (NSImageRep* imageRep in [image representations]) { - NSSize imageRepSize = [imageRep size]; + NSSize imageRepSize = + NSMakeSize([imageRep pixelsWide], [imageRep pixelsHigh]); SkBitmap bitmap(gfx::NSImageRepToSkBitmap(imageRep, imageRepSize, false)); if (!bitmap.isNull() && !bitmap.empty()) { float scaleFactor = imageRepSize.width / [image size].width; diff --git a/ui/gfx/image/image_mac_unittest.mm b/ui/gfx/image/image_mac_unittest.mm index 8a2119a..924cebd 100644 --- a/ui/gfx/image/image_mac_unittest.mm +++ b/ui/gfx/image/image_mac_unittest.mm @@ -16,33 +16,63 @@ namespace { class ImageMacTest : public testing::Test { public: - void CreateBitmapImageRep(int width, int height, NSImageRep** image_rep) { - scoped_nsobject<NSImage> image( - [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); - [image lockFocus]; - [[NSColor redColor] set]; - NSRectFill(NSMakeRect(0, 0, width, height)); - [image unlockFocus]; - EXPECT_TRUE([[[image representations] lastObject] - isKindOfClass:[NSImageRep class]]); - *image_rep = [[image representations] lastObject]; + void CreateBitmapImageRep(int width, int height, + NSBitmapImageRep** image_rep) { + *image_rep = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:NULL + pixelsWide:width + pixelsHigh:height + bitsPerSample:8 + samplesPerPixel:3 + hasAlpha:NO + isPlanar:NO + colorSpaceName:NSDeviceRGBColorSpace + bitmapFormat:0 + bytesPerRow:0 + bitsPerPixel:0]; } }; namespace gt = gfx::test; +TEST_F(ImageMacTest, NSImageWithResizedNSImageRepToImageSkia) { + const int kWidth1x = 10; + const int kHeight1x = 12; + const int kWidth2x = 20; + const int kHeight2x = 24; + + NSBitmapImageRep* image_rep; + CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep); + + scoped_nsobject<NSImage> ns_image( + [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); + [ns_image addRepresentation:image_rep]; + + [image_rep setSize:NSMakeSize(kWidth1x, kHeight1x)]; + + gfx::Image image(ns_image.release()); + const gfx::ImageSkia* image_skia = image.ToImageSkia(); + + float scale_factor; + const SkBitmap& bitmap = image_skia->GetBitmapForScale(2.0f, 2.0f, + &scale_factor); + EXPECT_EQ(2.0f, scale_factor); + EXPECT_EQ(kWidth2x, bitmap.width()); + EXPECT_EQ(kHeight2x, bitmap.height()); +} + TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { - const int width1x = 10; - const int height1x = 12; - const int width2x = 20; - const int height2x = 24; - - NSImageRep* image_rep_1; - CreateBitmapImageRep(width1x, height1x, &image_rep_1); - NSImageRep* image_rep_2; - CreateBitmapImageRep(width2x, height2x, &image_rep_2); + const int kWidth1x = 10; + const int kHeight1x = 12; + const int kWidth2x = 20; + const int kHeight2x = 24; + + NSBitmapImageRep* image_rep_1; + CreateBitmapImageRep(kWidth1x, kHeight1x, &image_rep_1); + NSBitmapImageRep* image_rep_2; + CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep_2); scoped_nsobject<NSImage> ns_image( - [[NSImage alloc] initWithSize:NSMakeSize(width1x, height1x)]); + [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); [ns_image addRepresentation:image_rep_1]; [ns_image addRepresentation:image_rep_2]; @@ -58,29 +88,29 @@ TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { &scale_factor); EXPECT_TRUE(!bitmap1x.isNull()); EXPECT_EQ(1.0f, scale_factor); - EXPECT_EQ(width1x, bitmap1x.width()); - EXPECT_EQ(height1x, bitmap1x.height()); + EXPECT_EQ(kWidth1x, bitmap1x.width()); + EXPECT_EQ(kHeight1x, bitmap1x.height()); const SkBitmap& bitmap2x = image_skia->GetBitmapForScale(2.0f, 2.0f, &scale_factor); EXPECT_TRUE(!bitmap2x.isNull()); EXPECT_EQ(2.0f, scale_factor); - EXPECT_EQ(width2x, bitmap2x.width()); - EXPECT_EQ(height2x, bitmap2x.height()); + EXPECT_EQ(kWidth2x, bitmap2x.width()); + EXPECT_EQ(kHeight2x, bitmap2x.height()); // ToImageSkia should create a second representation. EXPECT_EQ(2u, image.RepresentationCount()); } TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { - const int width1x = 10; - const int height1x= 12; - const int width2x = 20; - const int height2x = 24; + const int kWidth1x = 10; + const int kHeight1x= 12; + const int kWidth2x = 20; + const int kHeight2x = 24; gfx::ImageSkia image_skia; - image_skia.AddBitmapForScale(gt::CreateBitmap(width1x, height1x), 1.0f); - image_skia.AddBitmapForScale(gt::CreateBitmap(width2x, height2x), 2.0f); + image_skia.AddBitmapForScale(gt::CreateBitmap(kWidth1x, kHeight1x), 1.0f); + image_skia.AddBitmapForScale(gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f); gfx::Image image(image_skia); @@ -91,23 +121,23 @@ TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { EXPECT_TRUE(ns_image); // Image size should be the same as the 1x bitmap. - EXPECT_EQ([ns_image size].width, width1x); - EXPECT_EQ([ns_image size].height, height1x); + EXPECT_EQ([ns_image size].width, kWidth1x); + EXPECT_EQ([ns_image size].height, kHeight1x); EXPECT_EQ(2u, [[image representations] count]); NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; - if ([image_rep_1 size].width == width1x) { - EXPECT_EQ([image_rep_1 size].width, width1x); - EXPECT_EQ([image_rep_1 size].height, height1x); - EXPECT_EQ([image_rep_2 size].width, width2x); - EXPECT_EQ([image_rep_2 size].height, height2x); + if ([image_rep_1 size].width == kWidth1x) { + EXPECT_EQ([image_rep_1 size].width, kWidth1x); + EXPECT_EQ([image_rep_1 size].height, kHeight1x); + EXPECT_EQ([image_rep_2 size].width, kWidth2x); + EXPECT_EQ([image_rep_2 size].height, kHeight2x); } else { - EXPECT_EQ([image_rep_1 size].width, width2x); - EXPECT_EQ([image_rep_1 size].height, height2x); - EXPECT_EQ([image_rep_2 size].width, width1x); - EXPECT_EQ([image_rep_2 size].height, height1x); + EXPECT_EQ([image_rep_1 size].width, kWidth2x); + EXPECT_EQ([image_rep_1 size].height, kHeight2x); + EXPECT_EQ([image_rep_2 size].width, kWidth1x); + EXPECT_EQ([image_rep_2 size].height, kHeight1x); } // Cast to NSImage* should create a second representation. |