Skip to content

Quick proposal to fix .wait method #742

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 45 additions & 2 deletions Sources/SnapshotTesting/Common/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,48 @@
}
}

//<<<<<<< HEAD
// if size.width == 0 || size.height == 0 {
// // Try to call sizeToFit() if the view still has invalid size
// view.sizeToFit()
// view.setNeedsLayout()
// view.layoutIfNeeded()
// }
//
// return dispose
//}
//
//func snapshotView(
// config: ViewImageConfig,
// drawHierarchyInKeyWindow: Bool,
// traits: UITraitCollection,
// view: UIView,
// viewController: UIViewController,
// delay: Double? = nil
// )
// -> Async<UIImage> {
// let initialFrame = view.frame
// let dispose = 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) }
//
// return (view.snapshot ?? Async { callback in
// addImagesForRenderedViews(view).sequence().delay(by: delay).run { views in
// callback(
// renderer(bounds: view.bounds, for: traits).image { ctx in
// if drawHierarchyInKeyWindow {
// view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
// } else {
// view.layer.render(in: ctx.cgContext)
// }
// }
//=======
func prepareView(
config: ViewImageConfig,
drawHierarchyInKeyWindow: Bool,
Expand Down Expand Up @@ -970,7 +1012,8 @@
drawHierarchyInKeyWindow: Bool,
traits: UITraitCollection,
view: UIView,
viewController: UIViewController
viewController: UIViewController,
delay: Double? = nil
)
-> Async<UIImage>
{
Expand All @@ -988,7 +1031,7 @@
return
(view.snapshot
?? Async { callback in
addImagesForRenderedViews(view).sequence().run { views in
addImagesForRenderedViews(view).sequence().delay(by: delay).run { views in
callback(
renderer(bounds: view.bounds, for: traits).image { ctx in
if drawHierarchyInKeyWindow {
Expand Down
20 changes: 20 additions & 0 deletions Sources/SnapshotTesting/Extensions/Async+Delay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

extension Async {
/// Delays Async `run` block
/// - Parameter timeInterval: Optional duration for Async to wait before running `run`
/// - Returns: Delayed `Async` unless no duration was provided then returns original `Async`
func delay(by timeInterval: TimeInterval?) -> Async<Value> {
guard let timeInterval = timeInterval else {
return self
}

return Async<Value> { callback in
run { value in
DispatchQueue.main.asyncAfter(deadline: .now() + timeInterval) {
callback(value)
}
}
}
}
}
8 changes: 6 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
return .image()
}


/// A snapshot strategy for comparing view controller views based on pixel equality.
///
/// - Parameters:
Expand All @@ -18,12 +19,14 @@
/// human eye.
/// - size: A view size override.
/// - traits: A trait collection override.
/// - delay: A time in seconds, for how long to wait before making snapshot
public static func image(
on config: ViewImageConfig,
precision: Float = 1,
perceptualPrecision: Float = 1,
size: CGSize? = nil,
traits: UITraitCollection = .init()
traits: UITraitCollection = .init(),
delay: Double? = nil
)
-> Snapshotting
{
Expand All @@ -37,7 +40,8 @@
drawHierarchyInKeyWindow: false,
traits: traits,
view: viewController.view,
viewController: viewController
viewController: viewController,
delay: delay
)
}
}
Expand Down
63 changes: 63 additions & 0 deletions Tests/SnapshotTestingTests/DelayTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import XCTest
@testable import SnapshotTesting

class DelayTests: XCTestCase {
func testDelayViewDidLoad() {
let sut = DelayedViewController()

assertSnapshot(matching: sut, as: .image(on: .iPhone13, delay: 2))
}
func testViewDidLoad() {
let sut = DelayedViewController()

assertSnapshot(matching: sut, as: .image(on: .iPhone13))
}
func testWaitViewDidLoad() {
let sut = DelayedViewController()

assertSnapshot(matching: sut, as: .wait(for: 2, on: .image(on: .iPhone13)))
}
}

private final class DelayedViewController: UIViewController {

private lazy var topLabel = UILabel()
private lazy var bottomLabel = UILabel()

override func viewDidLoad() {
super.viewDidLoad()

setupUI()

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.topLabel.text = "Goodbye viewDidLoad"
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.bottomLabel.text = "Goodbye viewDidAppear"
}
}

private func setupUI() {
view.backgroundColor = .white

topLabel.text = "Hello viewDidLoad"
bottomLabel.text = "Hello viewDidAppear"
topLabel.translatesAutoresizingMaskIntoConstraints = false
bottomLabel.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(topLabel)
view.addSubview(bottomLabel)

NSLayoutConstraint.activate([
topLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
topLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
bottomLabel.topAnchor.constraint(equalTo: topLabel.bottomAnchor),
bottomLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
}
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.