FIRMessagingPubSubTest.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // fake APNS Token
  34. messaging.apnsToken = "eb706b132b2f9270faac751e4ceab283f1803b729ac1dd399db3fd2a98bb101b"
  35. .data(using: .utf8)
  36. }
  37. override func tearDown() {
  38. messaging = nil
  39. }
  40. func testSubscribeTopic() {
  41. let expectation = self.expectation(description: "Successfully subscribe topic")
  42. assertDefaultToken()
  43. messaging.subscribe(toTopic: "cat_video") { error in
  44. XCTAssertNil(error)
  45. expectation.fulfill()
  46. }
  47. wait(for: [expectation], timeout: 5)
  48. }
  49. func testUnsubscribeTopic() {
  50. let expectation = self.expectation(description: "Successfully unsubscribe topic")
  51. assertDefaultToken()
  52. messaging.unsubscribe(fromTopic: "cat_video") { error in
  53. XCTAssertNil(error)
  54. expectation.fulfill()
  55. }
  56. wait(for: [expectation], timeout: 5)
  57. }
  58. func assertDefaultToken() {
  59. let expectation = self.expectation(description: "getToken")
  60. messaging.token { token, error in
  61. XCTAssertNil(error)
  62. XCTAssertNotNil(token)
  63. expectation.fulfill()
  64. }
  65. wait(for: [expectation], timeout: 5)
  66. }
  67. }
  68. #endif // !TARGET_OS_OSX