Browse files

Saving new and diff images from view tests

  • Loading branch information...
1 parent ec07ed5 commit dcdfd6af51d62314a66e01f5b7af44a415244a60 @johnboiles johnboiles committed Jul 24, 2012
View
2 Classes-iOS/GHUnitIOSTestViewController.m
@@ -118,7 +118,7 @@ - (void)testViewDidApproveChange:(GHUnitIOSTestView *)testView {
// Save new image as the approved version
NSString *imageFilename = [testNode_.test.exception.userInfo objectForKey:@"ImageFilename"];
UIImage *renderedImage = [testNode_.test.exception.userInfo objectForKey:@"RenderedImage"];
- [GHViewTestCase saveToDocumentsWithImage:renderedImage filename:imageFilename];
+ [GHViewTestCase saveApprovedViewTestImage:renderedImage filename:imageFilename];
testNode_.test.status = GHTestStatusSucceeded;
[self _runTest];
}
View
2 Classes-iOS/GHUnitIOSViewController.m
@@ -84,7 +84,7 @@ - (void)loadView {
[self reload];
}
-- (void) viewDidAppear:(BOOL)animated {
+- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self reload];
}
View
2 Classes/GHTestCase.m
@@ -47,7 +47,7 @@ - (void)setUpClass { }
- (void)tearDownClass { }
- (BOOL)shouldRunOnMainThread {
- return YES;
+ return NO;
}
- (void)handleException:(NSException *)exception {
View
6 Classes/GHViewTestCase.h
@@ -90,17 +90,17 @@ reason:@"GHVerifyView can only be called from within a GHViewTestCase class"] ra
}
/*!
- Clear all test images in the documents directory
+ Clear all test images in the documents directory.
*/
+ (void)clearTestImages;
/*!
- Save an image to the documents directory as filename
+ Save an approved view test image to the view test images directory.
@param image Image to save
@param filename Filename for the saved image
*/
-+ (void)saveToDocumentsWithImage:(UIImage *)image filename:(NSString *)filename;
++ (void)saveApprovedViewTestImage:(UIImage *)image filename:(NSString *)filename;
/*!
Size for a given view. Subclasses can override this to provide custom sizes
View
105 Classes/GHViewTestCase.m
@@ -37,43 +37,52 @@
@implementation GHViewTestCase
-+ (NSString *)imagesDirectory {
+#pragma mark File Operations
+
++ (NSString *)approvedTestImagesDirectory {
return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/TestImages"];
}
-+ (NSString *)pathForFilename:(NSString *)filename {
- return [NSString stringWithFormat:@"%@/%@", [self imagesDirectory], filename];
++ (NSString *)failedTestImagesDirectory {
+ return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/FailedTestImages"];
}
-+ (void)createImagesDirectory {
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSError *error = nil;
- [fileManager createDirectoryAtPath:[self imagesDirectory] withIntermediateDirectories:YES attributes:nil error:&error];
- if (error) GHUDebug(@"Unable to create directory %@", [self imagesDirectory]);
++ (NSString *)approvedTestImagePathForFilename:(NSString *)filename {
+ return [NSString stringWithFormat:@"%@/%@", [self approvedTestImagesDirectory], filename];
}
-+ (UIImage *)imageWithView:(UIView *)view {
- [view setNeedsDisplay];
- UIGraphicsBeginImageContext(view.frame.size);
- CALayer *layer = view.layer;
- CGContextRef context = UIGraphicsGetCurrentContext();
- [layer renderInContext:context];
- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return viewImage;
++ (NSString *)failedTestImagePathForFilename:(NSString *)filename {
+ return [NSString stringWithFormat:@"%@/%@", [self failedTestImagesDirectory], filename];
}
-+ (void)saveToDocumentsWithImage:(UIImage *)image filename:(NSString *)filename {
- NSString *filePath = [self pathForFilename:filename];
- GHUDebug(@"Saving view test image to %@", filePath);
++ (void)createDirectory:(NSString *)directory {
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+ NSError *error = nil;
+ [fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
+ if (error) GHUDebug(@"Unable to create directory %@", directory);
+}
+
++ (void)saveImage:(UIImage *)image filePath:(NSString *)filePath {
+ GHUDebug(@"Saving image to %@", filePath);
// Save image as PNG
- [self createImagesDirectory];
BOOL saved = [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
if (!saved) GHUDebug(@"Unable to save image to %@", filePath);
}
-+ (UIImage *)readImageWithFilename:(NSString *)filename {
- NSString *filePath = [self pathForFilename:filename];
++ (void)saveApprovedViewTestImage:(UIImage *)image filename:(NSString *)filename {
+ [self createDirectory:[self approvedTestImagesDirectory]];
+ NSString *filePath = [self approvedTestImagePathForFilename:filename];
+ [self saveImage:image filePath:filePath];
+}
+
++ (void)saveFailedViewTestImage:(UIImage *)image filename:(NSString *)filename {
+ [self createDirectory:[self failedTestImagesDirectory]];
+ NSString *filePath = [self failedTestImagePathForFilename:filename];
+ [self saveImage:image filePath:filePath];
+}
+
++ (UIImage *)readSavedTestImageWithFilename:(NSString *)filename {
+ NSString *filePath = [self approvedTestImagePathForFilename:filename];
GHUDebug(@"Trying to load image at path %@", filePath);
// First look in the documents directory for the image
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
@@ -86,19 +95,35 @@ + (UIImage *)readImageWithFilename:(NSString *)filename {
return image;
}
-// Delete all test images from the documents directory
-+ (void)clearTestImages {
++ (void)deleteFilesAtPath:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *directory = [self imagesDirectory];
NSError *error = nil;
- for (NSString *file in [fileManager contentsOfDirectoryAtPath:directory error:&error]) {
- BOOL success = [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directory, file] error:&error];
+ for (NSString *file in [fileManager contentsOfDirectoryAtPath:path error:&error]) {
+ BOOL success = [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", path, file] error:&error];
if (!success || error) {
- GHUDebug(@"Unable to delete file %@%@", directory, file);
+ GHUDebug(@"Unable to delete file %@%@", path, file);
}
}
}
++ (void)clearTestImages {
+ [self deleteFilesAtPath:[self approvedTestImagesDirectory]];
+ [self deleteFilesAtPath:[self failedTestImagesDirectory]];
+}
+
+#pragma mark Image Operations
+
++ (UIImage *)imageWithView:(UIView *)view {
+ [view setNeedsDisplay];
+ UIGraphicsBeginImageContext(view.frame.size);
+ CALayer *layer = view.layer;
+ CGContextRef context = UIGraphicsGetCurrentContext();
+ [layer renderInContext:context];
+ UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+ return viewImage;
+}
+
+ (BOOL)compareImage:(UIImage *)image withRenderedImage:(UIImage *)renderedImage {
if (!image || !renderedImage) return NO;
// If the images are different sizes, just fail
@@ -188,10 +213,14 @@ + (UIImage *)diffWithImage:(UIImage *)image renderedImage:(UIImage *)renderedIma
return returnImage;
}
+#pragma mark Private
+
- (void)_setUp {
imageVerifyCount_ = 0;
}
+#pragma mark Public
+
- (CGSize)sizeForView:(UIView *)view {
// If the view is a UIScrollView, return the contentSize
if ([view isKindOfClass:[UIScrollView class]]) {
@@ -208,13 +237,14 @@ - (void)verifyView:(UIView *)view filename:(NSString *)filename lineNumber:(int)
if (CGSizeEqualToSize(view.frame.size, CGSizeZero)) [[NSException ghu_failureInFile:filename atLine:lineNumber withDescription:@"View must have a nonzero size in GHVerifyView"] raise];
// View testing file names have the format [test class name]-[test selector name]-[UIScreen scale]-[# of verify in selector]-[view class name]
- NSString *imageFilename = [NSString stringWithFormat:@"%@-%@-%1.0f-%d-%@.png",
- NSStringFromClass([self class]),
- NSStringFromSelector(currentSelector_),
- [[UIScreen mainScreen] scale],
- imageVerifyCount_,
- NSStringFromClass([view class])];
- UIImage *originalViewImage = [[self class] readImageWithFilename:imageFilename];
+ NSString *imageFilenamePrefix = [NSString stringWithFormat:@"%@-%@-%1.0f-%d-%@",
+ NSStringFromClass([self class]),
+ NSStringFromSelector(currentSelector_),
+ [[UIScreen mainScreen] scale],
+ imageVerifyCount_,
+ NSStringFromClass([view class])];
+ NSString *imageFilename = [imageFilenamePrefix stringByAppendingString:@".png"];
+ UIImage *originalViewImage = [[self class] readSavedTestImageWithFilename:imageFilename];
CGSize viewSize = [self sizeForView:view];
view.frame = CGRectMake(0, 0, viewSize.width, viewSize.height);
@@ -233,6 +263,9 @@ - (void)verifyView:(UIView *)view filename:(NSString *)filename lineNumber:(int)
UIImage *diffImage = [[self class] diffWithImage:originalViewImage renderedImage:newViewImage];
if (diffImage) [exceptionDictionary setObject:diffImage forKey:@"DiffImage"];
if (originalViewImage) [exceptionDictionary setObject:originalViewImage forKey:@"SavedImage"];
+ // Save new and diff images
+ [[self class] saveFailedViewTestImage:diffImage filename:[imageFilenamePrefix stringByAppendingString:@"-diff.png"]];
+ [[self class] saveFailedViewTestImage:newViewImage filename:[imageFilenamePrefix stringByAppendingString:@"-new.png"]];
[[NSException exceptionWithName:@"GHViewChangeException" reason:@"View has changed" userInfo:exceptionDictionary] raise];
}
imageVerifyCount_++;

0 comments on commit dcdfd6a

Please sign in to comment.