diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 2fa0c9d..f84328c 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -33,7 +33,6 @@ F5F5B69F27D94B1800D998B5 /* NotchGradientLoadingBarViewModelTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5F5B69B27D94B1800D998B5 /* NotchGradientLoadingBarViewModelTestCase.swift */; }; F5FC82FA27DA0BBF00FE1D7D /* GradientActivityIndicatorViewTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5FC82E827DA0BBF00FE1D7D /* GradientActivityIndicatorViewTestCase.swift */; }; F5FC82FE27DA0BBF00FE1D7D /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = F5FC82EF27DA0BBF00FE1D7D /* README.md */; }; - F5FC82FF27DA0BBF00FE1D7D /* Snapshotting+WindowedImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5FC82F127DA0BBF00FE1D7D /* Snapshotting+WindowedImage.swift */; }; F5FC830027DA0BBF00FE1D7D /* GradientLoadingBarControllerTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5FC82F327DA0BBF00FE1D7D /* GradientLoadingBarControllerTestCase.swift */; }; F5FC830127DA0BBF00FE1D7D /* NotchGradientLoadingBarControllerTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5FC82F427DA0BBF00FE1D7D /* NotchGradientLoadingBarControllerTestCase.swift */; }; /* End PBXBuildFile section */ @@ -90,7 +89,6 @@ F5FC82DE27DA0B3200FE1D7D /* ExampleSnapshotTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ExampleSnapshotTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; F5FC82E827DA0BBF00FE1D7D /* GradientActivityIndicatorViewTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GradientActivityIndicatorViewTestCase.swift; sourceTree = ""; }; F5FC82EF27DA0BBF00FE1D7D /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; - F5FC82F127DA0BBF00FE1D7D /* Snapshotting+WindowedImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Snapshotting+WindowedImage.swift"; sourceTree = ""; }; F5FC82F327DA0BBF00FE1D7D /* GradientLoadingBarControllerTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GradientLoadingBarControllerTestCase.swift; sourceTree = ""; }; F5FC82F427DA0BBF00FE1D7D /* NotchGradientLoadingBarControllerTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotchGradientLoadingBarControllerTestCase.swift; sourceTree = ""; }; FAA1A12E84EBC1F48401EB10 /* Pods-Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.debug.xcconfig"; path = "Target Support Files/Pods-Example/Pods-Example.debug.xcconfig"; sourceTree = ""; }; @@ -294,7 +292,6 @@ F5FC82DF27DA0B3200FE1D7D /* ExampleSnapshotTests */ = { isa = PBXGroup; children = ( - F5FC82F027DA0BBF00FE1D7D /* _Helper */, F5FC82E727DA0BBF00FE1D7D /* GradientActivityIndicatorView */, F5FC82F227DA0BBF00FE1D7D /* GradientLoadingBarController */, F5FC830727DA0D8400FE1D7D /* GradientLoadingBarView */, @@ -311,14 +308,6 @@ path = GradientActivityIndicatorView; sourceTree = ""; }; - F5FC82F027DA0BBF00FE1D7D /* _Helper */ = { - isa = PBXGroup; - children = ( - F5FC82F127DA0BBF00FE1D7D /* Snapshotting+WindowedImage.swift */, - ); - path = _Helper; - sourceTree = ""; - }; F5FC82F227DA0BBF00FE1D7D /* GradientLoadingBarController */ = { isa = PBXGroup; children = ( @@ -646,7 +635,6 @@ buildActionMask = 2147483647; files = ( F5FC82FA27DA0BBF00FE1D7D /* GradientActivityIndicatorViewTestCase.swift in Sources */, - F5FC82FF27DA0BBF00FE1D7D /* Snapshotting+WindowedImage.swift in Sources */, F57C83C927DA11580025AA11 /* GradientLoadingBarViewTestCase.swift in Sources */, F5FC830027DA0BBF00FE1D7D /* GradientLoadingBarControllerTestCase.swift in Sources */, F5FC830127DA0BBF00FE1D7D /* NotchGradientLoadingBarControllerTestCase.swift in Sources */, diff --git a/Example/ExampleSnapshotTests/GradientLoadingBarController/GradientLoadingBarControllerTestCase.swift b/Example/ExampleSnapshotTests/GradientLoadingBarController/GradientLoadingBarControllerTestCase.swift index 9a06e44..4d84d92 100644 --- a/Example/ExampleSnapshotTests/GradientLoadingBarController/GradientLoadingBarControllerTestCase.swift +++ b/Example/ExampleSnapshotTests/GradientLoadingBarController/GradientLoadingBarControllerTestCase.swift @@ -12,6 +12,13 @@ import SnapshotTesting @testable import GradientLoadingBar final class GradientLoadingBarControllerTestCase: XCTestCase { + // MARK: - Config + + private enum Config { + /// The percentage of pixels that must match. + static let precision: Float = 0.99 + } + // MARK: - Tests func test_gradientLoadingBarController() { @@ -30,6 +37,7 @@ final class GradientLoadingBarControllerTestCase: XCTestCase { // Then wait(for: [expectation], timeout: 1) - assertSnapshot(matching: rootViewController, as: .windowedImage) + assertSnapshot(matching: rootViewController, + as: .image(drawHierarchyInKeyWindow: true, precision: Config.precision)) } } diff --git a/Example/ExampleSnapshotTests/GradientLoadingBarController/NotchGradientLoadingBarControllerTestCase.swift b/Example/ExampleSnapshotTests/GradientLoadingBarController/NotchGradientLoadingBarControllerTestCase.swift index ac092fd..57c08d1 100644 --- a/Example/ExampleSnapshotTests/GradientLoadingBarController/NotchGradientLoadingBarControllerTestCase.swift +++ b/Example/ExampleSnapshotTests/GradientLoadingBarController/NotchGradientLoadingBarControllerTestCase.swift @@ -14,6 +14,13 @@ import SnapshotTesting final class NotchGradientLoadingBarControllerTestCase: XCTestCase { // swiftlint:disable:previous type_name + // MARK: - Config + + private enum Config { + /// The percentage of pixels that must match. + static let precision: Float = 0.99 + } + // MARK: - Tests func test_notchGradientLoadingBarController() { @@ -32,6 +39,7 @@ final class NotchGradientLoadingBarControllerTestCase: XCTestCase { // Then wait(for: [expectation], timeout: 1) - assertSnapshot(matching: rootViewController, as: .windowedImage) + assertSnapshot(matching: rootViewController, + as: .image(drawHierarchyInKeyWindow: true, precision: Config.precision)) } } diff --git a/Example/ExampleSnapshotTests/_Helper/Snapshotting+WindowedImage.swift b/Example/ExampleSnapshotTests/_Helper/Snapshotting+WindowedImage.swift deleted file mode 100644 index 5dca880..0000000 --- a/Example/ExampleSnapshotTests/_Helper/Snapshotting+WindowedImage.swift +++ /dev/null @@ -1,46 +0,0 @@ -// -// Snapshotting+WindowedImage.swift -// ExampleSnapshotTests -// -// Created by Felix Mau on 14.06.20. -// Copyright © 2020 Felix Mau. All rights reserved. -// - -import SnapshotTesting - -@testable import GradientLoadingBar - -// MARK: - Config - -private enum Config { - /// Allow for a 1% pixel difference. - static let precision: Float = 0.99 -} - -// MARK: - Public properties - -extension Snapshotting where Value: UIViewController, Format == UIImage { - /// Based on: - /// and - static var windowedImage: Snapshotting { - Snapshotting.image(precision: Config.precision, scale: nil).asyncPullback { viewController in - Async { callback in - UIView.setAnimationsEnabled(false) - - guard let keyWindow = UIApplication.shared.keyWindowInConnectedScenes else { - fatalError("⚠️ – Failed to get key window from application!") - } - keyWindow.rootViewController = viewController - - DispatchQueue.main.async { - let image = UIGraphicsImageRenderer(bounds: keyWindow.bounds).image { _ in - keyWindow.drawHierarchy(in: keyWindow.bounds, afterScreenUpdates: true) - } - callback(image) - - UIView.setAnimationsEnabled(true) - } - } - } - } -}