OLD | NEW |
---|---|
(Empty) | |
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 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( | |
21 CGColorSpaceCreateDeviceRGB()); | |
22 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate( | |
23 NULL, | |
24 target_size.width, | |
25 target_size.height, | |
26 8, | |
27 target_size.width * 4, | |
28 color_space, | |
29 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)); | |
30 | |
31 CGRect target_rect = CGRectMake(0, 0, | |
32 target_size.width, target_size.height); | |
33 CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]); | |
34 CGContextFillRect(context, target_rect); | |
35 | |
36 base::ScopedCFTypeRef<CGImageRef> cg_image( | |
37 CGBitmapContextCreateImage(context)); | |
38 return [UIImage imageWithCGImage:cg_image | |
39 scale:scale | |
40 orientation:UIImageOrientationUp]; | |
41 } | |
42 | |
43 class ImageIOSTest : public testing::Test { | |
44 public: | |
45 ImageIOSTest() {} | |
46 virtual ~ImageIOSTest() {} | |
47 | |
48 private: | |
49 DISALLOW_COPY_AND_ASSIGN(ImageIOSTest); | |
50 }; | |
51 | |
52 // Tests image conversion when the scale factor of the source image is not in | |
53 // the list of supported scale factors.. | |
lliabraa
2014/09/16 14:13:36
drop extra period
rohitrao (ping after 24h)
2014/09/16 14:16:48
Done.
| |
54 TEST_F(ImageIOSTest, ImageConversionWithUnsupportedScaleFactor) { | |
55 const CGFloat kWidth = 200; | |
56 const CGFloat kHeight = 100; | |
57 const CGFloat kTestScales[3] = { 1.0f, 2.0f, 3.0f }; | |
58 | |
59 for (size_t i = 0; i < arraysize(kTestScales); ++i) { | |
60 for (size_t j = 0; j < arraysize(kTestScales); ++j) { | |
61 const CGFloat source_scale = kTestScales[i]; | |
62 const CGFloat supported_scale = kTestScales[j]; | |
63 | |
64 // Set the supported scale for testing. | |
65 std::vector<float> supported_scales; | |
66 supported_scales.push_back(supported_scale); | |
67 gfx::ImageSkia::SetSupportedScales(supported_scales); | |
68 | |
69 // Create an UIImage with the appropriate source_scale. | |
70 UIImage* ui_image = | |
71 UIImageWithSizeAndScale(kWidth, kHeight, source_scale); | |
72 ASSERT_EQ(kWidth, ui_image.size.width); | |
73 ASSERT_EQ(kHeight, ui_image.size.height); | |
74 ASSERT_EQ(source_scale, ui_image.scale); | |
75 | |
76 // Convert to SkBitmap and test its size. | |
77 gfx::Image to_skbitmap([ui_image retain]); | |
78 const SkBitmap* bitmap = to_skbitmap.ToSkBitmap(); | |
79 ASSERT_TRUE(bitmap != NULL); | |
80 EXPECT_EQ(kWidth * supported_scale, bitmap->width()); | |
81 EXPECT_EQ(kHeight * supported_scale, bitmap->height()); | |
82 | |
83 // Convert to ImageSkia and test its size. | |
84 gfx::Image to_imageskia([ui_image retain]); | |
85 const gfx::ImageSkia* imageskia = to_imageskia.ToImageSkia(); | |
86 EXPECT_EQ(kWidth, imageskia->width()); | |
87 EXPECT_EQ(kHeight, imageskia->height()); | |
88 | |
89 // TODO(rohitrao): Convert from ImageSkia back to UIImage. This should | |
90 // scale the image based on the current set of supported scales. | |
91 } | |
92 } | |
93 } | |
94 | |
95 } // namespace | |
OLD | NEW |