1 // Copyright (c) 2014 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.
5 #import <QuartzCore/QuartzCore.h>
6 #import <UIKit/UIKit.h>
8 #include "base/logging.h"
9 #include "base/mac/scoped_cftyperef.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/image/image.h"
12 #include "ui/gfx/image/image_skia.h"
16 // Helper function to return a UIImage with the given size and scale.
17 UIImage* UIImageWithSizeAndScale(CGFloat width, CGFloat height, CGFloat scale) {
18 CGSize target_size = CGSizeMake(width * scale, height * scale);
20 // Create a UIImage directly from a CGImage in order to control the exact
21 // pixel size of the underlying image.
22 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
23 CGColorSpaceCreateDeviceRGB());
24 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
29 target_size.width * 4,
31 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
33 CGRect target_rect = CGRectMake(0, 0,
34 target_size.width, target_size.height);
35 CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
36 CGContextFillRect(context, target_rect);
38 base::ScopedCFTypeRef<CGImageRef> cg_image(
39 CGBitmapContextCreateImage(context));
40 return [UIImage imageWithCGImage:cg_image
42 orientation:UIImageOrientationUp];
46 class ImageIOSTest : public testing::Test {
49 ~ImageIOSTest() override {}
51 void SetUp() override {
52 original_scale_factors_ = gfx::ImageSkia::GetSupportedScales();
55 void TearDown() override {
56 gfx::ImageSkia::SetSupportedScales(original_scale_factors_);
60 // Used to save and restore the scale factors in effect before this test.
61 std::vector<float> original_scale_factors_;
63 DISALLOW_COPY_AND_ASSIGN(ImageIOSTest);
66 // Tests image conversion when the scale factor of the source image is not in
67 // the list of supported scale factors.
68 TEST_F(ImageIOSTest, ImageConversionWithUnsupportedScaleFactor) {
69 const CGFloat kWidth = 200;
70 const CGFloat kHeight = 100;
71 const CGFloat kTestScales[3] = { 1.0f, 2.0f, 3.0f };
73 for (size_t i = 0; i < arraysize(kTestScales); ++i) {
74 for (size_t j = 0; j < arraysize(kTestScales); ++j) {
75 const CGFloat source_scale = kTestScales[i];
76 const CGFloat supported_scale = kTestScales[j];
78 // Set the supported scale for testing.
79 std::vector<float> supported_scales;
80 supported_scales.push_back(supported_scale);
81 gfx::ImageSkia::SetSupportedScales(supported_scales);
83 // Create an UIImage with the appropriate source_scale.
85 UIImageWithSizeAndScale(kWidth, kHeight, source_scale);
86 ASSERT_EQ(kWidth, ui_image.size.width);
87 ASSERT_EQ(kHeight, ui_image.size.height);
88 ASSERT_EQ(source_scale, ui_image.scale);
90 // Convert to SkBitmap and test its size.
91 gfx::Image to_skbitmap([ui_image retain]);
92 const SkBitmap* bitmap = to_skbitmap.ToSkBitmap();
93 ASSERT_TRUE(bitmap != NULL);
94 EXPECT_EQ(kWidth * supported_scale, bitmap->width());
95 EXPECT_EQ(kHeight * supported_scale, bitmap->height());
97 // Convert to ImageSkia and test its size.
98 gfx::Image to_imageskia([ui_image retain]);
99 const gfx::ImageSkia* imageskia = to_imageskia.ToImageSkia();
100 EXPECT_EQ(kWidth, imageskia->width());
101 EXPECT_EQ(kHeight, imageskia->height());
103 // TODO(rohitrao): Convert from ImageSkia back to UIImage. This should
104 // scale the image based on the current set of supported scales.