diff --git a/swift-sdk.xcodeproj/project.pbxproj b/swift-sdk.xcodeproj/project.pbxproj index 0d2b838f..d3e1062f 100644 --- a/swift-sdk.xcodeproj/project.pbxproj +++ b/swift-sdk.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 00B6FACE210E88ED007535CF /* prod-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FACD210E874D007535CF /* prod-1.mobileprovision */; }; 00B6FAD1210E8D90007535CF /* dev-1.mobileprovision in Resources */ = {isa = PBXBuildFile; fileRef = 00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */; }; 00CB31B621096129004ACDEC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CB31B4210960C4004ACDEC /* TestUtils.swift */; }; + 092D01942D3038F600E3066A /* NotificationObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 092D01932D3038F600E3066A /* NotificationObserverTests.swift */; }; 1CBFFE1A2A97AEEF00ED57EE /* EmbeddedManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CBFFE162A97AEEE00ED57EE /* EmbeddedManagerTests.swift */; }; 1CBFFE1B2A97AEEF00ED57EE /* EmbeddedMessagingProcessorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CBFFE172A97AEEE00ED57EE /* EmbeddedMessagingProcessorTests.swift */; }; 1CBFFE1C2A97AEEF00ED57EE /* EmbeddedSessionManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CBFFE182A97AEEE00ED57EE /* EmbeddedSessionManagerTests.swift */; }; @@ -543,6 +544,7 @@ 00B6FACD210E874D007535CF /* prod-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "prod-1.mobileprovision"; sourceTree = ""; }; 00B6FAD0210E8D90007535CF /* dev-1.mobileprovision */ = {isa = PBXFileReference; lastKnownFileType = file; path = "dev-1.mobileprovision"; sourceTree = ""; }; 00CB31B4210960C4004ACDEC /* TestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = ""; }; + 092D01932D3038F600E3066A /* NotificationObserverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationObserverTests.swift; sourceTree = ""; }; 1CBFFE162A97AEEE00ED57EE /* EmbeddedManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmbeddedManagerTests.swift; sourceTree = ""; }; 1CBFFE172A97AEEE00ED57EE /* EmbeddedMessagingProcessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmbeddedMessagingProcessorTests.swift; sourceTree = ""; }; 1CBFFE182A97AEEE00ED57EE /* EmbeddedSessionManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmbeddedSessionManagerTests.swift; sourceTree = ""; }; @@ -936,6 +938,7 @@ 552A0AA9280E249C00A80963 /* notification-tests */ = { isa = PBXGroup; children = ( + 092D01932D3038F600E3066A /* NotificationObserverTests.swift */, 55B37FC32297135F0042F13A /* NotificationMetadataTests.swift */, AC2C667F20D31B1F00D46CC9 /* NotificationResponseTests.swift */, ); @@ -2186,6 +2189,7 @@ 5588DFD128C0465E000697D7 /* MockAPNSTypeChecker.swift in Sources */, 00B6FACC210E8484007535CF /* APNSTypeCheckerTests.swift in Sources */, AC8F35A2239806B500302994 /* InboxViewControllerViewModelTests.swift in Sources */, + 092D01942D3038F600E3066A /* NotificationObserverTests.swift in Sources */, AC995F9A2166EEB50099A184 /* CommonMocks.swift in Sources */, 5588DFE128C046B7000697D7 /* MockLocalStorage.swift in Sources */, 1CBFFE1B2A97AEEF00ED57EE /* EmbeddedMessagingProcessorTests.swift in Sources */, diff --git a/tests/unit-tests/Mocks.swift b/tests/unit-tests/Mocks.swift index a76afc55..8e254ab5 100644 --- a/tests/unit-tests/Mocks.swift +++ b/tests/unit-tests/Mocks.swift @@ -10,19 +10,19 @@ import XCTest // Note: This is used only by swift tests. So can't put this in Common class MockNotificationStateProvider: NotificationStateProviderProtocol { - func isNotificationsEnabled(withCallback callback: @escaping (Bool) -> Void) { - callback(enabled) - } - - func registerForRemoteNotifications() { - expectation?.fulfill() - } + var enabled: Bool + private let expectation: XCTestExpectation? init(enabled: Bool, expectation: XCTestExpectation? = nil) { self.enabled = enabled self.expectation = expectation } - private let enabled: Bool - private let expectation: XCTestExpectation? + func isNotificationsEnabled(withCallback callback: @escaping (Bool) -> Void) { + callback(enabled) + } + + func registerForRemoteNotifications() { + expectation?.fulfill() + } } diff --git a/tests/unit-tests/NotificationObserverTests.swift b/tests/unit-tests/NotificationObserverTests.swift new file mode 100644 index 00000000..67109e3b --- /dev/null +++ b/tests/unit-tests/NotificationObserverTests.swift @@ -0,0 +1,44 @@ +import XCTest +@testable import IterableSDK + +class NotificationObserverTests: XCTestCase { + private var internalAPI: InternalIterableAPI! + private var mockNotificationStateProvider: MockNotificationStateProvider! + private var mockLocalStorage: MockLocalStorage! + private var mockNotificationCenter: MockNotificationCenter! + + override func setUp() { + super.setUp() + + mockNotificationStateProvider = MockNotificationStateProvider(enabled: false) + mockLocalStorage = MockLocalStorage() + mockNotificationCenter = MockNotificationCenter() + + let config = IterableConfig() + internalAPI = InternalIterableAPI.initializeForTesting( + config: config, + notificationStateProvider: mockNotificationStateProvider, + localStorage: mockLocalStorage, + notificationCenter: mockNotificationCenter + ) + } + + func testNotificationStateChangeUpdatesStorage() { + // Arrange + mockLocalStorage.isNotificationsEnabled = false + mockNotificationStateProvider.enabled = true + + // Act + mockNotificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil, userInfo: nil) + + // Small delay to allow async operation to complete + let expectation = XCTestExpectation(description: "Wait for state update") + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { + expectation.fulfill() + } + wait(for: [expectation], timeout: 1.0) + + // Assert + XCTAssertTrue(mockLocalStorage.isNotificationsEnabled) + } +}