LifecycleNotifications.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Copyright 2022 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. import XCTest
  16. import Dispatch
  17. #if os(iOS) || os(tvOS)
  18. import UIKit
  19. #elseif os(macOS)
  20. import AppKit
  21. import Cocoa
  22. #elseif os(watchOS)
  23. import WatchKit
  24. #endif // os(iOS) || os(tvOS)
  25. // swift(>=5.9) implies Xcode 15+
  26. // Need to have this Swift version check to use os(visionOS) macro, VisionOS support.
  27. // TODO: Remove this check and add `os(visionOS)` to the `os(iOS) || os(tvOS)` conditional above
  28. // when Xcode 15 is the minimum supported by Firebase.
  29. #if swift(>=5.9)
  30. #if os(visionOS)
  31. import UIKit
  32. #endif // os(visionOS)
  33. #endif // swift(>=5.9)
  34. private func _postBackgroundedNotificationInternal() {
  35. let notificationCenter = NotificationCenter.default
  36. #if os(iOS) || os(tvOS)
  37. notificationCenter.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
  38. #elseif os(macOS)
  39. notificationCenter.post(name: NSApplication.didResignActiveNotification, object: nil)
  40. #elseif os(watchOS)
  41. if #available(watchOSApplicationExtension 7.0, *) {
  42. notificationCenter.post(
  43. name: WKExtension.applicationDidEnterBackgroundNotification,
  44. object: nil
  45. )
  46. }
  47. #endif // os(iOS) || os(tvOS)
  48. // swift(>=5.9) implies Xcode 15+
  49. // Need to have this Swift version check to use os(visionOS) macro, VisionOS support.
  50. // TODO: Remove this check and add `os(visionOS)` to the `os(iOS) || os(tvOS)` conditional above
  51. // when Xcode 15 is the minimum supported by Firebase.
  52. #if swift(>=5.9)
  53. #if os(visionOS)
  54. notificationCenter.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
  55. #endif // os(visionOS)
  56. #endif // swift(>=5.9)
  57. }
  58. private func _postForegroundedNotificationInternal() {
  59. let notificationCenter = NotificationCenter.default
  60. #if os(iOS) || os(tvOS)
  61. notificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil)
  62. #elseif os(macOS)
  63. notificationCenter.post(name: NSApplication.didBecomeActiveNotification, object: nil)
  64. #elseif os(watchOS)
  65. if #available(watchOSApplicationExtension 7.0, *) {
  66. notificationCenter.post(
  67. name: WKExtension.applicationDidBecomeActiveNotification,
  68. object: nil
  69. )
  70. }
  71. #endif // os(iOS) || os(tvOS)
  72. // swift(>=5.9) implies Xcode 15+
  73. // Need to have this Swift version check to use os(visionOS) macro, VisionOS support.
  74. // TODO: Remove this check and add `os(visionOS)` to the `os(iOS) || os(tvOS)` conditional above
  75. // when Xcode 15 is the minimum supported by Firebase.
  76. #if swift(>=5.9)
  77. #if os(visionOS)
  78. notificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil)
  79. #endif // os(visionOS)
  80. #endif // swift(>=5.9)
  81. }
  82. extension XCTestCase {
  83. func postBackgroundedNotification() {
  84. // On Catalyst, the notifications can only be called on a the main thread
  85. if Thread.isMainThread {
  86. _postBackgroundedNotificationInternal()
  87. } else {
  88. DispatchQueue.main.sync {
  89. _postBackgroundedNotificationInternal()
  90. }
  91. }
  92. }
  93. func postForegroundedNotification() {
  94. // On Catalyst, the notifications can only be called on a the main thread
  95. if Thread.isMainThread {
  96. _postForegroundedNotificationInternal()
  97. } else {
  98. DispatchQueue.main.sync {
  99. _postForegroundedNotificationInternal()
  100. }
  101. }
  102. }
  103. }