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

feat: Improve Testing #169

Merged
merged 4 commits into from
Oct 13, 2023
Merged
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
32 changes: 26 additions & 6 deletions Showcase/ShowcaseSnapshotTests/NonRegressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SwiftUI
@testable import VitaminSwiftUI
@testable import Showcase

final class NonRegressionUIKitTests: XCTestCase {
final class NonRegressionUIKitTests: VitaminTestCase {
// https://decathlon.atlassian.net/browse/CNT-1085
func testCNT1085() throws {
let restartButton = VitaminButton()
Expand All @@ -24,15 +24,15 @@ final class NonRegressionUIKitTests: XCTestCase {
restartButton.setTitleColor(VitaminColor.Core.Content.primaryReversed, for: .normal)
restartButton.setTitleColor(VitaminColor.Core.Content.primaryReversed, for: .highlighted)
restartButton.frame = CGRect(origin: .zero, size: CGSize(width: 320, height: 80))
assertSnapshot(matching: restartButton, as: .image, named: "restartButton")
assertSnapshot(matching: restartButton, as: .image(perceptualPrecision: perceptualPrecision), named: "restartButton")

let updateButton = VitaminButton()
updateButton.style = .secondary
updateButton.setIconType(.trailing(image: Vitamix.Line.Design.edit.image), for: .normal)
updateButton.setTitle("", for: .normal) // For iOS 12
updateButton.setTitle("Update Button", for: .normal)
updateButton.frame = CGRect(origin: .zero, size: CGSize(width: 320, height: 80))
assertSnapshot(matching: updateButton, as: .image)
assertSnapshot(matching: updateButton, as: .image(perceptualPrecision: perceptualPrecision))

let deleteButton = VitaminButton()
deleteButton.style = .ghost
Expand All @@ -42,12 +42,15 @@ final class NonRegressionUIKitTests: XCTestCase {
deleteButton.setTitleColor(VitaminColor.Core.Content.negative, for: .normal)
deleteButton.setTitleColor(VitaminColor.Core.Content.negative, for: .highlighted)
deleteButton.frame = CGRect(origin: .zero, size: CGSize(width: 320, height: 80))
assertSnapshot(matching: deleteButton, as: .image)
assertSnapshot(matching: deleteButton, as: .image(perceptualPrecision: perceptualPrecision))
}

func testButtonAlignement() throws {
@State var bt1: CGSize = .zero
@State var bt2: CGSize = .zero

let gear = UIImage(systemName: "gear")?.resize(CGSize(width: 24, height: 24))

let container = HStack {
VitaminButton(
text: "Modifier le profil",
Expand All @@ -60,12 +63,29 @@ final class NonRegressionUIKitTests: XCTestCase {
VitaminButton(
style: .tertiary,
size: .medium,
iconType: .alone(image: UIImage(systemName: "gear")!, renderingMode: .template),
iconType: .alone(image: gear!, renderingMode: .template),
action: {})
.fixedSize()
.readSize(onChange: { bt2 = $0 })
}.padding()
assertSnapshot(matching: UIHostingController(rootView: container), as: .recursiveDescription(on: .iPhone12))
assertSnapshot(matching: UIHostingController(rootView: container), as: .image)
assertSnapshot(matching: UIHostingController(rootView: container), as: .image(perceptualPrecision: perceptualPrecision))
}
}

class VitaminTestCase: XCTestCase {
// The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
var perceptualPrecision: Float = 0.99

override class func setUp() {
// Helix Visual Merge Tool (P4Merge) is a versatile tool for three-way merging and side-by-side file comparisons. It's useful for visualizing merges, accessing detailed file histories, and comparing a wide range of image and text files.
// You can find more about it here: https://www.perforce.com/products/helix-core-apps/merge-diff-tool-p4merge
// We're using this free tool to simplify the process of diffing two image or text files.
// To use it effectively, make sure to have P4Merge installed on your system.
SnapshotTesting.diffTool = "/Applications/p4merge.app/Contents/MacOS/p4merge"

// In order to update the test fixture you just have to un comment the code below then recomment it before commit.
// SnapshotTesting.isRecording = true
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import PreviewSnapshotsTesting
@testable import VitaminCore

@available(iOS 13, *)
final class ShowcaseSnapshotSwiftUITests: XCTestCase {
final class ShowcaseSnapshotSwiftUITests: VitaminTestCase {

func testButtonsView() throws {
ButtonsView_Previews.snapshots.assertSnapshots()
ButtonsView_Previews.snapshots.assertSnapshots(as: .image(perceptualPrecision: perceptualPrecision))
}

func testTextFields() throws {
TextFieldsView_Previews.snapshots.assertSnapshots()
TextFieldsView_Previews.snapshots.assertSnapshots(as: .image(perceptualPrecision: perceptualPrecision))
}


func testBadgesView() throws {
BadgesView_Previews.snapshots.assertSnapshots()
BadgesView_Previews.snapshots.assertSnapshots(as: .image(perceptualPrecision: perceptualPrecision))
}
}
22 changes: 11 additions & 11 deletions Showcase/ShowcaseSnapshotTests/ShowcaseSnapshotUIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ import UIKit
@testable import Vitamin
@testable import Showcase

final class ShowcaseSnapshotUIKitTests: XCTestCase {
final class ShowcaseSnapshotUIKitTests: VitaminTestCase {

func testVitaminButton() throws {
let vc = UINavigationController(rootViewController: ButtonsViewController())
vc.view.frame = CGRect(origin: .zero, size: CGSize(width: 390, height: 1750))
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testTextFieldViewController() throws {
let vc = UINavigationController(rootViewController: TextFieldViewController())
vc.view.frame = CGRect(origin: .zero, size: CGSize(width: 390, height: 2800))
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testBadgesViewController() throws {
let vc = UINavigationController(rootViewController: BadgesViewController())
vc.view.frame = CGRect(origin: .zero, size: CGSize(width: 390, height: 1300))
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testProgressbarViewController() throws {
let vc = UINavigationController(rootViewController: ProgressbarViewController())
vc.view.frame = CGRect(origin: .zero, size: CGSize(width: 390, height: 5000))
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testTagViewController() throws {
let vc = UINavigationController(rootViewController: TagViewController())
vc.view.frame = CGRect(origin: .zero, size: CGSize(width: 390, height: 3800))
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testSnackbarViewController() throws {
Expand Down Expand Up @@ -69,23 +69,23 @@ final class ShowcaseSnapshotUIKitTests: XCTestCase {
),
actionOnTap: nil
)
assertSnapshot(matching: snack, as: .image)
assertSnapshot(matching: snack2, as: .image)
assertSnapshot(matching: snack, as: .image(perceptualPrecision: perceptualPrecision))
assertSnapshot(matching: snack2, as: .image(perceptualPrecision: perceptualPrecision))
}

func testChipViewController() throws {
let vc = UINavigationController(rootViewController: ChipViewController())
vc.view.frame = CGRect(origin: .zero, size: CGSize(width: 390, height: 1280))
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testSwitchViewController() throws {
let vc = UINavigationController(rootViewController: SwitchViewController())
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}

func testSegmentedControlViewController() throws {
let vc = UINavigationController(rootViewController: SegmentedControlViewController())
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .image(perceptualPrecision: perceptualPrecision))
}
}
19 changes: 19 additions & 0 deletions Showcase/ShowcaseSnapshotTests/VitaminTestCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation
import XCTest
import SnapshotTesting

class VitaminTestCase: XCTestCase {
// The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
var perceptualPrecision: Float = 0.99

override class func setUp() {
// Helix Visual Merge Tool (P4Merge) is a versatile tool for three-way merging and side-by-side file comparisons. It's useful for visualizing merges, accessing detailed file histories, and comparing a wide range of image and text files.
// You can find more about it here: https://www.perforce.com/products/helix-core-apps/merge-diff-tool-p4merge
// We're using this free tool to simplify the process of diffing two image or text files.
// To use it effectively, make sure to have P4Merge installed on your system.
SnapshotTesting.diffTool = "/Applications/p4merge.app/Contents/MacOS/p4merge"

// In order to update the test fixture you just have to un comment the code below then recomment it before commit.
// SnapshotTesting.isRecording = true
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<_TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentGVS_6HStackGVS_9TupleViewTGS1_GS1_GS1_V14VitaminSwiftUI13VitaminButtonVS_16_FixedSizeLayout_GVS_19_BackgroundModifierGVS_14GeometryReaderGS1_VS_5ColorGVS_26_PreferenceWritingModifierV21ShowcaseSnapshotTests21ReadSizePreferenceKey_____GVS_25_PreferenceActionModifierS12___GS1_GS1_GS1_S5_S6__GS7_GS8_GS1_S9_GS10_S12______GS13_S12______VS_14_PaddingLayout__; frame = (0 0; 390 844); autoresize = W+H; gestureRecognizers = <NSArray>; backgroundColor = <UIDynamicSystemColor; name = systemBackgroundColor>; layer = <CALayer>>
| <_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView; frame = (67 406; 188 52); anchorPoint = (0, 0); autoresizesSubviews = NO; backgroundColor = UIExtendedSRGBColorSpace 0.905882 0.952941 0.976471 1; layer = <CALayer>>
| <SwiftUI._UIGraphicsView; frame = (87 422; 20 20); anchorPoint = (0, 0); autoresizesSubviews = NO; layer = <SwiftUI.ImageLayer>>
| <_TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView; frame = (117 422.667; 118 19); anchorPoint = (0, 0); opaque = NO; autoresizesSubviews = NO; layer = <_TtCOCV7SwiftUI11DisplayList11ViewUpdater8PlatformP33_65A81BD07F0108B0485D2E15DE104A7514CGDrawingLayer>>
| <_TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView; frame = (117 422; 118 20); anchorPoint = (0, 0); opaque = NO; autoresizesSubviews = NO; layer = <_TtCOCV7SwiftUI11DisplayList11ViewUpdater8PlatformP33_65A81BD07F0108B0485D2E15DE104A7514CGDrawingLayer>>
| <_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView; frame = (263 406; 60 52); anchorPoint = (0, 0); autoresizesSubviews = NO; backgroundColor = UIExtendedSRGBColorSpace 0.905882 0.952941 0.976471 1; layer = <CALayer>>
| <SwiftUI._UIGraphicsView; frame = (283 422; 20 20); anchorPoint = (0, 0); autoresizesSubviews = NO; layer = <SwiftUI.ImageLayer>>
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.
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.
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.
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.
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