PageRenderTime 73ms CodeModel.GetById 14ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/iPhone/GTMUIImage+ResizeTest.m

http://macfuse.googlecode.com/
Objective C | 284 lines | 209 code | 37 blank | 38 comment | 0 complexity | afba4d5a189577a11f17db73a3c19560 MD5 | raw file
  1//
  2//  GTMUIImage+ResizeTest.m
  3//
  4//  Copyright 2009 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMSenTestCase.h"
 20#import "GTMNSObject+UnitTesting.h"
 21#import "GTMUIImage+Resize.h"
 22
 23#define GTMUIImageResizeAssertImageEqual(imageObject, imageSuffix) \
 24    GTMAssertObjectImageEqualToImageNamed(imageObject, \
 25                                          @"GTMUIImage+Resize_" imageSuffix,\
 26                                          @"Resized image mismatched.")
 27
 28@interface GTMUIImage_ResizeTest : GTMTestCase
 29- (UIImage *)testImageNamed:(NSString *)imageName;
 30@end
 31
 32@implementation GTMUIImage_ResizeTest
 33
 34- (UIImage *)testImageNamed:(NSString *)imageName {
 35  NSBundle *myBundle = [NSBundle bundleForClass:[self class]];
 36  NSString *imagePath = [myBundle pathForResource:imageName ofType:@"png"];
 37  UIImage *result = [UIImage imageWithContentsOfFile:imagePath];
 38  return result;
 39}
 40
 41- (void)testNilImage {
 42  UIImage *image = [[UIImage alloc] init];
 43  UIImage *actual = [image gtm_imageByResizingToSize:CGSizeMake(100, 100)
 44                                 preserveAspectRatio:YES
 45                                           trimToFit:NO];
 46  STAssertNil(actual, @"Invalid inputs should return nil");
 47}
 48
 49- (void)testInvalidInput {
 50  UIImage *actual;
 51  UIImage *image
 52      = [UIImage imageNamed:@"GTMUIImage+Resize_100x50.png"];
 53  actual = [image gtm_imageByResizingToSize:CGSizeZero
 54                        preserveAspectRatio:YES
 55                                  trimToFit:NO];
 56  STAssertNil(actual, @"CGSizeZero resize should be ignored.");
 57
 58  actual = [image gtm_imageByResizingToSize:CGSizeMake(0.1, 0.1)
 59                        preserveAspectRatio:YES
 60                                  trimToFit:NO];
 61  STAssertNil(actual, @"Invalid size should be ignored.");
 62
 63  actual = [image gtm_imageByResizingToSize:CGSizeMake(-100, -100)
 64                        preserveAspectRatio:YES
 65                                  trimToFit:NO];
 66  STAssertNil(actual, @"Invalid size should be ignored.");
 67}
 68
 69- (void)testImageByResizingWithoutPreservingAspectRatio {
 70  UIImage *actual = nil;
 71  // Square image.
 72  UIImage *originalImage = [self testImageNamed:@"GTMUIImage+Resize_100x100"];
 73  STAssertNotNil(originalImage, @"Unable to read image.");
 74
 75  // Resize with same aspect ratio.
 76  CGSize size50x50 = CGSizeMake(50, 50);
 77  actual = [originalImage gtm_imageByResizingToSize:size50x50
 78                                preserveAspectRatio:NO
 79                                          trimToFit:NO];
 80  STAssertTrue(CGSizeEqualToSize([actual size], size50x50),
 81               @"Resized image should equal size: %@ actual: %@",
 82               NSStringFromCGSize(size50x50),
 83               NSStringFromCGSize([actual size]));
 84  GTMUIImageResizeAssertImageEqual(actual, @"100x100_to_50x50");
 85
 86  // Resize with different aspect ratio
 87  CGSize size60x40 = CGSizeMake(60, 40);
 88  actual = [originalImage gtm_imageByResizingToSize:size60x40
 89                                preserveAspectRatio:NO
 90                                          trimToFit:NO];
 91  STAssertTrue(CGSizeEqualToSize([actual size], size60x40),
 92               @"Resized image should equal size: %@ actual: %@",
 93               NSStringFromCGSize(size60x40),
 94               NSStringFromCGSize([actual size]));
 95  GTMUIImageResizeAssertImageEqual(actual, @"100x100_to_60x40");
 96
 97  CGSize size40x60 = CGSizeMake(40, 60);
 98  actual = [originalImage gtm_imageByResizingToSize:size40x60
 99                                preserveAspectRatio:NO
100                                          trimToFit:NO];
101  STAssertTrue(CGSizeEqualToSize([actual size], size40x60),
102               @"Resized image should equal size: %@ actual: %@",
103               NSStringFromCGSize(size40x60),
104               NSStringFromCGSize([actual size]));
105  GTMUIImageResizeAssertImageEqual(actual, @"100x100_to_40x60");
106}
107
108- (void)testImageByResizingPreservingAspectRatioWithoutClip {
109  UIImage *actual = nil;
110  UIImage *landscapeImage = [self testImageNamed:@"GTMUIImage+Resize_100x50"];
111  STAssertNotNil(landscapeImage, @"Unable to read image.");
112
113  // Landscape resize to 50x50, but clipped to 50x25.
114  CGSize size50x50 = CGSizeMake(50, 50);
115  CGSize expected50x25 = CGSizeMake(50, 25);
116  actual = [landscapeImage gtm_imageByResizingToSize:size50x50
117                                 preserveAspectRatio:YES
118                                           trimToFit:NO];
119  STAssertTrue(CGSizeEqualToSize([actual size], expected50x25),
120               @"Resized image should equal size: %@ actual: %@",
121               NSStringFromCGSize(expected50x25),
122               NSStringFromCGSize([actual size]));
123  GTMUIImageResizeAssertImageEqual(actual, @"100x50_to_50x50_noclip");
124
125  // Landscape resize to 60x40, but clipped to 60x30.
126  CGSize size60x40 = CGSizeMake(60, 40);
127  CGSize expected60x30 = CGSizeMake(60, 30);
128
129  actual = [landscapeImage gtm_imageByResizingToSize:size60x40
130                                 preserveAspectRatio:YES
131                                           trimToFit:NO];
132  STAssertTrue(CGSizeEqualToSize([actual size], expected60x30),
133               @"Resized image should equal size: %@ actual: %@",
134               NSStringFromCGSize(expected60x30),
135               NSStringFromCGSize([actual size]));
136  GTMUIImageResizeAssertImageEqual(actual, @"100x50_to_60x40_noclip");
137
138  // Landscape resize to 40x60, but clipped to 40x20.
139  CGSize expected40x20 = CGSizeMake(40, 20);
140  CGSize size40x60 = CGSizeMake(40, 60);
141  actual = [landscapeImage gtm_imageByResizingToSize:size40x60
142                                 preserveAspectRatio:YES
143                                           trimToFit:NO];
144  STAssertTrue(CGSizeEqualToSize([actual size], expected40x20),
145               @"Resized image should equal size: %@ actual: %@",
146               NSStringFromCGSize(expected40x20),
147               NSStringFromCGSize([actual size]));
148  GTMUIImageResizeAssertImageEqual(actual, @"100x50_to_40x60_noclip");
149
150  // Portrait Image
151  UIImage *portraitImage = [self testImageNamed:@"GTMUIImage+Resize_50x100"];
152
153  // Portrait resize to 50x50, but clipped to 25x50.
154  CGSize expected25x50 = CGSizeMake(25, 50);
155  actual = [portraitImage gtm_imageByResizingToSize:size50x50
156                                preserveAspectRatio:YES
157                                          trimToFit:NO];
158  STAssertTrue(CGSizeEqualToSize([actual size], expected25x50),
159               @"Resized image should equal size: %@ actual: %@",
160               NSStringFromCGSize(expected25x50),
161               NSStringFromCGSize([actual size]));
162  GTMUIImageResizeAssertImageEqual(actual, @"50x100_to_50x50_noclip");
163
164  // Portrait resize to 60x40, but clipped to 20x40.
165  CGSize expected20x40 = CGSizeMake(20, 40);
166  actual = [portraitImage gtm_imageByResizingToSize:size60x40
167                                preserveAspectRatio:YES
168                                          trimToFit:NO];
169  STAssertTrue(CGSizeEqualToSize([actual size], expected20x40),
170               @"Resized image should equal size: %@ actual: %@",
171               NSStringFromCGSize(expected20x40),
172               NSStringFromCGSize([actual size]));
173  GTMUIImageResizeAssertImageEqual(actual, @"50x100_to_60x40_noclip");
174
175  // Portrait resize to 40x60, but clipped to 30x60.
176  CGSize expected30x60 = CGSizeMake(30, 60);
177  actual = [portraitImage gtm_imageByResizingToSize:size40x60
178                                preserveAspectRatio:YES
179                                          trimToFit:NO];
180  STAssertTrue(CGSizeEqualToSize([actual size], expected30x60),
181               @"Resized image should equal size: %@ actual: %@",
182               NSStringFromCGSize(expected30x60),
183               NSStringFromCGSize([actual size]));
184  GTMUIImageResizeAssertImageEqual(actual, @"50x100_to_40x60_noclip");
185}
186
187- (void)testImageByResizingPreservingAspectRatioWithClip {
188  UIImage *actual = nil;
189  UIImage *landscapeImage = [self testImageNamed:@"GTMUIImage+Resize_100x50"];
190  STAssertNotNil(landscapeImage, @"Unable to read image.");
191
192  // Landscape resize to 50x50
193  CGSize size50x50 = CGSizeMake(50, 50);
194  actual = [landscapeImage gtm_imageByResizingToSize:size50x50
195                                 preserveAspectRatio:YES
196                                           trimToFit:YES];
197  STAssertTrue(CGSizeEqualToSize([actual size], size50x50),
198               @"Resized image should equal size: %@ actual: %@",
199               NSStringFromCGSize(size50x50),
200               NSStringFromCGSize([actual size]));
201  GTMUIImageResizeAssertImageEqual(actual, @"100x50_to_50x50_clip");
202
203  // Landscape resize to 60x40
204  CGSize size60x40 = CGSizeMake(60, 40);
205  actual = [landscapeImage gtm_imageByResizingToSize:size60x40
206                                 preserveAspectRatio:YES
207                                           trimToFit:YES];
208  STAssertTrue(CGSizeEqualToSize([actual size], size60x40),
209               @"Resized image should equal size: %@ actual: %@",
210               NSStringFromCGSize(size60x40),
211               NSStringFromCGSize([actual size]));
212  GTMUIImageResizeAssertImageEqual(actual, @"100x50_to_60x40_clip");
213
214  // Landscape resize to 40x60
215  CGSize size40x60 = CGSizeMake(40, 60);
216  actual = [landscapeImage gtm_imageByResizingToSize:size40x60
217                                 preserveAspectRatio:YES
218                                           trimToFit:YES];
219  STAssertTrue(CGSizeEqualToSize([actual size], size40x60),
220               @"Resized image should equal size: %@ actual: %@",
221               NSStringFromCGSize(size40x60),
222               NSStringFromCGSize([actual size]));
223  GTMUIImageResizeAssertImageEqual(actual, @"100x50_to_40x60_clip");
224
225  // Portrait Image.
226  UIImage *portraitImage = [self testImageNamed:@"GTMUIImage+Resize_50x100"];
227
228  // Portrait resize to 50x50
229  actual = [portraitImage gtm_imageByResizingToSize:size50x50
230                                  preserveAspectRatio:YES
231                                             trimToFit:YES];
232  STAssertTrue(CGSizeEqualToSize([actual size], size50x50),
233               @"Resized image should equal size: %@ actual: %@",
234               NSStringFromCGSize(size50x50),
235               NSStringFromCGSize([actual size]));
236  GTMUIImageResizeAssertImageEqual(actual, @"50x100_to_50x50_clip");
237
238  // Portrait resize to 60x40
239  actual = [portraitImage gtm_imageByResizingToSize:size60x40
240                                preserveAspectRatio:YES
241                                          trimToFit:YES];
242  STAssertTrue(CGSizeEqualToSize([actual size], size60x40),
243               @"Resized image should equal size: %@ actual: %@",
244               NSStringFromCGSize(size60x40),
245               NSStringFromCGSize([actual size]));
246  GTMUIImageResizeAssertImageEqual(actual, @"50x100_to_60x40_clip");
247
248  // Portrait resize to 40x60.
249  actual = [portraitImage gtm_imageByResizingToSize:size40x60
250                                preserveAspectRatio:YES
251                                          trimToFit:YES];
252  STAssertTrue(CGSizeEqualToSize([actual size], size40x60),
253               @"Resized image should equal size: %@ actual: %@",
254               NSStringFromCGSize(size40x60),
255               NSStringFromCGSize([actual size]));
256  GTMUIImageResizeAssertImageEqual(actual, @"50x100_to_40x60_clip");
257}
258
259- (void)testImageByRotating {
260  UIImage *actual = nil;
261  UIImage *landscapeImage = [self testImageNamed:@"GTMUIImage+Resize_100x50"];
262  STAssertNotNil(landscapeImage, @"Unable to read image.");
263
264  // Rotate 90 degrees.
265  actual = [landscapeImage gtm_imageByRotating:UIImageOrientationRight];
266  GTMUIImageResizeAssertImageEqual(actual, @"50x100");
267
268  // Rotate 180 degrees.
269  actual = [landscapeImage gtm_imageByRotating:UIImageOrientationDown];
270  GTMUIImageResizeAssertImageEqual(actual,
271                                   @"100x50_flipped");
272
273
274  // Rotate 270 degrees.
275  actual = [landscapeImage gtm_imageByRotating:UIImageOrientationLeft];
276  GTMUIImageResizeAssertImageEqual(actual,
277                                   @"50x100_flipped");
278
279  // Rotate 360 degrees.
280  actual = [landscapeImage gtm_imageByRotating:UIImageOrientationUp];
281  GTMUIImageResizeAssertImageEqual(actual, @"100x50");
282}
283
284@end