From 569a2fdf54beabbbc77ab9a73d91733cd6aa7f95 Mon Sep 17 00:00:00 2001 From: Steven Tran Date: Sat, 18 Apr 2020 19:47:40 -0400 Subject: [PATCH 1/2] Support taking snapshots in light and dark mode --- src/iOSSnapshotTestCase/SwiftSupport.swift | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/iOSSnapshotTestCase/SwiftSupport.swift b/src/iOSSnapshotTestCase/SwiftSupport.swift index c5339cf..65157f0 100644 --- a/src/iOSSnapshotTestCase/SwiftSupport.swift +++ b/src/iOSSnapshotTestCase/SwiftSupport.swift @@ -31,6 +31,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 snapshot in light mode + 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 snapshot in dark mode + 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) From a5b642ae82525cc337539df0dc171dc7b62e471a Mon Sep 17 00:00:00 2001 From: Steven Tran Date: Tue, 21 Apr 2020 18:22:50 -0400 Subject: [PATCH 2/2] Mark method as available for iOS 13 and above --- src/iOSSnapshotTestCase/SwiftSupport.swift | 38 +++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/iOSSnapshotTestCase/SwiftSupport.swift b/src/iOSSnapshotTestCase/SwiftSupport.swift index 65157f0..4c7af4c 100644 --- a/src/iOSSnapshotTestCase/SwiftSupport.swift +++ b/src/iOSSnapshotTestCase/SwiftSupport.swift @@ -31,6 +31,7 @@ public extension FBSnapshotTestCase { FBSnapshotVerifyViewOrLayer(layer, identifier: identifier, suffixes: suffixes, perPixelTolerance: perPixelTolerance, overallTolerance: overallTolerance, file: file, line: line) } + @available(iOS 13, *) 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) @@ -38,31 +39,30 @@ public extension FBSnapshotTestCase { FBSnapshotVerifyViewControllerForLightDarkMode(viewController, identifier: identifier, delay: delay, perPixelTolerance: perPixelTolerance, overallTolerance: overallTolerance, file: file, line: line) } + @available(iOS 13, *) 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() + let navigationController = UINavigationController(rootViewController: viewController) + let window = UIWindow() + window.rootViewController = navigationController + window.makeKeyAndVisible() - // Take snapshot in light mode - navigationController.overrideUserInterfaceStyle = .light - RunLoop.main.run(until: Date(timeIntervalSinceNow: delay)) + // Take snapshot in light mode + 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) + let lightId = [identifier, "light"].compactMap { $0 }.joined(separator: "_") + FBSnapshotVerifyView(navigationController.view, identifier: lightId, suffixes: ["Light"], perPixelTolerance: 0, overallTolerance: 0, file: file, line: line) - // Take snapshot in dark mode - window.rootViewController = nil - window.rootViewController = navigationController - navigationController.overrideUserInterfaceStyle = .dark - RunLoop.main.run(until: Date(timeIntervalSinceNow: delay)) + // Take snapshot in dark mode + 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) + 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 - } + 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) {