Chromium Code Reviews| 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 #include <Cocoa/Cocoa.h> | 5 #include <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_nsobject.h" | 8 #include "base/memory/scoped_nsobject.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #include "ui/gfx/image/image.h" | 11 #include "ui/gfx/image/image.h" |
| 12 #include "ui/gfx/image/image_skia.h" | 12 #include "ui/gfx/image/image_skia.h" |
| 13 #include "ui/gfx/image/image_unittest_util.h" | 13 #include "ui/gfx/image/image_unittest_util.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class ImageMacTest : public testing::Test { | 17 class ImageMacTest : public testing::Test { |
| 18 public: | 18 public: |
| 19 void CreateBitmapImageRep(int width, int height, NSImageRep** image_rep) { | 19 void CreateBitmapImageRep(int width, int height, NSImageRep** image_rep) { |
| 20 // Create NSImageRep with point size |width|x|height|. | |
| 21 // Use resulting NSImageRep pixel size to detect device scale factor. | |
| 20 scoped_nsobject<NSImage> image( | 22 scoped_nsobject<NSImage> image( |
| 21 [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); | 23 [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); |
| 22 [image lockFocus]; | 24 [image lockFocus]; |
| 23 [[NSColor redColor] set]; | 25 [[NSColor redColor] set]; |
| 24 NSRectFill(NSMakeRect(0, 0, width, height)); | 26 NSRectFill(NSMakeRect(0, 0, width, height)); |
| 25 [image unlockFocus]; | 27 [image unlockFocus]; |
| 26 EXPECT_TRUE([[[image representations] lastObject] | 28 EXPECT_TRUE([[[image representations] lastObject] |
| 27 isKindOfClass:[NSImageRep class]]); | 29 isKindOfClass:[NSImageRep class]]); |
| 28 *image_rep = [[image representations] lastObject]; | 30 NSImageRep* point_sized_image_rep = [[image representations] lastObject]; |
| 31 | |
| 32 // Create new NSBitmapImageRep with the correct size. | |
| 33 float x_scale = [point_sized_image_rep pixelsWide] / width; | |
| 34 float y_scale = [point_sized_image_rep pixelsHigh] / height; | |
| 35 NSRect request_rect = NSMakeRect(0, 0, static_cast<float>(width) / x_scale, | |
|
Nico
2012/06/11 05:05:14
Remove static_cast<>, promotion rules do that impl
| |
| 36 static_cast<float>(height) / y_scale); | |
| 37 [image lockFocus]; | |
| 38 *image_rep = [[[NSBitmapImageRep alloc] | |
| 39 initWithFocusedViewRect:request_rect] autorelease]; | |
| 40 [image unlockFocus]; | |
|
Nico
2012/06/11 05:05:14
…hm, this is pretty convoluted. Maybe it's simpler
| |
| 29 } | 41 } |
| 30 }; | 42 }; |
| 31 | 43 |
| 32 namespace gt = gfx::test; | 44 namespace gt = gfx::test; |
| 33 | 45 |
| 46 TEST_F(ImageMacTest, NSImageWithResizedNSImageRepToImageSkia) { | |
| 47 const int kWidth1x = 10; | |
| 48 const int kHeight1x = 12; | |
| 49 const int kWidth2x = 20; | |
| 50 const int kHeight2x = 24; | |
| 51 | |
| 52 NSImageRep* image_rep; | |
| 53 CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep); | |
| 54 | |
| 55 scoped_nsobject<NSImage> ns_image( | |
| 56 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | |
| 57 [ns_image addRepresentation:image_rep]; | |
| 58 | |
| 59 [image_rep setSize:NSMakeSize(kWidth1x, kHeight1x)]; | |
| 60 | |
| 61 gfx::Image image(ns_image.release()); | |
| 62 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | |
| 63 | |
| 64 float scale_factor; | |
| 65 const SkBitmap& bitmap = image_skia->GetBitmapForScale(2.0f, 2.0f, | |
| 66 &scale_factor); | |
| 67 EXPECT_EQ(2.0f, scale_factor); | |
| 68 EXPECT_EQ(kWidth2x, bitmap.width()); | |
| 69 EXPECT_EQ(kHeight2x, bitmap.height()); | |
| 70 } | |
| 71 | |
| 34 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { | 72 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { |
| 35 const int width1x = 10; | 73 const int kWidth1x = 10; |
| 36 const int height1x = 12; | 74 const int kHeight1x = 12; |
| 37 const int width2x = 20; | 75 const int kWidth2x = 20; |
| 38 const int height2x = 24; | 76 const int kHeight2x = 24; |
| 39 | 77 |
| 40 NSImageRep* image_rep_1; | 78 NSImageRep* image_rep_1; |
| 41 CreateBitmapImageRep(width1x, height1x, &image_rep_1); | 79 CreateBitmapImageRep(kWidth1x, kHeight1x, &image_rep_1); |
| 42 NSImageRep* image_rep_2; | 80 NSImageRep* image_rep_2; |
| 43 CreateBitmapImageRep(width2x, height2x, &image_rep_2); | 81 CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep_2); |
| 44 scoped_nsobject<NSImage> ns_image( | 82 scoped_nsobject<NSImage> ns_image( |
| 45 [[NSImage alloc] initWithSize:NSMakeSize(width1x, height1x)]); | 83 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
| 46 [ns_image addRepresentation:image_rep_1]; | 84 [ns_image addRepresentation:image_rep_1]; |
| 47 [ns_image addRepresentation:image_rep_2]; | 85 [ns_image addRepresentation:image_rep_2]; |
| 48 | 86 |
| 49 gfx::Image image(ns_image.release()); | 87 gfx::Image image(ns_image.release()); |
| 50 | 88 |
| 51 EXPECT_EQ(1u, image.RepresentationCount()); | 89 EXPECT_EQ(1u, image.RepresentationCount()); |
| 52 | 90 |
| 53 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 91 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
| 54 EXPECT_EQ(2u, image_skia->bitmaps().size()); | 92 EXPECT_EQ(2u, image_skia->bitmaps().size()); |
| 55 | 93 |
| 56 float scale_factor; | 94 float scale_factor; |
| 57 const SkBitmap& bitmap1x = image_skia->GetBitmapForScale(1.0f, 1.0f, | 95 const SkBitmap& bitmap1x = image_skia->GetBitmapForScale(1.0f, 1.0f, |
| 58 &scale_factor); | 96 &scale_factor); |
| 59 EXPECT_TRUE(!bitmap1x.isNull()); | 97 EXPECT_TRUE(!bitmap1x.isNull()); |
| 60 EXPECT_EQ(1.0f, scale_factor); | 98 EXPECT_EQ(1.0f, scale_factor); |
| 61 EXPECT_EQ(width1x, bitmap1x.width()); | 99 EXPECT_EQ(kWidth1x, bitmap1x.width()); |
| 62 EXPECT_EQ(height1x, bitmap1x.height()); | 100 EXPECT_EQ(kHeight1x, bitmap1x.height()); |
| 63 | 101 |
| 64 const SkBitmap& bitmap2x = image_skia->GetBitmapForScale(2.0f, 2.0f, | 102 const SkBitmap& bitmap2x = image_skia->GetBitmapForScale(2.0f, 2.0f, |
| 65 &scale_factor); | 103 &scale_factor); |
| 66 EXPECT_TRUE(!bitmap2x.isNull()); | 104 EXPECT_TRUE(!bitmap2x.isNull()); |
| 67 EXPECT_EQ(2.0f, scale_factor); | 105 EXPECT_EQ(2.0f, scale_factor); |
| 68 EXPECT_EQ(width2x, bitmap2x.width()); | 106 EXPECT_EQ(kWidth2x, bitmap2x.width()); |
| 69 EXPECT_EQ(height2x, bitmap2x.height()); | 107 EXPECT_EQ(kHeight2x, bitmap2x.height()); |
| 70 | 108 |
| 71 // ToImageSkia should create a second representation. | 109 // ToImageSkia should create a second representation. |
| 72 EXPECT_EQ(2u, image.RepresentationCount()); | 110 EXPECT_EQ(2u, image.RepresentationCount()); |
| 73 } | 111 } |
| 74 | 112 |
| 75 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { | 113 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { |
| 76 const int width1x = 10; | 114 const int kWidth1x = 10; |
| 77 const int height1x= 12; | 115 const int kHeight1x= 12; |
| 78 const int width2x = 20; | 116 const int kWidth2x = 20; |
| 79 const int height2x = 24; | 117 const int kHeight2x = 24; |
| 80 | 118 |
| 81 gfx::ImageSkia image_skia; | 119 gfx::ImageSkia image_skia; |
| 82 image_skia.AddBitmapForScale(gt::CreateBitmap(width1x, height1x), 1.0f); | 120 image_skia.AddBitmapForScale(gt::CreateBitmap(kWidth1x, kHeight1x), 1.0f); |
| 83 image_skia.AddBitmapForScale(gt::CreateBitmap(width2x, height2x), 2.0f); | 121 image_skia.AddBitmapForScale(gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f); |
| 84 | 122 |
| 85 gfx::Image image(image_skia); | 123 gfx::Image image(image_skia); |
| 86 | 124 |
| 87 EXPECT_EQ(1u, image.RepresentationCount()); | 125 EXPECT_EQ(1u, image.RepresentationCount()); |
| 88 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); | 126 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); |
| 89 | 127 |
| 90 NSImage* ns_image = image; | 128 NSImage* ns_image = image; |
| 91 EXPECT_TRUE(ns_image); | 129 EXPECT_TRUE(ns_image); |
| 92 | 130 |
| 93 // Image size should be the same as the 1x bitmap. | 131 // Image size should be the same as the 1x bitmap. |
| 94 EXPECT_EQ([ns_image size].width, width1x); | 132 EXPECT_EQ([ns_image size].width, kWidth1x); |
| 95 EXPECT_EQ([ns_image size].height, height1x); | 133 EXPECT_EQ([ns_image size].height, kHeight1x); |
| 96 | 134 |
| 97 EXPECT_EQ(2u, [[image representations] count]); | 135 EXPECT_EQ(2u, [[image representations] count]); |
| 98 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; | 136 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; |
| 99 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; | 137 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; |
| 100 | 138 |
| 101 if ([image_rep_1 size].width == width1x) { | 139 if ([image_rep_1 size].width == kWidth1x) { |
| 102 EXPECT_EQ([image_rep_1 size].width, width1x); | 140 EXPECT_EQ([image_rep_1 size].width, kWidth1x); |
| 103 EXPECT_EQ([image_rep_1 size].height, height1x); | 141 EXPECT_EQ([image_rep_1 size].height, kHeight1x); |
| 104 EXPECT_EQ([image_rep_2 size].width, width2x); | 142 EXPECT_EQ([image_rep_2 size].width, kWidth2x); |
| 105 EXPECT_EQ([image_rep_2 size].height, height2x); | 143 EXPECT_EQ([image_rep_2 size].height, kHeight2x); |
| 106 } else { | 144 } else { |
| 107 EXPECT_EQ([image_rep_1 size].width, width2x); | 145 EXPECT_EQ([image_rep_1 size].width, kWidth2x); |
| 108 EXPECT_EQ([image_rep_1 size].height, height2x); | 146 EXPECT_EQ([image_rep_1 size].height, kHeight2x); |
| 109 EXPECT_EQ([image_rep_2 size].width, width1x); | 147 EXPECT_EQ([image_rep_2 size].width, kWidth1x); |
| 110 EXPECT_EQ([image_rep_2 size].height, height1x); | 148 EXPECT_EQ([image_rep_2 size].height, kHeight1x); |
| 111 } | 149 } |
| 112 | 150 |
| 113 // Cast to NSImage* should create a second representation. | 151 // Cast to NSImage* should create a second representation. |
| 114 EXPECT_EQ(2u, image.RepresentationCount()); | 152 EXPECT_EQ(2u, image.RepresentationCount()); |
| 115 } | 153 } |
| 116 | 154 |
| 117 } // namespace | 155 } // namespace |
| OLD | NEW |