FIRMessagingInstanceTest.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2019 Google
  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. import FirebaseCore
  17. import FirebaseMessaging
  18. import XCTest
  19. class FIRMessagingInstanceTest: XCTestCase {
  20. func testSingleton_worksAfterDelete() {
  21. // This is an example of a functional test case.
  22. // Use XCTAssert and related functions to verify your tests produce the correct results.
  23. let options = FirebaseOptions(googleAppID: "1:123:ios:123abc", gcmSenderID: "valid-sender-id")
  24. FirebaseApp.configure(options: options)
  25. let original = Messaging.messaging()
  26. // Ensure the client is set up as expected.
  27. guard let isOrigClientSetup = original.value(forKey: "isClientSetup") as? Bool else {
  28. XCTFail("Could not get internal Messaging variable `isClientSetup`.")
  29. return
  30. }
  31. XCTAssertTrue(isOrigClientSetup, "Property `isClientSetup` should be true after creation.")
  32. // Get and delete the default app.
  33. guard let defaultApp = FirebaseApp.app() else {
  34. XCTFail("Default app was not configured properly.")
  35. return
  36. }
  37. // The delete API is synchronous, so the default app will be deleted afterwards.
  38. defaultApp.delete { (success) in
  39. XCTAssertTrue(success, "FirebaseApp deletion should be successful")
  40. }
  41. XCTAssertNil(FirebaseApp.app(), "The default app should be `nil` at this point.")
  42. // Re-configure the app to trigger Messaging to re-instantiate.
  43. FirebaseApp.configure(options: options)
  44. // Get another instance of Messaging, make sure it's not the same instance.
  45. let postDelete = Messaging.messaging()
  46. XCTAssertNotEqual(original, postDelete)
  47. // Ensure the new client is set up as expected.
  48. guard let isClientSetup = postDelete.value(forKey: "isClientSetup") as? Bool else {
  49. XCTFail("Could not get internal Messaging variable `isClientSetup`.")
  50. return
  51. }
  52. XCTAssertTrue(isClientSetup, "Property `isClientSetup` should be true after creation.")
  53. }
  54. }