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, | 19 void BitmapImageRep(int width, int height, |
20 NSBitmapImageRep** image_rep) { | 20 NSBitmapImageRep** image_rep) { |
21 *image_rep = [[NSBitmapImageRep alloc] | 21 *image_rep = [[[NSBitmapImageRep alloc] |
22 initWithBitmapDataPlanes:NULL | 22 initWithBitmapDataPlanes:NULL |
23 pixelsWide:width | 23 pixelsWide:width |
24 pixelsHigh:height | 24 pixelsHigh:height |
25 bitsPerSample:8 | 25 bitsPerSample:8 |
26 samplesPerPixel:3 | 26 samplesPerPixel:3 |
27 hasAlpha:NO | 27 hasAlpha:NO |
28 isPlanar:NO | 28 isPlanar:NO |
29 colorSpaceName:NSDeviceRGBColorSpace | 29 colorSpaceName:NSDeviceRGBColorSpace |
30 bitmapFormat:0 | 30 bitmapFormat:0 |
31 bytesPerRow:0 | 31 bytesPerRow:0 |
32 bitsPerPixel:0]; | 32 bitsPerPixel:0] |
| 33 autorelease]; |
33 } | 34 } |
34 }; | 35 }; |
35 | 36 |
36 namespace gt = gfx::test; | 37 namespace gt = gfx::test; |
37 | 38 |
38 TEST_F(ImageMacTest, NSImageWithResizedNSImageRepToImageSkia) { | 39 TEST_F(ImageMacTest, NSImageWithResizedNSImageRepToImageSkia) { |
39 const int kWidth1x = 10; | 40 const int kWidth1x = 10; |
40 const int kHeight1x = 12; | 41 const int kHeight1x = 12; |
41 const int kWidth2x = 20; | 42 const int kWidth2x = 20; |
42 const int kHeight2x = 24; | 43 const int kHeight2x = 24; |
43 | 44 |
44 NSBitmapImageRep* image_rep; | 45 NSBitmapImageRep* image_rep; |
45 CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep); | 46 BitmapImageRep(kWidth2x, kHeight2x, &image_rep); |
46 | 47 |
47 scoped_nsobject<NSImage> ns_image( | 48 scoped_nsobject<NSImage> ns_image( |
48 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | 49 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
49 [ns_image addRepresentation:image_rep]; | 50 [ns_image addRepresentation:image_rep]; |
50 | 51 |
51 [image_rep setSize:NSMakeSize(kWidth1x, kHeight1x)]; | 52 [image_rep setSize:NSMakeSize(kWidth1x, kHeight1x)]; |
52 | 53 |
53 gfx::Image image(ns_image.release()); | 54 gfx::Image image(ns_image.release()); |
54 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 55 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
55 | 56 |
56 float scale_factor; | 57 float scale_factor; |
57 const SkBitmap& bitmap = image_skia->GetBitmapForScale(2.0f, 2.0f, | 58 const SkBitmap& bitmap = image_skia->GetBitmapForScale(2.0f, 2.0f, |
58 &scale_factor); | 59 &scale_factor); |
59 EXPECT_EQ(2.0f, scale_factor); | 60 EXPECT_EQ(2.0f, scale_factor); |
60 EXPECT_EQ(kWidth2x, bitmap.width()); | 61 EXPECT_EQ(kWidth2x, bitmap.width()); |
61 EXPECT_EQ(kHeight2x, bitmap.height()); | 62 EXPECT_EQ(kHeight2x, bitmap.height()); |
62 } | 63 } |
63 | 64 |
64 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { | 65 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { |
65 const int kWidth1x = 10; | 66 const int kWidth1x = 10; |
66 const int kHeight1x = 12; | 67 const int kHeight1x = 12; |
67 const int kWidth2x = 20; | 68 const int kWidth2x = 20; |
68 const int kHeight2x = 24; | 69 const int kHeight2x = 24; |
69 | 70 |
70 NSBitmapImageRep* image_rep_1; | 71 NSBitmapImageRep* image_rep_1; |
71 CreateBitmapImageRep(kWidth1x, kHeight1x, &image_rep_1); | 72 BitmapImageRep(kWidth1x, kHeight1x, &image_rep_1); |
72 NSBitmapImageRep* image_rep_2; | 73 NSBitmapImageRep* image_rep_2; |
73 CreateBitmapImageRep(kWidth2x, kHeight2x, &image_rep_2); | 74 BitmapImageRep(kWidth2x, kHeight2x, &image_rep_2); |
74 scoped_nsobject<NSImage> ns_image( | 75 scoped_nsobject<NSImage> ns_image( |
75 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); | 76 [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); |
76 [ns_image addRepresentation:image_rep_1]; | 77 [ns_image addRepresentation:image_rep_1]; |
77 [ns_image addRepresentation:image_rep_2]; | 78 [ns_image addRepresentation:image_rep_2]; |
78 | 79 |
79 gfx::Image image(ns_image.release()); | 80 gfx::Image image(ns_image.release()); |
80 | 81 |
81 EXPECT_EQ(1u, image.RepresentationCount()); | 82 EXPECT_EQ(1u, image.RepresentationCount()); |
82 | 83 |
83 const gfx::ImageSkia* image_skia = image.ToImageSkia(); | 84 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 EXPECT_EQ([image_rep_1 size].height, kHeight2x); | 139 EXPECT_EQ([image_rep_1 size].height, kHeight2x); |
139 EXPECT_EQ([image_rep_2 size].width, kWidth1x); | 140 EXPECT_EQ([image_rep_2 size].width, kWidth1x); |
140 EXPECT_EQ([image_rep_2 size].height, kHeight1x); | 141 EXPECT_EQ([image_rep_2 size].height, kHeight1x); |
141 } | 142 } |
142 | 143 |
143 // Cast to NSImage* should create a second representation. | 144 // Cast to NSImage* should create a second representation. |
144 EXPECT_EQ(2u, image.RepresentationCount()); | 145 EXPECT_EQ(2u, image.RepresentationCount()); |
145 } | 146 } |
146 | 147 |
147 } // namespace | 148 } // namespace |
OLD | NEW |