Kaynağa Gözat

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
Ryan Wilson 4 yıl önce
ebeveyn
işleme
5e9f08932c

+ 58 - 0
FirebaseMessaging/Tests/UnitTestsSwift/FIRMessagingAPITest.swift

@@ -22,15 +22,69 @@ import UserNotifications
 // exercised in the integration tests.
 func apis() {
   let messaging = Messaging.messaging()
+  let delegate = CustomDelegate()
+  messaging.delegate = delegate
+  messaging.isAutoInitEnabled = false
+  messaging.token(completion: { _, _ in
+  })
+  messaging.deleteToken { _ in
+  }
+  messaging.retrieveFCMToken(forSenderID: "fakeSenderID") { _, _ in
+  }
+  messaging.deleteData { _ in
+  }
+  messaging.deleteFCMToken(forSenderID: "fakeSenderID") { _ in
+  }
 
   if let _ = messaging.apnsToken {}
 
   let apnsToken = Data()
   messaging.setAPNSToken(apnsToken, type: .prod)
 
+  // Use random to eliminate the warning that we're evaluating to a constant.
+  let type: MessagingAPNSTokenType = Bool.random() ? .prod : .sandbox
+  switch type {
+  case .prod: ()
+  case .sandbox: ()
+  case .unknown: ()
+    // Intentionally leave the warning for not handling unknown values as that's the only way this
+    // will fail to compile if we add a case.
+  }
+
+  // Use random to eliminate the warning that we're evaluating to a constant.
+  let messagingError: MessagingError = Bool.random() ? .unknown : .authentication
+  switch messagingError {
+  case .unknown: ()
+  case .authentication: ()
+  case .noAccess: ()
+  case .timeout: ()
+  case .network: ()
+  case .operationInProgress: ()
+  case .invalidRequest: ()
+  case .invalidTopicName: ()
+    // Intentionally leave the warning for not handling unknown values as that's the only way this
+    // will fail to compile if we add a case.
+  }
+
+  // Use random to eliminate the warning that we're evaluating to a constant.
+  let status: MessagingMessageStatus = Bool.random() ? .unknown : .new
+  switch status {
+  case .new: ()
+  case .unknown: ()
+    // Intentionally leave the warning for not handling unknown values as that's the only way this
+    // will fail to compile if we add a case.
+  }
+
+  // TODO: Mark the initializer as unavialable, as devs shouldn't be able to instantiate this.
+  _ = MessagingMessageInfo().status
+
+  NotificationCenter.default.post(name: .MessagingRegistrationTokenRefreshed, object: nil)
+
   let topic = "cat_video"
   messaging.subscribe(toTopic: topic)
   messaging.unsubscribe(fromTopic: topic)
+  messaging.unsubscribe(fromTopic: topic) { _ in
+  }
 
   messaging.appDidReceiveMessage([:])
 
@@ -43,6 +97,10 @@ func apis() {
   }
 }
 
+class CustomDelegate: NSObject, MessagingDelegate {
+  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {}
+}
+
 @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
 func apiAsync() async throws {
   let messaging = Messaging.messaging()

+ 1 - 1
SwiftDashboard.md

@@ -10,7 +10,7 @@ tasks for additional Swift improvements.
 |                       | AB  | An     | ApC    | ApD    | Aut    | Cor    | Crs    | DB     | Fst    | Fn     | IAM    | Ins    | Msg    | MLM    | Prf    | RC     |    Str |
 |   :---                | :--- | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: | :----: |
 | **Swift Library**     | ❌   |   ✔   | ❌     |❌     | ❌     | ❌     | ❌      |  ✔     |  ✔    | 1      |  ✔     | ❌    | ❌     | ✔      | ❌     |  ✔    | ✔     |
-| **API Tests**         | ❌   |  ❌    |  ✔    |❌     | ✔      | ✔       | ❌     | 3      | 2     |  ✔     | 2      | ✔      | 2     | 2      | ❌    |  ✔     | ✔    |
+| **API Tests**         | ❌   |  ❌    |  ✔    |❌     | ✔      | ✔       | ❌     | 3      | 2     |  ✔     | 2      | ✔      |      | 2      | ❌    |  ✔     | ✔    |
 | **async/await**       | ❌   |  n/a   |  ✔    |❌     | ✔      |  ✔      | ❌     | 3     |  ✔     |  ✔     | ❌     | ❌    | ✔     | ❌    | ❌     |  ✔    | ✔    |
 | **Swift Errors**      |  ❌  |  ❌    | ❌    |❌     | 4      | ❌     | ❌     | ❌     | ❌    | ❌     | ❌     | ❌    | ❌     | ✔      | ❌     |  ✔   | 5   |
 | **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   |