Skip to content

Commit

Permalink
Add imprecise comparison variants to iOSSnapshotTestCase extension
Browse files Browse the repository at this point in the history
This adds variants of the `SnapshotVerify*(...)` methods that allow for imprecise comparisons, i.e. using the `perPixelTolerance` and `overallTolerance` parameters.

 ## Why is this necessary?

Adding tolerances has been a highly requested feature (see #63) to work around...
  • Loading branch information
NickEntin committed Aug 15, 2023
1 parent cf1f342 commit 3a05c49
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Example/AccessibilitySnapshot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
3D3F2E142263E6B900F7608E /* InvertColorsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D3F2E132263E6B900F7608E /* InvertColorsViewController.swift */; };
3D3F2E162263E94D00F7608E /* InvertColorsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D3F2E152263E94D00F7608E /* InvertColorsTests.swift */; };
3D4674682116A3F100278B57 /* ViewAccessibilityPropertiesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D4674672116A3F100278B57 /* ViewAccessibilityPropertiesViewController.swift */; };
3D9334942A8B2E520078A142 /* ImpreciseObjectiveCTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D9334932A8B2E520078A142 /* ImpreciseObjectiveCTests.m */; };
3D9894F9213509C8006C16F6 /* DescriptionEdgeCasesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D9894F8213509C8006C16F6 /* DescriptionEdgeCasesViewController.swift */; };
3DA12A3222405B9E00EB3C33 /* DataTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DA12A3122405B9E00EB3C33 /* DataTableViewController.swift */; };
3DBAC28722406EBB00EF4D0A /* AccessibilityContainersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DBAC28622406EBB00EF4D0A /* AccessibilityContainersTests.swift */; };
Expand Down Expand Up @@ -94,6 +95,7 @@
3D4674672116A3F100278B57 /* ViewAccessibilityPropertiesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewAccessibilityPropertiesViewController.swift; sourceTree = "<group>"; };
3D881956246E03C00061DA6A /* UnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3D88195A246E03C00061DA6A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3D9334932A8B2E520078A142 /* ImpreciseObjectiveCTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ImpreciseObjectiveCTests.m; sourceTree = "<group>"; };
3D9894F8213509C8006C16F6 /* DescriptionEdgeCasesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DescriptionEdgeCasesViewController.swift; sourceTree = "<group>"; };
3DA12A3122405B9E00EB3C33 /* DataTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataTableViewController.swift; sourceTree = "<group>"; };
3DBAC28622406EBB00EF4D0A /* AccessibilityContainersTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessibilityContainersTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -294,6 +296,7 @@
isa = PBXGroup;
children = (
3D13DB512221124000066519 /* ObjectiveCTests.m */,
3D9334932A8B2E520078A142 /* ImpreciseObjectiveCTests.m */,
3DBAC28622406EBB00EF4D0A /* AccessibilityContainersTests.swift */,
607FACEB1AFB9204008FA782 /* AccessibilityPropertiesTests.swift */,
3D39BFAF2239BC42009C3EF4 /* ActivationPointTests.swift */,
Expand Down Expand Up @@ -637,6 +640,7 @@
3D220A2B252AF72900359C1E /* AccessibleContainerView.swift in Sources */,
1635CE4E251EAC6700907101 /* SnapshotTestingTests.swift in Sources */,
3DBEAA5D2223C0CE00FAE61D /* SwitchControlsTests.swift in Sources */,
3D9334942A8B2E520078A142 /* ImpreciseObjectiveCTests.m in Sources */,
3DF46500220D5FB00048D446 /* ElementSelectionTests.swift in Sources */,
3DBAC28722406EBB00EF4D0A /* AccessibilityContainersTests.swift in Sources */,
3DC488392212B40C006D1E15 /* ModalTests.swift in Sources */,
Expand Down
92 changes: 92 additions & 0 deletions Example/SnapshotTests/ImpreciseObjectiveCTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// Copyright 2023 Block Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

@import AccessibilitySnapshot;
@import FBSnapshotTestCase;
@import XCTest;


@interface ImpreciseObjectiveCTests : FBSnapshotTestCase

@end


@implementation ImpreciseObjectiveCTests

- (void)setUp;
{
[super setUp];

self.fileNameOptions = FBSnapshotTestCaseFileNameIncludeOptionOS | FBSnapshotTestCaseFileNameIncludeOptionScreenSize | FBSnapshotTestCaseFileNameIncludeOptionScreenScale;
}

- (void)testSimpleView;
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 150, 30)];
label.text = @"Objective-C Snapshot";
label.textColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];

SnapshotImpreciseVerifyAccessibility(view, nil, 0, 0);
}

- (void)testSimpleViewWithIdentifier;
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 150, 30)];
label.text = @"Objective-C Snapshot";
label.textColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];

SnapshotImpreciseVerifyAccessibility(view, @"identifier", 0, 0);
}

- (void)testSimpleViewWithActivationPointAlways;
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 150, 30)];
label.text = @"Objective-C Snapshot";
label.textColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];

SnapshotImpreciseVerifyAccessibilityWithOptions(view, nil, YES, YES, 0, 0);
}

- (void)testViewWithInvertedColors;
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[view setBackgroundColor:[UIColor redColor]];

UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
[subview setBackgroundColor:[UIColor greenColor]];

[subview setAccessibilityIgnoresInvertColors:YES];
[view addSubview:subview];

SnapshotImpreciseVerifyWithInvertedColors(view, nil, 0, 0);
}

@end
Binary file added ...ciseObjectiveCTests/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ...ciseObjectiveCTests/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ...ciseObjectiveCTests/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ...ciseObjectiveCTests/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019 Square Inc.
// Copyright 2023 Block Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,11 @@
{\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyAccessibility:identifier:);\
SEL selector = @selector(snapshotVerifyAccessibility:identifier:perPixelTolerance:overallTolerance:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *);\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, CGFloat, CGFloat);\
SnapshotMethod snapshotVerifyAccessibility = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyAccessibility(self, selector, view__, identifier__ ?: @"");\
NSString *errorDescription = snapshotVerifyAccessibility(self, selector, view__, identifier__ ?: @"", 0, 0);\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
Expand All @@ -34,11 +34,11 @@
{\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyAccessibility:identifier:showActivationPoints:useMonochromeSnapshot:);\
SEL selector = @selector(snapshotVerifyAccessibility:identifier:showActivationPoints:useMonochromeSnapshot:perPixelTolerance:overallTolerance:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, BOOL, BOOL);\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, BOOL, BOOL, CGFloat, CGFloat);\
SnapshotMethod snapshotVerifyAccessibility = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyAccessibility(self, selector, view__, identifier__ ?: @"", showActivationPoints__, useMonochromeSnapshot__);\
NSString *errorDescription = snapshotVerifyAccessibility(self, selector, view__, identifier__ ?: @"", showActivationPoints__, useMonochromeSnapshot__, 0, 0);\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
Expand All @@ -50,11 +50,11 @@
{\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyWithInvertedColors:identifier:);\
SEL selector = @selector(snapshotVerifyWithInvertedColors:identifier:perPixelTolerance:overallTolerance:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *);\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, CGFloat, CGFloat);\
SnapshotMethod snapshotVerifyWithInvertedColors = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyWithInvertedColors(self, selector, view__, identifier__ ?: @"");\
NSString *errorDescription = snapshotVerifyWithInvertedColors(self, selector, view__, identifier__ ?: @"", 0, 0);\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright 2023 Block Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#define SnapshotImpreciseVerifyAccessibility(view__, identifier__, perPixelTolerance__, overallTolerance__)\
{\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyAccessibility:identifier:perPixelTolerance:overallTolerance:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, CGFloat, CGFloat);\
SnapshotMethod snapshotVerifyAccessibility = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyAccessibility(self, selector, view__, identifier__ ?: @"", perPixelTolerance__, overallTolerance__);\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
XCTFail("%@", errorDescription);\
}\
}

#define SnapshotImpreciseVerifyAccessibilityWithOptions(view__, identifier__, showActivationPoints__, useMonochromeSnapshot__, perPixelTolerance__, overallTolerance__)\
{\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyAccessibility:identifier:showActivationPoints:useMonochromeSnapshot:perPixelTolerance:overallTolerance:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, BOOL, BOOL, CGFloat, CGFloat);\
SnapshotMethod snapshotVerifyAccessibility = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyAccessibility(self, selector, view__, identifier__ ?: @"", showActivationPoints__, useMonochromeSnapshot__, perPixelTolerance__, overallTolerance__);\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
XCTFail("%@", errorDescription);\
}\
}

#define SnapshotImpreciseVerifyWithInvertedColors(view__, identifier__, perPixelTolerance__, overallTolerance__)\
{\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Wundeclared-selector\"")\
SEL selector = @selector(snapshotVerifyWithInvertedColors:identifier:perPixelTolerance:overallTolerance:);\
_Pragma("clang diagnostic pop")\
typedef NSString * (*SnapshotMethod)(id, SEL, UIView *, NSString *, CGFloat, CGFloat);\
SnapshotMethod snapshotVerifyWithInvertedColors = (SnapshotMethod)[self methodForSelector:selector];\
NSString *errorDescription = snapshotVerifyWithInvertedColors(self, selector, view__, identifier__ ?: @"", perPixelTolerance__, overallTolerance__);\
if (errorDescription == nil) {\
XCTAssertTrue(YES);\
} else {\
XCTFail("%@", errorDescription);\
}\
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019 Square Inc.
// Copyright 2023 Block Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 3a05c49

Please sign in to comment.