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