Browse files

Writing a view test for view tests

  • Loading branch information...
1 parent dcdfd6a commit 3cbaebe063c8beef653d4ea46007ab6bdcb3d9b4 @johnboiles johnboiles committed Jul 24, 2012
View
9 Classes/GHViewTestCase.h
@@ -95,6 +95,15 @@ reason:@"GHVerifyView can only be called from within a GHViewTestCase class"] ra
+ (void)clearTestImages;
/*!
+ Creates a UIImage from the passed in view. This can be useful for testing
+ views that require images.
+
+ @param view UIView to render
+ @result UIImage of the rendered UIView
+ */
++ (UIImage *)imageWithView:(UIView *)view;
+
+/*!
Save an approved view test image to the view test images directory.
@param image Image to save
View
15 Classes/GHViewTestCase.m
@@ -233,8 +233,15 @@ - (CGSize)sizeForView:(UIView *)view {
- (void)verifyView:(UIView *)view filename:(NSString *)filename lineNumber:(int)lineNumber {
// Fail if the view is nil
if (!view) [[NSException ghu_failureInFile:filename atLine:lineNumber withDescription:@"View cannot be nil in GHVerifyView"] raise];
- // Fail if the view has CGSizeZero
- if (CGSizeEqualToSize(view.frame.size, CGSizeZero)) [[NSException ghu_failureInFile:filename atLine:lineNumber withDescription:@"View must have a nonzero size in GHVerifyView"] raise];
+
+ CGSize viewSize = [self sizeForView:view];
+ view.frame = CGRectMake(0, 0, viewSize.width, viewSize.height);
+
+ // Fail if the view has width == 0 or height == 0
+ if (CGRectIsEmpty(view.frame)) {
+ NSString *description = [NSString stringWithFormat:@"View must have a nonzero size in GHVerifyView (view.frame was %@)", NSStringFromCGRect(view.frame)];
+ [[NSException ghu_failureInFile:filename atLine:lineNumber withDescription:description] raise];
+ }
// View testing file names have the format [test class name]-[test selector name]-[UIScreen scale]-[# of verify in selector]-[view class name]
NSString *imageFilenamePrefix = [NSString stringWithFormat:@"%@-%@-%1.0f-%d-%@",
@@ -245,10 +252,6 @@ - (void)verifyView:(UIView *)view filename:(NSString *)filename lineNumber:(int)
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);
-
UIImage *newViewImage = [[self class] imageWithView:view];
NSMutableDictionary *exceptionDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
newViewImage, @"RenderedImage",
View
19 Project-iOS/GHUnitIOS.xcodeproj/project.pbxproj
@@ -145,6 +145,7 @@
00CEA042156C2E0B00BA3276 /* GHUIImageViewControl.h in Headers */ = {isa = PBXBuildFile; fileRef = 00CEA03F156C2E0B00BA3276 /* GHUIImageViewControl.h */; };
00CEA043156C2E0B00BA3276 /* GHUIImageViewControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 00CEA040156C2E0B00BA3276 /* GHUIImageViewControl.m */; };
00CEA044156C2E0B00BA3276 /* GHUIImageViewControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 00CEA040156C2E0B00BA3276 /* GHUIImageViewControl.m */; };
+ 42187C1815BF472300E702B6 /* GHUnitIOSTestViewTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 42187C1715BF472300E702B6 /* GHUnitIOSTestViewTest.m */; };
421C7B8F13F0AF1D00E21E74 /* GHUnitIOSTestView.h in Headers */ = {isa = PBXBuildFile; fileRef = 421C7B8D13F0AF1D00E21E74 /* GHUnitIOSTestView.h */; };
421C7B9013F0AF1D00E21E74 /* GHUnitIOSTestView.h in Headers */ = {isa = PBXBuildFile; fileRef = 421C7B8D13F0AF1D00E21E74 /* GHUnitIOSTestView.h */; };
421C7B9113F0AF1D00E21E74 /* GHUnitIOSTestView.m in Sources */ = {isa = PBXBuildFile; fileRef = 421C7B8E13F0AF1D00E21E74 /* GHUnitIOSTestView.m */; };
@@ -271,6 +272,7 @@
00C979CE13BA8A5000FC285C /* GHUnitIPhoneAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitIPhoneAppDelegate.m; sourceTree = "<group>"; };
00CEA03F156C2E0B00BA3276 /* GHUIImageViewControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GHUIImageViewControl.h; sourceTree = "<group>"; };
00CEA040156C2E0B00BA3276 /* GHUIImageViewControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUIImageViewControl.m; sourceTree = "<group>"; };
+ 42187C1715BF472300E702B6 /* GHUnitIOSTestViewTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitIOSTestViewTest.m; sourceTree = "<group>"; };
421C7B8D13F0AF1D00E21E74 /* GHUnitIOSTestView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GHUnitIOSTestView.h; sourceTree = "<group>"; };
421C7B8E13F0AF1D00E21E74 /* GHUnitIOSTestView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitIOSTestView.m; sourceTree = "<group>"; };
4228630F1458BC1200BF3ED2 /* GHViewTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GHViewTestCase.h; sourceTree = "<group>"; };
@@ -543,6 +545,7 @@
00C9798A13BA809600FC285C /* GHTestLogTest.m */,
00C9798B13BA809600FC285C /* GHTestMacrosTest.m */,
00C9798C13BA809600FC285C /* GHTestOnMainThread.m */,
+ 42187C1715BF472300E702B6 /* GHUnitIOSTestViewTest.m */,
00C9798D13BA809600FC285C /* GHUnitIOSTestMain.m */,
00C9798E13BA809600FC285C /* GTM */,
);
@@ -690,6 +693,7 @@
0097C4A413BA7FC800D3BEF0 /* Sources */,
0097C4A513BA7FC800D3BEF0 /* Frameworks */,
0097C4A613BA7FC800D3BEF0 /* Resources */,
+ 42187C1A15BF519500E702B6 /* Copy Test Images */,
00C979CC13BA886700FC285C /* ShellScript */,
);
buildRules = (
@@ -770,6 +774,20 @@
shellPath = /bin/sh;
shellScript = "sh ../Scripts/RunTests.sh";
};
+ 42187C1A15BF519500E702B6 /* Copy Test Images */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ name = "Copy Test Images";
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = ../Scripts/PrepareUITests.sh;
+ };
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
@@ -861,6 +879,7 @@
00C979A113BA809600FC285C /* GHTestOnMainThread.m in Sources */,
00C979A213BA809600FC285C /* GHUnitIOSTestMain.m in Sources */,
00C979A313BA809600FC285C /* GTMSenTestCase.m in Sources */,
+ 42187C1815BF472300E702B6 /* GHUnitIOSTestViewTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
BIN Project-iOS/TestImages/GHUnitIOSTestViewTest-testNoSavedImage-1-0-GHUnitIOSTestView.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
90 Tests/GHUnitIOSTestViewTest.m
@@ -0,0 +1,90 @@
+//
+// GHUnitIOSTestViewTest.m
+// GHUnitIOS
+//
+// Created by John Boiles on 7/24/12.
+// Copyright (c) 2012. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#import "GHViewTestCase.h"
+#import "GHUnitIOSTestView.h"
+
+@interface GHUnitIOSTestViewTest : GHViewTestCase
+@end
+
+@implementation GHUnitIOSTestViewTest
+
+- (void)testNoSavedImage {
+ GHUnitIOSTestView *testView = [[GHUnitIOSTestView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
+ NSString *testText =
+@"GHUnitIOSTestViewTest/test ✘ 4.79s\n"
+"\n"
+" Name: GHViewUnavailableException\n"
+" File: /Users/johnb/Programming/gh-unit2/Tests/GHUnitIOSTestViewTest.m\n"
+" Line: 21\n"
+" Reason: No image saved for view\n"
+" \n"
+" (\n"
+" 0 CoreFoundation 0x0142a03e __exceptionPreprocess + 206\n"
+" 1 libobjc.A.dylib 0x015bbcd6 objc_exception_throw + 44\n"
+" 2 CoreFoundation 0x01429ee1 -[NSException raise] + 17\n"
+" 3 Tests 0x00031531 -[GHViewTestCase verifyView:filename:lineNumber:] + 1825\n"
+" 4 Tests 0x0000e4fc -[GHUnitIOSTestViewTest test] + 780\n"
+" 5 CoreFoundation 0x0142bdea -[NSObject performSelector:] + 58\n"
+" 6 Tests 0x00018123 +[GHTesting runTestWithTarget:selector:exception:interval:reraiseExceptions:] + 787\n"
+" 7 Tests 0x00010f1d -[GHTest run:] + 429\n"
+" 8 Tests 0x00027794 -[GHUnitIOSTestViewController _runTest] + 228\n"
+" 9 CoreFoundation 0x0142be99 -[NSObject performSelector:withObject:withObject:] + 73\n"
+" 10 UIKit 0x0007714e -[UIApplication sendAction:to:from:forEvent:] + 96\n"
+" 11 UIKit 0x002b5a0e -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 145\n"
+" 12 CoreFoundation 0x0142be99 -[NSObject performSelector:withObject:withObject:] + 73\n"
+" 13 UIKit 0x0007714e -[UIApplication sendAction:to:from:forEvent:] + 96\n"
+" 14 UIKit 0x000770e6 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61\n"
+" 15 UIKit 0x0011dade -[UIControl sendAction:to:forEvent:] + 66\n"
+" 16 UIKit 0x0011dfa7 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503\n"
+" 17 UIKit 0x0011d266 -[UIControl touchesEnded:withEvent:] + 549\n"
+" 18 UIKit 0x0009c3c0 -[UIWindow _sendTouchesForEvent:] + 513\n"
+" 19 UIKit 0x0009c5e6 -[UIWindow sendEvent:] + 273\n"
+" 20 UIKit 0x00082dc4 -[UIApplication sendEvent:] + 464\n"
+" 21 UIKit 0x00076634 _UIApplicationHandleEvent + 8196\n"
+" 22 GraphicsServices 0x01314ef5 PurpleEventCallback + 1274\n"
+" 23 CoreFoundation 0x013fe195 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53\n"
+" 24 CoreFoundation 0x01362ff2 __CFRunLoopDoSource1 + 146\n"
+" 25 CoreFoundation 0x013618da __CFRunLoopRun + 2218\n"
+" 26 CoreFoundation 0x01360d84 CFRunLoopRunSpecific + 212\n"
+" 27 CoreFoundation 0x01360c9b CFRunLoopRunInMode + 123\n"
+" 28 GraphicsServices 0x013137d8 GSEventRunModal + 190\n"
+" 29 GraphicsServices 0x0131388a GSEventRun + 103\n"
+" 30 UIKit 0x00074626 UIApplicationMain + 1163\n"
+" 31 Tests 0x0000bb86 main + 134\n"
+" 32 Tests 0x00001f15 start + 53\n"
+" )";
+ // Create an image for a new view
+ UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
+ colorView.backgroundColor = [UIColor greenColor];
+ [testView setSavedImage:nil renderedImage:[GHViewTestCase imageWithView:colorView] text:testText];
+ GHVerifyView(testView);
+}
+
+@end

0 comments on commit 3cbaebe

Please sign in to comment.