FIRMessagingTokenRefreshTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 FirebaseMessaging
  22. import XCTest
  23. // This class should be only used once to ensure the test is independent
  24. class fakeAppDelegate: NSObject, MessagingDelegate {
  25. var messaging = Messaging.messaging()
  26. var delegateIsCalled = false
  27. func messaging(_: Messaging, didReceiveRegistrationToken _: String?) {
  28. delegateIsCalled = true
  29. }
  30. }
  31. class FIRMessagingTokenRefreshTests: XCTestCase {
  32. var app: FirebaseApp!
  33. var messaging: Messaging?
  34. override class func setUp() {
  35. FirebaseApp.configure()
  36. }
  37. override func setUp() {
  38. messaging = Messaging.messaging()
  39. }
  40. override func tearDown() {
  41. messaging = nil
  42. }
  43. func testDeleteTokenWithTokenRefreshDelegatesAndNotifications() {
  44. let expectation = self.expectation(description: "delegate method and notification are called")
  45. assertTokenWithAuthorizedEntity()
  46. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  47. .MessagingRegistrationTokenRefreshed,
  48. object: nil,
  49. handler: nil)
  50. let testDelegate = fakeAppDelegate()
  51. messaging?.delegate = testDelegate
  52. testDelegate.delegateIsCalled = false
  53. guard let messaging = self.messaging else {
  54. return
  55. }
  56. messaging.deleteFCMToken(forSenderID: tokenAuthorizedEntity(), completion: { error in
  57. XCTAssertNil(error)
  58. XCTAssertTrue(testDelegate.delegateIsCalled)
  59. expectation.fulfill()
  60. })
  61. wait(for: [expectation, notificationExpectation], timeout: 5)
  62. }
  63. func testDeleteDefaultTokenWithTokenRefreshDelegatesAndNotifications() {
  64. let expectation = self.expectation(description: "delegate method and notification are called")
  65. assertDefaultToken()
  66. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  67. .MessagingRegistrationTokenRefreshed,
  68. object: nil,
  69. handler: nil)
  70. let testDelegate = fakeAppDelegate()
  71. messaging?.delegate = testDelegate
  72. testDelegate.delegateIsCalled = false
  73. guard let messaging = self.messaging else {
  74. return
  75. }
  76. messaging.deleteToken { error in
  77. XCTAssertNil(error)
  78. XCTAssertTrue(testDelegate.delegateIsCalled)
  79. expectation.fulfill()
  80. }
  81. wait(for: [expectation, notificationExpectation], timeout: 5)
  82. }
  83. // pragma mark - Helpers
  84. func assertTokenWithAuthorizedEntity() {
  85. let expectation = self.expectation(description: "tokenWithAuthorizedEntity")
  86. guard let messaging = self.messaging else {
  87. return
  88. }
  89. messaging.retrieveFCMToken(forSenderID: tokenAuthorizedEntity()) { token, error in
  90. XCTAssertNil(error)
  91. XCTAssertNotNil(token)
  92. expectation.fulfill()
  93. }
  94. wait(for: [expectation], timeout: 5)
  95. }
  96. func assertDefaultToken() {
  97. let expectation = self.expectation(description: "getToken")
  98. guard let messaging = self.messaging else {
  99. return
  100. }
  101. messaging.token { token, error in
  102. XCTAssertNil(error)
  103. XCTAssertNotNil(token)
  104. expectation.fulfill()
  105. }
  106. wait(for: [expectation], timeout: 5)
  107. }
  108. func tokenAuthorizedEntity() -> String {
  109. guard let app = FirebaseApp.app() else {
  110. return ""
  111. }
  112. return app.options.gcmSenderID
  113. }
  114. }
  115. #endif // !TARGET_OS_OSX