Skip to content

Commit

Permalink
Add more settings tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bunn committed Dec 6, 2024
1 parent 194eb2e commit e12b0a6
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions DuckDuckGoTests/AIChat/AIChatSettingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ class AIChatSettingsTests: XCTestCase {
private var mockPrivacyConfigurationManager: PrivacyConfigurationManagerMock!
private var mockInternalUserDecider: MockInternalUserDecider!
private var mockUserDefaults: UserDefaults!
private var mockNotificationCenter: NotificationCenter!

override func setUp() {
super.setUp()
mockPrivacyConfigurationManager = PrivacyConfigurationManagerMock()
mockInternalUserDecider = MockInternalUserDecider()
mockUserDefaults = UserDefaults(suiteName: "TestDefaults")
mockNotificationCenter = NotificationCenter()
}

override func tearDown() {
mockUserDefaults.removePersistentDomain(forName: "TestDefaults")
mockPrivacyConfigurationManager = nil
mockInternalUserDecider = nil
mockUserDefaults = nil
mockNotificationCenter = nil
super.tearDown()
}

func testAIChatURLReturnsDefaultWhenRemoteSettingsMissing() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults)
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

(mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.settings = [:]

Expand All @@ -58,7 +62,8 @@ class AIChatSettingsTests: XCTestCase {
func testAIChatURLReturnsRemoteSettingWhenAvailable() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults)
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

let remoteURL = "https://example.com/ai-chat"
(mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.settings = [
Expand All @@ -71,7 +76,8 @@ class AIChatSettingsTests: XCTestCase {
func testIsAIChatFeatureEnabledWhenFeatureIsEnabled() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults)
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

(mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.enabledFeaturesForVersions = [
.aiChat: [AppVersionProvider().appVersion() ?? ""]
Expand All @@ -83,7 +89,8 @@ class AIChatSettingsTests: XCTestCase {
func testIsAIChatFeatureEnabledForInternalUser() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults)
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

mockInternalUserDecider.mockIsInternalUser = true
XCTAssertTrue(settings.isAIChatFeatureEnabled)
Expand All @@ -92,7 +99,8 @@ class AIChatSettingsTests: XCTestCase {
func testEnableAIChatBrowsingMenuUserSettings() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults)
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

(mockPrivacyConfigurationManager.privacyConfig as? PrivacyConfigurationMock)?.enabledFeaturesForVersions = [
.aiChat: [AppVersionProvider().appVersion() ?? ""]
Expand All @@ -107,8 +115,37 @@ class AIChatSettingsTests: XCTestCase {
settings.enableAIChatBrowsingMenuUserSettings(enable: true)
XCTAssertTrue(settings.isAIChatBrowsingToolbarShortcutFeatureEnabled)
}
}

func testEnableAIChatAddressBarUserSettings() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

settings.enableAIChatAddressBarUserSettings(enable: false)
XCTAssertFalse(settings.isAIChatAddressBarUserSettingsEnabled)

settings.enableAIChatAddressBarUserSettings(enable: true)
XCTAssertTrue(settings.isAIChatAddressBarUserSettingsEnabled)
}

func testNotificationPostedWhenSettingsChange() {
let settings = AIChatSettings(privacyConfigurationManager: mockPrivacyConfigurationManager,
internalUserDecider: mockInternalUserDecider,
userDefaults: mockUserDefaults,
notificationCenter: mockNotificationCenter)

let expectation = self.expectation(description: "Notification should be posted")

let observer = mockNotificationCenter.addObserver(forName: .aiChatSettingsChanged, object: nil, queue: nil) { _ in
expectation.fulfill()
}

settings.enableAIChatBrowsingMenuUserSettings(enable: false)
waitForExpectations(timeout: 1, handler: nil)
mockNotificationCenter.removeObserver(observer)
}
}

final private class MockInternalUserDecider: InternalUserDecider {
var mockIsInternalUser: Bool = false
Expand Down

0 comments on commit e12b0a6

Please sign in to comment.