Skip to content

Commit 5b5ce67

Browse files
committed
trigger ci
Update OneSignalNotificationsTests.swift
1 parent be40d3d commit 5b5ce67

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

iOS_SDK/OneSignalSDK/OneSignalNotificationsTests/OneSignalNotificationsTests.swift

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,75 @@ final class OneSignalNotificationsTests: XCTestCase {
4040
// Put teardown code here. This method is called after the invocation of each test method in the class.
4141
}
4242

43+
/// Helper to get cached badge count from OneSignalUserDefaults
44+
private func getCachedBadgeCount() -> Int {
45+
return OneSignalUserDefaults.initShared().getSavedInteger(forKey: "onesignalBadgeCount", defaultValue: 0)
46+
}
47+
48+
/// Helper to set badge count
49+
private func setBadgeCount(_ count: Int, completion: @escaping () -> Void = {}) {
50+
if #available(iOS 16.0, *) {
51+
UNUserNotificationCenter.current().setBadgeCount(count) { error in
52+
if let error = error {
53+
XCTFail("Failed to set badge count: \(error)")
54+
}
55+
completion()
56+
}
57+
} else {
58+
// Fallback for iOS 15 and earlier
59+
UIApplication.shared.applicationIconBadgeNumber = count
60+
completion()
61+
}
62+
}
63+
4364
func testClearBadgesWhenAppEntersForeground() throws {
4465
// NotificationManager Start to register lifecycle listener
4566
OSNotificationsManager.start()
4667
// Set badge count > 0
47-
UIApplication.shared.applicationIconBadgeNumber = 1
68+
let expectation = self.expectation(description: "Badge set")
69+
setBadgeCount(1) {
70+
expectation.fulfill()
71+
}
72+
wait(for: [expectation], timeout: 0.5)
73+
74+
// Verify badge was set
75+
XCTAssertEqual(getCachedBadgeCount(), 1)
76+
4877
// Then background the app
4978
OneSignalCoreMocks.backgroundApp()
5079
// Foreground the app
5180
OneSignalCoreMocks.foregroundApp()
81+
82+
// Wait for async badge clearing on iOS 16+
83+
Thread.sleep(forTimeInterval: 0.1)
84+
5285
// Ensure that badge count == 0
53-
XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 0)
86+
XCTAssertEqual(getCachedBadgeCount(), 0)
5487
}
5588

5689
func testDontclearBadgesWhenAppBecomesActive() throws {
5790
// NotificationManager Start to register lifecycle listener
5891
OSNotificationsManager.start()
5992
// Set badge count > 0
60-
UIApplication.shared.applicationIconBadgeNumber = 1
93+
let expectation = self.expectation(description: "Badge set")
94+
setBadgeCount(1) {
95+
expectation.fulfill()
96+
}
97+
wait(for: [expectation], timeout: 0.5)
98+
99+
// Verify badge was set
100+
XCTAssertEqual(getCachedBadgeCount(), 1)
101+
61102
// Then resign active
62103
OneSignalCoreMocks.resignActive()
63104
// App becomes active the app
64105
OneSignalCoreMocks.becomeActive()
106+
107+
// Wait for async badge clearing on iOS 16+
108+
Thread.sleep(forTimeInterval: 0.1)
109+
65110
// Ensure that badge count == 1
66-
XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 1)
111+
XCTAssertEqual(getCachedBadgeCount(), 1)
67112
}
68113

69114
func testUpdateNotificationTypesOnAppEntersForeground() throws {

0 commit comments

Comments
 (0)