Skip to content

Commit

Permalink
Add notifications unit tests for foregrounding the app
Browse files Browse the repository at this point in the history
  • Loading branch information
emawby committed Jun 20, 2024
1 parent 14beee9 commit 899541c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 31 deletions.
8 changes: 8 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@
DE971754274C48CF00FC409E /* OSPrivacyConsentController.h in Headers */ = {isa = PBXBuildFile; fileRef = DE971753274C48CF00FC409E /* OSPrivacyConsentController.h */; settings = {ATTRIBUTES = (Public, ); }; };
DE9717642756BCFB00FC409E /* OneSignalExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17F927026BA3002D3A5D /* OneSignalExtension.framework */; };
DE9717662756BCFD00FC409E /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
DE9776FF2C24DFE800ACB25F /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE9776FE2C24DFE800ACB25F /* UIKit.framework */; };
DE9777012C24DFF300ACB25F /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE9777002C24DFF300ACB25F /* UserNotifications.framework */; };
DEA4B44E2888AF8900E9FE12 /* OneSignalUNUserNotificationCenterOverrider.m in Sources */ = {isa = PBXBuildFile; fileRef = DEA4B44C2888AF8900E9FE12 /* OneSignalUNUserNotificationCenterOverrider.m */; };
DEA4B45A2888BFAB00E9FE12 /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
DEA4B45C2888C1D000E9FE12 /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
Expand Down Expand Up @@ -1485,6 +1487,8 @@
DE7D18DE2703B49B002D3A5D /* OSFocusRequests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSFocusRequests.m; sourceTree = "<group>"; };
DE971751274C48B700FC409E /* OSPrivacyConsentController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSPrivacyConsentController.m; sourceTree = "<group>"; };
DE971753274C48CF00FC409E /* OSPrivacyConsentController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSPrivacyConsentController.h; sourceTree = "<group>"; };
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; };
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; };
DE9877292591654600DE07D5 /* NSDateFormatter+OneSignal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+OneSignal.h"; sourceTree = "<group>"; };
DE98772A2591655800DE07D5 /* NSDateFormatter+OneSignal.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+OneSignal.m"; sourceTree = "<group>"; };
DEA4B44C2888AF8900E9FE12 /* OneSignalUNUserNotificationCenterOverrider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OneSignalUNUserNotificationCenterOverrider.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1763,7 +1767,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DE9777012C24DFF300ACB25F /* UserNotifications.framework in Frameworks */,
DEBA2A302C24DA5C00E234DB /* OneSignalCoreMocks.framework in Frameworks */,
DE9776FF2C24DFE800ACB25F /* UIKit.framework in Frameworks */,
DEBA2A2B2C24DA5800E234DB /* OneSignalCore.framework in Frameworks */,
DEBA2A1E2C20E35E00E234DB /* OneSignalNotifications.framework in Frameworks */,
);
Expand Down Expand Up @@ -1917,6 +1923,8 @@
37747F9519147D6500558FAD /* Frameworks */ = {
isa = PBXGroup;
children = (
DE9777002C24DFF300ACB25F /* UserNotifications.framework */,
DE9776FE2C24DFE800ACB25F /* UIKit.framework */,
DEFB3E622BB731BD00E65DAD /* ActivityKit.framework */,
3C7A39D42B7C18EE0082665E /* XCTest.framework */,
DEBAAE9A2A4222B000BF2C1C /* CoreGraphics.framework */,
Expand Down
44 changes: 44 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalCoreMocks/OneSignalCoreMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,48 @@ public class OneSignalCoreMocks: NSObject {
let expectation = XCTestExpectation(description: "Wait for \(seconds) seconds")
_ = XCTWaiter.wait(for: [expectation], timeout: seconds)
}

@objc public static func backgroundApp() {
if (OSBundleUtils.isAppUsingUIScene()) {
if #available(iOS 13.0, *) {
NotificationCenter.default.post(name: UIScene.willDeactivateNotification, object: nil)
NotificationCenter.default.post(name: UIScene.didEnterBackgroundNotification, object: nil)
}
} else {
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
}
}

@objc public static func foregroundApp() {
if (OSBundleUtils.isAppUsingUIScene()) {
if #available(iOS 13.0, *) {
NotificationCenter.default.post(name: UIScene.willEnterForegroundNotification, object: nil)
NotificationCenter.default.post(name: UIScene.didActivateNotification, object: nil)
}
} else {
NotificationCenter.default.post(name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
}
}

@objc public static func resignActive() {
if (OSBundleUtils.isAppUsingUIScene()) {
if #available(iOS 13.0, *) {
NotificationCenter.default.post(name: UIScene.willDeactivateNotification, object: nil)
}
} else {
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
}
}

@objc public static func becomeActive() {
if (OSBundleUtils.isAppUsingUIScene()) {
if #available(iOS 13.0, *) {
NotificationCenter.default.post(name: UIScene.didActivateNotification, object: nil)
}
} else {
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,78 @@
//

import XCTest
import OneSignalNotifications
import OneSignalCoreMocks
import UIKit

final class OneSignalNotificationsTests: XCTestCase {

var notifTypes: Int32 = 0
var token: String = ""

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

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

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
}
}

func testClearBadgesWhenAppEntersForeground() throws {
// NotificationManager Start? Or Mock NotificationManager start
// Mock receive a notification or have badge count > 0

// NotificationManager Start to register lifecycle listener
OSNotificationsManager.start()
// Set badge count > 0
UIApplication.shared.applicationIconBadgeNumber = 1
// Then background the app

OneSignalCoreMocks.backgroundApp()
// Foreground the app

OneSignalCoreMocks.foregroundApp()
// Ensure that badge count == 0
XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 0)
}

func testDontclearBadgesWhenAppBecomesActive() throws {
// NotificationManager Start? Or Mock NotificationManager start
// Mock receive a notification or have badge count > 0

// NotificationManager Start to register lifecycle listener
OSNotificationsManager.start()
// Set badge count > 0
UIApplication.shared.applicationIconBadgeNumber = 1
// Then resign active

OneSignalCoreMocks.resignActive()
// App becomes active the app

// Ensure that badge count == previous badge count
OneSignalCoreMocks.becomeActive()
// Ensure that badge count == 0
XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 1)
}

func testUpdateNotificationTypesOnAppEntersForeground() throws {
// NotificationManager Start? Or Mock NotificationManager start
// Deny notification permission
// NotificationManager Start to register lifecycle listener
OSNotificationsManager.start()

// Then background the app
OSNotificationsManager.delegate = self

// Change app notification permissions
XCTAssertEqual(self.notifTypes, 0)

// Then background the app
OneSignalCoreMocks.backgroundApp()

// Foreground the app for within 30 seconds
OneSignalCoreMocks.foregroundApp()

// Ensure that we update the notification types
// Ensure that the delegate is updated with the new notification type
XCTAssertEqual(self.notifTypes, ERROR_PUSH_NEVER_PROMPTED)
}


}

extension OneSignalNotificationsTests: OneSignalNotificationsDelegate {
public func setNotificationTypes(_ notificationTypes: Int32) {
self.notifTypes = notificationTypes
}

public func setPushToken(_ pushToken: String) {
self.token = pushToken
}
}

0 comments on commit 899541c

Please sign in to comment.