15
15
import XCTest
16
16
@testable import FirebaseSessions
17
17
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
+
18
27
class InitiatorTests : XCTestCase {
19
28
// 2021-11-01 @ 00:00:00 (EST)
20
29
let date = Date ( timeIntervalSince1970: 1_635_739_200 )
21
30
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
+
22
63
func test_beginListening_initiatesColdStart( ) throws {
23
64
let initiator = SessionInitiator ( )
24
65
var initiateCalled = false
25
66
initiator. beginListening {
26
67
initiateCalled = true
27
68
}
28
- assert ( initiateCalled)
69
+ XCTAssert ( initiateCalled)
29
70
}
30
71
31
72
func test_appForegrounded_initiatesNewSession( ) throws {
@@ -36,24 +77,24 @@ class InitiatorTests: XCTestCase {
36
77
initiator. beginListening {
37
78
sessionCount += 1
38
79
}
39
- assert ( sessionCount == 1 )
80
+ XCTAssert ( sessionCount == 1 )
40
81
41
82
// When
42
83
// Background, advance time by 30 minutes + 1 second, then foreground
43
- initiator . appBackgrounded ( )
84
+ postBackgroundedNotification ( )
44
85
pausedClock. addTimeInterval ( 30 * 60 + 1 )
45
- initiator . appForegrounded ( )
86
+ postForegroundedNotification ( )
46
87
// Then
47
88
// Session count increases because time spent in background > 30 minutes
48
- assert ( sessionCount == 2 )
89
+ XCTAssert ( sessionCount == 2 )
49
90
50
91
// When
51
92
// Background, advance time by exactly 30 minutes, then foreground
52
- initiator . appBackgrounded ( )
93
+ postBackgroundedNotification ( )
53
94
pausedClock. addTimeInterval ( 30 * 60 )
54
- initiator . appForegrounded ( )
95
+ postForegroundedNotification ( )
55
96
// Then
56
97
// Session count doesn't increase because time spent in background <= 30 minutes
57
- assert ( sessionCount == 2 )
98
+ XCTAssert ( sessionCount == 2 )
58
99
}
59
100
}
0 commit comments