|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | import XCTest
|
| 10 | +import OneSignalNotifications |
| 11 | +import OneSignalCoreMocks |
| 12 | +import UIKit |
10 | 13 |
|
11 | 14 | final class OneSignalNotificationsTests: XCTestCase {
|
| 15 | + |
| 16 | + var notifTypes: Int32 = 0 |
| 17 | + var token: String = "" |
12 | 18 |
|
13 | 19 | override func setUpWithError() throws {
|
14 | 20 | // 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 = "" |
15 | 23 | }
|
16 | 24 |
|
17 | 25 | override func tearDownWithError() throws {
|
18 | 26 | // Put teardown code here. This method is called after the invocation of each test method in the class.
|
19 | 27 | }
|
20 | 28 |
|
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 |
| - |
36 | 29 | 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 |
40 | 34 | // Then background the app
|
41 |
| - |
| 35 | + OneSignalCoreMocks.backgroundApp() |
42 | 36 | // Foreground the app
|
43 |
| - |
| 37 | + OneSignalCoreMocks.foregroundApp() |
44 | 38 | // Ensure that badge count == 0
|
| 39 | + XCTAssertEqual(UIApplication.shared.applicationIconBadgeNumber, 0) |
45 | 40 | }
|
46 | 41 |
|
47 | 42 | 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 |
51 | 47 | // Then resign active
|
52 |
| - |
| 48 | + OneSignalCoreMocks.resignActive() |
53 | 49 | // 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) |
56 | 53 | }
|
57 | 54 |
|
58 | 55 | func testUpdateNotificationTypesOnAppEntersForeground() throws {
|
59 |
| - // NotificationManager Start? Or Mock NotificationManager start |
60 |
| - // Deny notification permission |
| 56 | + // NotificationManager Start to register lifecycle listener |
| 57 | + OSNotificationsManager.start() |
61 | 58 |
|
62 |
| - // Then background the app |
| 59 | + OSNotificationsManager.delegate = self |
63 | 60 |
|
64 |
| - // Change app notification permissions |
| 61 | + XCTAssertEqual(self.notifTypes, 0) |
| 62 | + |
| 63 | + // Then background the app |
| 64 | + OneSignalCoreMocks.backgroundApp() |
65 | 65 |
|
66 | 66 | // Foreground the app for within 30 seconds
|
| 67 | + OneSignalCoreMocks.foregroundApp() |
67 | 68 |
|
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) |
69 | 71 | }
|
70 | 72 |
|
71 | 73 |
|
72 | 74 | }
|
| 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