FIRStorageComponentTests.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2018 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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseStorage/Sources/FIRStorageComponent.h"
  20. #import "SharedTestUtilities/FIRComponentTestUtilities.h"
  21. // Make FIRComponentRegistrant conformance visible to the tests and expose the initializer.
  22. @interface FIRStorageComponent () <FIRLibrary>
  23. /// Internal initializer.
  24. - (instancetype)initWithApp:(FIRApp *)app;
  25. @end
  26. @interface FIRStorageComponentTests : XCTestCase
  27. @end
  28. @implementation FIRStorageComponentTests
  29. #pragma mark - Interoperability Tests
  30. // Tests that the right number of components are being provided for the container.
  31. - (void)testComponentsBeingRegistered {
  32. // Check the count, because any time a new component is added it should be added to the test suite
  33. // as well.
  34. NSArray<FIRComponent *> *components = [FIRStorageComponent componentsToRegister];
  35. XCTAssertTrue(components.count == 1);
  36. FIRComponent *component = [components firstObject];
  37. XCTAssert(component.protocol == @protocol(FIRStorageMultiBucketProvider));
  38. }
  39. /// Tests that a FIRStorage instance can be created properly by the FIRStorageComponent.
  40. - (void)testStorageInstanceCreation {
  41. // Get a mocked app, but don't use the default helper since it uses this class in the
  42. // implementation.
  43. id app = [self appMockWithOptions];
  44. FIRStorageComponent *component = [[FIRStorageComponent alloc] initWithApp:app];
  45. FIRStorage *storage = [component storageForBucket:@"someBucket"];
  46. XCTAssertNotNil(storage);
  47. }
  48. /// Tests that the component container caches instances of FIRStorageComponent.
  49. - (void)testMultipleComponentInstancesCreated {
  50. // App isn't used in any of this, so a simple class mock works for simplicity.
  51. id app = OCMClassMock([FIRApp class]);
  52. NSMutableSet *registrants = [NSMutableSet setWithObject:[FIRStorageComponent class]];
  53. FIRComponentContainer *container = [[FIRComponentContainer alloc] initWithApp:app
  54. registrants:registrants];
  55. id<FIRStorageMultiBucketProvider> provider1 =
  56. FIR_COMPONENT(FIRStorageMultiBucketProvider, container);
  57. XCTAssertNotNil(provider1);
  58. id<FIRStorageMultiBucketProvider> provider2 =
  59. FIR_COMPONENT(FIRStorageMultiBucketProvider, container);
  60. XCTAssertNotNil(provider2);
  61. // Ensure they're the same instance.
  62. XCTAssertEqual(provider1, provider2);
  63. }
  64. /// Tests that instances of FIRStorage created are different.
  65. - (void)testMultipleStorageInstancesCreated {
  66. // Get a mocked app, but don't use the default helper since is uses this class in the
  67. // implementation.
  68. id app = [self appMockWithOptions];
  69. NSMutableSet *registrants = [NSMutableSet setWithObject:[FIRStorageComponent class]];
  70. FIRComponentContainer *container = [[FIRComponentContainer alloc] initWithApp:app
  71. registrants:registrants];
  72. id<FIRStorageMultiBucketProvider> provider =
  73. FIR_COMPONENT(FIRStorageMultiBucketProvider, container);
  74. XCTAssertNotNil(provider);
  75. FIRStorage *storage1 = [provider storageForBucket:@"randomBucket"];
  76. XCTAssertNotNil(storage1);
  77. FIRStorage *storage2 = [provider storageForBucket:@"randomBucket"];
  78. XCTAssertNotNil(storage2);
  79. // Ensure that they're the same instance
  80. XCTAssertEqual(storage1, storage2);
  81. XCTAssertEqualObjects(storage1, storage2);
  82. // Create another bucket with a different provider from above.
  83. FIRStorage *storage3 = [provider storageForBucket:@"differentBucket"];
  84. XCTAssertNotNil(storage3);
  85. // Ensure it's a different object.
  86. XCTAssertNotEqualObjects(storage2, storage3);
  87. }
  88. #pragma mark - Test Helpers
  89. /// Creates a FIRApp mock with a googleAppID.
  90. - (id)appMockWithOptions {
  91. id app = OCMClassMock([FIRApp class]);
  92. OCMStub([app name]).andReturn(@"__FIRAPP_DEFAULT");
  93. id options = OCMClassMock([FIROptions class]);
  94. OCMStub([options googleAppID]).andReturn(@"dummyAppID");
  95. OCMStub([(FIRApp *)app options]).andReturn(options);
  96. return app;
  97. }
  98. @end