Skip to content

Commit 5e9f089

Browse files
authored
Complete Swift API coverage tests for Messaging. (#9342)
* Complete Swift API coverage tests for Messaging. This does have two warnings, but keeping them enables us to get an actual break when the enum changes. * Update Swift dashboard
1 parent 37c273f commit 5e9f089

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

FirebaseMessaging/Tests/UnitTestsSwift/FIRMessagingAPITest.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,69 @@ import UserNotifications
2222
// exercised in the integration tests.
2323
func apis() {
2424
let messaging = Messaging.messaging()
25+
let delegate = CustomDelegate()
26+
messaging.delegate = delegate
27+
messaging.isAutoInitEnabled = false
28+
messaging.token(completion: { _, _ in
29+
})
30+
messaging.deleteToken { _ in
31+
}
32+
messaging.retrieveFCMToken(forSenderID: "fakeSenderID") { _, _ in
33+
}
34+
messaging.deleteData { _ in
35+
}
36+
messaging.deleteFCMToken(forSenderID: "fakeSenderID") { _ in
37+
}
2538

2639
if let _ = messaging.apnsToken {}
2740

2841
let apnsToken = Data()
2942
messaging.setAPNSToken(apnsToken, type: .prod)
3043

44+
// Use random to eliminate the warning that we're evaluating to a constant.
45+
let type: MessagingAPNSTokenType = Bool.random() ? .prod : .sandbox
46+
switch type {
47+
case .prod: ()
48+
case .sandbox: ()
49+
case .unknown: ()
50+
// Intentionally leave the warning for not handling unknown values as that's the only way this
51+
// will fail to compile if we add a case.
52+
}
53+
54+
// Use random to eliminate the warning that we're evaluating to a constant.
55+
let messagingError: MessagingError = Bool.random() ? .unknown : .authentication
56+
switch messagingError {
57+
case .unknown: ()
58+
case .authentication: ()
59+
case .noAccess: ()
60+
case .timeout: ()
61+
case .network: ()
62+
case .operationInProgress: ()
63+
case .invalidRequest: ()
64+
case .invalidTopicName: ()
65+
// Intentionally leave the warning for not handling unknown values as that's the only way this
66+
// will fail to compile if we add a case.
67+
}
68+
69+
// Use random to eliminate the warning that we're evaluating to a constant.
70+
let status: MessagingMessageStatus = Bool.random() ? .unknown : .new
71+
switch status {
72+
case .new: ()
73+
case .unknown: ()
74+
// Intentionally leave the warning for not handling unknown values as that's the only way this
75+
// will fail to compile if we add a case.
76+
}
77+
78+
// TODO: Mark the initializer as unavialable, as devs shouldn't be able to instantiate this.
79+
_ = MessagingMessageInfo().status
80+
81+
NotificationCenter.default.post(name: .MessagingRegistrationTokenRefreshed, object: nil)
82+
3183
let topic = "cat_video"
3284
messaging.subscribe(toTopic: topic)
3385
messaging.unsubscribe(fromTopic: topic)
86+
messaging.unsubscribe(fromTopic: topic) { _ in
87+
}
3488

3589
messaging.appDidReceiveMessage([:])
3690

@@ -43,6 +97,10 @@ func apis() {
4397
}
4498
}
4599

100+
class CustomDelegate: NSObject, MessagingDelegate {
101+
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {}
102+
}
103+
46104
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
47105
func apiAsync() async throws {
48106
let messaging = Messaging.messaging()

SwiftDashboard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tasks for additional Swift improvements.
1010
| | AB | An | ApC | ApD | Aut | Cor | Crs | DB | Fst | Fn | IAM | Ins | Msg | MLM | Prf | RC | Str |
1111
| :--- | :--- | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: |
1212
| **Swift Library** |||||||||| 1 ||||||||
13-
| **API Tests** |||||||| 3 | 2 || 2 || 2 | 2 ||||
13+
| **API Tests** |||||||| 3 | 2 || 2 || | 2 ||||
1414
| **async/await** || n/a |||||| 3 ||||||||||
1515
| **Swift Errors** ||||| 4 |||||||||||| 5 |
1616
| **Codable** | n/a | n/a | n/a |n/a | n/a | n/a |n/a ||| 1 | n/a | n/a || n/a | n/a ||n/a |

0 commit comments

Comments
 (0)