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