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 snapshot methods for Light and Dark mode screens #112

Closed
wants to merge 3 commits into from
Closed
Changes from 2 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
34 changes: 34 additions & 0 deletions FBSnapshotTestCase/SwiftSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@ public extension FBSnapshotTestCase {
FBSnapshotVerifyViewOrLayer(layer, identifier: identifier, suffixes: suffixes, perPixelTolerance: perPixelTolerance, overallTolerance: overallTolerance, file: file, line: line)
}

func FBSnapshotVerifyViewForLightDarkMode(_ view: UIView, identifier: String? = nil, delay: TimeInterval = 0, perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
let viewController = UIViewController()
viewController.view.addSubview(view)

FBSnapshotVerifyViewControllerForLightDarkMode(viewController, identifier: identifier, delay: delay, perPixelTolerance: perPixelTolerance, overallTolerance: overallTolerance, file: file, line: line)
}

func FBSnapshotVerifyViewControllerForLightDarkMode(_ viewController: UIViewController, identifier: String? = nil, delay: TimeInterval = 0, perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
if #available(iOS 13.0, *) {
let navigationController = UINavigationController(rootViewController: viewController)
let window = UIWindow()
window.rootViewController = navigationController
window.makeKeyAndVisible()

// Take Light Mode snapshot
navigationController.overrideUserInterfaceStyle = .light
RunLoop.main.run(until: Date(timeIntervalSinceNow: delay))

let lightId = [identifier, "light"].compactMap { $0 }.joined(separator: "_")
FBSnapshotVerifyView(navigationController.view, identifier: lightId, suffixes: ["Light"], perPixelTolerance: 0, overallTolerance: 0, file: file, line: line)

// Take Dark Mode snapshot
window.rootViewController = nil
window.rootViewController = navigationController
navigationController.overrideUserInterfaceStyle = .dark
RunLoop.main.run(until: Date(timeIntervalSinceNow: delay))

let darkId = [identifier, "dark"].compactMap { $0 }.joined(separator: "_")
FBSnapshotVerifyView(navigationController.view, identifier: darkId, suffixes: ["Dark"], perPixelTolerance: 0, overallTolerance: 0, file: file, line: line)

window.rootViewController = nil
}
}

private func FBSnapshotVerifyViewOrLayer(_ viewOrLayer: AnyObject, identifier: String? = nil, suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), perPixelTolerance: CGFloat = 0, overallTolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
let envReferenceImageDirectory = self.getReferenceImageDirectory(withDefault: nil)
let envImageDiffDirectory = self.getImageDiffDirectory(withDefault: nil)
Expand Down