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 "base/memory/scoped_ptr.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
11 #include "ui/gfx/image/image.h" | 12 #include "ui/gfx/image/image.h" |
12 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
13 #include "ui/gfx/image/image_unittest_util.h" | 14 #include "ui/gfx/image/image_unittest_util.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 class ImageMacTest : public testing::Test { | 18 class ImageMacTest : public testing::Test { |
18 public: | 19 public: |
19 void CreateBitmapImageRep(int width, int height, NSImageRep** image_rep) { | 20 void CreateBitmapImageRep(int width, int height, NSImageRep** image_rep) { |
20 scoped_nsobject<NSImage> image( | 21 scoped_nsobject<NSImage> image( |
21 [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); | 22 [[NSImage alloc] initWithSize:NSMakeSize(width, height)]); |
22 [image lockFocus]; | 23 [image lockFocus]; |
23 [[NSColor redColor] set]; | 24 [[NSColor redColor] set]; |
24 NSRectFill(NSMakeRect(0, 0, width, height)); | 25 NSRectFill(NSMakeRect(0, 0, width, height)); |
25 [image unlockFocus]; | 26 [image unlockFocus]; |
26 EXPECT_TRUE([[[image representations] lastObject] | 27 EXPECT_TRUE([[[image representations] lastObject] |
27 isKindOfClass:[NSImageRep class]]); | 28 isKindOfClass:[NSImageRep class]]); |
28 *image_rep = [[image representations] lastObject]; | 29 *image_rep = [[image representations] lastObject]; |
29 } | 30 } |
30 }; | 31 }; |
31 | 32 |
32 namespace gt = gfx::test; | 33 namespace gt = gfx::test; |
33 | 34 |
34 TEST_F(ImageMacTest, MultiResolutionNSImageToSkBitmap) { | 35 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { |
35 const int width1 = 10; | 36 const int width1x = 10; |
36 const int height1 = 12; | 37 const int height1x = 12; |
37 const int width2 = 20; | 38 const int width2x = 20; |
38 const int height2 = 24; | 39 const int height2x = 24; |
39 | 40 |
40 NSImageRep* image_rep_1; | 41 NSImageRep* image_rep_1; |
41 CreateBitmapImageRep(width1, height1, &image_rep_1); | 42 CreateBitmapImageRep(width1x, height1x, &image_rep_1); |
42 NSImageRep* image_rep_2; | 43 NSImageRep* image_rep_2; |
43 CreateBitmapImageRep(width2, height2, &image_rep_2); | 44 CreateBitmapImageRep(width2x, height2x, &image_rep_2); |
44 scoped_nsobject<NSImage> ns_image( | 45 scoped_nsobject<NSImage> ns_image( |
45 [[NSImage alloc] initWithSize:NSMakeSize(width1, height1)]); | 46 [[NSImage alloc] initWithSize:NSMakeSize(width1x, height1x)]); |
46 [ns_image addRepresentation:image_rep_1]; | 47 [ns_image addRepresentation:image_rep_1]; |
47 [ns_image addRepresentation:image_rep_2]; | 48 [ns_image addRepresentation:image_rep_2]; |
48 | 49 |
49 gfx::Image image(ns_image.release()); | 50 gfx::Image image(ns_image.release()); |
50 | 51 |
51 EXPECT_EQ(1u, image.RepresentationCount()); | 52 EXPECT_EQ(1u, image.RepresentationCount()); |
52 const std::vector<const SkBitmap*>& bitmaps = image.ToImageSkia()->bitmaps(); | |
53 EXPECT_EQ(2u, bitmaps.size()); | |
54 | 53 |
55 const SkBitmap* bitmap1 = bitmaps[0]; | 54 const gfx::ImageSkia* image_skia = image.ToImageSkia(); |
56 EXPECT_TRUE(bitmap1); | 55 EXPECT_EQ(2u, image_skia->bitmaps().size()); |
57 const SkBitmap* bitmap2 = bitmaps[1]; | |
58 EXPECT_TRUE(bitmap2); | |
59 | 56 |
60 if (bitmap1->width() == width1) { | 57 const SkBitmap* bitmap; |
61 EXPECT_EQ(bitmap1->height(), height1); | 58 float scale_factor; |
62 EXPECT_EQ(bitmap2->width(), width2); | 59 EXPECT_TRUE(image_skia->GetBitmapForScale(1.0f, 1.0f, &bitmap, |
63 EXPECT_EQ(bitmap2->height(), height2); | 60 &scale_factor)); |
64 } else { | 61 EXPECT_EQ(width1x, bitmap->width()); |
65 EXPECT_EQ(bitmap1->width(), width2); | 62 EXPECT_EQ(height1x, bitmap->height()); |
66 EXPECT_EQ(bitmap1->height(), height2); | 63 EXPECT_EQ(scale_factor, 1.0f); |
67 EXPECT_EQ(bitmap2->width(), width1); | 64 |
68 EXPECT_EQ(bitmap2->height(), height1); | 65 EXPECT_TRUE(image_skia->GetBitmapForScale(2.0f, 2.0f, &bitmap, |
69 } | 66 &scale_factor)); |
| 67 EXPECT_EQ(width2x, bitmap->width()); |
| 68 EXPECT_EQ(height2x, bitmap->height()); |
| 69 EXPECT_EQ(scale_factor, 2.0f); |
70 | 70 |
71 // ToImageSkia should create a second representation. | 71 // ToImageSkia should create a second representation. |
72 EXPECT_EQ(2u, image.RepresentationCount()); | 72 EXPECT_EQ(2u, image.RepresentationCount()); |
73 } | 73 } |
74 | 74 |
75 TEST_F(ImageMacTest, MultiResolutionSkBitmapToNSImage) { | 75 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) { |
76 const int width1 = 10; | 76 const int width1x = 10; |
77 const int height1 = 12; | 77 const int height1x= 12; |
78 const int width2 = 20; | 78 const int width2x = 20; |
79 const int height2 = 24; | 79 const int height2x = 24; |
80 | 80 |
81 std::vector<const SkBitmap*> bitmaps; | 81 scoped_ptr<SkBitmap> bitmap1x(gt::CreateBitmap(width1x, height1x)); |
82 bitmaps.push_back(gt::CreateBitmap(width1, height1)); | 82 scoped_ptr<SkBitmap> bitmap2x(gt::CreateBitmap(width2x, height2x)); |
83 bitmaps.push_back(gt::CreateBitmap(width2, height2)); | 83 |
84 gfx::Image image(bitmaps); | 84 gfx::ImageSkia image_skia; |
| 85 image_skia.AddBitmapForScale(*bitmap1x, 1.0f); |
| 86 image_skia.AddBitmapForScale(*bitmap2x, 2.0f); |
| 87 |
| 88 gfx::Image image(image_skia); |
85 | 89 |
86 EXPECT_EQ(1u, image.RepresentationCount()); | 90 EXPECT_EQ(1u, image.RepresentationCount()); |
87 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); | 91 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); |
88 | 92 |
89 NSImage* ns_image = image; | 93 NSImage* ns_image = image; |
90 EXPECT_TRUE(ns_image); | 94 EXPECT_TRUE(ns_image); |
91 | 95 |
| 96 // Image size should be the same as the 1x bitmap. |
| 97 EXPECT_EQ([ns_image size].width, width1x); |
| 98 EXPECT_EQ([ns_image size].height, height1x); |
| 99 |
92 EXPECT_EQ(2u, [[image representations] count]); | 100 EXPECT_EQ(2u, [[image representations] count]); |
93 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; | 101 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; |
94 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; | 102 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; |
95 | 103 |
96 if ([image_rep_1 size].width == width1) { | 104 if ([image_rep_1 size].width == width1x) { |
97 EXPECT_EQ([image_rep_1 size].height, height1); | 105 EXPECT_EQ([image_rep_1 size].width, width1x); |
98 EXPECT_EQ([image_rep_2 size].width, width2); | 106 EXPECT_EQ([image_rep_1 size].height, height1x); |
99 EXPECT_EQ([image_rep_2 size].height, height2); | 107 EXPECT_EQ([image_rep_2 size].width, width2x); |
| 108 EXPECT_EQ([image_rep_2 size].height, height2x); |
100 } else { | 109 } else { |
101 EXPECT_EQ([image_rep_1 size].width, width2); | 110 EXPECT_EQ([image_rep_1 size].width, width2x); |
102 EXPECT_EQ([image_rep_1 size].height, height2); | 111 EXPECT_EQ([image_rep_1 size].height, height2x); |
103 EXPECT_EQ([image_rep_2 size].width, width1); | 112 EXPECT_EQ([image_rep_2 size].width, width1x); |
104 EXPECT_EQ([image_rep_2 size].height, height1); | 113 EXPECT_EQ([image_rep_2 size].height, height1x); |
105 } | 114 } |
106 | 115 |
107 // Cast to NSImage* should create a second representation. | 116 // Cast to NSImage* should create a second representation. |
108 EXPECT_EQ(2u, image.RepresentationCount()); | 117 EXPECT_EQ(2u, image.RepresentationCount()); |
109 } | 118 } |
110 | 119 |
111 } // namespace | 120 } // namespace |
OLD | NEW |