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

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

Issue 6849030: Add support for multi resolution icons (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit test for gfx::Image Created 9 years, 8 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
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <Cocoa/Cocoa.h>
6
7 #include "base/logging.h"
8 #include "base/memory/scoped_nsobject.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/gfx/image.h"
12 #include "ui/gfx/image_unittest.h"
13
14 namespace {
15
16 class ImageMacTest : public testing::Test {
17 public:
18 size_t GetRepCount(const gfx::Image& image) {
19 return image.representations_.size();
20 }
21 };
22
23 NSImageRep* CreateBitmapImageRep(int width, int height) {
24 scoped_nsobject<NSImage> image([[NSImage alloc]
25 initWithSize:NSMakeSize(width, height)]);
26 [image lockFocus];
27 [[NSColor redColor] set];
28 NSRectFill(NSMakeRect(0, 0, width, height));
29 [image unlockFocus];
30 CHECK([[[image representations] lastObject]
Robert Sesek 2011/04/19 02:41:41 CHECK-ing is usually bad in unit tests because it
sail 2011/04/19 04:56:59 Ahh, I didn't realize that a void return type was
31 isKindOfClass:[NSImageRep class]]);
32 return [[image representations] lastObject];
33 }
34
35 namespace gt = gfx::test;
36
37 TEST_F(ImageMacTest, MultiResolutionNSImageToSkBitmap) {
38 const int width1 = 10;
39 const int height1 = 12;
40 const int width2 = 20;
41 const int height2 = 24;
42
43 NSImageRep* imageRep1 = CreateBitmapImageRep(width1, height1);
44 NSImageRep* imageRep2 = CreateBitmapImageRep(width2, height2);
45 scoped_nsobject<NSImage> nsImage([[NSImage alloc]
Robert Sesek 2011/04/19 02:41:41 nit: ns_image
sail 2011/04/19 04:56:59 Fixed
46 initWithSize:NSMakeSize(width1, height1)]);
47 [nsImage addRepresentation:imageRep1];
48 [nsImage addRepresentation:imageRep2];
49
50 gfx::Image image(nsImage.release());
51
52 EXPECT_EQ(1u, GetRepCount(image));
53 EXPECT_EQ(2u, image.GetNumberOfSkBitmaps());
54
55 const SkBitmap* bitmap1 = image.GetSkBitmapAtIndex(0);
56 EXPECT_TRUE(bitmap1);
57 const SkBitmap* bitmap2 = image.GetSkBitmapAtIndex(1);
58 EXPECT_TRUE(bitmap2);
59
60 if (bitmap1->width() == width1) {
61 EXPECT_EQ(bitmap1->height(), height1);
62 EXPECT_EQ(bitmap2->width(), width2);
63 EXPECT_EQ(bitmap2->height(), height2);
64 } else {
65 EXPECT_EQ(bitmap1->width(), width2);
66 EXPECT_EQ(bitmap1->height(), height2);
67 EXPECT_EQ(bitmap2->width(), width1);
68 EXPECT_EQ(bitmap2->height(), height1);
69 }
70
71 // GetNumberOfSkBitmaps and GetSkBitmapAtIndex should create a second
72 // representation.
73 EXPECT_EQ(2u, GetRepCount(image));
74 }
75
76 TEST_F(ImageMacTest, MultiResolutionSkBitmapToNSImage) {
77 const int width1 = 10;
78 const int height1 = 12;
79 const int width2 = 20;
80 const int height2 = 24;
81
82 std::vector<const SkBitmap*> bitmaps;
83 bitmaps.push_back(gt::CreateBitmap(width1, height1));
84 bitmaps.push_back(gt::CreateBitmap(width2, height2));
85 gfx::Image image(bitmaps);
86
87 EXPECT_EQ(1u, GetRepCount(image));
88 EXPECT_EQ(2u, image.GetNumberOfSkBitmaps());
89
90 NSImage* nsImage = image;
91 EXPECT_TRUE(nsImage);
92
93 EXPECT_EQ(2u, [[image representations] count]);
94 NSImageRep* imageRep1 = [[image representations] objectAtIndex:0];
95 NSImageRep* imageRep2 = [[image representations] objectAtIndex:1];
96
97 if ([imageRep1 size].width == width1) {
98 EXPECT_EQ([imageRep1 size].height, height1);
99 EXPECT_EQ([imageRep2 size].width, width2);
100 EXPECT_EQ([imageRep2 size].height, height2);
101 } else {
102 EXPECT_EQ([imageRep1 size].width, width2);
103 EXPECT_EQ([imageRep1 size].height, height2);
104 EXPECT_EQ([imageRep2 size].width, width1);
105 EXPECT_EQ([imageRep2 size].height, height1);
106 }
107
108 // Cast to NSImage* should create a second representation.
109 EXPECT_EQ(2u, GetRepCount(image));
110 }
111
112 } // namespace
OLDNEW
« no previous file with comments | « ui/gfx/image_mac.mm ('k') | ui/gfx/image_unittest.h » ('j') | ui/gfx/image_unittest.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698