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. 4 5 #import <QuartzCore/QuartzCore.h> 6 #import <UIKit/UIKit.h> 7 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" 13 14 namespace { 15 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); 19 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( 25 NULL, 26 target_size.width, 27 target_size.height, 28 8, 29 target_size.width * 4, 30 color_space, 31 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)); 32 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); 37 38 base::ScopedCFTypeRef<CGImageRef> cg_image( 39 CGBitmapContextCreateImage(context)); 40 return [UIImage imageWithCGImage:cg_image 41 scale:scale 42 orientation:UIImageOrientationUp]; 43 } 44 45 46 class ImageIOSTest : public testing::Test { 47 public: 48 ImageIOSTest() {} 49 virtual ~ImageIOSTest() {} 50 51 virtual void SetUp() OVERRIDE { 52 original_scale_factors_ = gfx::ImageSkia::GetSupportedScales(); 53 } 54 55 virtual void TearDown() OVERRIDE { 56 gfx::ImageSkia::SetSupportedScales(original_scale_factors_); 57 } 58 59 private: 60 // Used to save and restore the scale factors in effect before this test. 61 std::vector<float> original_scale_factors_; 62 63 DISALLOW_COPY_AND_ASSIGN(ImageIOSTest); 64 }; 65 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 }; 72 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]; 77 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); 82 83 // Create an UIImage with the appropriate source_scale. 84 UIImage* ui_image = 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); 89 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()); 96 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()); 102 103 // TODO(rohitrao): Convert from ImageSkia back to UIImage. This should 104 // scale the image based on the current set of supported scales. 105 } 106 } 107 } 108 109 } // namespace 110