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

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: Nicer diff 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
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 "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 float scale_factor;
61 EXPECT_EQ(bitmap1->height(), height1); 58 const SkBitmap& bitmap1x = image_skia.GetBitmapForScale(1.0f, 1.0f,
62 EXPECT_EQ(bitmap2->width(), width2); 59 &scale_factor);
63 EXPECT_EQ(bitmap2->height(), height2); 60 EXPECT_TRUE(!bitmap1x.isNull());
64 } else { 61 EXPECT_EQ(1.0f, scale_factor);
65 EXPECT_EQ(bitmap1->width(), width2); 62 EXPECT_EQ(width1x, bitmap1x.width());
66 EXPECT_EQ(bitmap1->height(), height2); 63 EXPECT_EQ(height1x, bitmap1x.height());
67 EXPECT_EQ(bitmap2->width(), width1); 64
68 EXPECT_EQ(bitmap2->height(), height1); 65 const SkBitmap& bitmap2x = image_skia.GetBitmapForScale(2.0f, 2.0f,
69 } 66 &scale_factor);
67 EXPECT_TRUE(!bitmap2x.isNull());
68 EXPECT_EQ(2.0f, scale_factor);
69 EXPECT_EQ(width2x, bitmap2x.width());
70 EXPECT_EQ(height2x, bitmap2x.height());
70 71
71 // ToImageSkia should create a second representation. 72 // ToImageSkia should create a second representation.
72 EXPECT_EQ(2u, image.RepresentationCount()); 73 EXPECT_EQ(2u, image.RepresentationCount());
73 } 74 }
74 75
75 TEST_F(ImageMacTest, MultiResolutionSkBitmapToNSImage) { 76 TEST_F(ImageMacTest, MultiResolutionImageSkiaToNSImage) {
76 const int width1 = 10; 77 const int width1x = 10;
77 const int height1 = 12; 78 const int height1x= 12;
78 const int width2 = 20; 79 const int width2x = 20;
79 const int height2 = 24; 80 const int height2x = 24;
80 81
81 std::vector<const SkBitmap*> bitmaps; 82 scoped_ptr<SkBitmap> bitmap1x(gt::CreateBitmap(width1x, height1x));
82 bitmaps.push_back(gt::CreateBitmap(width1, height1)); 83 scoped_ptr<SkBitmap> bitmap2x(gt::CreateBitmap(width2x, height2x));
83 bitmaps.push_back(gt::CreateBitmap(width2, height2)); 84
84 gfx::Image image(bitmaps); 85 gfx::ImageSkia image_skia;
86 image_skia.AddBitmapForScale(*bitmap1x, 1.0f);
87 image_skia.AddBitmapForScale(*bitmap2x, 2.0f);
88
89 gfx::Image image(image_skia);
85 90
86 EXPECT_EQ(1u, image.RepresentationCount()); 91 EXPECT_EQ(1u, image.RepresentationCount());
87 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size()); 92 EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size());
88 93
89 NSImage* ns_image = image; 94 NSImage* ns_image = image;
90 EXPECT_TRUE(ns_image); 95 EXPECT_TRUE(ns_image);
91 96
97 // Image size should be the same as the 1x bitmap.
98 EXPECT_EQ([ns_image size].width, width1x);
99 EXPECT_EQ([ns_image size].height, height1x);
100
92 EXPECT_EQ(2u, [[image representations] count]); 101 EXPECT_EQ(2u, [[image representations] count]);
93 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0]; 102 NSImageRep* image_rep_1 = [[image representations] objectAtIndex:0];
94 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1]; 103 NSImageRep* image_rep_2 = [[image representations] objectAtIndex:1];
95 104
96 if ([image_rep_1 size].width == width1) { 105 if ([image_rep_1 size].width == width1x) {
97 EXPECT_EQ([image_rep_1 size].height, height1); 106 EXPECT_EQ([image_rep_1 size].width, width1x);
98 EXPECT_EQ([image_rep_2 size].width, width2); 107 EXPECT_EQ([image_rep_1 size].height, height1x);
99 EXPECT_EQ([image_rep_2 size].height, height2); 108 EXPECT_EQ([image_rep_2 size].width, width2x);
109 EXPECT_EQ([image_rep_2 size].height, height2x);
100 } else { 110 } else {
101 EXPECT_EQ([image_rep_1 size].width, width2); 111 EXPECT_EQ([image_rep_1 size].width, width2x);
102 EXPECT_EQ([image_rep_1 size].height, height2); 112 EXPECT_EQ([image_rep_1 size].height, height2x);
103 EXPECT_EQ([image_rep_2 size].width, width1); 113 EXPECT_EQ([image_rep_2 size].width, width1x);
104 EXPECT_EQ([image_rep_2 size].height, height1); 114 EXPECT_EQ([image_rep_2 size].height, height1x);
105 } 115 }
106 116
107 // Cast to NSImage* should create a second representation. 117 // Cast to NSImage* should create a second representation.
108 EXPECT_EQ(2u, image.RepresentationCount()); 118 EXPECT_EQ(2u, image.RepresentationCount());
109 } 119 }
110 120
111 } // namespace 121 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698