| 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 |scales|. | 19 // described by |width|, |height|, and |scale_factors|. |
| 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 |scales|. | 22 // - |ns_image| has NSImageReps of |scale_factors|. |
| 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. | 24 // scale_factor. |
| 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<float>& scales) { | 29 const std::vector<ui::ScaleFactor>& scale_factors) { |
| 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 != scales.size()) { | 33 [ns_image representations].count != scale_factors.size()) { |
| 34 return false; | 34 return false; |
| 35 } | 35 } |
| 36 | 36 |
| 37 for (size_t i = 0; i < scales.size(); ++i) { | 37 for (size_t i = 0; i < scale_factors.size(); ++i) { |
| 38 float scale = scales[i]; | 38 float scale = ui::GetScaleFactorScale(scale_factors[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 gfx::ImageSkia::SetSupportedScales(gfx::test::Get1xAnd2xScales()); | 78 : supported_scale_factors_(gfx::test::Get1xAnd2xScaleFactors()) { |
| 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 |
| 85 DISALLOW_COPY_AND_ASSIGN(ImageMacTest); | 87 DISALLOW_COPY_AND_ASSIGN(ImageMacTest); |
| 86 }; | 88 }; |
| 87 | 89 |
| 88 namespace gt = gfx::test; | 90 namespace gt = gfx::test; |
| 89 | 91 |
| 90 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { | 92 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { |
| 91 const int kWidth1x = 10; | 93 const int kWidth1x = 10; |
| 92 const int kHeight1x = 12; | 94 const int kHeight1x = 12; |
| 93 const int kWidth2x = 20; | 95 const int kWidth2x = 20; |
| 94 const int kHeight2x = 24; | 96 const int kHeight2x = 24; |
| 95 | 97 |
| 96 NSBitmapImageRep* ns_image_rep1; | 98 NSBitmapImageRep* ns_image_rep1; |
| 97 BitmapImageRep(kWidth1x, kHeight1x, &ns_image_rep1); | 99 BitmapImageRep(kWidth1x, kHeight1x, &ns_image_rep1); |
| 98 NSBitmapImageRep* ns_image_rep2; | 100 NSBitmapImageRep* ns_image_rep2; |
| 99 BitmapImageRep(kWidth2x, kHeight2x, &ns_image_rep2); | 101 BitmapImageRep(kWidth2x, kHeight2x, &ns_image_rep2); |
| 100 base::scoped_nsobject<NSImage> ns_image( | 102 base::scoped_nsobject<NSImage> ns_image( |
| 101 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | 103 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
| 102 [ns_image addRepresentation:ns_image_rep1]; | 104 [ns_image addRepresentation:ns_image_rep1]; |
| 103 [ns_image addRepresentation:ns_image_rep2]; | 105 [ns_image addRepresentation:ns_image_rep2]; |
| 104 | 106 |
| 105 gfx::Image image(ns_image.release()); | 107 gfx::Image image(ns_image.release()); |
| 106 | 108 |
| 107 EXPECT_EQ(1u, image.RepresentationCount()); | 109 EXPECT_EQ(1u, image.RepresentationCount()); |
| 108 | 110 |
| 109 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 111 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
| 110 | 112 |
| 111 std::vector<float> scales; | 113 std::vector<ui::ScaleFactor> scale_factors; |
| 112 scales.push_back(1.0f); | 114 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 113 scales.push_back(2.0f); | 115 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
| 114 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, | 116 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, |
| 115 scales)); | 117 scale_factors)); |
| 116 | 118 |
| 117 // ToImageSkia should create a second representation. | 119 // ToImageSkia should create a second representation. |
| 118 EXPECT_EQ(2u, image.RepresentationCount()); | 120 EXPECT_EQ(2u, image.RepresentationCount()); |
| 119 } | 121 } |
| 120 | 122 |
| 121 // Test that convertng to an ImageSkia from an NSImage with scale factors | 123 // Test that convertng to an ImageSkia from an NSImage with scale factors |
| 122 // other than 1x and 2x results in an ImageSkia with scale factors 1x and | 124 // other than 1x and 2x results in an ImageSkia with scale factors 1x and |
| 123 // 2x; | 125 // 2x; |
| 124 TEST_F(ImageMacTest, UnalignedMultiResolutionNSImageToImageSkia) { | 126 TEST_F(ImageMacTest, UnalignedMultiResolutionNSImageToImageSkia) { |
| 125 const int kWidth1x = 10; | 127 const int kWidth1x = 10; |
| 126 const int kHeight1x= 12; | 128 const int kHeight1x= 12; |
| 127 const int kWidth4x = 40; | 129 const int kWidth4x = 40; |
| 128 const int kHeight4x = 48; | 130 const int kHeight4x = 48; |
| 129 | 131 |
| 130 NSBitmapImageRep* ns_image_rep4; | 132 NSBitmapImageRep* ns_image_rep4; |
| 131 BitmapImageRep(kWidth4x, kHeight4x, &ns_image_rep4); | 133 BitmapImageRep(kWidth4x, kHeight4x, &ns_image_rep4); |
| 132 base::scoped_nsobject<NSImage> ns_image( | 134 base::scoped_nsobject<NSImage> ns_image( |
| 133 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | 135 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
| 134 [ns_image addRepresentation:ns_image_rep4]; | 136 [ns_image addRepresentation:ns_image_rep4]; |
| 135 | 137 |
| 136 gfx::Image image(ns_image.release()); | 138 gfx::Image image(ns_image.release()); |
| 137 | 139 |
| 138 EXPECT_EQ(1u, image.RepresentationCount()); | 140 EXPECT_EQ(1u, image.RepresentationCount()); |
| 139 | 141 |
| 140 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 142 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
| 141 | 143 |
| 142 std::vector<float> scales; | 144 std::vector<ui::ScaleFactor> scale_factors; |
| 143 scales.push_back(1.0f); | 145 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 144 scales.push_back(2.0f); | 146 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
| 145 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, | 147 EXPECT_TRUE(gt::ImageSkiaStructureMatches(*image_skia, kWidth1x, kHeight1x, |
| 146 scales)); | 148 scale_factors)); |
| 147 | 149 |
| 148 // ToImageSkia should create a second representation. | 150 // ToImageSkia should create a second representation. |
| 149 EXPECT_EQ(2u, image.RepresentationCount()); | 151 EXPECT_EQ(2u, image.RepresentationCount()); |
| 150 } | 152 } |
| 151 | 153 |
| 152 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { | 154 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { |
| 153 const int kWidth1x = 10; | 155 const int kWidth1x = 10; |
| 154 const int kHeight1x= 12; | 156 const int kHeight1x= 12; |
| 155 const int kWidth2x = 20; | 157 const int kWidth2x = 20; |
| 156 const int kHeight2x = 24; | 158 const int kHeight2x = 24; |
| 157 | 159 |
| 158 gfx::ImageSkia image_skia; | 160 gfx::ImageSkia image_skia; |
| 159 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 161 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
| 160 gt::CreateBitmap(kWidth1x, kHeight1x), 1.0f)); | 162 gt::CreateBitmap(kWidth1x, kHeight1x), ui::SCALE_FACTOR_100P)); |
| 161 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 163 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
| 162 gt::CreateBitmap(kWidth2x, kHeight2x), 2.0f)); | 164 gt::CreateBitmap(kWidth2x, kHeight2x), ui::SCALE_FACTOR_200P)); |
| 163 | 165 |
| 164 gfx::Image image(image_skia); | 166 gfx::Image image(image_skia); |
| 165 | 167 |
| 166 EXPECT_EQ(1u, image.RepresentationCount()); | 168 EXPECT_EQ(1u, image.RepresentationCount()); |
| 167 EXPECT_EQ(2u, image.ToImageSkia()->image_reps().size()); | 169 EXPECT_EQ(2u, image.ToImageSkia()->image_reps().size()); |
| 168 | 170 |
| 169 NSImage* ns_image = image.ToNSImage(); | 171 NSImage* ns_image = image.ToNSImage(); |
| 170 | 172 |
| 171 std::vector<float> scales; | 173 std::vector<ui::ScaleFactor> scale_factors; |
| 172 scales.push_back(1.0f); | 174 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 173 scales.push_back(2.0f); | 175 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
| 174 EXPECT_TRUE(NSImageStructureMatches(ns_image, kWidth1x, kHeight1x, scales)); | 176 EXPECT_TRUE(NSImageStructureMatches(ns_image, kWidth1x, kHeight1x, |
| 177 scale_factors)); |
| 175 | 178 |
| 176 // Request for NSImage* should create a second representation. | 179 // Request for NSImage* should create a second representation. |
| 177 EXPECT_EQ(2u, image.RepresentationCount()); | 180 EXPECT_EQ(2u, image.RepresentationCount()); |
| 178 } | 181 } |
| 179 | 182 |
| 180 TEST_F(ImageMacTest, MultiResolutionPNGToNSImage) { | 183 TEST_F(ImageMacTest, MultiResolutionPNGToNSImage) { |
| 181 const int kSize1x = 25; | 184 const int kSize1x = 25; |
| 182 const int kSize2x = 50; | 185 const int kSize2x = 50; |
| 183 | 186 |
| 184 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); | 187 scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); |
| 185 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); | 188 scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); |
| 186 std::vector<gfx::ImagePNGRep> image_png_reps; | 189 std::vector<gfx::ImagePNGRep> image_png_reps; |
| 187 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, 1.0f)); | 190 image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); |
| 188 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, 2.0f)); | 191 image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); |
| 189 | 192 |
| 190 gfx::Image image(image_png_reps); | 193 gfx::Image image(image_png_reps); |
| 191 | 194 |
| 192 NSImage* ns_image = image.ToNSImage(); | 195 NSImage* ns_image = image.ToNSImage(); |
| 193 std::vector<float> scales; | 196 std::vector<ui::ScaleFactor> scale_factors; |
| 194 scales.push_back(1.0f); | 197 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
| 195 scales.push_back(2.0f); | 198 scale_factors.push_back(ui::SCALE_FACTOR_200P); |
| 196 EXPECT_TRUE(NSImageStructureMatches(ns_image, kSize1x, kSize1x, scales)); | 199 EXPECT_TRUE(NSImageStructureMatches(ns_image, kSize1x, kSize1x, |
| 200 scale_factors)); |
| 197 | 201 |
| 198 // Converting from PNG to NSImage should not go through ImageSkia. | 202 // Converting from PNG to NSImage should not go through ImageSkia. |
| 199 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); | 203 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kImageRepSkia)); |
| 200 | 204 |
| 201 // Convert to ImageSkia to check pixel contents of NSImageReps. | 205 // Convert to ImageSkia to check pixel contents of NSImageReps. |
| 202 gfx::ImageSkia image_skia = gfx::ImageSkiaFromNSImage(ns_image); | 206 gfx::ImageSkia image_skia = gfx::ImageSkiaFromNSImage(ns_image); |
| 203 EXPECT_TRUE(gt::IsEqual(bytes1x, | 207 EXPECT_TRUE(gt::IsEqual(bytes1x, |
| 204 image_skia.GetRepresentation(1.0f).sk_bitmap())); | 208 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P).sk_bitmap())); |
| 205 EXPECT_TRUE(gt::IsEqual(bytes2x, | 209 EXPECT_TRUE(gt::IsEqual(bytes2x, |
| 206 image_skia.GetRepresentation(2.0f).sk_bitmap())); | 210 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P).sk_bitmap())); |
| 207 } | 211 } |
| 208 | 212 |
| 209 } // namespace | 213 } // namespace |
| OLD | NEW |