-
Notifications
You must be signed in to change notification settings - Fork 601
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
Configurable recursive description #141
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ffeba83
Configurable recursive description
stephencelis e0327d2
Make private again
stephencelis 17046de
Fix comment
stephencelis 51f40c1
Remove stale snap
stephencelis 1118cbc
Change tests
stephencelis 5137d65
Update Available-Snapshot-Strategies.md
stephencelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -472,6 +472,39 @@ private final class NavigationDelegate: NSObject, WKNavigationDelegate { | |
#endif | ||
|
||
#if os(iOS) || os(tvOS) | ||
func prepareView( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we can prepare views for text-based snapshots, as well. |
||
config: ViewImageConfig, | ||
drawHierarchyInKeyWindow: Bool, | ||
traits: UITraitCollection, | ||
view: UIView, | ||
viewController: UIViewController | ||
) { | ||
let size = config.size ?? viewController.view.frame.size | ||
guard size.width > 0, size.height > 0 else { | ||
fatalError("View not renderable to image at size \(size)") | ||
} | ||
view.frame.size = size | ||
if view != viewController.view { | ||
viewController.view.bounds = view.bounds | ||
viewController.view.addSubview(view) | ||
} | ||
let traits = UITraitCollection(traitsFrom: [config.traits, traits]) | ||
let window: UIWindow | ||
if drawHierarchyInKeyWindow { | ||
guard let keyWindow = UIApplication.shared.keyWindow else { | ||
fatalError("'drawHierarchyInKeyWindow' requires tests to be run in a host application") | ||
} | ||
window = keyWindow | ||
window.frame.size = size | ||
} else { | ||
window = Window( | ||
config: .init(safeArea: config.safeArea, size: config.size ?? size, traits: traits), | ||
viewController: viewController | ||
) | ||
} | ||
add(traits: traits, viewController: viewController, to: window) | ||
} | ||
|
||
func snapshotView( | ||
config: ViewImageConfig, | ||
drawHierarchyInKeyWindow: Bool, | ||
|
@@ -480,33 +513,16 @@ func snapshotView( | |
viewController: UIViewController | ||
) | ||
-> Async<UIImage> { | ||
let size = config.size ?? viewController.view.frame.size | ||
guard size.width > 0, size.height > 0 else { | ||
fatalError("View not renderable to image at size \(size)") | ||
} | ||
let initialFrame = view.frame | ||
prepareView( | ||
config: config, | ||
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow, | ||
traits: traits, | ||
view: view, | ||
viewController: viewController | ||
) | ||
// NB: Avoid safe area influence. | ||
if config.safeArea == .zero { view.frame.origin = .init(x: offscreen, y: offscreen) } | ||
view.frame.size = size | ||
if view != viewController.view { | ||
viewController.view.bounds = view.bounds | ||
viewController.view.addSubview(view) | ||
} | ||
let traits = UITraitCollection(traitsFrom: [config.traits, traits]) | ||
let window: UIWindow | ||
if drawHierarchyInKeyWindow { | ||
guard let keyWindow = UIApplication.shared.keyWindow else { | ||
fatalError("'drawHierarchyInKeyWindow' requires tests to be run in a host application") | ||
} | ||
window = keyWindow | ||
window.frame.size = size | ||
} else { | ||
window = Window( | ||
config: .init(safeArea: config.safeArea, size: config.size ?? size, traits: traits), | ||
viewController: viewController | ||
) | ||
} | ||
add(traits: traits, viewController: viewController, to: window) | ||
return view.snapshot ?? Async { callback in | ||
addImagesForRenderedViews(view).sequence().run { views in | ||
callback( | ||
|
@@ -554,7 +570,7 @@ private func add(traits: UITraitCollection, viewController: UIViewController, to | |
rootViewController.view.layoutIfNeeded() | ||
} | ||
|
||
private class Window: UIWindow { | ||
private final class Window: UIWindow { | ||
var config: ViewImageConfig | ||
|
||
init(config: ViewImageConfig, viewController: UIViewController) { | ||
|
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
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
Binary file modified
BIN
+7.45 KB
(150%)
...otTestingTests/__Snapshots__/SnapshotTestingTests/testTableViewController.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.ipad-mini.txt
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,5 @@ | ||
<UIView; frame = (0 0; 1024 768); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (484.5 20; 55.5 20.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 384; 25 20.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (985 384; 39 20.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (508.5 750; 7.5 18); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.ipad-pro-10-5.txt
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,5 @@ | ||
<UIView; frame = (0 0; 1112 834); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (528.5 20; 55.5 20.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 417; 25 20.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (1073 417; 39 20.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (552.5 816; 7.5 18); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.ipad-pro-12-9.txt
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,5 @@ | ||
<UIView; frame = (0 0; 1366 1024); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (655.5 20; 55.5 20.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 512; 25 20.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (1327 512; 39 20.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (679.5 1006; 7.5 18); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.iphone-8-plus.txt
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,5 @@ | ||
<UIView; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (180.5 20; 53 19.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 368.5; 24 19.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (377 368.5; 37 19.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (203.5 719; 7.5 17); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.iphone-8.txt
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,5 @@ | ||
<UIView; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (161 20; 53 19.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 334; 24 19.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (338 334; 37 19.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (184 650; 7.5 17); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.iphone-se.txt
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,5 @@ | ||
<UIView; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (133.5 20; 53 19.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 284.5; 24 19.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (283 284.5; 37 19.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (156.5 551; 7.5 17); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
5 changes: 5 additions & 0 deletions
5
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testTraits.iphone-x.txt
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,5 @@ | ||
<UIView; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer>> | ||
| <UILabel; frame = (161 44; 53 19.5); text = 'What's'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (0 401.5; 24 19.5); text = 'the'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (338 401.5; 37 19.5); text = 'point'; userInteractionEnabled = NO; layer = <_UILabelLayer>> | ||
| <UILabel; frame = (184 761; 7.5 17); text = '?'; userInteractionEnabled = NO; layer = <_UILabelLayer>> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding your
.hierarchy
strategy to the documentation.