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, |
20 scoped_nsobject<NSImage> image( | 20 NSBitmapImageRep** image_rep) { |
21 [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); | 21 *image_rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL |
Nico
2012/06/11 15:23:07
nit: break before initWith…, and align subsequent
| |
22 [image lockFocus]; | 22 pixelsWide:width |
23 [[NSColor redColor] set]; | 23 pixelsHigh:height |
24 NSRectFill(NSMakeRect(0, 0, width, height)); | 24 bitsPerSample:8 |
25 [image unlockFocus]; | 25 samplesPerPixel:3 |
26 EXPECT_TRUE([[[image representations] lastObject] | 26 hasAlpha:NO |
27 isKindOfClass:[NSImageRep class]]); | 27 isPlanar:NO |
28 *image_rep = [[image representations] lastObject]; | 28 colorSpaceName:@"NSDeviceRGBColorSpace" |
Nico
2012/06/11 15:23:07
I think NSDeviceRGBColorSpace is a named constant
| |
29 bitmapFormat:0 | |
30 bytesPerRow:0 | |
31 bitsPerPixel:0]; | |
29 } | 32 } |
30 }; | 33 }; |
31 | 34 |
32 namespace gt = gfx::test; | 35 namespace gt = gfx::test; |
33 | 36 |
37 TEST_F(ImageMacTest, NSImageWithResizedNSImageRepToImageSkia) { | |
38 const int kWidth1x = 10; | |
39 const int kHeight1x = 12; | |
40 const int kWidth2x = 20; | |
41 const int kHeight2x = 24; | |
42 | |
43 NSBitmapImageRep* image_rep; | |
44 CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep); | |
45 | |
46 scoped_nsobject<NSImage> ns_image( | |
47 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | |
48 [ns_image addRepresentation:image_rep]; | |
49 | |
50 [image_rep setSize:NSMakeSize(kWidth1x, kHeight1x)]; | |
51 | |
52 gfx::Image image(ns_image.release()); | |
53 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | |
54 | |
55 float scale_factor; | |
56 const SkBitmap& bitmap = image_skia->GetBitmapForScale(2.0f, 2.0f, | |
57 &scale_factor); | |
58 EXPECT_EQ(2.0f, scale_factor); | |
59 EXPECT_EQ(kWidth2x, bitmap.width()); | |
60 EXPECT_EQ(kHeight2x, bitmap.height()); | |
61 } | |
62 | |
34 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { | 63 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { |
35 const int width1x = 10; | 64 const int kWidth1x = 10; |
36 const int height1x = 12; | 65 const int kHeight1x = 12; |
37 const int width2x = 20; | 66 const int kWidth2x = 20; |
38 const int height2x = 24; | 67 const int kHeight2x = 24; |
39 | 68 |
40 NSImageRep* image_rep_1; | 69 NSBitmapImageRep* image_rep_1; |
41 CreateBitmapImageRep(width1x, height1x, &image_rep_1); | 70 CreateBitmapImageRep(kWidth1x, kHeight1x, &image_rep_1); |
42 NSImageRep* image_rep_2; | 71 NSBitmapImageRep* image_rep_2; |
43 CreateBitmapImageRep(width2x, height2x, &image_rep_2); | 72 CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep_2); |
44 scoped_nsobject<NSImage> ns_image( | 73 scoped_nsobject<NSImage> ns_image( |
45 [[NSImage alloc] initWithSize:NSMakeSize(width1x, height1x)]); | 74 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
46 [ns_image addRepresentation:image_rep_1]; | 75 [ns_image addRepresentation:image_rep_1]; |
47 [ns_image addRepresentation:image_rep_2]; | 76 [ns_image addRepresentation:image_rep_2]; |
48 | 77 |
49 gfx::Image image(ns_image.release()); | 78 gfx::Image image(ns_image.release()); |
50 | 79 |
51 EXPECT_EQ(1u, image.RepresentationCount()); | 80 EXPECT_EQ(1u, image.RepresentationCount()); |
52 | 81 |
53 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 82 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
54 EXPECT_EQ(2u, image_skia->bitmaps().size()); | 83 EXPECT_EQ(2u, image_skia->bitmaps().size()); |
55 | 84 |
56 float scale_factor; | 85 float scale_factor; |
57 const SkBitmap& bitmap1x = image_skia->GetBitmapForScale(1.0f, 1.0f, | 86 const SkBitmap& bitmap1x = image_skia->GetBitmapForScale(1.0f, 1.0f, |
58 &scale_factor); | 87 &scale_factor); |
59 EXPECT_TRUE(!bitmap1x.isNull()); | 88 EXPECT_TRUE(!bitmap1x.isNull()); |
60 EXPECT_EQ(1.0f, scale_factor); | 89 EXPECT_EQ(1.0f, scale_factor); |
61 EXPECT_EQ(width1x, bitmap1x.width()); | 90 EXPECT_EQ(kWidth1x, bitmap1x.width()); |
62 EXPECT_EQ(height1x, bitmap1x.height()); | 91 EXPECT_EQ(kHeight1x, bitmap1x.height()); |
63 | 92 |
64 const SkBitmap& bitmap2x = image_skia->GetBitmapForScale(2.0f, 2.0f, | 93 const SkBitmap& bitmap2x = image_skia->GetBitmapForScale(2.0f, 2.0f, |
65 &scale_factor); | 94 &scale_factor); |
66 EXPECT_TRUE(!bitmap2x.isNull()); | 95 EXPECT_TRUE(!bitmap2x.isNull()); |
67 EXPECT_EQ(2.0f, scale_factor); | 96 EXPECT_EQ(2.0f, scale_factor); |
68 EXPECT_EQ(width2x, bitmap2x.width()); | 97 EXPECT_EQ(kWidth2x, bitmap2x.width()); |
69 EXPECT_EQ(height2x, bitmap2x.height()); | 98 EXPECT_EQ(kHeight2x, bitmap2x.height()); |
70 | 99 |
71 // ToImageSkia should create a second representation. | 100 // ToImageSkia should create a second representation. |
72 EXPECT_EQ(2u, image.RepresentationCount()); | 101 EXPECT_EQ(2u, image.RepresentationCount()); |
73 } | 102 } |
74 | 103 |
75 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { | 104 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { |
76 const int width1x = 10; | 105 const int kWidth1x = 10; |
77 const int height1x= 12; | 106 const int kHeight1x= 12; |
78 const int width2x = 20; | 107 const int kWidth2x = 20; |
79 const int height2x = 24; | 108 const int kHeight2x = 24; |
80 | 109 |
81 gfx::ImageSkia image_skia; | 110 gfx::ImageSkia image_skia; |
82 image_skia.AddBitmapForScale(gt::CreateBitmap(width1x, height1x), 1.0f); | 111 image_skia.AddBitmapForScale(gt::CreateBitmap(kWidth1x, kHeight1x), 1.0f); |
83 image_skia.AddBitmapForScale(gt::CreateBitmap(width2x, height2x), 2.0f); | 112 image_skia.AddBitmapForScale(gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f); |
84 | 113 |
85 gfx::Image image(image_skia); | 114 gfx::Image image(image_skia); |
86 | 115 |
87 EXPECT_EQ(1u, image.RepresentationCount()); | 116 EXPECT_EQ(1u, image.RepresentationCount()); |
88 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); | 117 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); |
89 | 118 |
90 NSImage* ns_image = image; | 119 NSImage* ns_image = image; |
91 EXPECT_TRUE(ns_image); | 120 EXPECT_TRUE(ns_image); |
92 | 121 |
93 // Image size should be the same as the 1x bitmap. | 122 // Image size should be the same as the 1x bitmap. |
94 EXPECT_EQ([ns_image size].width, width1x); | 123 EXPECT_EQ([ns_image size].width, kWidth1x); |
95 EXPECT_EQ([ns_image size].height, height1x); | 124 EXPECT_EQ([ns_image size].height, kHeight1x); |
96 | 125 |
97 EXPECT_EQ(2u, [[image representations] count]); | 126 EXPECT_EQ(2u, [[image representations] count]); |
98 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; | 127 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; |
99 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; | 128 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; |
100 | 129 |
101 if ([image_rep_1 size].width == width1x) { | 130 if ([image_rep_1 size].width == kWidth1x) { |
102 EXPECT_EQ([image_rep_1 size].width, width1x); | 131 EXPECT_EQ([image_rep_1 size].width, kWidth1x); |
103 EXPECT_EQ([image_rep_1 size].height, height1x); | 132 EXPECT_EQ([image_rep_1 size].height, kHeight1x); |
104 EXPECT_EQ([image_rep_2 size].width, width2x); | 133 EXPECT_EQ([image_rep_2 size].width, kWidth2x); |
105 EXPECT_EQ([image_rep_2 size].height, height2x); | 134 EXPECT_EQ([image_rep_2 size].height, kHeight2x); |
106 } else { | 135 } else { |
107 EXPECT_EQ([image_rep_1 size].width, width2x); | 136 EXPECT_EQ([image_rep_1 size].width, kWidth2x); |
108 EXPECT_EQ([image_rep_1 size].height, height2x); | 137 EXPECT_EQ([image_rep_1 size].height, kHeight2x); |
109 EXPECT_EQ([image_rep_2 size].width, width1x); | 138 EXPECT_EQ([image_rep_2 size].width, kWidth1x); |
110 EXPECT_EQ([image_rep_2 size].height, height1x); | 139 EXPECT_EQ([image_rep_2 size].height, kHeight1x); |
111 } | 140 } |
112 | 141 |
113 // Cast to NSImage* should create a second representation. | 142 // Cast to NSImage* should create a second representation. |
114 EXPECT_EQ(2u, image.RepresentationCount()); | 143 EXPECT_EQ(2u, image.RepresentationCount()); |
115 } | 144 } |
116 | 145 |
117 } // namespace | 146 } // namespace |
OLD | NEW |