FIRStorageComponentTests.m 4.6 KB

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