From b3699de40fd1cb2249091fd62675528e8b08a51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20=C5=9Apiewak?= Date: Tue, 10 Sep 2024 14:59:43 +0200 Subject: [PATCH] Extract NTP-related persistence from AppUserDefaults --- DuckDuckGo.xcodeproj/project.pbxproj | 12 +++++ DuckDuckGo/AppSettings.swift | 6 --- DuckDuckGo/AppUserDefaults.swift | 15 ------ DuckDuckGo/NewTabPageIntroMessageSetup.swift | 10 ++-- DuckDuckGo/NewTabPageManager.swift | 19 ++++++-- DuckDuckGo/NewTabPageModel.swift | 16 +++---- DuckDuckGo/NewTabPageSectionsDebugView.swift | 26 +++++----- .../NewTabPageSectionsSettingsStorage.swift | 3 +- .../NewTabPageSettingsPersistentStorage.swift | 14 +++--- .../NewTabPageShortcutsSettingsStorage.swift | 3 +- DuckDuckGo/NewTabPageStorage.swift | 47 +++++++++++++++++++ .../NewTabPageIntroMessageSetupTests.swift | 21 +++++---- DuckDuckGoTests/NewTabPageModelTests.swift | 20 ++++---- ...NewTabPageSectionsSettingsModelTests.swift | 7 ++- ...abPageSettingsPersistentStorageTests.swift | 8 ++-- ...ewTabPageShortcutsSettingsModelTests.swift | 2 +- 16 files changed, 144 insertions(+), 85 deletions(-) create mode 100644 DuckDuckGo/NewTabPageStorage.swift diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index 6e7bb29455..bfcd8d6845 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -333,6 +333,7 @@ 6FD8E5202C5BA23200345670 /* NewTabPageModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FD8E51F2C5BA23200345670 /* NewTabPageModel.swift */; }; 6FD8E5222C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FD8E5212C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift */; }; 6FDA1FB32B59584400AC962A /* AddressDisplayHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDA1FB22B59584400AC962A /* AddressDisplayHelper.swift */; }; + 6FDC63FF2C906DD300DB71B3 /* NewTabPageStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDC63FE2C906DD300DB71B3 /* NewTabPageStorage.swift */; }; 6FE018402C25CB3F001F680D /* FavoritesSectionHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE0183F2C25CB3F001F680D /* FavoritesSectionHeader.swift */; }; 6FE095D82BD90AFB00490FF8 /* UniversalOmniBarState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE095D72BD90AFB00490FF8 /* UniversalOmniBarState.swift */; }; 6FE127382C20492500EB5724 /* NewTabPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE127372C20492500EB5724 /* NewTabPage.swift */; }; @@ -1590,6 +1591,7 @@ 6FD8E51F2C5BA23200345670 /* NewTabPageModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageModel.swift; sourceTree = ""; }; 6FD8E5212C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageIntroMessageSetup.swift; sourceTree = ""; }; 6FDA1FB22B59584400AC962A /* AddressDisplayHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressDisplayHelper.swift; sourceTree = ""; }; + 6FDC63FE2C906DD300DB71B3 /* NewTabPageStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageStorage.swift; sourceTree = ""; }; 6FE0183F2C25CB3F001F680D /* FavoritesSectionHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesSectionHeader.swift; sourceTree = ""; }; 6FE095D72BD90AFB00490FF8 /* UniversalOmniBarState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniversalOmniBarState.swift; sourceTree = ""; }; 6FE127372C20492500EB5724 /* NewTabPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPage.swift; sourceTree = ""; }; @@ -3873,9 +3875,18 @@ name = IntroMessage; sourceTree = ""; }; + 6FDC63FD2C906DBB00DB71B3 /* Storage */ = { + isa = PBXGroup; + children = ( + 6FDC63FE2C906DD300DB71B3 /* NewTabPageStorage.swift */, + ); + name = Storage; + sourceTree = ""; + }; 6FE127362C20436A00EB5724 /* HomeRedesign */ = { isa = PBXGroup; children = ( + 6FDC63FD2C906DBB00DB71B3 /* Storage */, 6FE1273B2C204C0D00EB5724 /* Subviews */, 6F03CAF82C32C3AA004179A8 /* Messages */, 6FE127372C20492500EB5724 /* NewTabPage.swift */, @@ -7411,6 +7422,7 @@ 85F2FFCF2211F8E5006BB258 /* TabSwitcherViewController+KeyCommands.swift in Sources */, 3157B43327F497E90042D3D7 /* SaveLoginView.swift in Sources */, F17922E01E71BB59006E3D97 /* AutocompleteViewControllerDelegate.swift in Sources */, + 6FDC63FF2C906DD300DB71B3 /* NewTabPageStorage.swift in Sources */, BDE91CDC2C62AA3A0005CB74 /* DefaultMetadataCollector.swift in Sources */, D664C7C82B289AA200CBFA76 /* SubscriptionFlowView.swift in Sources */, EE458D142ABB652900FC651A /* NetworkProtectionDebugUtilities.swift in Sources */, diff --git a/DuckDuckGo/AppSettings.swift b/DuckDuckGo/AppSettings.swift index c0613b3fd5..d3b99acc70 100644 --- a/DuckDuckGo/AppSettings.swift +++ b/DuckDuckGo/AppSettings.swift @@ -82,14 +82,8 @@ protocol AppSettings: AnyObject, AppDebugSettings { var duckPlayerMode: DuckPlayerMode { get set } var duckPlayerAskModeOverlayHidden: Bool { get set } - - var newTabPageShortcutsSettings: Data? { get set } - var newTabPageSectionsSettings: Data? { get set } - var newTabPageIntroMessageEnabled: Bool? { get set } - var newTabPageIntroMessageSeenCount: Int { get set } } protocol AppDebugSettings { - var newTabPageSectionsEnabled: Bool { get set } var onboardingHighlightsEnabled: Bool { get set } } diff --git a/DuckDuckGo/AppUserDefaults.swift b/DuckDuckGo/AppUserDefaults.swift index ceb2538043..2c54a3a749 100644 --- a/DuckDuckGo/AppUserDefaults.swift +++ b/DuckDuckGo/AppUserDefaults.swift @@ -383,9 +383,6 @@ public class AppUserDefaults: AppSettings { userDefaults?.setValue(newValue.rawValue, forKey: Keys.crashCollectionOptInStatus) } } - - @UserDefaultsWrapper(key: .debugNewTabPageSectionsEnabledKey, defaultValue: false) - var newTabPageSectionsEnabled: Bool var duckPlayerMode: DuckPlayerMode { get { @@ -418,18 +415,6 @@ public class AppUserDefaults: AppSettings { } } - @UserDefaultsWrapper(key: .newTabPageShortcutsSettings, defaultValue: nil) - var newTabPageShortcutsSettings: Data? - - @UserDefaultsWrapper(key: .newTabPageSectionsSettings, defaultValue: nil) - var newTabPageSectionsSettings: Data? - - @UserDefaultsWrapper(key: .newTabPageIntroMessageEnabled, defaultValue: nil) - var newTabPageIntroMessageEnabled: Bool? - - @UserDefaultsWrapper(key: .newTabPageIntroMessageSeenCount, defaultValue: 0) - var newTabPageIntroMessageSeenCount: Int - @UserDefaultsWrapper(key: .debugOnboardingHighlightsEnabledKey, defaultValue: false) var onboardingHighlightsEnabled: Bool } diff --git a/DuckDuckGo/NewTabPageIntroMessageSetup.swift b/DuckDuckGo/NewTabPageIntroMessageSetup.swift index 41010acdf5..5ee9a2df42 100644 --- a/DuckDuckGo/NewTabPageIntroMessageSetup.swift +++ b/DuckDuckGo/NewTabPageIntroMessageSetup.swift @@ -21,24 +21,24 @@ import BrowserServicesKit import Core struct NewTabPageIntroMessageSetup { - let appSettings: AppSettings + let storage: NewTabPageIntroDataStoring let statistics: StatisticsStore let newTabPageManager: NewTabPageManaging - init(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, + init(storage: NewTabPageIntroDataStoring = NewTabPageIntroDataUserDefaultsStorage(), statistics: StatisticsStore = StatisticsUserDefaults(), newTabPageManager: NewTabPageManaging = NewTabPageManager()) { - self.appSettings = appSettings + self.storage = storage self.statistics = statistics self.newTabPageManager = newTabPageManager } func perform() { - let isNotSetUp = appSettings.newTabPageIntroMessageEnabled == nil + let isNotSetUp = storage.newTabPageIntroMessageEnabled == nil guard newTabPageManager.isAvailableInPublicRelease && isNotSetUp else { return } // For new users we **don't** want intro message - appSettings.newTabPageIntroMessageEnabled = statistics.installDate != nil + storage.newTabPageIntroMessageEnabled = statistics.installDate != nil } } diff --git a/DuckDuckGo/NewTabPageManager.swift b/DuckDuckGo/NewTabPageManager.swift index 9723dd37d3..cc37391dad 100644 --- a/DuckDuckGo/NewTabPageManager.swift +++ b/DuckDuckGo/NewTabPageManager.swift @@ -31,15 +31,24 @@ protocol NewTabPageDebugging: NewTabPageManaging { var isFeatureFlagEnabled: Bool { get } } +protocol NewTabPageLocalFlagStoring: AnyObject { + var newTabPageSectionsEnabled: Bool { get set } +} + +final class NewTabPageLocalFlagUserDefaultsStorage: NewTabPageLocalFlagStoring { + @UserDefaultsWrapper(key: .debugNewTabPageSectionsEnabledKey, defaultValue: false) + var newTabPageSectionsEnabled: Bool +} + final class NewTabPageManager: NewTabPageManaging, NewTabPageDebugging { - var appDefaults: AppDebugSettings + let localFlagStorage: NewTabPageLocalFlagStoring let featureFlagger: FeatureFlagger - init(appDefaults: AppDebugSettings = AppDependencyProvider.shared.appSettings, + init(localFlagStorage: NewTabPageLocalFlagStoring = NewTabPageLocalFlagUserDefaultsStorage(), featureFlager: FeatureFlagger = AppDependencyProvider.shared.featureFlagger) { - self.appDefaults = appDefaults + self.localFlagStorage = localFlagStorage self.featureFlagger = featureFlager } @@ -62,10 +71,10 @@ final class NewTabPageManager: NewTabPageManaging, NewTabPageDebugging { var isLocalFlagEnabled: Bool { get { - appDefaults.newTabPageSectionsEnabled + localFlagStorage.newTabPageSectionsEnabled } set { - appDefaults.newTabPageSectionsEnabled = newValue + localFlagStorage.newTabPageSectionsEnabled = newValue } } diff --git a/DuckDuckGo/NewTabPageModel.swift b/DuckDuckGo/NewTabPageModel.swift index f9d826133b..8cd18b0d00 100644 --- a/DuckDuckGo/NewTabPageModel.swift +++ b/DuckDuckGo/NewTabPageModel.swift @@ -26,15 +26,15 @@ final class NewTabPageModel: ObservableObject { @Published private(set) var isOnboarding: Bool @Published var isShowingSettings: Bool - private let appSettings: AppSettings + private var introDataStorage: NewTabPageIntroDataStoring private let pixelFiring: PixelFiring.Type - init(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, + init(introDataStorage: NewTabPageIntroDataStoring = NewTabPageIntroDataUserDefaultsStorage(), pixelFiring: PixelFiring.Type = Pixel.self) { - self.appSettings = appSettings + self.introDataStorage = introDataStorage self.pixelFiring = pixelFiring - isIntroMessageVisible = appSettings.newTabPageIntroMessageEnabled ?? false + isIntroMessageVisible = introDataStorage.newTabPageIntroMessageEnabled ?? false isOnboarding = false isShowingSettings = false } @@ -42,16 +42,16 @@ final class NewTabPageModel: ObservableObject { func introMessageDisplayed() { pixelFiring.fire(.newTabPageMessageDisplayed, withAdditionalParameters: [:]) - appSettings.newTabPageIntroMessageSeenCount += 1 - if appSettings.newTabPageIntroMessageSeenCount >= 3 { - appSettings.newTabPageIntroMessageEnabled = false + introDataStorage.newTabPageIntroMessageSeenCount += 1 + if introDataStorage.newTabPageIntroMessageSeenCount >= 3 { + introDataStorage.newTabPageIntroMessageEnabled = false } } func dismissIntroMessage() { pixelFiring.fire(.newTabPageMessageDismissed, withAdditionalParameters: [:]) - appSettings.newTabPageIntroMessageEnabled = false + introDataStorage.newTabPageIntroMessageEnabled = false isIntroMessageVisible = false } diff --git a/DuckDuckGo/NewTabPageSectionsDebugView.swift b/DuckDuckGo/NewTabPageSectionsDebugView.swift index fc0f5e634a..79d9b08d74 100644 --- a/DuckDuckGo/NewTabPageSectionsDebugView.swift +++ b/DuckDuckGo/NewTabPageSectionsDebugView.swift @@ -22,8 +22,8 @@ import SwiftUI struct NewTabPageSectionsDebugView: View { private var newTabPageDebugging: NewTabPageDebugging - private var appSettings: AppSettings - + private let introDataStorage: NewTabPageIntroDataStoring + @State private var isFeatureEnabled: Bool @State private var introMessageCount: Int @State private var isIntroMessageInitialized: Bool @@ -34,24 +34,24 @@ struct NewTabPageSectionsDebugView: View { } set: { newTabPageDebugging.isLocalFlagEnabled = $0 isFeatureEnabled = newTabPageDebugging.isNewTabPageSectionsEnabled - isIntroMessageInitialized = appSettings.newTabPageIntroMessageEnabled != nil + isIntroMessageInitialized = introDataStorage.newTabPageIntroMessageEnabled != nil } } private var introMessageEnabled: Binding { Binding { - appSettings.newTabPageIntroMessageEnabled ?? false + introDataStorage.newTabPageIntroMessageEnabled ?? false } set: { - appSettings.newTabPageIntroMessageEnabled = $0 - isIntroMessageInitialized = appSettings.newTabPageIntroMessageEnabled != nil + introDataStorage.newTabPageIntroMessageEnabled = $0 + isIntroMessageInitialized = introDataStorage.newTabPageIntroMessageEnabled != nil } } private var introMessageCountBinding: Binding { Binding { - appSettings.newTabPageIntroMessageSeenCount + introDataStorage.newTabPageIntroMessageSeenCount } set: { - appSettings.newTabPageIntroMessageSeenCount = $0 + introDataStorage.newTabPageIntroMessageSeenCount = $0 introMessageCount = $0 } } @@ -60,10 +60,10 @@ struct NewTabPageSectionsDebugView: View { let manager = NewTabPageManager() newTabPageDebugging = manager isFeatureEnabled = manager.isNewTabPageSectionsEnabled - - appSettings = AppDependencyProvider.shared.appSettings - introMessageCount = appSettings.newTabPageIntroMessageSeenCount - isIntroMessageInitialized = appSettings.newTabPageIntroMessageEnabled != nil + + introDataStorage = NewTabPageIntroDataUserDefaultsStorage() + introMessageCount = introDataStorage.newTabPageIntroMessageSeenCount + isIntroMessageInitialized = introDataStorage.newTabPageIntroMessageEnabled != nil } var body: some View { @@ -134,7 +134,7 @@ struct NewTabPageSectionsDebugView: View { }) Button("Reset intro message", action: { - appSettings.newTabPageIntroMessageEnabled = nil + introDataStorage.newTabPageIntroMessageEnabled = nil introMessageCountBinding.wrappedValue = 0 isIntroMessageInitialized = false }) diff --git a/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift b/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift index 12f38c47e6..94d4a42aac 100644 --- a/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift +++ b/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift @@ -28,7 +28,8 @@ typealias NewTabPageSectionsSettingsStorage = NewTabPageSettingsPersistentStorag extension NewTabPageSettingsPersistentStorage { convenience init() { - self.init(keyPath: \.newTabPageSectionsSettings, + self.init(persistence: NewTabPageSettingsDataUserDefaultsStorage(), + keyPath: \.newTabPageSectionsSettings, defaultOrder: NewTabPageSection.allCases, defaultEnabledItems: NewTabPageSection.allCases) } diff --git a/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift b/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift index 9c746bccf6..48d249c124 100644 --- a/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift +++ b/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift @@ -28,14 +28,14 @@ final class NewTabPageSettingsPersistentStorage - private var appSettings: AppSettings - private let keyPath: WritableKeyPath + private var persistence: NewTabPageSettingsDataStoring + private let keyPath: WritableKeyPath - init(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, - keyPath: WritableKeyPath, + init(persistence: NewTabPageSettingsDataStoring, + keyPath: WritableKeyPath, defaultOrder: [Item], defaultEnabledItems: [Item]) { - self.appSettings = appSettings + self.persistence = persistence self.keyPath = keyPath self.itemsOrder = defaultOrder self.enabledItems = Set(defaultEnabledItems) @@ -62,12 +62,12 @@ final class NewTabPageSettingsPersistentStorage.self, from: settingsData) { itemsOrder = settings.itemsOrder enabledItems = settings.enabledItems diff --git a/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift b/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift index c769e31b44..7bdcb52710 100644 --- a/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift +++ b/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift @@ -23,7 +23,8 @@ typealias NewTabPageShortcutsSettingsStorage = NewTabPageSettingsPersistentStora extension NewTabPageSettingsPersistentStorage { convenience init() { - self.init(keyPath: \.newTabPageShortcutsSettings, + self.init(persistence: NewTabPageSettingsDataUserDefaultsStorage(), + keyPath: \.newTabPageShortcutsSettings, defaultOrder: NewTabPageShortcut.allCases, defaultEnabledItems: NewTabPageShortcut.allCases) } diff --git a/DuckDuckGo/NewTabPageStorage.swift b/DuckDuckGo/NewTabPageStorage.swift new file mode 100644 index 0000000000..2db10a3943 --- /dev/null +++ b/DuckDuckGo/NewTabPageStorage.swift @@ -0,0 +1,47 @@ +// +// NewTabPageStorage.swift +// DuckDuckGo +// +// Copyright © 2024 DuckDuckGo. All rights reserved. +// +// 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 Foundation +import Core + +protocol NewTabPageIntroDataStoring: AnyObject { + var newTabPageIntroMessageEnabled: Bool? { get set } + var newTabPageIntroMessageSeenCount: Int { get set } +} + +protocol NewTabPageSettingsDataStoring: AnyObject { + var newTabPageShortcutsSettings: Data? { get set } + var newTabPageSectionsSettings: Data? { get set } +} + +final class NewTabPageIntroDataUserDefaultsStorage: NewTabPageIntroDataStoring { + @UserDefaultsWrapper(key: .newTabPageIntroMessageEnabled, defaultValue: nil) + var newTabPageIntroMessageEnabled: Bool? + + @UserDefaultsWrapper(key: .newTabPageIntroMessageSeenCount, defaultValue: 0) + var newTabPageIntroMessageSeenCount: Int +} + +final class NewTabPageSettingsDataUserDefaultsStorage: NewTabPageSettingsDataStoring { + @UserDefaultsWrapper(key: .newTabPageShortcutsSettings, defaultValue: nil) + var newTabPageShortcutsSettings: Data? + + @UserDefaultsWrapper(key: .newTabPageSectionsSettings, defaultValue: nil) + var newTabPageSectionsSettings: Data? +} diff --git a/DuckDuckGoTests/NewTabPageIntroMessageSetupTests.swift b/DuckDuckGoTests/NewTabPageIntroMessageSetupTests.swift index 53780e2e9f..5cc797fa44 100644 --- a/DuckDuckGoTests/NewTabPageIntroMessageSetupTests.swift +++ b/DuckDuckGoTests/NewTabPageIntroMessageSetupTests.swift @@ -22,7 +22,7 @@ import XCTest final class NewTabPageIntroMessageSetupTests: XCTestCase { - private let appSettings = AppSettingsMock() + private let storage = NewTabPageIntroDataStoringMock() private let statistics = MockStatisticsStore() private let ntpManagerMock = NewTabPageManagerMock() @@ -32,7 +32,7 @@ final class NewTabPageIntroMessageSetupTests: XCTestCase { sut.perform() - XCTAssertEqual(appSettings.newTabPageIntroMessageEnabled, true) + XCTAssertEqual(storage.newTabPageIntroMessageEnabled, true) } func testDisablesFeatureForNewUser() { @@ -41,32 +41,32 @@ final class NewTabPageIntroMessageSetupTests: XCTestCase { sut.perform() - XCTAssertEqual(appSettings.newTabPageIntroMessageEnabled, false) + XCTAssertEqual(storage.newTabPageIntroMessageEnabled, false) } func testDoesNothingIfSetAlready() { let sut = createSUT() statistics.installDate = nil - appSettings.newTabPageIntroMessageEnabled = true + storage.newTabPageIntroMessageEnabled = true sut.perform() - XCTAssertEqual(appSettings.newTabPageIntroMessageEnabled, true) + XCTAssertEqual(storage.newTabPageIntroMessageEnabled, true) } func testDoesNothingIfNotPubliclyReleased() { let sut = createSUT() statistics.installDate = nil ntpManagerMock.isAvailableInPublicRelease = false - appSettings.newTabPageIntroMessageEnabled = nil + storage.newTabPageIntroMessageEnabled = nil sut.perform() - XCTAssertNil(appSettings.newTabPageIntroMessageEnabled) + XCTAssertNil(storage.newTabPageIntroMessageEnabled) } private func createSUT() -> NewTabPageIntroMessageSetup { - NewTabPageIntroMessageSetup(appSettings: appSettings, statistics: statistics, newTabPageManager: ntpManagerMock) + NewTabPageIntroMessageSetup(storage: storage, statistics: statistics, newTabPageManager: ntpManagerMock) } } @@ -74,3 +74,8 @@ private final class NewTabPageManagerMock: NewTabPageManaging { var isNewTabPageSectionsEnabled: Bool = true var isAvailableInPublicRelease: Bool = true } + +final class NewTabPageIntroDataStoringMock: NewTabPageIntroDataStoring { + var newTabPageIntroMessageEnabled: Bool? + var newTabPageIntroMessageSeenCount: Int = 0 +} diff --git a/DuckDuckGoTests/NewTabPageModelTests.swift b/DuckDuckGoTests/NewTabPageModelTests.swift index bf71f38f22..7025fed9c1 100644 --- a/DuckDuckGoTests/NewTabPageModelTests.swift +++ b/DuckDuckGoTests/NewTabPageModelTests.swift @@ -22,45 +22,45 @@ import XCTest final class NewTabPageModelTests: XCTestCase { - let appSettings = AppSettingsMock() + let introDataStorage = NewTabPageIntroDataStoringMock() override func tearDown() { PixelFiringMock.tearDown() } func testDoesNotShowIntroIfSettingUndefined() { - let sut = NewTabPageModel(appSettings: appSettings) + let sut = NewTabPageModel(introDataStorage: introDataStorage) XCTAssertFalse(sut.isIntroMessageVisible) } func testShowsIntroMessage() { - appSettings.newTabPageIntroMessageEnabled = true - let sut = NewTabPageModel(appSettings: appSettings) + introDataStorage.newTabPageIntroMessageEnabled = true + let sut = NewTabPageModel(introDataStorage: introDataStorage) XCTAssertTrue(sut.isIntroMessageVisible) } func testDisablesIntroMessageWhenDismissed() { - appSettings.newTabPageIntroMessageEnabled = true - let sut = NewTabPageModel(appSettings: appSettings) + introDataStorage.newTabPageIntroMessageEnabled = true + let sut = NewTabPageModel(introDataStorage: introDataStorage) sut.dismissIntroMessage() XCTAssertFalse(sut.isIntroMessageVisible) - XCTAssertEqual(appSettings.newTabPageIntroMessageEnabled, false) + XCTAssertEqual(introDataStorage.newTabPageIntroMessageEnabled, false) } func testDisablesIntroMessageAfterMultipleImpressions() { - appSettings.newTabPageIntroMessageEnabled = true - let sut = NewTabPageModel(appSettings: appSettings) + introDataStorage.newTabPageIntroMessageEnabled = true + let sut = NewTabPageModel(introDataStorage: introDataStorage) for _ in 1...3 { sut.introMessageDisplayed() } XCTAssertTrue(sut.isIntroMessageVisible) // We want to keep the message visible on last occurence - XCTAssertEqual(appSettings.newTabPageIntroMessageEnabled, false) + XCTAssertEqual(introDataStorage.newTabPageIntroMessageEnabled, false) } func testFiresPixelWhenIntroMessageDismissed() { diff --git a/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift b/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift index 4a7fa31fae..5837f39c63 100644 --- a/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift +++ b/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift @@ -60,7 +60,7 @@ final class NewTabPageSectionsSettingsModelTests: XCTestCase { private func createSUT() -> NewTabPageSectionsSettingsModel { let storage = NewTabPageSectionsSettingsStorage( - appSettings: AppSettingsMock(), + persistence: NewTabPageSettingsDataStoringMock(), keyPath: \.newTabPageSectionsSettings, defaultOrder: NewTabPageSection.allCases, defaultEnabledItems: NewTabPageSection.allCases @@ -69,3 +69,8 @@ final class NewTabPageSectionsSettingsModelTests: XCTestCase { return NewTabPageSectionsSettingsModel(storage: storage, pixelFiring: PixelFiringMock.self) } } + +final class NewTabPageSettingsDataStoringMock: NewTabPageSettingsDataStoring { + var newTabPageSectionsSettings: Data? + var newTabPageShortcutsSettings: Data? +} diff --git a/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift b/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift index 30f58cf733..6333f15784 100644 --- a/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift +++ b/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift @@ -23,7 +23,7 @@ import XCTest final class NewTabPageSettingsPersistentStorageTests: XCTestCase { - private var appSettings = AppSettingsMock() + private var settingsDataStorage = NewTabPageSettingsDataStoringMock() func testLoadsInitialStateFromDefaults() { let sut = createSUT() @@ -35,7 +35,7 @@ final class NewTabPageSettingsPersistentStorageTests: XCTestCase { func testUsesDefaultsIfDataCorrupted() { let sut = createSUT() - appSettings[keyPath: Constant.keyPath] = "Random data".data(using: .utf8) + settingsDataStorage[keyPath: Constant.keyPath] = "Random data".data(using: .utf8) XCTAssertEqual(sut.itemsOrder, Constant.defaultItems) XCTAssertEqual(sut.enabledItems, Constant.defaultItems) @@ -90,7 +90,7 @@ final class NewTabPageSettingsPersistentStorageTests: XCTestCase { } private func createSUT(defaultOrder: [StorageItem] = Constant.defaultItems, defaultEnabledItems: [StorageItem] = Constant.defaultItems) -> NewTabPageSettingsPersistentStorage { - NewTabPageSettingsPersistentStorage(appSettings: appSettings, + NewTabPageSettingsPersistentStorage(persistence: settingsDataStorage, keyPath: Constant.keyPath, defaultOrder: defaultOrder, defaultEnabledItems: defaultEnabledItems) @@ -98,7 +98,7 @@ final class NewTabPageSettingsPersistentStorageTests: XCTestCase { private enum Constant { static let defaultItems = [StorageItem.one, .two, .three] - static let keyPath = \AppSettings.newTabPageSectionsSettings + static let keyPath = \NewTabPageSettingsDataStoring.newTabPageSectionsSettings } } diff --git a/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift b/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift index 463dc48c07..78a775446c 100644 --- a/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift +++ b/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift @@ -54,7 +54,7 @@ final class NewTabPageShortcutsSettingsModelTests: XCTestCase { private func createSUT() -> NewTabPageShortcutsSettingsModel { let storage = NewTabPageShortcutsSettingsStorage( - appSettings: AppSettingsMock(), + persistence: NewTabPageSettingsDataStoringMock(), keyPath: \.newTabPageShortcutsSettings, defaultOrder: NewTabPageShortcut.allCases, defaultEnabledItems: NewTabPageShortcut.allCases