Skip to content

Commit

Permalink
Replace Prefire with a very similar but simpler and more direct appro…
Browse files Browse the repository at this point in the history
…ach (#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`
  • Loading branch information
stefanceriu authored Sep 9, 2024
1 parent a4166de commit 2f6fea2
Show file tree
Hide file tree
Showing 1,196 changed files with 1,416 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UITests/Sources/__Snapshots__/** filter=lfs diff=lfs merge=lfs -text
UnitTests/Resources/** filter=lfs diff=lfs merge=lfs -text
UnitTests/__Snapshots__/** filter=lfs diff=lfs merge=lfs -text
PreviewTests/__Snapshots__/** filter=lfs diff=lfs merge=lfs -text
PreviewTests/Sources/__Snapshots__/** filter=lfs diff=lfs merge=lfs -text
95 changes: 46 additions & 49 deletions ElementX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "1b1fb14b1ac1438c44ffd2f0151af65c83018d71b8674cd7f13ec21abeb31bcd",
"originHash" : "52385e2a478cc9455693d0b93dc33b988ac1d4742acceeaee288944dff8b78e0",
"pins" : [
{
"identity" : "compound-design-tokens",
Expand Down
58 changes: 58 additions & 0 deletions ElementX/Sources/Other/Extensions/Snapshotting.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 1 addition & 3 deletions ElementX/Sources/Other/TestablePreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@

import SwiftUI

import Prefire

protocol TestablePreview: PrefireProvider { }
protocol TestablePreview { }
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions ElementX/Sources/Screens/HomeScreen/View/HomeScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct FormattedBodyText_Previews: PreviewProvider, TestablePreview {
.padding()
}
.previewLayout(.sizeThatFits)
.snapshot(delay: 0.25)
.snapshotPreferences(delay: 0.25)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 14 additions & 3 deletions ElementX/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,26 @@ targets:
else
echo "warning: SwiftGen not installed, download from https://github.com/SwiftGen/SwiftGen"
fi
- name: 🧙 Sourcery
- name: 🧙 Sourcery - Generate mocks
runOnlyWhenInstalling: false
basedOnDependencyAnalysis: false
shell: /bin/sh
script: |
export PATH="$PATH:/opt/homebrew/bin"
if which sourcery >/dev/null; then
sourcery --config Tools/Sourcery/sourcery_automockable_config.yml
sourcery --config Tools/Sourcery/AutoMockableConfig.yml
else
echo "warning: Sourcery not installed, run swift run tools setup-project"
fi
- name: 🧙 Sourcery - Generate preview tests
runOnlyWhenInstalling: false
basedOnDependencyAnalysis: false
shell: /bin/sh
script: |
export PATH="$PATH:/opt/homebrew/bin"
if which sourcery >/dev/null; then
sourcery --config Tools/Sourcery/PreviewTestsConfig.yml
else
echo "warning: Sourcery not installed, run swift run tools setup-project"
fi
Expand Down Expand Up @@ -202,7 +214,6 @@ targets:
- package: Version
- package: Emojibase
- package: WysiwygComposer
- package: Prefire
- package: SwiftOGG
- package: DSWaveformImage
product: DSWaveformImageViews
Expand Down
4 changes: 2 additions & 2 deletions NSE/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ targets:
- package: DeviceKit
- package: DTCoreText
- package: LRUCache
- package: Prefire
- package: Version

info:
Expand Down Expand Up @@ -76,7 +75,8 @@ targets:
- path: ../../ElementX/Sources/AppHooks/AppHooks.swift
- path: ../../ElementX/Sources/AppHooks/Hooks/ClientBuilderHook.swift
- path: ../../ElementX/Sources/Application/AppSettings.swift
- path: ../../ElementX/Sources/Generated
- path: ../../ElementX/Sources/Generated/Assets.swift
- path: ../../ElementX/Sources/Generated/Strings.swift
- path: ../../ElementX/Sources/Other/AvatarSize.swift
- path: ../../ElementX/Sources/Other/CurrentValuePublisher.swift
- path: ../../ElementX/Sources/Other/Extensions/AttributedString.swift
Expand Down
7 changes: 0 additions & 7 deletions PreviewTests/.prefire.yml

This file was deleted.

Loading

0 comments on commit 2f6fea2

Please sign in to comment.