From 9478841dffc8f9c7a1a2720deb7a3669b4c6af18 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Fri, 20 Sep 2024 09:21:41 +0200 Subject: [PATCH] Makes AppDependencyProvider non-instantiatable (#3373) Task/Issue URL: https://app.asana.com/0/414235014887631/1208353288582614/f ## Description This is a follow up to [NetworkProtectionTunnelController leaking](https://app.asana.com/0/inbox/1203108348814444/1208331423616717/1208348963925370) We want to make AppDependencyProvider non instantiatable, since it's really not meant to be. --- DuckDuckGo/AppDependencyProvider.swift | 12 +++++++++--- DuckDuckGoTests/AutoClearSettingsScreenTests.swift | 2 +- DuckDuckGoTests/LargeOmniBarStateTests.swift | 2 +- DuckDuckGoTests/MockDependencyProvider.swift | 2 +- DuckDuckGoTests/SmallOmniBarStateTests.swift | 2 +- ...ubscriptionPagesUseSubscriptionFeatureTests.swift | 2 +- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/DuckDuckGo/AppDependencyProvider.swift b/DuckDuckGo/AppDependencyProvider.swift index e0840dde47..c25532703b 100644 --- a/DuckDuckGo/AppDependencyProvider.swift +++ b/DuckDuckGo/AppDependencyProvider.swift @@ -54,7 +54,7 @@ protocol DependencyProvider { /// Provides dependencies for objects that are not directly instantiated /// through `init` call (e.g. ViewControllers created from Storyboards). -class AppDependencyProvider: DependencyProvider { +final class AppDependencyProvider: DependencyProvider { static var shared: DependencyProvider = AppDependencyProvider() @@ -89,12 +89,12 @@ class AppDependencyProvider: DependencyProvider { let networkProtectionTunnelController: NetworkProtectionTunnelController let subscriptionAppGroup = Bundle.main.appGroup(bundle: .subs) - + let connectionObserver: ConnectionStatusObserver = ConnectionStatusObserverThroughSession() let serverInfoObserver: ConnectionServerInfoObserver = ConnectionServerInfoObserverThroughSession() let vpnSettings = VPNSettings(defaults: .networkProtectionGroupDefaults) - init() { + private init() { featureFlagger = DefaultFeatureFlagger(internalUserDecider: internalUserDecider, privacyConfigManager: ContentBlocking.shared.privacyConfigurationManager) @@ -144,4 +144,10 @@ class AppDependencyProvider: DependencyProvider { vpnFeatureVisibility = DefaultNetworkProtectionVisibility(userDefaults: .networkProtectionGroupDefaults, accountManager: accountManager) } + + /// Only meant to be used for testing. + /// + static func makeTestingInstance() -> Self { + Self.init() + } } diff --git a/DuckDuckGoTests/AutoClearSettingsScreenTests.swift b/DuckDuckGoTests/AutoClearSettingsScreenTests.swift index e4ce725474..95d056cf4a 100644 --- a/DuckDuckGoTests/AutoClearSettingsScreenTests.swift +++ b/DuckDuckGoTests/AutoClearSettingsScreenTests.swift @@ -35,7 +35,7 @@ class AutoClearSettingsScreenTests: XCTestCase { override func tearDown() { super.tearDown() - AppDependencyProvider.shared = AppDependencyProvider() + AppDependencyProvider.shared = AppDependencyProvider.makeTestingInstance() } func testWhenOpeningSettingsThenClearDataToggleIsSetBasedOnAppSettings() { diff --git a/DuckDuckGoTests/LargeOmniBarStateTests.swift b/DuckDuckGoTests/LargeOmniBarStateTests.swift index 0df7041e5b..d237d7ff96 100644 --- a/DuckDuckGoTests/LargeOmniBarStateTests.swift +++ b/DuckDuckGoTests/LargeOmniBarStateTests.swift @@ -36,7 +36,7 @@ class LargeOmniBarStateTests: XCTestCase { override func tearDown() { super.tearDown() - AppDependencyProvider.shared = AppDependencyProvider() + AppDependencyProvider.shared = AppDependencyProvider.makeTestingInstance() } func testWhenInHomeEmptyEditingStateWithoutVoiceSearchThenCorrectButtonsAreShown() { diff --git a/DuckDuckGoTests/MockDependencyProvider.swift b/DuckDuckGoTests/MockDependencyProvider.swift index f0df5f6a4a..a1e7a7e5bd 100644 --- a/DuckDuckGoTests/MockDependencyProvider.swift +++ b/DuckDuckGoTests/MockDependencyProvider.swift @@ -51,7 +51,7 @@ class MockDependencyProvider: DependencyProvider { var vpnSettings: NetworkProtection.VPNSettings init() { - let defaultProvider = AppDependencyProvider() + let defaultProvider = AppDependencyProvider.makeTestingInstance() appSettings = defaultProvider.appSettings variantManager = defaultProvider.variantManager featureFlagger = defaultProvider.featureFlagger diff --git a/DuckDuckGoTests/SmallOmniBarStateTests.swift b/DuckDuckGoTests/SmallOmniBarStateTests.swift index d7b2f0d3e1..8eb0ceb1ef 100644 --- a/DuckDuckGoTests/SmallOmniBarStateTests.swift +++ b/DuckDuckGoTests/SmallOmniBarStateTests.swift @@ -36,7 +36,7 @@ class SmallOmniBarStateTests: XCTestCase { override func tearDown() { super.tearDown() - AppDependencyProvider.shared = AppDependencyProvider() + AppDependencyProvider.shared = AppDependencyProvider.makeTestingInstance() } func testWhenInHomeEmptyEditingStateWithoutVoiceSearchThenCorrectButtonsAreShown() { diff --git a/DuckDuckGoTests/Subscription/SubscriptionPagesUseSubscriptionFeatureTests.swift b/DuckDuckGoTests/Subscription/SubscriptionPagesUseSubscriptionFeatureTests.swift index a3b80166f4..d51d38feca 100644 --- a/DuckDuckGoTests/Subscription/SubscriptionPagesUseSubscriptionFeatureTests.swift +++ b/DuckDuckGoTests/Subscription/SubscriptionPagesUseSubscriptionFeatureTests.swift @@ -169,7 +169,7 @@ final class SubscriptionPagesUseSubscriptionFeatureTests: XCTestCase { pixelsFired.removeAll() HTTPStubs.removeAllStubs() - AppDependencyProvider.shared = AppDependencyProvider() + AppDependencyProvider.shared = AppDependencyProvider.makeTestingInstance() subscriptionService = nil authService = nil