Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: ui/gfx/image/image_mac_unittest.mm

Issue 10245003: Makes ImageSkia more like SkBitmap (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated comment Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/image/image_mac.mm ('k') | ui/gfx/image/image_skia.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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, MultiResolutionNSImageToSkBitmap) { 34 TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) {
35 const int width1 = 10; 35 const int width1x = 10;
36 const int height1 = 12; 36 const int height1x = 12;
37 const int width2 = 20; 37 const int width2x = 20;
38 const int height2 = 24; 38 const int height2x = 24;
39 39
40 NSImageRep* image_rep_1; 40 NSImageRep* image_rep_1;
41 CreateBitmapImageRep(width1, height1, &image_rep_1); 41 CreateBitmapImageRep(width1x, height1x, &image_rep_1);
42 NSImageRep* image_rep_2; 42 NSImageRep* image_rep_2;
43 CreateBitmapImageRep(width2, height2, &image_rep_2); 43 CreateBitmapImageRep(width2x, height2x, &image_rep_2);
44 scoped_nsobject<NSImage> ns_image( 44 scoped_nsobject<NSImage> ns_image(
45 [[NSImage alloc] initWithSize:NSMakeSize(width1, height1)]); 45 [[NSImage alloc] initWithSize:NSMakeSize(width1x, height1x)]);
46 [ns_image addRepresentation:image_rep_1]; 46 [ns_image addRepresentation:image_rep_1];
47 [ns_image addRepresentation:image_rep_2]; 47 [ns_image addRepresentation:image_rep_2];
48 48
49 gfx::Image image(ns_image.release()); 49 gfx::Image image(ns_image.release());
50 50
51 EXPECT_EQ(1u, image.RepresentationCount()); 51 EXPECT_EQ(1u, image.RepresentationCount());
52 const std::vector<const SkBitmap*>& bitmaps = image.ToImageSkia()->bitmaps();
53 EXPECT_EQ(2u, bitmaps.size());
54 52
55 const SkBitmap* bitmap1 = bitmaps[0]; 53 const gfx::ImageSkia* image_skia = image.ToImageSkia();
56 EXPECT_TRUE(bitmap1); 54 EXPECT_EQ(2u, image_skia->bitmaps().size());
57 const SkBitmap* bitmap2 = bitmaps[1];
58 EXPECT_TRUE(bitmap2);
59 55
60 if (bitmap1->width() == width1) { 56 float scale_factor;
61 EXPECT_EQ(bitmap1->height(), height1); 57 const SkBitmap& bitmap1x = image_skia->GetBitmapForScale(1.0f, 1.0f,
62 EXPECT_EQ(bitmap2->width(), width2); 58 &scale_factor);
63 EXPECT_EQ(bitmap2->height(), height2); 59 EXPECT_TRUE(!bitmap1x.isNull());
64 } else { 60 EXPECT_EQ(1.0f, scale_factor);
65 EXPECT_EQ(bitmap1->width(), width2); 61 EXPECT_EQ(width1x, bitmap1x.width());
66 EXPECT_EQ(bitmap1->height(), height2); 62 EXPECT_EQ(height1x, bitmap1x.height());
67 EXPECT_EQ(bitmap2->width(), width1); 63
68 EXPECT_EQ(bitmap2->height(), height1); 64 const SkBitmap& bitmap2x = image_skia->GetBitmapForScale(2.0f, 2.0f,
69 } 65 &scale_factor);
66 EXPECT_TRUE(!bitmap2x.isNull());
67 EXPECT_EQ(2.0f, scale_factor);
68 EXPECT_EQ(width2x, bitmap2x.width());
69 EXPECT_EQ(height2x, bitmap2x.height());
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 gfx::ImageSkia image_skia;
82 bitmaps.push_back(new SkBitmap(gt::CreateBitmap(width1, height1))); 82 image_skia.AddBitmapForScale(gt::CreateBitmap(width1x, height1x), 1.0f);
83 bitmaps.push_back(new SkBitmap(gt::CreateBitmap(width2, height2))); 83 image_skia.AddBitmapForScale(gt::CreateBitmap(width2x, height2x), 2.0f);
84 gfx::Image image(bitmaps); 84
85 gfx::Image image(image_skia);
85 86
86 EXPECT_EQ(1u, image.RepresentationCount()); 87 EXPECT_EQ(1u, image.RepresentationCount());
87 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); 88 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size());
88 89
89 NSImage* ns_image = image; 90 NSImage* ns_image = image;
90 EXPECT_TRUE(ns_image); 91 EXPECT_TRUE(ns_image);
91 92
93 // Image size should be the same as the 1x bitmap.
94 EXPECT_EQ([ns_image size].width, width1x);
95 EXPECT_EQ([ns_image size].height, height1x);
96
92 EXPECT_EQ(2u, [[image representations] count]); 97 EXPECT_EQ(2u, [[image representations] count]);
93 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; 98 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0];
94 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; 99 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1];
95 100
96 if ([image_rep_1 size].width == width1) { 101 if ([image_rep_1 size].width == width1x) {
97 EXPECT_EQ([image_rep_1 size].height, height1); 102 EXPECT_EQ([image_rep_1 size].width, width1x);
98 EXPECT_EQ([image_rep_2 size].width, width2); 103 EXPECT_EQ([image_rep_1 size].height, height1x);
99 EXPECT_EQ([image_rep_2 size].height, height2); 104 EXPECT_EQ([image_rep_2 size].width, width2x);
105 EXPECT_EQ([image_rep_2 size].height, height2x);
100 } else { 106 } else {
101 EXPECT_EQ([image_rep_1 size].width, width2); 107 EXPECT_EQ([image_rep_1 size].width, width2x);
102 EXPECT_EQ([image_rep_1 size].height, height2); 108 EXPECT_EQ([image_rep_1 size].height, height2x);
103 EXPECT_EQ([image_rep_2 size].width, width1); 109 EXPECT_EQ([image_rep_2 size].width, width1x);
104 EXPECT_EQ([image_rep_2 size].height, height1); 110 EXPECT_EQ([image_rep_2 size].height, height1x);
105 } 111 }
106 112
107 // Cast to NSImage* should create a second representation. 113 // Cast to NSImage* should create a second representation.
108 EXPECT_EQ(2u, image.RepresentationCount()); 114 EXPECT_EQ(2u, image.RepresentationCount());
109 } 115 }
110 116
111 } // namespace 117 } // namespace
OLDNEW
« no previous file with comments | « ui/gfx/image/image_mac.mm ('k') | ui/gfx/image/image_skia.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698