Replace Prefire with a very similar but simpler and more direct approach (#3232)

* Replace Prefire with a very similar but simpler and more direct approach

* Move PreviewTest snapshots to their new location (snapshotting default, mirrors UITests)

* Update acknowledgements

* Switch snapshot configuration extensions to a custom version

* Exclude snapshots from the PreviewTests target

* Fix acknowledgements

* Rename snapshotting preference keys

* Remove `skipPackagePluginValidation`
This commit is contained in:
Stefan Ceriu
2024-09-09 15:07:41 +03:00
committed by GitHub
parent a4166de502
commit 2f6fea29f3
1196 changed files with 1416 additions and 116 deletions
@@ -0,0 +1,58 @@
//
// Copyright 2024 New Vector Ltd
//
// 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.
//
import SwiftUI
public struct SnapshotDelayPreferenceKey: PreferenceKey {
public static var defaultValue: TimeInterval = 0.0
public static func reduce(value: inout TimeInterval, nextValue: () -> TimeInterval) {
value = nextValue()
}
}
public struct SnapshotPrecisionPreferenceKey: PreferenceKey {
public static var defaultValue: Float = 1.0
public static func reduce(value: inout Float, nextValue: () -> Float) {
value = nextValue()
}
}
public struct SnapshotPerceptualPrecisionPreferenceKey: PreferenceKey {
public static var defaultValue: Float = 1.0
public static func reduce(value: inout Float, nextValue: () -> Float) {
value = nextValue()
}
}
public extension SwiftUI.View {
/// Use this modifier when you want to apply snapshot-specific preferences,
/// like delay and precision, to the view.
/// These preferences can then be retrieved and used elsewhere in your view hierarchy.
///
/// - Parameters:
/// - delay: The delay time in seconds that you want to set as a preference to the View.
/// - precision: The percentage of pixels that must match.
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. 98-99% mimics the precision of the human eye.
@inlinable
func snapshotPreferences(delay: TimeInterval = .zero, precision: Float = 1.0, perceptualPrecision: Float = 1.0) -> some SwiftUI.View {
preference(key: SnapshotDelayPreferenceKey.self, value: delay)
.preference(key: SnapshotPrecisionPreferenceKey.self, value: precision)
.preference(key: SnapshotPerceptualPrecisionPreferenceKey.self, value: perceptualPrecision)
}
}
+1 -3
View File
@@ -7,6 +7,4 @@
import SwiftUI
import Prefire
protocol TestablePreview: PrefireProvider { }
protocol TestablePreview { }
@@ -86,7 +86,7 @@ struct EmojiPickerScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
EmojiPickerScreen(context: viewModel.context, selectedEmojis: ["😀", "😄"])
.previewDisplayName("Screen")
.snapshot(delay: 0.5)
.snapshotPreferences(delay: 0.5)
}
}
@@ -202,13 +202,13 @@ struct HomeScreen_Previews: PreviewProvider, TestablePreview {
HomeScreen(context: emptyViewModel.context)
}
.previewDisplayName("Empty")
.snapshot(delay: 4.0)
.snapshotPreferences(delay: 4.0)
NavigationStack {
HomeScreen(context: loadedViewModel.context)
}
.previewDisplayName("Loaded")
.snapshot(delay: 4.0)
.snapshotPreferences(delay: 4.0)
}
static func viewModel(_ mode: HomeScreenRoomListMode) -> HomeScreenViewModel {
@@ -111,25 +111,25 @@ struct JoinRoomScreen_Previews: PreviewProvider, TestablePreview {
JoinRoomScreen(context: unknownViewModel.context)
}
.previewDisplayName("Unknown")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
NavigationStack {
JoinRoomScreen(context: knockViewModel.context)
}
.previewDisplayName("Knock")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
NavigationStack {
JoinRoomScreen(context: joinViewModel.context)
}
.previewDisplayName("Join")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
NavigationStack {
JoinRoomScreen(context: inviteViewModel.context)
}
.previewDisplayName("Invite")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
static func makeViewModel(mode: JoinRoomScreenInteractionMode) -> JoinRoomScreenViewModel {
@@ -113,7 +113,7 @@ struct IdentityConfirmationScreen_Previews: PreviewProvider, TestablePreview {
NavigationStack {
IdentityConfirmationScreen(context: viewModel.context)
}
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
private static var viewModel: IdentityConfirmationScreenViewModel {
@@ -172,12 +172,12 @@ struct RoomDetailsEditScreen_Previews: PreviewProvider, TestablePreview {
RoomDetailsEditScreen(context: readOnlyViewModel.context)
}
.previewDisplayName("Read only")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
NavigationStack {
RoomDetailsEditScreen(context: editableViewModel.context)
}
.previewDisplayName("Editable")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
}
@@ -392,12 +392,12 @@ struct RoomDetailsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
RoomDetailsScreen(context: simpleRoomViewModel.context)
.previewDisplayName("Simple Room")
.snapshot(delay: 2)
.snapshotPreferences(delay: 2)
RoomDetailsScreen(context: dmRoomViewModel.context)
.previewDisplayName("DM Room")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
RoomDetailsScreen(context: genericRoomViewModel.context)
.previewDisplayName("Generic Room")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
}
@@ -98,6 +98,6 @@ struct RoomDirectorySearchScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
RoomDirectorySearchScreen(context: viewModel.context)
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
}
}
@@ -124,13 +124,13 @@ struct RoomMemberDetailsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
RoomMemberDetailsScreen(context: otherUserViewModel.context)
.previewDisplayName("Other User")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
RoomMemberDetailsScreen(context: accountOwnerViewModel.context)
.previewDisplayName("Account Owner")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
RoomMemberDetailsScreen(context: ignoredUserViewModel.context)
.previewDisplayName("Ignored User")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
static func makeViewModel(member: RoomMemberProxyMock) -> RoomMemberDetailsScreenViewModel {
@@ -72,13 +72,13 @@ struct RoomMembersListManageMemberSheet_Previews: PreviewProvider, TestablePrevi
actions: [.kick, .ban],
context: viewModel.context)
.previewDisplayName("Joined")
.snapshot(delay: 0.2)
.snapshotPreferences(delay: 0.2)
RoomMembersListManageMemberSheet(member: .init(withProxy: RoomMemberProxyMock.mockBanned[3]),
actions: [],
context: viewModel.context)
.previewDisplayName("Banned")
.snapshot(delay: 0.2)
.snapshotPreferences(delay: 0.2)
}
}
@@ -117,31 +117,31 @@ struct RoomMembersListScreen_Previews: PreviewProvider, TestablePreview {
NavigationStack {
RoomMembersListScreen(context: viewModel.context)
}
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
.previewDisplayName("Member")
NavigationStack {
RoomMembersListScreen(context: invitesViewModel.context)
}
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
.previewDisplayName("Invites")
NavigationStack {
RoomMembersListScreen(context: adminViewModel.context)
}
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
.previewDisplayName("Admin: Members")
NavigationStack {
RoomMembersListScreen(context: bannedViewModel.context)
}
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
.previewDisplayName("Admin: Banned")
NavigationStack {
RoomMembersListScreen(context: emptyBannedViewModel.context)
}
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
.previewDisplayName("Admin: Empty Banned")
}
@@ -151,12 +151,12 @@ struct RoomPollsHistoryScreen_Previews: PreviewProvider, TestablePreview {
RoomPollsHistoryScreen(context: viewModelEmpty.context)
}
.previewDisplayName("No polls")
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
NavigationStack {
RoomPollsHistoryScreen(context: viewModel.context)
}
.previewDisplayName("polls")
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
}
}
@@ -148,7 +148,7 @@ struct SecureBackupScreen_Previews: PreviewProvider, TestablePreview {
}
.previewDisplayName("Recovery incomplete")
}
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
}
static func viewModel(keyBackupState: SecureBackupKeyBackupState,
@@ -253,9 +253,9 @@ struct NotificationSettingsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
NotificationSettingsScreen(context: viewModel.context)
.snapshot(delay: 2.0)
.snapshotPreferences(delay: 2.0)
NotificationSettingsScreen(context: viewModelConfigurationMismatch.context)
.snapshot(delay: 2.0)
.snapshotPreferences(delay: 2.0)
.previewDisplayName("Configuration mismatch")
}
}
@@ -224,7 +224,7 @@ struct SettingsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
NavigationStack {
SettingsScreen(context: viewModel.context)
.snapshot(delay: 1.0)
.snapshotPreferences(delay: 1.0)
}
}
}
@@ -317,7 +317,7 @@ struct TimelineReplyView_Previews: PreviewProvider, TestablePreview {
.padding()
.environmentObject(viewModel.context)
// Allow member names to load. Reduce precission as the `imageSource` randomly renders slightly differently
.snapshot(delay: 0.2, precision: 0.98)
.snapshotPreferences(delay: 0.2, precision: 0.98)
.previewLayout(.sizeThatFits)
}
}
@@ -205,7 +205,7 @@ struct FormattedBodyText_Previews: PreviewProvider, TestablePreview {
.padding()
}
.previewLayout(.sizeThatFits)
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
}
@@ -97,10 +97,10 @@ struct UserProfileScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
UserProfileScreen(context: otherUserViewModel.context)
.previewDisplayName("Other User")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
UserProfileScreen(context: accountOwnerViewModel.context)
.previewDisplayName("Account Owner")
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
static func makeViewModel(userID: String) -> UserProfileScreenViewModel {