-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add imprecise comparison variants to iOSSnapshotTestCase extension
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 some simulator changes introduced in iOS 13. Historically the simulator has supported CPU-based rendering, giving us very stable image representations of views that we can compare pixel-by-pixel. Unfortunately, with iOS 13, Apple changed the simulator to use exclusively GPU-based rendering, which means that the resulting snapshots may differ slightly across machines (see uber/ios-snapshot-test-case#109). The negative effects of this were mitigated in iOSSnapshotTestCase by adding two tolerances to snapshot comparisons: a **per-pixel tolerance** that controls how close in color two pixels need to be to count as unchanged and an **overall tolerance** that controls what portion of pixels between two images need to be the same (based on the per-pixel calculation) for the images to be considered unchanged. Setting these tolerances to non-zero values enables engineers to record tests on one machine and run them on another (e.g. record new reference images on their laptop and then run tests on CI) without worrying about the tests failing due to differences in GPU rendering. This is great in theory, but from our testing we've found even the lowest tolerance values to consistently handle GPU differences between machine types let through a significant number of visual regressions. In other words, there is no magic tolerance threshold that avoids false negatives based on GPU rendering and also avoids false positives based on minor visual regressions. This is especially true for accessibility snapshots. To start, tolerances seem to be more reliable when applied to relatively small snapshot images, but accessibility snapshots tend to be fairly large since they include both the view and the legend. Additionally, the text in the legend can change meaningfully and reflect only a small number of pixel changes. For example, I ran a test of full screen snapshot on an iPhone 12 Pro with two columns of legend. Even an overall tolerance of only `0.0001` (0.01%) was enough to let through a regression where one of the elements lost its `.link` trait (represented by the text "Link." appended to the element's description in the snapshot). But this low a tolerance _wasn't_ enough to handle the GPU rendering differences between a MacBook Pro and a Mac Mini. This is a simplified example since it only uses `overallTolerance`, not `perPixelTolerance`, but we've found many similar situations arise even with the combination. Some teams have developed infrastructure to allow snapshots to run on the same hardware consistently and have built a developer process around that infrastructure, but many others have accepted tolerances as a necessity today. ## Why create separate "imprecise" variants? The simplest approach to adding tolerances would be adding the `perPixelTolerance` and `overallTolerance` parameters to the existing snapshot methods, however I feel adding separate methods with an "imprecise" prefix is better in the long run. The naming is motivated by the idea that **it needs to be very obvious when what you're doing might result in unexpected/undesirable behavior**. In other words, when using one of the core snapshot methods, you should have extremely high confidence that a test passing means there's no regressions. When you use an "imprecise" variant, it's up to you to set your confidence levels according to your chosen tolerances. This is similar to the "unsafe" terminology around memory in the Swift API. You should generally feel very confident in the memory safety of your code, but any time you see "unsafe" it's a sign to be extra careful and not gather unwarranted confidence from the compiler. Longer term, I'm hopeful we can find alternative comparison algorithms that allow for GPU rendering differences without opening the door to regressions. We can integrate these into the core snapshot methods as long as they do not introduce opportunities for regressions, or add additional comparison variants to iterate on different approaches.
- Loading branch information
Showing
15 changed files
with
362 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...litySnapshot/iOSSnapshotTestCase/ObjC/include/FBSnapshotTestCase_ImpreciseAccessibility.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);\ | ||
}\ | ||
} |
2 changes: 1 addition & 1 deletion
2
...es/AccessibilitySnapshot/iOSSnapshotTestCase/Swift/FBSnapshotTestCase+Accessibility.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.