FIRMessagingTokenRefreshTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. if FirebaseApp.app() == nil {
  36. FirebaseApp.configure()
  37. }
  38. }
  39. override func setUpWithError() throws {
  40. messaging = try XCTUnwrap(Messaging.messaging())
  41. }
  42. override func tearDown() {
  43. messaging = nil
  44. }
  45. func testDeleteTokenWithTokenRefreshDelegatesAndNotifications() {
  46. let expectation = self.expectation(description: "delegate method and notification are called")
  47. assertTokenWithAuthorizedEntity()
  48. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  49. .MessagingRegistrationTokenRefreshed,
  50. object: nil,
  51. handler: nil)
  52. let testDelegate = fakeAppDelegate()
  53. messaging.delegate = testDelegate
  54. testDelegate.delegateIsCalled = false
  55. messaging.deleteFCMToken(forSenderID: tokenAuthorizedEntity(), completion: { error in
  56. XCTAssertNil(error)
  57. XCTAssertTrue(testDelegate.delegateIsCalled)
  58. expectation.fulfill()
  59. })
  60. wait(for: [expectation, notificationExpectation], timeout: 5)
  61. }
  62. func testDeleteDefaultTokenWithTokenRefreshDelegatesAndNotifications() {
  63. let expectation = self.expectation(description: "delegate method and notification are called")
  64. assertDefaultToken()
  65. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  66. .MessagingRegistrationTokenRefreshed,
  67. object: nil,
  68. handler: nil)
  69. let testDelegate = fakeAppDelegate()
  70. messaging?.delegate = testDelegate
  71. testDelegate.delegateIsCalled = false
  72. messaging.deleteToken { error in
  73. XCTAssertNil(error)
  74. XCTAssertTrue(testDelegate.delegateIsCalled)
  75. expectation.fulfill()
  76. }
  77. wait(for: [expectation, notificationExpectation], timeout: 5)
  78. }
  79. func testDeleteDataWithTokenRefreshDelegatesAndNotifications() {
  80. let expectation = self.expectation(description: "delegate method and notification are called")
  81. assertDefaultToken()
  82. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  83. .MessagingRegistrationTokenRefreshed,
  84. object: nil,
  85. handler: nil)
  86. let testDelegate = fakeAppDelegate()
  87. messaging?.delegate = testDelegate
  88. testDelegate.delegateIsCalled = false
  89. messaging.deleteData { error in
  90. XCTAssertNil(error)
  91. XCTAssertTrue(testDelegate.delegateIsCalled)
  92. expectation.fulfill()
  93. }
  94. wait(for: [expectation, notificationExpectation], timeout: 5)
  95. }
  96. // pragma mark - Helpers
  97. func assertTokenWithAuthorizedEntity() {
  98. let expectation = self.expectation(description: "tokenWithAuthorizedEntity")
  99. messaging.retrieveFCMToken(forSenderID: tokenAuthorizedEntity()) { token, error in
  100. XCTAssertNil(error)
  101. XCTAssertNotNil(token)
  102. expectation.fulfill()
  103. }
  104. wait(for: [expectation], timeout: 5)
  105. }
  106. func assertDefaultToken() {
  107. let expectation = self.expectation(description: "getToken")
  108. messaging.token { token, error in
  109. XCTAssertNil(error)
  110. XCTAssertNotNil(token)
  111. expectation.fulfill()
  112. }
  113. wait(for: [expectation], timeout: 5)
  114. }
  115. func tokenAuthorizedEntity() -> String {
  116. guard let app = FirebaseApp.app() else {
  117. return ""
  118. }
  119. return app.options.gcmSenderID
  120. }
  121. }
  122. #endif // !TARGET_OS_OSX