FIRMessagingPubSubTest.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2021 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. class FIRMessagingPubSubTest: XCTestCase {
  24. var app: FirebaseApp!
  25. var messaging: Messaging!
  26. override class func setUp() {
  27. if FirebaseApp.app() == nil {
  28. FirebaseApp.configure()
  29. }
  30. }
  31. override func setUpWithError() throws {
  32. messaging = try XCTUnwrap(Messaging.messaging())
  33. }
  34. override func tearDown() {
  35. messaging = nil
  36. }
  37. func testSubscribeTopic() {
  38. let expectation = self.expectation(description: "Successfully subscribe topic")
  39. assertDefaultToken()
  40. messaging.subscribe(toTopic: "cat_video") { error in
  41. XCTAssertNil(error)
  42. expectation.fulfill()
  43. }
  44. wait(for: [expectation], timeout: 5)
  45. }
  46. func testUnsubscribeTopic() {
  47. let expectation = self.expectation(description: "Successfully unsubscribe topic")
  48. assertDefaultToken()
  49. messaging.unsubscribe(fromTopic: "cat_video") { error in
  50. XCTAssertNil(error)
  51. expectation.fulfill()
  52. }
  53. wait(for: [expectation], timeout: 5)
  54. }
  55. func assertDefaultToken() {
  56. let expectation = self.expectation(description: "getToken")
  57. messaging.token { token, error in
  58. XCTAssertNil(error)
  59. XCTAssertNotNil(token)
  60. expectation.fulfill()
  61. }
  62. wait(for: [expectation], timeout: 5)
  63. }
  64. }
  65. #endif // !TARGET_OS_OSX