Chromium Code Reviews| Index: ui/gfx/image_mac_unittest.mm |
| diff --git a/ui/gfx/image_mac_unittest.mm b/ui/gfx/image_mac_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..613c5e775581b827180217d10b7a471cb9d02474 |
| --- /dev/null |
| +++ b/ui/gfx/image_mac_unittest.mm |
| @@ -0,0 +1,112 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <Cocoa/Cocoa.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_nsobject.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/gfx/image.h" |
| +#include "ui/gfx/image_unittest.h" |
| + |
| +namespace { |
| + |
| +class ImageMacTest : public testing::Test { |
| + public: |
| + size_t GetRepCount(const gfx::Image& image) { |
| + return image.representations_.size(); |
| + } |
| +}; |
| + |
| +NSImageRep* CreateBitmapImageRep(int width, int height) { |
| + scoped_nsobject<NSImage> image([[NSImage alloc] |
| + initWithSize:NSMakeSize(width, height)]); |
| + [image lockFocus]; |
| + [[NSColor redColor] set]; |
| + NSRectFill(NSMakeRect(0, 0, width, height)); |
| + [image unlockFocus]; |
| + 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
|
| + isKindOfClass:[NSImageRep class]]); |
| + return [[image representations] lastObject]; |
| +} |
| + |
| +namespace gt = gfx::test; |
| + |
| +TEST_F(ImageMacTest, MultiResolutionNSImageToSkBitmap) { |
| + const int width1 = 10; |
| + const int height1 = 12; |
| + const int width2 = 20; |
| + const int height2 = 24; |
| + |
| + NSImageRep* imageRep1 = CreateBitmapImageRep(width1, height1); |
| + NSImageRep* imageRep2 = CreateBitmapImageRep(width2, height2); |
| + 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
|
| + initWithSize:NSMakeSize(width1, height1)]); |
| + [nsImage addRepresentation:imageRep1]; |
| + [nsImage addRepresentation:imageRep2]; |
| + |
| + gfx::Image image(nsImage.release()); |
| + |
| + EXPECT_EQ(1u, GetRepCount(image)); |
| + EXPECT_EQ(2u, image.GetNumberOfSkBitmaps()); |
| + |
| + const SkBitmap* bitmap1 = image.GetSkBitmapAtIndex(0); |
| + EXPECT_TRUE(bitmap1); |
| + const SkBitmap* bitmap2 = image.GetSkBitmapAtIndex(1); |
| + EXPECT_TRUE(bitmap2); |
| + |
| + if (bitmap1->width() == width1) { |
| + EXPECT_EQ(bitmap1->height(), height1); |
| + EXPECT_EQ(bitmap2->width(), width2); |
| + EXPECT_EQ(bitmap2->height(), height2); |
| + } else { |
| + EXPECT_EQ(bitmap1->width(), width2); |
| + EXPECT_EQ(bitmap1->height(), height2); |
| + EXPECT_EQ(bitmap2->width(), width1); |
| + EXPECT_EQ(bitmap2->height(), height1); |
| + } |
| + |
| + // GetNumberOfSkBitmaps and GetSkBitmapAtIndex should create a second |
| + // representation. |
| + EXPECT_EQ(2u, GetRepCount(image)); |
| +} |
| + |
| +TEST_F(ImageMacTest, MultiResolutionSkBitmapToNSImage) { |
| + const int width1 = 10; |
| + const int height1 = 12; |
| + const int width2 = 20; |
| + const int height2 = 24; |
| + |
| + std::vector<const SkBitmap*> bitmaps; |
| + bitmaps.push_back(gt::CreateBitmap(width1, height1)); |
| + bitmaps.push_back(gt::CreateBitmap(width2, height2)); |
| + gfx::Image image(bitmaps); |
| + |
| + EXPECT_EQ(1u, GetRepCount(image)); |
| + EXPECT_EQ(2u, image.GetNumberOfSkBitmaps()); |
| + |
| + NSImage* nsImage = image; |
| + EXPECT_TRUE(nsImage); |
| + |
| + EXPECT_EQ(2u, [[image representations] count]); |
| + NSImageRep* imageRep1 = [[image representations] objectAtIndex:0]; |
| + NSImageRep* imageRep2 = [[image representations] objectAtIndex:1]; |
| + |
| + if ([imageRep1 size].width == width1) { |
| + EXPECT_EQ([imageRep1 size].height, height1); |
| + EXPECT_EQ([imageRep2 size].width, width2); |
| + EXPECT_EQ([imageRep2 size].height, height2); |
| + } else { |
| + EXPECT_EQ([imageRep1 size].width, width2); |
| + EXPECT_EQ([imageRep1 size].height, height2); |
| + EXPECT_EQ([imageRep2 size].width, width1); |
| + EXPECT_EQ([imageRep2 size].height, height1); |
| + } |
| + |
| + // Cast to NSImage* should create a second representation. |
| + EXPECT_EQ(2u, GetRepCount(image)); |
| +} |
| + |
| +} // namespace |