From b633fa8fb0e32c2cd933c3978a0fa0616bdfc9bf Mon Sep 17 00:00:00 2001 From: Alan Zeino Date: Thu, 10 Jan 2019 12:00:29 -0800 Subject: [PATCH] Remove deviceAgnostic and agnosticOptions, add new replacement called fileNameOptions --- FBSnapshotTestCase.xcodeproj/project.pbxproj | 4 +- FBSnapshotTestCase/FBSnapshotTestCase.h | 23 +++++----- FBSnapshotTestCase/FBSnapshotTestCase.m | 19 ++------ .../FBSnapshotTestCasePlatform.h | 44 ++++++++----------- .../FBSnapshotTestCasePlatform.m | 31 ++++--------- FBSnapshotTestCase/FBSnapshotTestController.h | 26 +++++------ FBSnapshotTestCase/FBSnapshotTestController.m | 16 +++---- .../FBSnapshotControllerTests.m | 14 +++--- 8 files changed, 68 insertions(+), 109 deletions(-) diff --git a/FBSnapshotTestCase.xcodeproj/project.pbxproj b/FBSnapshotTestCase.xcodeproj/project.pbxproj index 2eaaf7a..ea5c56c 100644 --- a/FBSnapshotTestCase.xcodeproj/project.pbxproj +++ b/FBSnapshotTestCase.xcodeproj/project.pbxproj @@ -151,9 +151,9 @@ B31987FF1AB782D100B0A900 /* Tests */, B31987F11AB782D000B0A900 /* Products */, ); - indentWidth = 2; + indentWidth = 4; sourceTree = ""; - tabWidth = 2; + tabWidth = 4; }; B31987F11AB782D000B0A900 /* Products */ = { isa = PBXGroup; diff --git a/FBSnapshotTestCase/FBSnapshotTestCase.h b/FBSnapshotTestCase/FBSnapshotTestCase.h index c2b6bde..2d7d574 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCase.h +++ b/FBSnapshotTestCase/FBSnapshotTestCase.h @@ -137,21 +137,18 @@ NS_ASSUME_NONNULL_BEGIN @property (readwrite, nonatomic, assign) BOOL recordMode; /** - When @c YES appends the name of the device model and OS to the snapshot file name. - The default value is @c NO. + When set, allows fine-grained control over what you want the file names to include. + + Allows you to combine which device or simulator specific details you want in your snapshot file names. + + The default value is FBSnapshotTestCaseFileNameIncludeOptionScreenScale. + + @discussion If you are migrating from the now deleted FBSnapshotTestCaseAgnosticOption to FBSnapshotTestCaseFileNameIncludeOption, we default to using FBSnapshotTestCaseFileNameIncludeOptionScreenScale for fileNameOptions to make the transition easy. If you don't want to have the screen scale included in your file name, you need to set fileNameOptions to a mask that doesn't include FBSnapshotTestCaseFileNameIncludeOptionScreenScale: + + self.fileNameOptions = (FBSnapshotTestCaseFileNameIncludeOptionDevice | FBSnapshotTestCaseFileNameIncludeOptionOS); */ -@property (readwrite, nonatomic, assign, getter=isDeviceAgnostic) BOOL deviceAgnostic DEPRECATED_MSG_ATTRIBUTE("Use agnosticOptions instead. deviceAgnostic will be removed in a future version of iOS Snapshot Test Case."); -/** - When set, allows fine-grained control over how agnostic you want the file names to be. - - Allows you to combine which agnostic options you want in your snapshot file names. - - The default value is FBSnapshotTestCaseAgnosticOptionNone. - - @attention If deviceAgnostic is YES, this bitmask is ignored. deviceAgnostic will be deprecated in a future version of FBSnapshotTestCase. - */ -@property (readwrite, nonatomic, assign) FBSnapshotTestCaseAgnosticOption agnosticOptions; +@property (readwrite, nonatomic, assign) FBSnapshotTestCaseFileNameIncludeOption fileNameOptions; /** Overrides the folder name in which the snapshot is going to be saved. diff --git a/FBSnapshotTestCase/FBSnapshotTestCase.m b/FBSnapshotTestCase/FBSnapshotTestCase.m index f7af94d..e926521 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCase.m +++ b/FBSnapshotTestCase/FBSnapshotTestCase.m @@ -39,26 +39,15 @@ - (void)setRecordMode:(BOOL)recordMode _snapshotController.recordMode = recordMode; } -- (BOOL)isDeviceAgnostic +- (FBSnapshotTestCaseFileNameIncludeOption)fileNameOptions { - return _snapshotController.deviceAgnostic; + return _snapshotController.fileNameOptions; } -- (void)setDeviceAgnostic:(BOOL)deviceAgnostic +- (void)setFileNameOptions:(FBSnapshotTestCaseFileNameIncludeOption)fileNameOptions { NSAssert1(_snapshotController, @"%s cannot be called before [super setUp]", __FUNCTION__); - _snapshotController.deviceAgnostic = deviceAgnostic; -} - -- (FBSnapshotTestCaseAgnosticOption)agnosticOptions -{ - return _snapshotController.agnosticOptions; -} - -- (void)setAgnosticOptions:(FBSnapshotTestCaseAgnosticOption)agnosticOptions -{ - NSAssert1(_snapshotController, @"%s cannot be called before [super setUp]", __FUNCTION__); - _snapshotController.agnosticOptions = agnosticOptions; + _snapshotController.fileNameOptions = fileNameOptions; } - (BOOL)usesDrawViewHierarchyInRect diff --git a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h index 0bdfcc2..f9d190d 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h +++ b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h @@ -14,20 +14,22 @@ extern "C" { #endif NS_ASSUME_NONNULL_BEGIN - + /** - An option mask that allows you to cherry pick which parts you want to 'be agnostic' in the snapshot file name. - - - FBSnapshotTestCaseAgnosticOptionNone: Don't make the file name agnostic at all. - - FBSnapshotTestCaseAgnosticOptionDevice: The file name should be agnostic on the device name, as returned by UIDevice.currentDevice.model. - - FBSnapshotTestCaseAgnosticOptionOS: The file name should be agnostic on the OS version, as returned by UIDevice.currentDevice.systemVersion. - - FBSnapshotTestCaseAgnosticOptionScreenSize: The file name should be agnostic on the screen size of the current keyWindow, as returned by UIApplication.sharedApplication.keyWindow.bounds.size. + An option mask that allows you to cherry pick which parts you want to include in the snapshot file name. + + - FBSnapshotTestCaseFileNameIncludeOptionNone: Don't include any of these options at all. + - FBSnapshotTestCaseFileNameIncludeOptionDevice: The file name should include the device name, as returned by UIDevice.currentDevice.model. + - FBSnapshotTestCaseFileNameIncludeOptionOS: The file name should include the OS version, as returned by UIDevice.currentDevice.systemVersion. + - FBSnapshotTestCaseFileNameIncludeOptionScreenSize: The file name should include the screen size of the current keyWindow, as returned by UIApplication.sharedApplication.keyWindow.bounds.size. + - FBSnapshotTestCaseFileNameIncludeOptionScreenScale: The file name should include the scale of the current device, as returned by UIScreen.mainScreen.scale. */ -typedef NS_OPTIONS(NSUInteger, FBSnapshotTestCaseAgnosticOption) { - FBSnapshotTestCaseAgnosticOptionNone = 1 << 0, - FBSnapshotTestCaseAgnosticOptionDevice = 1 << 1, - FBSnapshotTestCaseAgnosticOptionOS = 1 << 2, - FBSnapshotTestCaseAgnosticOptionScreenSize = 1 << 3 +typedef NS_OPTIONS(NSUInteger, FBSnapshotTestCaseFileNameIncludeOption) { + FBSnapshotTestCaseFileNameIncludeOptionNone = 1 << 0, + FBSnapshotTestCaseFileNameIncludeOptionDevice = 1 << 1, + FBSnapshotTestCaseFileNameIncludeOptionOS = 1 << 2, + FBSnapshotTestCaseFileNameIncludeOptionScreenSize = 1 << 3, + FBSnapshotTestCaseFileNameIncludeOptionScreenScale = 1 << 4 }; /** @@ -47,23 +49,15 @@ BOOL FBSnapshotTestCaseIs64Bit(void); */ NSOrderedSet *FBSnapshotTestCaseDefaultSuffixes(void); -/** - Returns a fully «normalized» file name. - Strips punctuation and spaces and replaces them with @c _. Also appends the device model, running OS and screen size to the file name. - - @returns An @c NSString object containing the passed @c fileName with the device model, OS and screen size appended at the end. - */ -NSString *FBDeviceAgnosticNormalizedFileName(NSString *fileName); - /** Returns a fully normalized file name as per the provided option mask. Strips punctuation and spaces and replaces them with @c _. - + @param fileName The file name to normalize. - @param option Agnostic options to use before normalization. - @return An @c NSString object containing the passed @c fileName and optionally, with the device model and/or OS and/or screen size appended at the end. + @param option File Name Include options to use before normalization. + @return An @c NSString object containing the passed @c fileName and optionally, with the device model and/or OS and/or screen size and/or screen scale appended at the end. */ -NSString *FBDeviceAgnosticNormalizedFileNameFromOption(NSString *fileName, FBSnapshotTestCaseAgnosticOption option); - +NSString *FBFileNameIncludeNormalizedFileNameFromOption(NSString *fileName, FBSnapshotTestCaseFileNameIncludeOption option); + NS_ASSUME_NONNULL_END #ifdef __cplusplus diff --git a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m index 98d7039..5f47c5d 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m +++ b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m @@ -30,43 +30,30 @@ BOOL FBSnapshotTestCaseIs64Bit(void) return [suffixesSet copy]; } -NSString *FBDeviceAgnosticNormalizedFileName(NSString *fileName) +NSString *FBFileNameIncludeNormalizedFileNameFromOption(NSString *fileName, FBSnapshotTestCaseFileNameIncludeOption option) { - UIDevice *device = [UIDevice currentDevice]; - UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; - CGSize screenSize = keyWindow.bounds.size; - NSString *os = device.systemVersion; - - fileName = [NSString stringWithFormat:@"%@_%@%@_%.0fx%.0f", fileName, device.model, os, screenSize.width, screenSize.height]; - - NSMutableCharacterSet *invalidCharacters = [NSMutableCharacterSet new]; - [invalidCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]]; - [invalidCharacters formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]]; - NSArray *validComponents = [fileName componentsSeparatedByCharactersInSet:invalidCharacters]; - fileName = [validComponents componentsJoinedByString:@"_"]; - - return fileName; -} - -NSString *FBDeviceAgnosticNormalizedFileNameFromOption(NSString *fileName, FBSnapshotTestCaseAgnosticOption option) -{ - if ((option & FBSnapshotTestCaseAgnosticOptionDevice) == FBSnapshotTestCaseAgnosticOptionDevice) { + if ((option & FBSnapshotTestCaseFileNameIncludeOptionDevice) == FBSnapshotTestCaseFileNameIncludeOptionDevice) { UIDevice *device = [UIDevice currentDevice]; fileName = [fileName stringByAppendingFormat:@"_%@", device.model]; } - if ((option & FBSnapshotTestCaseAgnosticOptionOS) == FBSnapshotTestCaseAgnosticOptionOS) { + if ((option & FBSnapshotTestCaseFileNameIncludeOptionOS) == FBSnapshotTestCaseFileNameIncludeOptionOS) { UIDevice *device = [UIDevice currentDevice]; NSString *os = device.systemVersion; fileName = [fileName stringByAppendingFormat:@"_%@", os]; } - if ((option & FBSnapshotTestCaseAgnosticOptionScreenSize) == FBSnapshotTestCaseAgnosticOptionScreenSize) { + if ((option & FBSnapshotTestCaseFileNameIncludeOptionScreenSize) == FBSnapshotTestCaseFileNameIncludeOptionScreenSize) { UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; CGSize screenSize = keyWindow.bounds.size; fileName = [fileName stringByAppendingFormat:@"_%.0fx%.0f", screenSize.width, screenSize.height]; } + if ((option & FBSnapshotTestCaseFileNameIncludeOptionScreenScale) == FBSnapshotTestCaseFileNameIncludeOptionScreenScale) { + CGFloat screenScale = [[UIScreen mainScreen] scale]; + fileName = [fileName stringByAppendingFormat:@"@%.fx", screenScale]; + } + NSMutableCharacterSet *invalidCharacters = [NSMutableCharacterSet new]; [invalidCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]]; [invalidCharacters formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]]; diff --git a/FBSnapshotTestCase/FBSnapshotTestController.h b/FBSnapshotTestCase/FBSnapshotTestController.h index 6064caa..77361cd 100644 --- a/FBSnapshotTestCase/FBSnapshotTestController.h +++ b/FBSnapshotTestCase/FBSnapshotTestController.h @@ -60,21 +60,17 @@ extern NSString *const FBDiffedImageKey; @property (readwrite, nonatomic, assign) BOOL recordMode; /** - When @c YES appends the name of the device model and OS to the snapshot file name. - The default value is @c NO. - */ -@property (readwrite, nonatomic, assign, getter=isDeviceAgnostic) BOOL deviceAgnostic; - -/** - When set, allows fine-grained control over how agnostic you want the file names to be. - - Allows you to combine which agnostic options you want in your snapshot file names. - - The default value is FBSnapshotTestCaseAgnosticOptionNone. - - @attention If deviceAgnostic is YES, this bitmask is ignored. deviceAgnostic will be deprecated in a future version of FBSnapshotTestCase. - */ -@property (readwrite, nonatomic, assign) FBSnapshotTestCaseAgnosticOption agnosticOptions; + When set, allows fine-grained control over what you want the file names to include. + + Allows you to combine which device or simulator specific details you want in your snapshot file names. + + The default value is FBSnapshotTestCaseFileNameIncludeOptionScreenScale. + + @discussion If you are migrating from the now deleted FBSnapshotTestCaseAgnosticOption to FBSnapshotTestCaseFileNameIncludeOption, we default to using FBSnapshotTestCaseFileNameIncludeOptionScreenScale for fileNameOptions to make the transition easier. If you don't want to have the screen scale included in your file name, you need to set fileNameOptions to a mask that doesn't include FBSnapshotTestCaseFileNameIncludeOptionScreenScale: + + self.fileNameOptions = (FBSnapshotTestCaseFileNameIncludeOptionDevice | FBSnapshotTestCaseFileNameIncludeOptionOS); + */ +@property (readwrite, nonatomic, assign) FBSnapshotTestCaseFileNameIncludeOption fileNameOptions; /** Uses drawViewHierarchyInRect:afterScreenUpdates: to draw the image instead of renderInContext: diff --git a/FBSnapshotTestCase/FBSnapshotTestController.m b/FBSnapshotTestCase/FBSnapshotTestController.m index ec3bc8b..f57ecc6 100644 --- a/FBSnapshotTestCase/FBSnapshotTestController.m +++ b/FBSnapshotTestCase/FBSnapshotTestController.m @@ -38,8 +38,7 @@ - (instancetype)initWithTestClass:(Class)testClass; { if (self = [super init]) { _folderName = NSStringFromClass(testClass); - _deviceAgnostic = NO; - _agnosticOptions = FBSnapshotTestCaseAgnosticOptionNone; + _fileNameOptions = FBSnapshotTestCaseFileNameIncludeOptionScreenScale; _fileManager = [[NSFileManager alloc] init]; } @@ -256,17 +255,12 @@ - (NSString *)_fileNameForSelector:(SEL)selector if (0 < identifier.length) { fileName = [fileName stringByAppendingFormat:@"_%@", identifier]; } - - BOOL noAgnosticOption = (self.agnosticOptions & FBSnapshotTestCaseAgnosticOptionNone) == FBSnapshotTestCaseAgnosticOptionNone; - if (self.isDeviceAgnostic) { - fileName = FBDeviceAgnosticNormalizedFileName(fileName); - } else if (!noAgnosticOption) { - fileName = FBDeviceAgnosticNormalizedFileNameFromOption(fileName, self.agnosticOptions); + + BOOL noFileNameOption = (self.fileNameOptions & FBSnapshotTestCaseFileNameIncludeOptionNone) == FBSnapshotTestCaseFileNameIncludeOptionNone; + if (!noFileNameOption) { + fileName = FBFileNameIncludeNormalizedFileNameFromOption(fileName, self.fileNameOptions); } - if ([[UIScreen mainScreen] scale] > 1) { - fileName = [fileName stringByAppendingFormat:@"@%.fx", [[UIScreen mainScreen] scale]]; - } fileName = [fileName stringByAppendingPathExtension:@"png"]; return fileName; } diff --git a/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m b/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m index a37efc8..47cd336 100644 --- a/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m +++ b/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m @@ -95,23 +95,25 @@ - (void)testCompareReferenceImageWithDifferentSizes XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferentSizes); } -- (void)testFailedImageWithDeviceAgnosticShouldHaveModelOnName +- (void)testFailedImageWithFileNameOptionShouldHaveModelOnName { UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"]; XCTAssertNotNil(referenceImage); UIImage *testImage = [self _bundledImageNamed:@"square_with_pixel" type:@"png"]; XCTAssertNotNil(testImage); - + + FBSnapshotTestCaseFileNameIncludeOption options = FBSnapshotTestCaseFileNameIncludeOptionDevice; + id testClass = nil; FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:testClass]; - [controller setDeviceAgnostic:YES]; + [controller setFileNameOptions:options]; [controller setReferenceImagesDirectory:@"/dev/null/"]; NSError *error = nil; - SEL selector = @selector(isDeviceAgnostic); + SEL selector = @selector(fileNameOptions); [controller referenceImageForSelector:selector identifier:@"" error:&error]; XCTAssertNotNil(error); - NSString *deviceAgnosticReferencePath = FBDeviceAgnosticNormalizedFileName(NSStringFromSelector(selector)); - XCTAssertTrue([(NSString *)[error.userInfo objectForKey:FBReferenceImageFilePathKey] containsString:deviceAgnosticReferencePath]); + NSString *deviceIncludedReferencePath = FBFileNameIncludeNormalizedFileNameFromOption(NSStringFromSelector(selector), options); + XCTAssertTrue([(NSString *)[error.userInfo objectForKey:FBReferenceImageFilePathKey] containsString:deviceIncludedReferencePath]); } - (void)testCompareReferenceImageWithLowPixelToleranceShouldNotMatch