From ba98a416d362dd84ad6abbb8ec47041d06d2dd8c Mon Sep 17 00:00:00 2001 From: Nathan Shaughnessy Date: Sun, 10 Apr 2016 16:58:51 -0400 Subject: [PATCH] Add includeOSVersionInFilename to FBSnapshotTestCase When deviceAgnostic is set to true, this variable will control whether the OS version is included in the snapshot file name. --- FBSnapshotTestCase/FBSnapshotTestCase.h | 12 +++++++++ FBSnapshotTestCase/FBSnapshotTestCase.m | 11 ++++++++ .../FBSnapshotTestCasePlatform.h | 2 +- .../FBSnapshotTestCasePlatform.m | 25 +++++++++++-------- FBSnapshotTestCase/FBSnapshotTestController.h | 12 +++++++++ FBSnapshotTestCase/FBSnapshotTestController.m | 3 ++- .../FBSnapshotControllerTests.m | 17 ++++++++++++- 7 files changed, 69 insertions(+), 13 deletions(-) diff --git a/FBSnapshotTestCase/FBSnapshotTestCase.h b/FBSnapshotTestCase/FBSnapshotTestCase.h index 159a724..fadddbe 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCase.h +++ b/FBSnapshotTestCase/FBSnapshotTestCase.h @@ -131,9 +131,21 @@ /** When @c YES appends the name of the device model and OS to the snapshot file name. The default value is @c NO. + + @see includeOSVersionInFilename */ @property (readwrite, nonatomic, assign, getter=isDeviceAgnostic) BOOL deviceAgnostic; +/** + Used in conjunction with @c deviceAgnostic, when @c YES the OS version will be included in the snapshot file name. + The default value is @c YES. + + @attention @c deviceAgnostic needs to be set to @c YES for this to take effect + + @see deviceAgnostic + */ +@property (readwrite, nonatomic, assign) BOOL includeOSVersionInFilename; + /** When YES, renders a snapshot of the complete view hierarchy as visible onscreen. There are several things that do not work if renderInContext: is used. diff --git a/FBSnapshotTestCase/FBSnapshotTestCase.m b/FBSnapshotTestCase/FBSnapshotTestCase.m index 3ee351f..4e80ae8 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCase.m +++ b/FBSnapshotTestCase/FBSnapshotTestCase.m @@ -52,6 +52,17 @@ - (void)setDeviceAgnostic:(BOOL)deviceAgnostic _snapshotController.deviceAgnostic = deviceAgnostic; } +- (BOOL)includeOSVersionInFilename +{ + return _snapshotController.includeOSVersionInFilename; +} + +- (void)setIncludeOSVersionInFilename:(BOOL)include +{ + NSAssert1(_snapshotController, @"%s cannot be called before [super setUp]", __FUNCTION__); + _snapshotController.includeOSVersionInFilename = include; +} + - (BOOL)usesDrawViewHierarchyInRect { return _snapshotController.usesDrawViewHierarchyInRect; diff --git a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h index e04acf2..721dedc 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h +++ b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.h @@ -37,7 +37,7 @@ NSOrderedSet *FBSnapshotTestCaseDefaultSuffixes(void); @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); +NSString *FBDeviceAgnosticNormalizedFileName(NSString *fileName, BOOL includeOSVersion); #ifdef __cplusplus } diff --git a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m index d8709d8..18c2a38 100644 --- a/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m +++ b/FBSnapshotTestCase/FBSnapshotTestCasePlatform.m @@ -32,20 +32,25 @@ BOOL FBSnapshotTestCaseIs64Bit(void) return [suffixesSet copy]; } -NSString *FBDeviceAgnosticNormalizedFileName(NSString *fileName) +NSString *FBDeviceAgnosticNormalizedFileName(NSString *fileName, BOOL includeOSVersion) { + NSMutableString *deviceAgnosticFilename = [fileName mutableCopy]; + UIDevice *device = [UIDevice currentDevice]; + [deviceAgnosticFilename appendFormat:@"_%@", device.model]; + + if (includeOSVersion) { + [deviceAgnosticFilename appendFormat:@"%@", device.systemVersion]; + } + UIWindow *keyWindow = [[UIApplication sharedApplication] fb_strictKeyWindow]; CGSize screenSize = keyWindow.bounds.size; - NSString *os = device.systemVersion; - - fileName = [NSString stringWithFormat:@"%@_%@%@_%.0fx%.0f", fileName, device.model, os, screenSize.width, screenSize.height]; - + [deviceAgnosticFilename appendFormat:@"_%.0fx%.0f", 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; -} \ No newline at end of file + NSArray *validComponents = [deviceAgnosticFilename componentsSeparatedByCharactersInSet:invalidCharacters]; + + return [validComponents componentsJoinedByString:@"_"]; +} diff --git a/FBSnapshotTestCase/FBSnapshotTestController.h b/FBSnapshotTestCase/FBSnapshotTestController.h index a0285ad..646c72f 100644 --- a/FBSnapshotTestCase/FBSnapshotTestController.h +++ b/FBSnapshotTestCase/FBSnapshotTestController.h @@ -58,9 +58,21 @@ extern NSString *const FBDiffedImageKey; /** When @c YES appends the name of the device model and OS to the snapshot file name. The default value is @c NO. + + @see includeOSVersionInFilename */ @property (readwrite, nonatomic, assign, getter=isDeviceAgnostic) BOOL deviceAgnostic; +/** + Used in conjunction with @c deviceAgnostic, when @c YES the OS version will be included in the snapshot file name. + The default value is @c YES. + + @attention @c deviceAgnostic needs to be set to @c YES for this to take effect + + @see deviceAgnostic + */ +@property (readwrite, nonatomic, assign) BOOL includeOSVersionInFilename; + /** Uses drawViewHierarchyInRect:afterScreenUpdates: to draw the image instead of renderInContext: */ diff --git a/FBSnapshotTestCase/FBSnapshotTestController.m b/FBSnapshotTestCase/FBSnapshotTestController.m index 74c5a0a..7344e51 100644 --- a/FBSnapshotTestCase/FBSnapshotTestController.m +++ b/FBSnapshotTestCase/FBSnapshotTestController.m @@ -47,6 +47,7 @@ - (instancetype)initWithTestName:(NSString *)testName if (self = [super init]) { _testName = [testName copy]; _deviceAgnostic = NO; + _includeOSVersionInFilename = YES; _fileManager = [[NSFileManager alloc] init]; } @@ -234,7 +235,7 @@ - (NSString *)_fileNameForSelector:(SEL)selector } if (self.isDeviceAgnostic) { - fileName = FBDeviceAgnosticNormalizedFileName(fileName); + fileName = FBDeviceAgnosticNormalizedFileName(fileName, self.includeOSVersionInFilename); } if ([[UIScreen mainScreen] scale] > 1) { diff --git a/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m b/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m index e9f46f6..c4400a2 100644 --- a/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m +++ b/FBSnapshotTestCaseTests/FBSnapshotControllerTests.m @@ -105,10 +105,25 @@ - (void)testFailedImageWithDeviceAgnosticShouldHaveModelOnName SEL selector = @selector(isDeviceAgnostic); [controller referenceImageForSelector:selector identifier:@"" error:&error]; XCTAssertNotNil(error); - NSString *deviceAgnosticReferencePath = FBDeviceAgnosticNormalizedFileName(NSStringFromSelector(selector)); + NSString *deviceAgnosticReferencePath = FBDeviceAgnosticNormalizedFileName(NSStringFromSelector(selector), true); XCTAssertTrue([(NSString *)[error.userInfo objectForKey:FBReferenceImageFilePathKey] containsString:deviceAgnosticReferencePath]); } +- (void)testFilenameDoesNotContainOSVersionWhenIncludeOSInFilenameIsFalse +{ + FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil]; + [controller setDeviceAgnostic:YES]; + [controller setIncludeOSVersionInFilename:NO]; + + [controller setReferenceImagesDirectory:@"/dev/null/"]; + NSError *error = nil; + SEL selector = @selector(includeOSVersionInFilename); + [controller referenceImageForSelector:selector identifier:@"" error:&error]; + XCTAssertNotNil(error); + NSString *includeOSVersionInFilenameReferencePath = FBDeviceAgnosticNormalizedFileName(NSStringFromSelector(selector), false); + XCTAssertTrue([(NSString *)[error.userInfo objectForKey:FBReferenceImageFilePathKey] containsString:includeOSVersionInFilenameReferencePath]); +} + #pragma mark - Private helper methods - (UIImage *)_bundledImageNamed:(NSString *)name type:(NSString *)type