StorageComponentTests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseCore
  16. @testable import FirebaseStorage
  17. import FirebaseAppCheckInterop
  18. import FirebaseAuthInterop
  19. import SharedTestUtilities
  20. import XCTest
  21. class StorageComponentTests: StorageTestHelpers {
  22. // MARK: Interoperability Tests
  23. /// Tests that the right number of components are being provided for the container.
  24. func testComponentsBeingRegistered() throws {
  25. let components = StorageComponent.componentsToRegister()
  26. XCTAssert(components.count == 1)
  27. }
  28. /// Tests that a Storage instance can be created properly by the StorageComponent.
  29. func testStorageInstanceCreation() throws {
  30. let app = try XCTUnwrap(StorageComponentTests.app)
  31. let component = StorageComponent(app: app)
  32. let storage = component.storage(for: "someBucket")
  33. XCTAssertNotNil(storage)
  34. }
  35. /// Tests that the component container caches instances of StorageComponent.
  36. func testMultipleComponentInstancesCreated() throws {
  37. let registrants = NSMutableSet(array: [StorageComponent.self])
  38. let container = FirebaseComponentContainer(
  39. app: StorageTestHelpers.app,
  40. registrants: registrants
  41. )
  42. let provider1 = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
  43. in: container)
  44. XCTAssertNotNil(provider1)
  45. let provider2 = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
  46. in: container)
  47. XCTAssertNotNil(provider2)
  48. // Ensure they're the same instance.
  49. XCTAssert(provider1 === provider2)
  50. }
  51. /// Tests that instances of Storage created are different.
  52. func testMultipleStorageInstancesCreated() throws {
  53. let app = try XCTUnwrap(StorageComponentTests.app)
  54. let registrants = NSMutableSet(array: [StorageComponent.self])
  55. let container = FirebaseComponentContainer(app: app, registrants: registrants)
  56. let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
  57. in: container)
  58. XCTAssertNotNil(provider)
  59. let storage1 = provider?.storage(for: "randomBucket")
  60. let storage2 = provider?.storage(for: "randomBucket")
  61. XCTAssertNotNil(storage1)
  62. // Ensure they're the same instance.
  63. XCTAssert(storage1 === storage2)
  64. let storage3 = provider?.storage(for: "differentBucket")
  65. XCTAssertNotNil(storage3)
  66. XCTAssert(storage1 !== storage3)
  67. }
  68. /// Test that Storage instances get deallocated.
  69. func testStorageLifecycle() throws {
  70. weak var weakApp: FirebaseApp?
  71. weak var weakStorage: Storage?
  72. try autoreleasepool {
  73. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  74. gcmSenderID: "00000000000000000-00000000000-000000000")
  75. options.projectID = "myProjectID"
  76. let app1 = FirebaseApp(instanceWithName: "transitory app", options: options)
  77. weakApp = try XCTUnwrap(app1)
  78. let storage = Storage(app: app1, bucket: "transitory bucket")
  79. weakStorage = storage
  80. XCTAssertNotNil(weakStorage)
  81. }
  82. XCTAssertNil(weakApp)
  83. XCTAssertNil(weakStorage)
  84. }
  85. }