Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion FBSnapshotTestCase/FBSnapshotTestController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import <FBSnapshotTestCase/UIImage+Snapshot.h>

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

NSString *const FBSnapshotTestControllerErrorDomain = @"FBSnapshotTestControllerErrorDomain";
NSString *const FBReferenceImageFilePathKey = @"FBReferenceImageFilePathKey";
Expand Down Expand Up @@ -181,6 +182,23 @@ - (BOOL)saveFailedReferenceImage:(UIImage *)referenceImage
identifier:(NSString *)identifier
error:(NSError **)errorPtr
{
UIImage *diffImage = [referenceImage fb_diffWithImage:testImage];

[XCTContext runActivityNamed:identifier ?: NSStringFromSelector(selector) block:^(id<XCTActivity> _Nonnull activity) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the user should be able to turn this off or on. If you're running a truly massive amount of snapshot tests, you may not want to do this, as it might make Xcode unusable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm that's a good point. I could easily inflate some of the tests we have locally to fail a couple hundred times if we want but perhaps using the Uber codebase would be a better litmus test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so I added a loop around a single test that was failing which ran 15000 times and caused 30000 failures which resulted in 90000 attachments.

I will admit that Xcode really chugged when the tests finished. It beach balled and was non-responsive for like 5-10 minutes while it processed all of the attachments. Navigating to test files as well as the test pane in the report navigator does take a bit of time to load but nothing that I would consider blocking.

If I change the loop to run 5000 times, causing 10000 failures and 30000 attachments there is nearly no issues at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue to track adding the ability to turn this feature off #89

XCTAttachment *referenceAttachment = [XCTAttachment attachmentWithImage:referenceImage];
referenceAttachment.name = @"Reference Image";

XCTAttachment *failedAttachment = [XCTAttachment attachmentWithImage:testImage];
failedAttachment.name = @"Failed Image";

XCTAttachment *diffAttachment = [XCTAttachment attachmentWithImage:diffImage];
diffAttachment.name = @"Diffed Image";

[activity addAttachment:referenceAttachment];
[activity addAttachment:failedAttachment];
[activity addAttachment:diffAttachment];
}];

NSData *referencePNGData = UIImagePNGRepresentation(referenceImage);
NSData *testPNGData = UIImagePNGRepresentation(testImage);

Expand Down Expand Up @@ -216,7 +234,6 @@ - (BOOL)saveFailedReferenceImage:(UIImage *)referenceImage
identifier:identifier
fileNameType:FBTestSnapshotFileNameTypeFailedTestDiff];

UIImage *diffImage = [referenceImage fb_diffWithImage:testImage];
NSData *diffImageData = UIImagePNGRepresentation(diffImage);

if (![diffImageData writeToFile:diffPath options:NSDataWritingAtomic error:errorPtr]) {
Expand Down Expand Up @@ -317,6 +334,13 @@ - (BOOL)_recordSnapshotOfViewOrLayer:(id)viewOrLayer
error:(NSError **)errorPtr
{
UIImage *snapshot = [self _imageForViewOrLayer:viewOrLayer];

[XCTContext runActivityNamed:identifier ?: NSStringFromSelector(selector) block:^(id<XCTActivity> _Nonnull activity) {
XCTAttachment *recordedAttachment = [XCTAttachment attachmentWithImage:snapshot];
recordedAttachment.name = @"Recorded Image";
[activity addAttachment:recordedAttachment];
}];

return [self _saveReferenceImage:snapshot selector:selector identifier:identifier error:errorPtr];
}

Expand Down
6 changes: 3 additions & 3 deletions FBSnapshotTestCase/SwiftSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
*/

public extension FBSnapshotTestCase {
func FBSnapshotVerifyView(_ view: UIView, identifier: String = "", suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
func FBSnapshotVerifyView(_ view: UIView, identifier: String? = nil, suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
FBSnapshotVerifyViewOrLayer(view, identifier: identifier, suffixes: suffixes, perPixelTolerance: perPixelTolerance, overallTolerance: overallTolerance, file: file, line: line)
}

func FBSnapshotVerifyLayer(_ layer: CALayer, identifier: String = "", suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
func FBSnapshotVerifyLayer(_ layer: CALayer, identifier: String? = nil, suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
FBSnapshotVerifyViewOrLayer(layer, identifier: identifier, suffixes: suffixes, perPixelTolerance: perPixelTolerance, overallTolerance: overallTolerance, file: file, line: line)
}

private func FBSnapshotVerifyViewOrLayer(_ viewOrLayer: AnyObject, identifier: String = "", suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
private func FBSnapshotVerifyViewOrLayer(_ viewOrLayer: AnyObject, identifier: String? = nil, suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
let envReferenceImageDirectory = self.getReferenceImageDirectory(withDefault: FB_REFERENCE_IMAGE_DIR)
let envImageDiffDirectory = self.getImageDiffDirectory(withDefault: IMAGE_DIFF_DIR)
var error: NSError?
Expand Down