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/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "ui/gfx/image/image.h" | 10 #include "ui/gfx/image/image.h" |
11 #include "ui/gfx/image/image_png_rep.h" | 11 #include "ui/gfx/image/image_png_rep.h" |
12 #include "ui/gfx/image/image_skia.h" | 12 #include "ui/gfx/image/image_skia.h" |
13 #include "ui/gfx/image/image_skia_util_mac.h" | 13 #include "ui/gfx/image/image_skia_util_mac.h" |
14 #include "ui/gfx/image/image_unittest_util.h" | 14 #include "ui/gfx/image/image_unittest_util.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Returns true if the structure of |ns_image| matches the structure | 18 // Returns true if the structure of |ns_image| matches the structure |
19 // described by |width|, |height|, and |scale_factors|. | 19 // described by |width|, |height|, and |scales|. |
20 // The structure matches if: | 20 // The structure matches if: |
21 // - |ns_image| is not nil. | 21 // - |ns_image| is not nil. |
22 // - |ns_image| has NSImageReps of |scale_factors|. | 22 // - |ns_image| has NSImageReps of |scales|. |
23 // - Each of the NSImageReps has a pixel size of [|ns_image| size] * | 23 // - Each of the NSImageReps has a pixel size of [|ns_image| size] * |
24 // scale_factor. | 24 // scale. |
25 bool NSImageStructureMatches( | 25 bool NSImageStructureMatches( |
26 NSImage* ns_image, | 26 NSImage* ns_image, |
27 int width, | 27 int width, |
28 int height, | 28 int height, |
29 const std::vector<ui::ScaleFactor>& scale_factors) { | 29 const std::vector<float>& scales) { |
30 if (!ns_image || | 30 if (!ns_image || |
31 [ns_image size].width != width || | 31 [ns_image size].width != width || |
32 [ns_image size].height != height || | 32 [ns_image size].height != height || |
33 [ns_image representations].count != scale_factors.size()) { | 33 [ns_image representations].count != scales.size()) { |
34 return false; | 34 return false; |
35 } | 35 } |
36 | 36 |
37 for (size_t i = 0; i < scale_factors.size(); ++i) { | 37 for (size_t i = 0; i < scales.size(); ++i) { |
38 float scale = ui::GetScaleFactorScale(scale_factors[i]); | 38 float scale = scales[i]; |
39 bool found_match = false; | 39 bool found_match = false; |
40 for (size_t j = 0; j < [ns_image representations].count; ++j) { | 40 for (size_t j = 0; j < [ns_image representations].count; ++j) { |
41 NSImageRep* ns_image_rep = [[ns_image representations] objectAtIndex:j]; | 41 NSImageRep* ns_image_rep = [[ns_image representations] objectAtIndex:j]; |
42 if (ns_image_rep && | 42 if (ns_image_rep && |
43 [ns_image_rep pixelsWide] == width * scale && | 43 [ns_image_rep pixelsWide] == width * scale && |
44 [ns_image_rep pixelsHigh] == height * scale) { | 44 [ns_image_rep pixelsHigh] == height * scale) { |
45 found_match = true; | 45 found_match = true; |
46 break; | 46 break; |
47 } | 47 } |
48 } | 48 } |
(...skipping 18 matching lines...) Expand all Loading... |
67 bytesPerRow:0 | 67 bytesPerRow:0 |
68 bitsPerPixel:0] | 68 bitsPerPixel:0] |
69 autorelease]; | 69 autorelease]; |
70 unsigned char* image_rep_data = [*image_rep bitmapData]; | 70 unsigned char* image_rep_data = [*image_rep bitmapData]; |
71 for (int i = 0; i < width * height * 3; ++i) | 71 for (int i = 0; i < width * height * 3; ++i) |
72 image_rep_data[i] = 255; | 72 image_rep_data[i] = 255; |
73 } | 73 } |
74 | 74 |
75 class ImageMacTest : public testing::Test { | 75 class ImageMacTest : public testing::Test { |
76 public: | 76 public: |
77 ImageMacTest() | 77 ImageMacTest() { |
78 : supported_scale_factors_(gfx::test::Get1xAnd2xScaleFactors()) { | 78 gfx::ImageSkia::SetSupportedScales(gfx::test::Get1xAnd2xScales()); |
79 } | 79 } |
80 | 80 |
81 virtual ~ImageMacTest() { | 81 virtual ~ImageMacTest() { |
82 } | 82 } |
83 | 83 |
84 private: | 84 private: |
85 ui::test::ScopedSetSupportedScaleFactors supported_scale_factors_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(ImageMacTest); | 85 DISALLOW_COPY_AND_ASSIGN(ImageMacTest); |
88 }; | 86 }; |
89 | 87 |
90 namespace gt = gfx::test; | 88 namespace gt = gfx::test; |
91 | 89 |
92 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { | 90 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { |
93 const int kWidth1x = 10; | 91 const int kWidth1x = 10; |
94 const int kHeight1x = 12; | 92 const int kHeight1x = 12; |
95 const int kWidth2x = 20; | 93 const int kWidth2x = 20; |
96 const int kHeight2x = 24; | 94 const int kHeight2x = 24; |
97 | 95 |
98 NSBitmapImageRep* ns_image_rep1; | 96 NSBitmapImageRep* ns_image_rep1; |
99 BitmapImageRep(kWidth1x, kHeight1x, &ns_image_rep1); | 97 BitmapImageRep(kWidth1x, kHeight1x, &ns_image_rep1); |
100 NSBitmapImageRep* ns_image_rep2; | 98 NSBitmapImageRep* ns_image_rep2; |
101 BitmapImageRep(kWidth2x, kHeight2x, &ns_image_rep2); | 99 BitmapImageRep(kWidth2x, kHeight2x, &ns_image_rep2); |
102 base::scoped_nsobject<NSImage> ns_image( | 100 base::scoped_nsobject<NSImage> ns_image( |
103 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | 101 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
104 [ns_image addRepresentation:ns_image_rep1]; | 102 [ns_image addRepresentation:ns_image_rep1]; |
105 [ns_image addRepresentation:ns_image_rep2]; | 103 [ns_image addRepresentation:ns_image_rep2]; |
106 | 104 |
107 gfx::Image image(ns_image.release()); | 105 gfx::Image image(ns_image.release()); |
108 | 106 |
109 EXPECT_EQ(1u, image.RepresentationCount()); | 107 EXPECT_EQ(1u, image.RepresentationCount()); |
110 | 108 |
111 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 109 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
112 | 110 |
113 std::vector<ui::ScaleFactor> scale_factors; | 111 std::vector<float> scales; |
114 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 112 scales.push_back(1.0f); |
115 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 113 scales.push_back(2.0f); |
116 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, | 114 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, |
117 scale_factors)); | 115 scales)); |
118 | 116 |
119 // ToImageSkia should create a second representation. | 117 // ToImageSkia should create a second representation. |
120 EXPECT_EQ(2u, image.RepresentationCount()); | 118 EXPECT_EQ(2u, image.RepresentationCount()); |
121 } | 119 } |
122 | 120 |
123 // Test that convertng to an ImageSkia from an NSImage with scale factors | 121 // Test that convertng to an ImageSkia from an NSImage with scale factors |
124 // other than 1x and 2x results in an ImageSkia with scale factors 1x and | 122 // other than 1x and 2x results in an ImageSkia with scale factors 1x and |
125 // 2x; | 123 // 2x; |
126 TEST_F(ImageMacTest, UnalignedMultiResolutionNSImageToImageSkia) { | 124 TEST_F(ImageMacTest, UnalignedMultiResolutionNSImageToImageSkia) { |
127 const int kWidth1x = 10; | 125 const int kWidth1x = 10; |
128 const int kHeight1x= 12; | 126 const int kHeight1x= 12; |
129 const int kWidth4x = 40; | 127 const int kWidth4x = 40; |
130 const int kHeight4x = 48; | 128 const int kHeight4x = 48; |
131 | 129 |
132 NSBitmapImageRep* ns_image_rep4; | 130 NSBitmapImageRep* ns_image_rep4; |
133 BitmapImageRep(kWidth4x, kHeight4x, &ns_image_rep4); | 131 BitmapImageRep(kWidth4x, kHeight4x, &ns_image_rep4); |
134 base::scoped_nsobject<NSImage> ns_image( | 132 base::scoped_nsobject<NSImage> ns_image( |
135 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | 133 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
136 [ns_image addRepresentation:ns_image_rep4]; | 134 [ns_image addRepresentation:ns_image_rep4]; |
137 | 135 |
138 gfx::Image image(ns_image.release()); | 136 gfx::Image image(ns_image.release()); |
139 | 137 |
140 EXPECT_EQ(1u, image.RepresentationCount()); | 138 EXPECT_EQ(1u, image.RepresentationCount()); |
141 | 139 |
142 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 140 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
143 | 141 |
144 std::vector<ui::ScaleFactor> scale_factors; | 142 std::vector<float> scales; |
145 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 143 scales.push_back(1.0f); |
146 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 144 scales.push_back(2.0f); |
147 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, | 145 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, |
148 scale_factors)); | 146 scales)); |
149 | 147 |
150 // ToImageSkia should create a second representation. | 148 // ToImageSkia should create a second representation. |
151 EXPECT_EQ(2u, image.RepresentationCount()); | 149 EXPECT_EQ(2u, image.RepresentationCount()); |
152 } | 150 } |
153 | 151 |
154 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { | 152 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { |
155 const int kWidth1x = 10; | 153 const int kWidth1x = 10; |
156 const int kHeight1x= 12; | 154 const int kHeight1x= 12; |
157 const int kWidth2x = 20; | 155 const int kWidth2x = 20; |
158 const int kHeight2x = 24; | 156 const int kHeight2x = 24; |
159 | 157 |
160 gfx::ImageSkia image_skia; | 158 gfx::ImageSkia image_skia; |
161 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 159 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
162 gt::CreateBitmap(kWidth1x, kHeight1x), ui::SCALE_FACTOR_100P)); | 160 gt::CreateBitmap(kWidth1x, kHeight1x), 1.0f)); |
163 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 161 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
164 gt::CreateBitmap(kWidth2x, kHeight2x), ui::SCALE_FACTOR_200P)); | 162 gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f)); |
165 | 163 |
166 gfx::Image image(image_skia); | 164 gfx::Image image(image_skia); |
167 | 165 |
168 EXPECT_EQ(1u, image.RepresentationCount()); | 166 EXPECT_EQ(1u, image.RepresentationCount()); |
169 EXPECT_EQ(2u, image.ToImageSkia()->image_reps().size()); | 167 EXPECT_EQ(2u, image.ToImageSkia()->image_reps().size()); |
170 | 168 |
171 NSImage* ns_image = image.ToNSImage(); | 169 NSImage* ns_image = image.ToNSImage(); |
172 | 170 |
173 std::vector<ui::ScaleFactor> scale_factors; | 171 std::vector<float> scales; |
174 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 172 scales.push_back(1.0f); |
175 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 173 scales.push_back(2.0f); |
176 EXPECT_TRUE(NSImageStructureMatches(ns_image, kWidth1x, kHeight1x, | 174 EXPECT_TRUE(NSImageStructureMatches(ns_image, kWidth1x, kHeight1x, scales)); |
177 scale_factors)); | |
178 | 175 |
179 // Request for NSImage* should create a second representation. | 176 // Request for NSImage* should create a second representation. |
180 EXPECT_EQ(2u, image.RepresentationCount()); | 177 EXPECT_EQ(2u, image.RepresentationCount()); |
181 } | 178 } |
182 | 179 |
183 TEST_F(ImageMacTest, MultiResolutionPNGToNSImage) { | 180 TEST_F(ImageMacTest, MultiResolutionPNGToNSImage) { |
184 const int kSize1x = 25; | 181 const int kSize1x = 25; |
185 const int kSize2x = 50; | 182 const int kSize2x = 50; |
186 | 183 |
187 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); | 184 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); |
188 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); | 185 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); |
189 std::vector<gfx::ImagePNGRep> image_png_reps; | 186 std::vector<gfx::ImagePNGRep> image_png_reps; |
190 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); | 187 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, 1.0f)); |
191 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); | 188 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, 2.0f)); |
192 | 189 |
193 gfx::Image image(image_png_reps); | 190 gfx::Image image(image_png_reps); |
194 | 191 |
195 NSImage* ns_image = image.ToNSImage(); | 192 NSImage* ns_image = image.ToNSImage(); |
196 std::vector<ui::ScaleFactor> scale_factors; | 193 std::vector<float> scales; |
197 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 194 scales.push_back(1.0f); |
198 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 195 scales.push_back(2.0f); |
199 EXPECT_TRUE(NSImageStructureMatches(ns_image, kSize1x, kSize1x, | 196 EXPECT_TRUE(NSImageStructureMatches(ns_image, kSize1x, kSize1x, scales)); |
200 scale_factors)); | |
201 | 197 |
202 // Converting from PNG to NSImage should not go through ImageSkia. | 198 // Converting from PNG to NSImage should not go through ImageSkia. |
203 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); | 199 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); |
204 | 200 |
205 // Convert to ImageSkia to check pixel contents of NSImageReps. | 201 // Convert to ImageSkia to check pixel contents of NSImageReps. |
206 gfx::ImageSkia image_skia = gfx::ImageSkiaFromNSImage(ns_image); | 202 gfx::ImageSkia image_skia = gfx::ImageSkiaFromNSImage(ns_image); |
207 EXPECT_TRUE(gt::IsEqual(bytes1x, | 203 EXPECT_TRUE(gt::IsEqual(bytes1x, |
208 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); | 204 image_skia.GetRepresentation(1.0f).sk_bitmap())); |
209 EXPECT_TRUE(gt::IsEqual(bytes2x, | 205 EXPECT_TRUE(gt::IsEqual(bytes2x, |
210 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P).sk_bitmap())); | 206 image_skia.GetRepresentation(2.0f).sk_bitmap())); |
211 } | 207 } |
212 | 208 |
213 } // namespace | 209 } // namespace |
OLD | NEW |