TestsBase.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2020 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. import Foundation
  17. import FirebaseAuth
  18. import XCTest
  19. class TestsBase: XCTestCase {
  20. static let kExpectationsTimeout = 10.0
  21. #if compiler(>=5.5.2) && canImport(_Concurrency)
  22. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  23. func signInAnonymouslyAsync() async throws {
  24. let auth = Auth.auth()
  25. try await auth.signInAnonymously()
  26. }
  27. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  28. func deleteCurrentUserAsync() async throws {
  29. let auth = Auth.auth()
  30. try await auth.currentUser?.delete()
  31. }
  32. #endif
  33. func signInAnonymously() {
  34. let auth = Auth.auth()
  35. let expectation = self.expectation(description: "Anonymous sign-in finished.")
  36. auth.signInAnonymously { result, error in
  37. if let error = error {
  38. print("Anonymous sign in error: \(error)")
  39. }
  40. expectation.fulfill()
  41. }
  42. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  43. }
  44. func signOut() {
  45. let auth = Auth.auth()
  46. do {
  47. try auth.signOut()
  48. } catch {
  49. print("Error signing out: \(error)")
  50. }
  51. }
  52. func deleteCurrentUser() {
  53. let auth = Auth.auth()
  54. let expectation = self.expectation(description: "Delete current user finished.")
  55. auth.currentUser?.delete { error in
  56. if let error = error {
  57. print("Anonymous sign in error: \(error)")
  58. }
  59. expectation.fulfill()
  60. }
  61. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  62. }
  63. }