2022-04-13 17:19:11 +03:00
|
|
|
//
|
2022-08-11 08:54:24 +01:00
|
|
|
// Copyright 2022 New Vector Ltd
|
2022-04-13 17:19:11 +03:00
|
|
|
//
|
2022-08-11 08:54:24 +01:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2022-04-13 17:19:11 +03:00
|
|
|
//
|
|
|
|
|
2022-08-11 15:02:47 +03:00
|
|
|
import SnapshotTesting
|
2022-04-13 17:19:11 +03:00
|
|
|
import XCTest
|
|
|
|
|
|
|
|
struct Application {
|
2023-02-14 16:25:24 +00:00
|
|
|
static func launch(_ identifier: UITestsScreenIdentifier) -> XCUIApplication {
|
2022-04-13 17:19:11 +03:00
|
|
|
let app = XCUIApplication()
|
2023-02-14 16:25:24 +00:00
|
|
|
app.launchEnvironment = [
|
|
|
|
"IS_RUNNING_UI_TESTS": "1",
|
|
|
|
"UI_TESTS_SCREEN": identifier.rawValue
|
|
|
|
]
|
2022-08-11 15:02:47 +03:00
|
|
|
Bundle.elementFallbackLanguage = "en"
|
2022-04-13 17:19:11 +03:00
|
|
|
app.launch()
|
2022-04-29 10:11:42 +03:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension XCUIApplication {
|
2022-08-11 15:02:47 +03:00
|
|
|
/// Assert screenshot for a screen with the given identifier. Does not fail if a screenshot is newly created.
|
|
|
|
/// - Parameter identifier: Identifier of the UI test screen
|
2023-01-05 16:35:20 +00:00
|
|
|
/// - Parameter step: An optional integer that can be used to take multiple snapshots per test identifier.
|
|
|
|
/// - Parameter insets: Optional insets with which to crop the image by.
|
2023-02-14 16:25:24 +00:00
|
|
|
func assertScreenshot(_ identifier: UITestsScreenIdentifier, step: Int? = nil, insets: UIEdgeInsets? = nil) {
|
2022-12-15 15:22:39 +02:00
|
|
|
var snapshotName = identifier.rawValue
|
|
|
|
if let step {
|
|
|
|
snapshotName += "-\(step)"
|
|
|
|
}
|
2023-01-05 16:35:20 +00:00
|
|
|
|
|
|
|
var snapshot = XCUIScreen.main.screenshot().image
|
|
|
|
|
|
|
|
if let insets {
|
|
|
|
snapshot = snapshot.inset(by: insets)
|
|
|
|
}
|
2022-12-15 15:22:39 +02:00
|
|
|
|
2023-01-05 16:35:20 +00:00
|
|
|
let failure = verifySnapshot(matching: snapshot,
|
2022-11-24 10:35:00 +02:00
|
|
|
as: .image(precision: 0.99, perceptualPrecision: 0.98, scale: nil),
|
2022-12-15 15:22:39 +02:00
|
|
|
named: snapshotName,
|
2022-08-11 15:02:47 +03:00
|
|
|
testName: testName)
|
|
|
|
|
2022-10-17 09:56:17 +01:00
|
|
|
if let failure,
|
2022-08-11 15:02:47 +03:00
|
|
|
!failure.contains("No reference was found on disk."),
|
|
|
|
!failure.contains("to test against the newly-recorded snapshot") {
|
|
|
|
XCTFail(failure)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var testName: String {
|
2022-11-24 10:35:00 +02:00
|
|
|
languageCode + "-" + regionCode + "-" + deviceName
|
2022-08-11 15:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private var deviceName: String {
|
2023-02-14 16:25:24 +00:00
|
|
|
var name = UIDevice.current.name
|
|
|
|
|
|
|
|
// When running with parallel execution simulators are named "Clone 2 of iPhone 14" etc.
|
|
|
|
// Tidy this prefix out of the name to generate snapshots with the correct name.
|
|
|
|
if name.starts(with: "Clone "), let range = name.range(of: " of ") {
|
|
|
|
name = String(name[range.upperBound...])
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
2022-08-11 15:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private var languageCode: String {
|
2022-10-28 17:08:51 +03:00
|
|
|
Locale.current.language.languageCode?.identifier ?? ""
|
2022-08-11 15:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private var regionCode: String {
|
2022-10-28 17:08:51 +03:00
|
|
|
Locale.current.language.region?.identifier ?? ""
|
2022-08-11 15:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private var osVersion: String {
|
|
|
|
UIDevice.current.systemVersion.replacingOccurrences(of: ".", with: "-")
|
|
|
|
}
|
2022-04-13 17:19:11 +03:00
|
|
|
}
|
2023-01-05 16:35:20 +00:00
|
|
|
|
|
|
|
private extension UIImage {
|
|
|
|
/// Adjusts the image by cropping it with the given edge insets.
|
|
|
|
func inset(by insets: UIEdgeInsets) -> UIImage {
|
|
|
|
let insetRect = CGRect(origin: .zero, size: size).inset(by: insets)
|
|
|
|
let renderer = UIGraphicsImageRenderer(size: insetRect.size)
|
|
|
|
|
|
|
|
return renderer.image { _ in
|
|
|
|
draw(at: CGPoint(x: -insets.left, y: -insets.top))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|