Skip to content

Commit

Permalink
Merge pull request #49 from stadiamaps/make-snapshot-tests-reproducible
Browse files Browse the repository at this point in the history
Make snapshot helpers reproducible
  • Loading branch information
ianthetechie committed Aug 24, 2024
2 parents cee1194 + 2c197b0 commit a789bbe
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest

final class CameraPreviewTests: XCTestCase {
func testCameraPreview() {
assertView(named: "CameraPreview") {
assertView {
CameraDirectManipulationPreview(
styleURL: URL(string: "https://demotiles.maplibre.org/style.json")!
)
Expand Down
87 changes: 56 additions & 31 deletions Tests/MapLibreSwiftUITests/Examples/MapControlsTests.swift
Original file line number Diff line number Diff line change
@@ -1,79 +1,104 @@
import CoreLocation
import MapLibreSwiftDSL
import SnapshotTesting
import XCTest
@testable import MapLibreSwiftUI

final class MapControlsTests: XCTestCase {
// NOTE: The map views in this test intentionally have a non-north orientation
// so that the compass will be rendered if present.

@MainActor
func testEmptyControls() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
// No controls
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
// No controls
}
}
}

@MainActor
func testLogoOnly() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
LogoView()
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
LogoView()
}
}
}

@MainActor
func testLogoChangePosition() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
LogoView()
.position(.topLeft)
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
LogoView()
.position(.topLeft)
}
}
}

@MainActor
func testCompassOnly() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
CompassView()
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
CompassView()
}
}
}

@MainActor
func testCompassChangePosition() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
CompassView()
.position(.topLeft)
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
CompassView()
.position(.topLeft)
}
}
}

@MainActor
func testAttributionOnly() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
AttributionButton()
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
AttributionButton()
}
}
}

@MainActor
func testAttributionChangePosition() {
assertView {
MapView(styleURL: demoTilesURL)
.mapControls {
AttributionButton()
.position(.topLeft)
}
MapView(
styleURL: demoTilesURL,
camera: .constant(.center(CLLocationCoordinate2D(), zoom: 4, direction: 45))
)
.mapControls {
AttributionButton()
.position(.topLeft)
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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.
3 changes: 3 additions & 0 deletions Tests/MapLibreSwiftUITests/Support/XCTestAssertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extension XCTestCase {
func assertView(
named name: String? = nil,
record: Bool = false,
colorScheme: ColorScheme = .light,
frame: CGSize = CGSize(width: 430, height: 932),
expectation _: XCTestExpectation? = nil,
@ViewBuilder content: () -> some View,
Expand All @@ -15,7 +16,9 @@ extension XCTestCase {
line: UInt = #line
) {
let view = content()
.environment(\.colorScheme, colorScheme)
.frame(width: frame.width, height: frame.height)
.background(Color(red: 130 / 255, green: 203 / 255, blue: 114 / 255))

assertSnapshot(of: view,
as: .image(precision: 0.9, perceptualPrecision: 0.95),
Expand Down

0 comments on commit a789bbe

Please sign in to comment.