Skip to content

Commit 7e15877

Browse files
authored
[Sessions] Use Notifications for Initiator tests (#10576)
1 parent 442efb8 commit 7e15877

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

FirebaseSessions/Sources/SessionInitiator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ class SessionInitiator {
8989
#endif
9090
}
9191

92-
@objc func appBackgrounded() {
92+
@objc private func appBackgrounded() {
9393
backgroundTime = currentTime()
9494
}
9595

96-
@objc func appForegrounded() {
96+
@objc private func appForegrounded() {
9797
let interval = currentTime().timeIntervalSince(backgroundTime)
9898
if interval > sessionTimeout {
9999
initiateSessionStart()

FirebaseSessions/Tests/Unit/InitiatorTests.swift

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,58 @@
1515
import XCTest
1616
@testable import FirebaseSessions
1717

18+
#if os(iOS) || os(tvOS)
19+
import UIKit
20+
#elseif os(macOS)
21+
import Cocoa
22+
import AppKit
23+
#elseif os(watchOS)
24+
import WatchKit
25+
#endif
26+
1827
class InitiatorTests: XCTestCase {
1928
// 2021-11-01 @ 00:00:00 (EST)
2029
let date = Date(timeIntervalSince1970: 1_635_739_200)
2130

31+
func postBackgroundedNotification() {
32+
let notificationCenter = NotificationCenter.default
33+
#if os(iOS) || os(tvOS)
34+
notificationCenter.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
35+
#elseif os(macOS)
36+
notificationCenter.post(name: NSApplication.didResignActiveNotification, object: nil)
37+
#elseif os(watchOS)
38+
if #available(watchOSApplicationExtension 7.0, *) {
39+
notificationCenter.post(
40+
name: WKExtension.applicationDidEnterBackgroundNotification,
41+
object: nil
42+
)
43+
}
44+
#endif
45+
}
46+
47+
func postForegroundedNotification() {
48+
let notificationCenter = NotificationCenter.default
49+
#if os(iOS) || os(tvOS)
50+
notificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil)
51+
#elseif os(macOS)
52+
notificationCenter.post(name: NSApplication.didBecomeActiveNotification, object: nil)
53+
#elseif os(watchOS)
54+
if #available(watchOSApplicationExtension 7.0, *) {
55+
notificationCenter.post(
56+
name: WKExtension.applicationDidBecomeActiveNotification,
57+
object: nil
58+
)
59+
}
60+
#endif
61+
}
62+
2263
func test_beginListening_initiatesColdStart() throws {
2364
let initiator = SessionInitiator()
2465
var initiateCalled = false
2566
initiator.beginListening {
2667
initiateCalled = true
2768
}
28-
assert(initiateCalled)
69+
XCTAssert(initiateCalled)
2970
}
3071

3172
func test_appForegrounded_initiatesNewSession() throws {
@@ -36,24 +77,24 @@ class InitiatorTests: XCTestCase {
3677
initiator.beginListening {
3778
sessionCount += 1
3879
}
39-
assert(sessionCount == 1)
80+
XCTAssert(sessionCount == 1)
4081

4182
// When
4283
// Background, advance time by 30 minutes + 1 second, then foreground
43-
initiator.appBackgrounded()
84+
postBackgroundedNotification()
4485
pausedClock.addTimeInterval(30 * 60 + 1)
45-
initiator.appForegrounded()
86+
postForegroundedNotification()
4687
// Then
4788
// Session count increases because time spent in background > 30 minutes
48-
assert(sessionCount == 2)
89+
XCTAssert(sessionCount == 2)
4990

5091
// When
5192
// Background, advance time by exactly 30 minutes, then foreground
52-
initiator.appBackgrounded()
93+
postBackgroundedNotification()
5394
pausedClock.addTimeInterval(30 * 60)
54-
initiator.appForegrounded()
95+
postForegroundedNotification()
5596
// Then
5697
// Session count doesn't increase because time spent in background <= 30 minutes
57-
assert(sessionCount == 2)
98+
XCTAssert(sessionCount == 2)
5899
}
59100
}

0 commit comments

Comments
 (0)