Skip to content

Commit

Permalink
Merge branch 'main' into sam/remove-mock-appdependencyprovider-from-o…
Browse files Browse the repository at this point in the history
…mnibar-tests

* main:
  Update AutoClearSettingsViewController to use DI for app settings (#3448)
  • Loading branch information
samsymons committed Oct 18, 2024
2 parents e64907f + 6a93586 commit 01b8e2c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
12 changes: 11 additions & 1 deletion DuckDuckGo/AutoClearSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ class AutoClearSettingsViewController: UITableViewController {
@IBOutlet weak var clearDataToggle: UISwitch!
@IBOutlet var labels: [UILabel]!

private lazy var appSettings = AppDependencyProvider.shared.appSettings
private var appSettings: AppSettings
private var clearDataSettings: AutoClearSettingsModel?

init?(appSettings: AppSettings, coder: NSCoder) {
self.appSettings = appSettings
super.init(coder: coder)
}

@available(*, unavailable, renamed: "init(appSettings:coder:)")
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
9 changes: 7 additions & 2 deletions DuckDuckGo/SettingsLegacyViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ class SettingsLegacyViewProvider: ObservableObject {
var autoConsent: UIViewController { instantiate("AutoconsentSettingsViewController", fromStoryboard: "Settings") }
var unprotectedSites: UIViewController { instantiate("UnprotectedSites", fromStoryboard: "Settings") }
var fireproofSites: UIViewController { instantiate("FireProofSites", fromStoryboard: "Settings") }
var autoclearData: UIViewController { instantiate("AutoClearSettingsViewController", fromStoryboard: "Settings") }
var keyboard: UIViewController { instantiate("Keyboard", fromStoryboard: "Settings") }
var feedback: UIViewController { instantiate("Feedback", fromStoryboard: "Feedback") }

var autoclearData: UIViewController {
let storyboard = UIStoryboard(name: "Settings", bundle: nil)
return storyboard.instantiateViewController(identifier: "AutoClearSettingsViewController", creator: { coder in
return AutoClearSettingsViewController(appSettings: self.appSettings, coder: coder)
})
}

@MainActor
func syncSettings(source: String? = nil) -> SyncSettingsViewController {
return SyncSettingsViewController(syncService: self.syncService,
Expand Down
39 changes: 13 additions & 26 deletions DuckDuckGoTests/AutoClearSettingsScreenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,20 @@ import XCTest

class AutoClearSettingsScreenTests: XCTestCase {

var mockDependencyProvider: MockDependencyProvider!

override func setUp() {
super.setUp()

mockDependencyProvider = MockDependencyProvider()
AppDependencyProvider.shared = mockDependencyProvider
}

override func tearDown() {
super.tearDown()

AppDependencyProvider.shared = AppDependencyProvider.makeTestingInstance()
}

func testWhenOpeningSettingsThenClearDataToggleIsSetBasedOnAppSettings() {
let appSettigns = AppUserDefaults()
appSettigns.autoClearAction = []
if let settingsController = AutoClearSettingsViewController.loadFromStoryboard() {
let appSettings = AppUserDefaults()
appSettings.autoClearAction = []

if let settingsController = AutoClearSettingsViewController.loadFromStoryboard(appSettings: appSettings) {
settingsController.loadViewIfNeeded()
XCTAssertFalse(settingsController.clearDataToggle.isOn)
} else {
assertionFailure("Could not load View Controller")
}

appSettigns.autoClearAction = .clearData
if let settingsController = AutoClearSettingsViewController.loadFromStoryboard() {
appSettings.autoClearAction = .clearData

if let settingsController = AutoClearSettingsViewController.loadFromStoryboard(appSettings: appSettings) {
settingsController.loadViewIfNeeded()
XCTAssert(settingsController.clearDataToggle.isOn)
} else {
Expand All @@ -63,7 +48,7 @@ class AutoClearSettingsScreenTests: XCTestCase {
let appSettings = AppUserDefaults()
appSettings.autoClearAction = []

guard let settingsController = AutoClearSettingsViewController.loadFromStoryboard() else {
guard let settingsController = AutoClearSettingsViewController.loadFromStoryboard(appSettings: appSettings) else {
assertionFailure("Could not load View Controller")
return
}
Expand All @@ -85,9 +70,11 @@ class AutoClearSettingsScreenTests: XCTestCase {

private extension AutoClearSettingsViewController {

static func loadFromStoryboard() -> AutoClearSettingsViewController? {
let controller = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "AutoClearSettingsViewController")
return controller as? AutoClearSettingsViewController
static func loadFromStoryboard(appSettings: AppSettings) -> AutoClearSettingsViewController? {
let storyboard = UIStoryboard(name: "Settings", bundle: nil)
return storyboard.instantiateViewController(identifier: "AutoClearSettingsViewController", creator: { coder in
return AutoClearSettingsViewController(appSettings: appSettings, coder: coder)
})
}

}

0 comments on commit 01b8e2c

Please sign in to comment.