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