Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precision/tolerance to api #64

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Example/SnapshotTests/ObjectiveCTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ - (void)testSimpleViewWithActivationPointAlways;
[view addSubview:label];

SnapshotVerifyAccessibilityWithOptions(view, nil, YES, YES);
SnapshotVerifyAccessibilityWithOptionsAndTolerance(view, nil, YES, YES, 0, 0);
}

- (void)testSimpleViewWithActivationPointNever;
Expand All @@ -107,6 +108,7 @@ - (void)testSimpleViewWithActivationPointNever;
label.accessibilityActivationPoint = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height / 2 - 10);

SnapshotVerifyAccessibilityWithOptions(view, nil, NO, YES);
SnapshotVerifyAccessibilityWithOptionsAndTolerance(view, nil, NO, YES, 0, 0);
}

- (void)testSimpleViewWithColorSnapshots;
Expand All @@ -124,6 +126,7 @@ - (void)testSimpleViewWithColorSnapshots;
label.accessibilityActivationPoint = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height / 2 - 10);

SnapshotVerifyAccessibilityWithOptions(view, nil, NO, NO);
SnapshotVerifyAccessibilityWithOptionsAndTolerance(view, nil, NO, NO, 0, 0);
}

- (void)testViewWithInvertedColors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ extension Snapshotting where Value == UIView, Format == UIImage {
/// - parameter drawHierarchyInKeyWindow: Whether or not to draw the view hierachy in the key window, rather than
/// rendering the view's layer. This enables the rendering of `UIAppearance` and `UIVisualEffect`s.
/// - parameter markerColors: The array of colors which will be chosen from when creating the overlays
/// - parameter precision: The percentage of pixels that must match.
public static func accessibilityImage(
showActivationPoints activationPointDisplayMode: ActivationPointDisplayMode = .whenOverridden,
useMonochromeSnapshot: Bool = true,
drawHierarchyInKeyWindow: Bool = false,
markerColors: [UIColor] = []
markerColors: [UIColor] = [],
precision: Float = 1
) -> Snapshotting {
guard isRunningInHostApplication else {
fatalError("Accessibility snapshot tests cannot be run in a test target without a host application")
}

return Snapshotting<UIView, UIImage>
.image(drawHierarchyInKeyWindow: drawHierarchyInKeyWindow)
.image(drawHierarchyInKeyWindow: drawHierarchyInKeyWindow, precision: precision)
.pullback { view in
let containerView = AccessibilitySnapshotView(
containedView: view,
Expand Down Expand Up @@ -160,18 +162,21 @@ extension Snapshotting where Value == UIViewController, Format == UIImage {
/// - parameter drawHierarchyInKeyWindow: Whether or not to draw the view hierachy in the key window, rather than
/// rendering the view's layer. This enables the rendering of `UIAppearance` and `UIVisualEffect`s.
/// - parameter markerColors: The array of colors which will be chosen from when creating the overlays
/// - parameter precision: The percentage of pixels that must match.
public static func accessibilityImage(
showActivationPoints activationPointDisplayMode: ActivationPointDisplayMode = .whenOverridden,
useMonochromeSnapshot: Bool = true,
drawHierarchyInKeyWindow: Bool = false,
markerColors: [UIColor] = []
markerColors: [UIColor] = [],
precision: Float = 1
) -> Snapshotting {
return Snapshotting<UIView, UIImage>
.accessibilityImage(
showActivationPoints: activationPointDisplayMode,
useMonochromeSnapshot: useMonochromeSnapshot,
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
markerColors: markerColors
markerColors: markerColors,
precision: precision
)
.pullback { viewController in
viewController.view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extension FBSnapshotTestCase {
/// read certain views. Defaults to `true`.
/// - parameter suffixes: NSOrderedSet object containing strings that are appended to the reference images directory.
/// Defaults to `FBSnapshotTestCaseDefaultSuffixes()`.
/// - parameter perPixelTolerance: The percentage a given pixel's R,G,B and A components can differ and still be considered 'identical'. Each color shade difference represents a 0.390625% change.
/// - parameter overallTolerance The percentage difference to still count as identical - 0 mean pixel perfect, 1 means I don't care.
/// - parameter file: The file in which the test result should be attributed.
/// - parameter line: The line in which the test result should be attributed.
public func SnapshotVerifyAccessibility(
Expand All @@ -44,6 +46,8 @@ extension FBSnapshotTestCase {
useMonochromeSnapshot: Bool = true,
markerColors: [UIColor] = [],
suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(),
perPixelTolerance: CGFloat = 0,
overallTolerance: CGFloat = 0,
file: StaticString = #file,
line: UInt = #line
) {
Expand Down Expand Up @@ -71,7 +75,15 @@ extension FBSnapshotTestCase {
containerView.parseAccessibility(useMonochromeSnapshot: useMonochromeSnapshot)
containerView.sizeToFit()

FBSnapshotVerifyView(containerView, identifier: identifier, suffixes: suffixes, file: file, line: line)
FBSnapshotVerifyView(
containerView,
identifier: identifier,
suffixes: suffixes,
perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
file: file,
line: line
)
}

/// Snapshots the `view` using the specified content size category to test Dynamic Type.
Expand All @@ -88,13 +100,17 @@ extension FBSnapshotTestCase {
/// in a given test method. Defaults to no identifier.
/// - parameter suffixes: NSOrderedSet object containing strings that are appended to the reference images directory.
/// Defaults to `FBSnapshotTestCaseDefaultSuffixes()`.
/// - parameter perPixelTolerance: The percentage a given pixel's R,G,B and A components can differ and still be considered 'identical'. Each color shade difference represents a 0.390625% change.
/// - parameter overallTolerance The percentage difference to still count as identical - 0 mean pixel perfect, 1 means I don't care.
/// - parameter file: The file in which the test result should be attributed.
/// - parameter line: The line in which the test result should be attributed.
func SnapshotVerify(
_ view: UIView,
at contentSizeCategory: UIContentSizeCategory,
identifier: String = "",
suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(),
perPixelTolerance: CGFloat = 0,
overallTolerance: CGFloat = 0,
file: StaticString = #file,
line: UInt = #line
) {
Expand All @@ -121,7 +137,15 @@ extension FBSnapshotTestCase {
// will be able to change the text size in production.
view.setNeedsLayout()

FBSnapshotVerifyView(view, identifier: identifier, suffixes: suffixes, file: file, line: line)
FBSnapshotVerifyView(
view,
identifier: identifier,
suffixes: suffixes,
perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
file: file,
line: line
)

// Restore the original content size category.
let overriddenTraitCollection = view.traitCollection
Expand All @@ -141,12 +165,16 @@ extension FBSnapshotTestCase {
/// in a given test method. Defaults to no identifier.
/// - parameter suffixes: NSOrderedSet object containing strings that are appended to the reference images directory.
/// Defaults to `FBSnapshotTestCaseDefaultSuffixes()`.
/// - parameter perPixelTolerance: The percentage a given pixel's R,G,B and A components can differ and still be considered 'identical'. Each color shade difference represents a 0.390625% change.
/// - parameter overallTolerance The percentage difference to still count as identical - 0 mean pixel perfect, 1 means I don't care.
/// - parameter file: The file in which the test result should be attributed.
/// - parameter line: The line in which the test result should be attributed.
public func SnapshotVerifyWithInvertedColors(
_ view: UIView,
identifier: String = "",
suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(),
perPixelTolerance: CGFloat = 0,
overallTolerance: CGFloat = 0,
file: StaticString = #file,
line: UInt = #line
) {
Expand Down Expand Up @@ -176,7 +204,14 @@ extension FBSnapshotTestCase {
}

let imageView = UIImageView(image: image)
FBSnapshotVerifyView(imageView, suffixes: suffixes, file: file, line: line)
FBSnapshotVerifyView(
imageView,
suffixes: suffixes,
perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
file: file,
line: line
)

statusUtility.unmockStatuses()
postNotification()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,43 @@ extension FBSnapshotTestCase {
identifier: String,
showActivationPoints: Bool,
useMonochromeSnapshot: Bool
) -> String? {
return 🚫objc_snapshotVerifyAccessibility(
view,
identifier: identifier,
showActivationPoints: showActivationPoints,
useMonochromeSnapshot: useMonochromeSnapshot,
perPixelTolerance: 0,
overallTolerance: 0
)
}

@objc(snapshotVerifyAccessibility:identifier:showActivationPoints:useMonochromeSnapshot:perPixelTolerance:overallTolerance:)
private func 🚫objc_snapshotVerifyAccessibility(
_ view: UIView,
identifier: String,
showActivationPoints: Bool,
useMonochromeSnapshot: Bool,
perPixelTolerance: CGFloat,
overallTolerance: CGFloat
) -> String? {
return snapshotVerifyAccessibility(
view,
identifier: identifier,
activationPointDisplayMode: showActivationPoints ? .always : .never,
useMonochromeSnapshot: useMonochromeSnapshot
useMonochromeSnapshot: useMonochromeSnapshot,
perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance
)
}

private func snapshotVerifyAccessibility(
_ view: UIView,
identifier: String,
activationPointDisplayMode: ActivationPointDisplayMode,
useMonochromeSnapshot: Bool
useMonochromeSnapshot: Bool,
perPixelTolerance: CGFloat = 0,
overallTolerance: CGFloat = 0
) -> String? {
guard isRunningInHostApplication else {
return "Accessibility snapshot tests cannot be run in a test target without a host application"
Expand All @@ -70,16 +93,28 @@ extension FBSnapshotTestCase {
return snapshotVerifyViewOrLayer(
containerView,
identifier: identifier,
suffixes: FBSnapshotTestCaseDefaultSuffixes(),
perPixelTolerance: 0,
overallTolerance: 0,
suffixes: FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
defaultReferenceDirectory: FB_REFERENCE_IMAGE_DIR,
defaultImageDiffDirectory: IMAGE_DIFF_DIR
)
}

@objc(snapshotVerifyWithInvertedColors:identifier:)
private func snapshotVerifyWithInvertedColors(_ view: UIView, identifier: String) -> String? {
private func snapshotVerifyWithInvertedColors(
_ view: UIView,
identifier: String
) -> String? {
return snapshotVerifyWithInvertedColors(view, identifier: identifier, perPixelTolerance: 0, overallTolerance: 0)
}

@objc(snapshotVerifyWithInvertedColors:identifier:perPixelTolerance:overallTolerance:)
private func snapshotVerifyWithInvertedColors(
_ view: UIView,
identifier: String,
perPixelTolerance: CGFloat,
overallTolerance: CGFloat
) -> String? {
func postNotification() {
NotificationCenter.default.post(
name: UIAccessibility.invertColorsStatusDidChangeNotification,
Expand Down Expand Up @@ -109,9 +144,8 @@ extension FBSnapshotTestCase {
let errorDescription = snapshotVerifyViewOrLayer(
imageView,
identifier: identifier,
suffixes: FBSnapshotTestCaseDefaultSuffixes(),
perPixelTolerance: 0,
overallTolerance: 0,
suffixes: FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: perPixelTolerance,
overallTolerance: overallTolerance,
defaultReferenceDirectory: FB_REFERENCE_IMAGE_DIR,
defaultImageDiffDirectory: IMAGE_DIFF_DIR
)
Expand All @@ -125,5 +159,4 @@ extension FBSnapshotTestCase {

return errorDescription
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
}\
}

#define SnapshotVerifyAccessibilityWithOptionsAndTolerance(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 SnapshotVerifyWithInvertedColors(view__, identifier__)\
{\
_Pragma("clang diagnostic push")\
Expand All @@ -63,3 +79,19 @@
XCTFail("%@", errorDescription);\
}\
}

#define SnapshotVerifyWithInvertedColorsAndTolerance(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);\
}\
}