Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit fbb2d27

Browse files
committed
Merge pull request #146 from ultramiraculous/error-improvements
Add images to comparison errors
2 parents ee57dee + d08d8b7 commit fbb2d27

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

FBSnapshotTestCase.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
B32447DC1AB78B5E00B1D6FF /* square_with_text.png in Resources */ = {isa = PBXBuildFile; fileRef = B32447D91AB78B5E00B1D6FF /* square_with_text.png */; };
4646
B32447DD1AB78B5E00B1D6FF /* square-copy.png in Resources */ = {isa = PBXBuildFile; fileRef = B32447DA1AB78B5E00B1D6FF /* square-copy.png */; };
4747
B32447DE1AB78B5E00B1D6FF /* square.png in Resources */ = {isa = PBXBuildFile; fileRef = B32447DB1AB78B5E00B1D6FF /* square.png */; };
48+
B76C68291C6BD6D200586E5B /* rect.png in Resources */ = {isa = PBXBuildFile; fileRef = B76C68271C6BD68100586E5B /* rect.png */; };
49+
B76C682A1C6BD6D500586E5B /* rect.png in Resources */ = {isa = PBXBuildFile; fileRef = B76C68271C6BD68100586E5B /* rect.png */; };
4850
BC45D5211C2AEFCE007C72F3 /* UIApplication+StrictKeyWindow.h in Headers */ = {isa = PBXBuildFile; fileRef = BC45D51F1C2AEFCE007C72F3 /* UIApplication+StrictKeyWindow.h */; };
4951
BC45D5221C2AEFCE007C72F3 /* UIApplication+StrictKeyWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = BC45D5201C2AEFCE007C72F3 /* UIApplication+StrictKeyWindow.m */; };
5052
E5C2CD621B1F399A00669887 /* square_with_pixel.png in Resources */ = {isa = PBXBuildFile; fileRef = E5C2CD611B1F399A00669887 /* square_with_pixel.png */; };
@@ -91,6 +93,7 @@
9193
B32447D91AB78B5E00B1D6FF /* square_with_text.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = square_with_text.png; sourceTree = "<group>"; };
9294
B32447DA1AB78B5E00B1D6FF /* square-copy.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "square-copy.png"; sourceTree = "<group>"; };
9395
B32447DB1AB78B5E00B1D6FF /* square.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = square.png; sourceTree = "<group>"; };
96+
B76C68271C6BD68100586E5B /* rect.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = rect.png; sourceTree = "<group>"; };
9497
BC45D51F1C2AEFCE007C72F3 /* UIApplication+StrictKeyWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+StrictKeyWindow.h"; sourceTree = "<group>"; };
9598
BC45D5201C2AEFCE007C72F3 /* UIApplication+StrictKeyWindow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+StrictKeyWindow.m"; sourceTree = "<group>"; };
9699
E5C2CD611B1F399A00669887 /* square_with_pixel.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = square_with_pixel.png; sourceTree = "<group>"; };
@@ -196,6 +199,7 @@
196199
B31987FF1AB782D100B0A900 /* Tests */ = {
197200
isa = PBXGroup;
198201
children = (
202+
B76C68271C6BD68100586E5B /* rect.png */,
199203
E5C2CD611B1F399A00669887 /* square_with_pixel.png */,
200204
B32447D91AB78B5E00B1D6FF /* square_with_text.png */,
201205
B32447DA1AB78B5E00B1D6FF /* square-copy.png */,
@@ -378,6 +382,7 @@
378382
isa = PBXResourcesBuildPhase;
379383
buildActionMask = 2147483647;
380384
files = (
385+
B76C682A1C6BD6D500586E5B /* rect.png in Resources */,
381386
827137A01C63AC0700354E42 /* square_with_pixel.png in Resources */,
382387
827137A21C63AC0D00354E42 /* square-copy.png in Resources */,
383388
827137A31C63AC0D00354E42 /* square.png in Resources */,
@@ -396,6 +401,7 @@
396401
isa = PBXResourcesBuildPhase;
397402
buildActionMask = 2147483647;
398403
files = (
404+
B76C68291C6BD6D200586E5B /* rect.png in Resources */,
399405
B32447DC1AB78B5E00B1D6FF /* square_with_text.png in Resources */,
400406
E5C2CD621B1F399A00669887 /* square_with_pixel.png in Resources */,
401407
B32447DE1AB78B5E00B1D6FF /* square.png in Resources */,

FBSnapshotTestCase/FBSnapshotTestController.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ extern NSString *const FBSnapshotTestControllerErrorDomain;
2828
*/
2929
extern NSString *const FBReferenceImageFilePathKey;
3030

31+
/**
32+
Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
33+
*/
34+
extern NSString *const FBReferenceImageKey;
35+
36+
/**
37+
Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
38+
*/
39+
extern NSString *const FBCapturedImageKey;
40+
41+
/**
42+
Errors returned by the methods of FBSnapshotTestController sometimes contain this key in the `userInfo` dictionary.
43+
*/
44+
extern NSString *const FBDiffedImageKey;
45+
3146
/**
3247
Provides the heavy-lifting for FBSnapshotTestCase. It loads and saves images, along with performing the actual pixel-
3348
by-pixel comparison of images.

FBSnapshotTestCase/FBSnapshotTestController.m

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
NSString *const FBSnapshotTestControllerErrorDomain = @"FBSnapshotTestControllerErrorDomain";
2020
NSString *const FBReferenceImageFilePathKey = @"FBReferenceImageFilePathKey";
21+
NSString *const FBReferenceImageKey = @"FBReferenceImageKey";
22+
NSString *const FBCapturedImageKey = @"FBCapturedImageKey";
23+
NSString *const FBDiffedImageKey = @"FBDiffedImageKey";
2124

2225
typedef NS_ENUM(NSUInteger, FBTestSnapshotFileNameType) {
2326
FBTestSnapshotFileNameTypeReference,
@@ -126,25 +129,25 @@ - (BOOL)compareReferenceImage:(UIImage *)referenceImage
126129
tolerance:(CGFloat)tolerance
127130
error:(NSError **)errorPtr
128131
{
129-
if (CGSizeEqualToSize(referenceImage.size, image.size)) {
130-
BOOL imagesEqual = [referenceImage fb_compareWithImage:image tolerance:tolerance];
131-
if (NULL != errorPtr) {
132-
*errorPtr = [NSError errorWithDomain:FBSnapshotTestControllerErrorDomain
133-
code:FBSnapshotTestControllerErrorCodeImagesDifferent
134-
userInfo:@{
135-
NSLocalizedDescriptionKey: @"Images different",
136-
}];
137-
}
138-
return imagesEqual;
132+
BOOL sameImageDimensions = CGSizeEqualToSize(referenceImage.size, image.size);
133+
if (sameImageDimensions && [referenceImage fb_compareWithImage:image tolerance:tolerance]) {
134+
return YES;
139135
}
136+
140137
if (NULL != errorPtr) {
138+
NSString *errorDescription = sameImageDimensions ? @"Images different" : @"Images different sizes";
139+
NSString *errorReason = sameImageDimensions ? [NSString stringWithFormat:@"image pixels differed by more than %.2f%% from the reference image", tolerance * 100]
140+
: [NSString stringWithFormat:@"referenceImage:%@, image:%@", NSStringFromCGSize(referenceImage.size), NSStringFromCGSize(image.size)];
141+
FBSnapshotTestControllerErrorCode errorCode = sameImageDimensions ? FBSnapshotTestControllerErrorCodeImagesDifferent : FBSnapshotTestControllerErrorCodeImagesDifferentSizes;
142+
141143
*errorPtr = [NSError errorWithDomain:FBSnapshotTestControllerErrorDomain
142-
code:FBSnapshotTestControllerErrorCodeImagesDifferentSizes
144+
code:errorCode
143145
userInfo:@{
144-
NSLocalizedDescriptionKey: @"Images different sizes",
145-
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"referenceImage:%@, image:%@",
146-
NSStringFromCGSize(referenceImage.size),
147-
NSStringFromCGSize(image.size)],
146+
NSLocalizedDescriptionKey: errorDescription,
147+
NSLocalizedFailureReasonErrorKey: errorReason,
148+
FBReferenceImageKey: referenceImage,
149+
FBCapturedImageKey: image,
150+
FBDiffedImageKey: [referenceImage fb_diffWithImage:image],
148151
}];
149152
}
150153
return NO;
@@ -279,11 +282,10 @@ - (BOOL)_performPixelComparisonWithViewOrLayer:(id)viewOrLayer
279282
UIImage *snapshot = [self _imageForViewOrLayer:viewOrLayer];
280283
BOOL imagesSame = [self compareReferenceImage:referenceImage toImage:snapshot tolerance:tolerance error:errorPtr];
281284
if (!imagesSame) {
282-
[self saveFailedReferenceImage:referenceImage
283-
testImage:snapshot
284-
selector:selector
285-
identifier:identifier
286-
error:errorPtr];
285+
NSError *saveError = nil;
286+
if ([self saveFailedReferenceImage:referenceImage testImage:snapshot selector:selector identifier:identifier error:&saveError] == NO) {
287+
NSLog(@"Error saving test images: %@", saveError);
288+
}
287289
}
288290
return imagesSame;
289291
}

FBSnapshotTestCaseTests/FBSnapshotControllerTests.m

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ - (void)testCompareReferenceImageToImageShouldBeEqual
2828
XCTAssertNotNil(testImage);
2929

3030
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
31-
XCTAssertTrue([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:nil]);
31+
NSError *error = nil;
32+
XCTAssertTrue([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:&error]);
33+
XCTAssertNil(error);
3234
}
3335

3436
- (void)testCompareReferenceImageToImageShouldNotBeEqual
@@ -39,7 +41,10 @@ - (void)testCompareReferenceImageToImageShouldNotBeEqual
3941
XCTAssertNotNil(testImage);
4042

4143
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
42-
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:nil]);
44+
NSError *error = nil;
45+
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:&error]);
46+
XCTAssertNotNil(error);
47+
XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferent);
4348
}
4449

4550
- (void)testCompareReferenceImageWithVeryLowToleranceShouldNotMatch
@@ -51,7 +56,10 @@ - (void)testCompareReferenceImageWithVeryLowToleranceShouldNotMatch
5156

5257
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
5358
// With virtually no margin for error, this should fail to be equal
54-
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0.0001 error:nil]);
59+
NSError *error = nil;
60+
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0.0001 error:&error]);
61+
XCTAssertNotNil(error);
62+
XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferent);
5563
}
5664

5765
- (void)testCompareReferenceImageWithVeryLowToleranceShouldMatch
@@ -63,7 +71,24 @@ - (void)testCompareReferenceImageWithVeryLowToleranceShouldMatch
6371

6472
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
6573
// With some tolerance these should be considered the same
66-
XCTAssertTrue([controller compareReferenceImage:referenceImage toImage:testImage tolerance:.001 error:nil]);
74+
NSError *error = nil;
75+
XCTAssertTrue([controller compareReferenceImage:referenceImage toImage:testImage tolerance:.001 error:&error]);
76+
XCTAssertNil(error);
77+
}
78+
79+
- (void)testCompareReferenceImageWithDifferentSizes
80+
{
81+
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
82+
XCTAssertNotNil(referenceImage);
83+
UIImage *testImage = [self _bundledImageNamed:@"rect" type:@"png"];
84+
XCTAssertNotNil(testImage);
85+
86+
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
87+
// With some tolerance these should be considered the same
88+
NSError *error = nil;
89+
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:&error]);
90+
XCTAssertNotNil(error);
91+
XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferentSizes);
6792
}
6893

6994
- (void)testFailedImageWithDeviceAgnosticShouldHaveModelOnName

FBSnapshotTestCaseTests/rect.png

3.34 KB
Loading

0 commit comments

Comments
 (0)