FIRMessagingTokenRefreshTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // macOS requests a user password when accessing the Keychain for the first time,
  17. // so the tests may fail. Disable integration tests on macOS so far.
  18. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  19. #if !os(OSX)
  20. import FirebaseCore
  21. import FirebaseInstanceID
  22. import FirebaseMessaging
  23. import XCTest
  24. // This class should be only used once to ensure the test is independent
  25. class fakeAppDelegate: NSObject, MessagingDelegate {
  26. var messaging = Messaging.messaging()
  27. var delegateIsCalled = false
  28. func messaging(_: Messaging, didReceiveRegistrationToken _: String) {
  29. delegateIsCalled = true
  30. }
  31. }
  32. class FIRMessagingTokenRefreshTests: XCTestCase {
  33. var app: FirebaseApp!
  34. var instanceID: InstanceID?
  35. var messaging: Messaging?
  36. override class func setUp() {
  37. FirebaseApp.configure()
  38. }
  39. override func setUp() {
  40. instanceID = InstanceID.instanceID()
  41. messaging = Messaging.messaging()
  42. }
  43. override func tearDown() {
  44. instanceID = nil
  45. messaging = nil
  46. }
  47. func testDeleteTokenWithTokenRefreshDelegatesAndNotifications() {
  48. let expectation = self.expectation(description: "delegate method and notification are called")
  49. assertTokenWithAuthorizedEntity()
  50. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  51. .MessagingRegistrationTokenRefreshed,
  52. object: nil,
  53. handler: nil)
  54. let testDelegate = fakeAppDelegate()
  55. messaging?.delegate = testDelegate
  56. testDelegate.delegateIsCalled = false
  57. guard let messaging = self.messaging else {
  58. return
  59. }
  60. messaging.deleteFCMToken(forSenderID: tokenAuthorizedEntity(), completion: { error in
  61. XCTAssertNil(error)
  62. XCTAssertTrue(testDelegate.delegateIsCalled)
  63. expectation.fulfill()
  64. })
  65. wait(for: [expectation, notificationExpectation], timeout: 5)
  66. }
  67. // pragma mark - Helpers
  68. func assertTokenWithAuthorizedEntity() {
  69. let expectation = self.expectation(description: "tokenWithAuthorizedEntity")
  70. guard let messaging = self.messaging else {
  71. return
  72. }
  73. messaging.retrieveFCMToken(forSenderID: tokenAuthorizedEntity()) { token, error in
  74. XCTAssertNil(error)
  75. XCTAssertNotNil(token)
  76. expectation.fulfill()
  77. }
  78. wait(for: [expectation], timeout: 5)
  79. }
  80. func tokenAuthorizedEntity() -> String {
  81. guard let app = FirebaseApp.app() else {
  82. return ""
  83. }
  84. return app.options.gcmSenderID
  85. }
  86. }
  87. #endif // !TARGET_OS_OSX