Skip to content

Commit 1a4b1a9

Browse files
committed
Add notifications unit tests for foregrounding the app
1 parent 14beee9 commit 1a4b1a9

File tree

3 files changed

+98
-31
lines changed

3 files changed

+98
-31
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@
454454
DE971754274C48CF00FC409E /* OSPrivacyConsentController.h in Headers */ = {isa = PBXBuildFile; fileRef = DE971753274C48CF00FC409E /* OSPrivacyConsentController.h */; settings = {ATTRIBUTES = (Public, ); }; };
455455
DE9717642756BCFB00FC409E /* OneSignalExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17F927026BA3002D3A5D /* OneSignalExtension.framework */; };
456456
DE9717662756BCFD00FC409E /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
457+
DE9776FF2C24DFE800ACB25F /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE9776FE2C24DFE800ACB25F /* UIKit.framework */; };
458+
DE9777012C24DFF300ACB25F /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE9777002C24DFF300ACB25F /* UserNotifications.framework */; };
457459
DEA4B44E2888AF8900E9FE12 /* OneSignalUNUserNotificationCenterOverrider.m in Sources */ = {isa = PBXBuildFile; fileRef = DEA4B44C2888AF8900E9FE12 /* OneSignalUNUserNotificationCenterOverrider.m */; };
458460
DEA4B45A2888BFAB00E9FE12 /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
459461
DEA4B45C2888C1D000E9FE12 /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
@@ -1485,6 +1487,8 @@
14851487
DE7D18DE2703B49B002D3A5D /* OSFocusRequests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSFocusRequests.m; sourceTree = "<group>"; };
14861488
DE971751274C48B700FC409E /* OSPrivacyConsentController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSPrivacyConsentController.m; sourceTree = "<group>"; };
14871489
DE971753274C48CF00FC409E /* OSPrivacyConsentController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSPrivacyConsentController.h; sourceTree = "<group>"; };
1490+
DE9776FE2C24DFE800ACB25F /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/System/iOSSupport/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
1491+
DE9777002C24DFF300ACB25F /* UserNotifications.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotifications.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/System/Library/Frameworks/UserNotifications.framework; sourceTree = DEVELOPER_DIR; };
14881492
DE9877292591654600DE07D5 /* NSDateFormatter+OneSignal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+OneSignal.h"; sourceTree = "<group>"; };
14891493
DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+OneSignal.m"; sourceTree = "<group>"; };
14901494
DEA4B44C2888AF8900E9FE12 /* OneSignalUNUserNotificationCenterOverrider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OneSignalUNUserNotificationCenterOverrider.m; sourceTree = "<group>"; };
@@ -1763,7 +1767,9 @@
17631767
isa = PBXFrameworksBuildPhase;
17641768
buildActionMask = 2147483647;
17651769
files = (
1770+
DE9777012C24DFF300ACB25F /* UserNotifications.framework in Frameworks */,
17661771
DEBA2A302C24DA5C00E234DB /* OneSignalCoreMocks.framework in Frameworks */,
1772+
DE9776FF2C24DFE800ACB25F /* UIKit.framework in Frameworks */,
17671773
DEBA2A2B2C24DA5800E234DB /* OneSignalCore.framework in Frameworks */,
17681774
DEBA2A1E2C20E35E00E234DB /* OneSignalNotifications.framework in Frameworks */,
17691775
);
@@ -1917,6 +1923,8 @@
19171923
37747F9519147D6500558FAD /* Frameworks */ = {
19181924
isa = PBXGroup;
19191925
children = (
1926+
DE9777002C24DFF300ACB25F /* UserNotifications.framework */,
1927+
DE9776FE2C24DFE800ACB25F /* UIKit.framework */,
19201928
DEFB3E622BB731BD00E65DAD /* ActivityKit.framework */,
19211929
3C7A39D42B7C18EE0082665E /* XCTest.framework */,
19221930
DEBAAE9A2A4222B000BF2C1C /* CoreGraphics.framework */,

iOS_SDK/OneSignalSDK/OneSignalCoreMocks/OneSignalCoreMocks.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,48 @@ public class OneSignalCoreMocks: NSObject {
5151
let expectation = XCTestExpectation(description: "Wait for \(seconds) seconds")
5252
_ = XCTWaiter.wait(for: [expectation], timeout: seconds)
5353
}
54+
55+
@objc public static func backgroundApp() {
56+
if (OSBundleUtils.isAppUsingUIScene()) {
57+
if #available(iOS 13.0, *) {
58+
NotificationCenter.default.post(name: UIScene.willDeactivateNotification, object: nil)
59+
NotificationCenter.default.post(name: UIScene.didEnterBackgroundNotification, object: nil)
60+
}
61+
} else {
62+
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
63+
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
64+
}
65+
}
66+
67+
@objc public static func foregroundApp() {
68+
if (OSBundleUtils.isAppUsingUIScene()) {
69+
if #available(iOS 13.0, *) {
70+
NotificationCenter.default.post(name: UIScene.willEnterForegroundNotification, object: nil)
71+
NotificationCenter.default.post(name: UIScene.didActivateNotification, object: nil)
72+
}
73+
} else {
74+
NotificationCenter.default.post(name: UIApplication.willEnterForegroundNotification, object: nil)
75+
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
76+
}
77+
}
78+
79+
@objc public static func resignActive() {
80+
if (OSBundleUtils.isAppUsingUIScene()) {
81+
if #available(iOS 13.0, *) {
82+
NotificationCenter.default.post(name: UIScene.willDeactivateNotification, object: nil)
83+
}
84+
} else {
85+
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
86+
}
87+
}
88+
89+
@objc public static func becomeActive() {
90+
if (OSBundleUtils.isAppUsingUIScene()) {
91+
if #available(iOS 13.0, *) {
92+
NotificationCenter.default.post(name: UIScene.didActivateNotification, object: nil)
93+
}
94+
} else {
95+
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
96+
}
97+
}
5498
}

iOS_SDK/OneSignalSDK/OneSignalNotificationsTests/OneSignalNotificationsTests.swift

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,81 @@
77
//
88

99
import XCTest
10+
import OneSignalNotifications
11+
import OneSignalCoreMocks
12+
import UIKit
1013

1114
final class OneSignalNotificationsTests: XCTestCase {
15+
16+
var notifTypes: Int32 = 0
17+
var token: String = ""
1218

1319
override func setUpWithError() throws {
1420
// Put setup code here. This method is called before the invocation of each test method in the class.
21+
self.notifTypes = 0
22+
self.token = ""
1523
}
1624

1725
override func tearDownWithError() throws {
1826
// Put teardown code here. This method is called after the invocation of each test method in the class.
1927
}
2028

21-
func testExample() throws {
22-
// This is an example of a functional test case.
23-
// Use XCTAssert and related functions to verify your tests produce the correct results.
24-
// Any test you write for XCTest can be annotated as throws and async.
25-
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
26-
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
27-
}
28-
29-
func testPerformanceExample() throws {
30-
// This is an example of a performance test case.
31-
measure {
32-
// Put the code you want to measure the time of here.
33-
}
34-
}
35-
3629
func testClearBadgesWhenAppEntersForeground() throws {
37-
// NotificationManager Start? Or Mock NotificationManager start
38-
// Mock receive a notification or have badge count > 0
39-
30+
// NotificationManager Start to register lifecycle listener
31+
OSNotificationsManager.start()
32+
// Set badge count > 0
33+
UIApplication.shared.applicationIconBadgeNumber = 1
4034
// Then background the app
41-
35+
OneSignalCoreMocks.backgroundApp()
4236
// Foreground the app
43-
37+
OneSignalCoreMocks.foregroundApp()
4438
// Ensure that badge count == 0
39+
XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 0)
4540
}
4641

4742
func testDontclearBadgesWhenAppBecomesActive() throws {
48-
// NotificationManager Start? Or Mock NotificationManager start
49-
// Mock receive a notification or have badge count > 0
50-
43+
// NotificationManager Start to register lifecycle listener
44+
OSNotificationsManager.start()
45+
// Set badge count > 0
46+
UIApplication.shared.applicationIconBadgeNumber = 1
5147
// Then resign active
52-
48+
OneSignalCoreMocks.resignActive()
5349
// App becomes active the app
54-
55-
// Ensure that badge count == previous badge count
50+
OneSignalCoreMocks.becomeActive()
51+
// Ensure that badge count == 0
52+
XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 1)
5653
}
5754

5855
func testUpdateNotificationTypesOnAppEntersForeground() throws {
59-
// NotificationManager Start? Or Mock NotificationManager start
60-
// Deny notification permission
56+
// NotificationManager Start to register lifecycle listener
57+
OSNotificationsManager.start()
6158

62-
// Then background the app
59+
OSNotificationsManager.delegate = self
6360

64-
// Change app notification permissions
61+
XCTAssertEqual(self.notifTypes, 0)
62+
63+
// Then background the app
64+
OneSignalCoreMocks.backgroundApp()
6565

6666
// Foreground the app for within 30 seconds
67+
OneSignalCoreMocks.foregroundApp()
6768

68-
// Ensure that we update the notification types
69+
// Ensure that the delegate is updated with the new notification type
70+
XCTAssertEqual(self.notifTypes, ERROR_PUSH_NEVER_PROMPTED)
6971
}
7072

7173

7274
}
75+
76+
extension OneSignalNotificationsTests: OneSignalNotificationsDelegate {
77+
// While we await app_id and privacy consent, these methods are a no-op
78+
// Once the UserManager is started in `init`, it calls these to set the state of the pushSubscriptionModel
79+
80+
public func setNotificationTypes(_ notificationTypes: Int32) {
81+
self.notifTypes = notificationTypes
82+
}
83+
84+
public func setPushToken(_ pushToken: String) {
85+
self.token = pushToken
86+
}
87+
}

0 commit comments

Comments
 (0)