FIRComponentContainerTest.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2018 Google
  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 "FirebaseCore/Tests/Unit/FIRTestCase.h"
  15. #import "FirebaseCore/Sources/FIRComponentContainerInternal.h"
  16. #import "FirebaseCore/Sources/Private/FIRComponentType.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "FirebaseCore/Tests/Unit/FIRTestComponents.h"
  19. #import "SharedTestUtilities/FIROptionsMock.h"
  20. /// Internally exposed methods and properties for testing.
  21. @interface FIRComponentContainer (TestInternal)
  22. @property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
  23. @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
  24. + (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass
  25. inSet:(NSMutableSet<Class> *)allRegistrants;
  26. - (instancetype)initWithApp:(FIRApp *)app registrants:(NSMutableSet<Class> *)allRegistrants;
  27. @end
  28. @interface FIRComponentContainer (TestInternalImplementations)
  29. - (instancetype)initWithApp:(FIRApp *)app
  30. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components;
  31. @end
  32. @implementation FIRComponentContainer (TestInternalImplementations)
  33. - (instancetype)initWithApp:(FIRApp *)app
  34. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components {
  35. self = [self initWithApp:app registrants:[[NSMutableSet alloc] init]];
  36. if (self) {
  37. self.components = [components mutableCopy];
  38. }
  39. return self;
  40. }
  41. @end
  42. @interface FIRComponentContainerTest : FIRTestCase {
  43. FIRApp *_hostApp;
  44. }
  45. @end
  46. @implementation FIRComponentContainerTest
  47. - (void)tearDown {
  48. _hostApp = nil;
  49. [super tearDown];
  50. }
  51. #pragma mark - Registration Tests
  52. - (void)testRegisteringConformingClass {
  53. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  54. Class testClass = [FIRTestClass class];
  55. [FIRComponentContainer registerAsComponentRegistrant:testClass inSet:allRegistrants];
  56. XCTAssertTrue([allRegistrants containsObject:testClass]);
  57. }
  58. - (void)testComponentsPopulatedOnInit {
  59. FIRComponentContainer *container = [self containerWithRegistrants:@[ [FIRTestClass class] ]];
  60. // Verify that the block is stored.
  61. NSString *protocolName = NSStringFromProtocol(@protocol(FIRTestProtocol));
  62. FIRComponentCreationBlock creationBlock = container.components[protocolName];
  63. XCTAssertNotNil(creationBlock);
  64. }
  65. #pragma mark - Caching Tests
  66. - (void)testInstanceCached {
  67. FIRComponentContainer *container =
  68. [self containerWithRegistrants:@[ [FIRTestClassCached class] ]];
  69. // Fetch an instance for `FIRTestProtocolCached`, then fetch it again to assert it's cached.
  70. id<FIRTestProtocolCached> instance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  71. XCTAssertNotNil(instance1);
  72. id<FIRTestProtocolCached> instance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  73. XCTAssertNotNil(instance2);
  74. XCTAssertEqual(instance1, instance2);
  75. }
  76. - (void)testInstanceNotCached {
  77. FIRComponentContainer *container = [self containerWithRegistrants:@[ [FIRTestClass class] ]];
  78. // Retrieve an instance from the container, then fetch it again and ensure it's not the same
  79. // instance.
  80. id<FIRTestProtocol> instance1 = FIR_COMPONENT(FIRTestProtocol, container);
  81. XCTAssertNotNil(instance1);
  82. id<FIRTestProtocol> instance2 = FIR_COMPONENT(FIRTestProtocol, container);
  83. XCTAssertNotNil(instance2);
  84. XCTAssertNotEqual(instance1, instance2);
  85. }
  86. - (void)testRemoveAllCachedInstances {
  87. FIRComponentContainer *container = [self containerWithRegistrants:@[
  88. [FIRTestClass class], [FIRTestClassCached class], [FIRTestClassEagerCached class],
  89. [FIRTestClassCachedWithDep class]
  90. ]];
  91. // Retrieve an instance of FIRTestClassCached to ensure it's cached.
  92. id<FIRTestProtocolCached> cachedInstance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  93. id<FIRTestProtocolEagerCached> eagerInstance1 =
  94. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  95. // FIRTestClassEagerCached and FIRTestClassCached instances should be cached at this point.
  96. XCTAssertTrue(container.cachedInstances.count == 2);
  97. // Remove the instances and verify cachedInstances is empty, and that new instances returned from
  98. // the container don't match the old ones.
  99. [container removeAllCachedInstances];
  100. XCTAssertTrue(container.cachedInstances.count == 0);
  101. id<FIRTestProtocolCached> cachedInstance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  102. XCTAssertNotEqual(cachedInstance1, cachedInstance2);
  103. id<FIRTestProtocolEagerCached> eagerInstance2 =
  104. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  105. XCTAssertNotEqual(eagerInstance1, eagerInstance2);
  106. }
  107. - (void)testRemoveAllComponents {
  108. FIRComponentContainer *container = [self containerWithRegistrants:@[
  109. [FIRTestClass class], [FIRTestClassCached class], [FIRTestClassEagerCached class],
  110. [FIRTestClassCachedWithDep class]
  111. ]];
  112. // Retrieve an instance of FIRTestClassCached to ensure it's cached.
  113. id<FIRTestProtocolCached> cachedInstance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  114. XCTAssertNotNil(cachedInstance1);
  115. id<FIRTestProtocolEagerCached> eagerInstance1 =
  116. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  117. XCTAssertNotNil(eagerInstance1);
  118. // FIRTestClassEagerCached and FIRTestClassCached instances should be cached at this point.
  119. XCTAssertTrue(container.cachedInstances.count == 2);
  120. // Remove all components.
  121. [container removeAllComponents];
  122. // Remove the instances.
  123. [container removeAllCachedInstances];
  124. // Verify that no new instances are created.
  125. id<FIRTestProtocolCached> cachedInstance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  126. XCTAssertNil(cachedInstance2);
  127. id<FIRTestProtocolEagerCached> eagerInstance2 =
  128. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  129. XCTAssertNil(eagerInstance2);
  130. }
  131. #pragma mark - Instantiation Tests
  132. - (void)testEagerInstantiation {
  133. // Create a container with `FIRTestClassEagerCached` as a registrant, which provides the
  134. // implementation for `FIRTestProtocolEagerCached` and requires eager instantiation as well as
  135. // caching so the test can verify it was eagerly instantiated.
  136. FIRComponentContainer *container =
  137. [self containerWithRegistrants:@[ [FIRTestClassEagerCached class] ]];
  138. NSString *protocolName = NSStringFromProtocol(@protocol(FIRTestProtocolEagerCached));
  139. XCTAssertNotNil(container.cachedInstances[protocolName]);
  140. }
  141. #pragma mark - Input Validation Tests
  142. - (void)testProtocolAlreadyRegistered {
  143. // Register two classes that provide the same protocol. Only one should be stored, and there
  144. // should be a log stating that the protocol has already been registered. Right now there's no
  145. // guarantee which one will be registered first since it's an NSSet under the hood, but that could
  146. // change in the future.
  147. // TODO(wilsonryan): Assert that the log gets called warning that it's already been registered.
  148. FIRComponentContainer *container =
  149. [self containerWithRegistrants:@[ [FIRTestClass class], [FIRTestClassDuplicate class] ]];
  150. XCTAssert(container.components.count == 1);
  151. }
  152. #pragma mark - Dependency Tests
  153. - (void)testDependencyDoesntBlock {
  154. /// Test a class that has a dependency, and fetching doesn't block the internal queue.
  155. FIRComponentContainer *container = [self
  156. containerWithRegistrants:@[ [FIRTestClassCached class], [FIRTestClassCachedWithDep class] ]];
  157. XCTAssert(container.components.count == 2);
  158. id<FIRTestProtocolCachedWithDep> instanceWithDep =
  159. FIR_COMPONENT(FIRTestProtocolCachedWithDep, container);
  160. XCTAssertNotNil(instanceWithDep);
  161. }
  162. - (void)testDependencyRemoveAllCachedInstancesDoesntBlock {
  163. /// Test a class that has a dependency, and fetching doesn't block the internal queue.
  164. FIRComponentContainer *container = [self
  165. containerWithRegistrants:@[ [FIRTestClassCached class], [FIRTestClassCachedWithDep class] ]];
  166. XCTAssert(container.components.count == 2);
  167. id<FIRTestProtocolCachedWithDep> instanceWithDep =
  168. FIR_COMPONENT(FIRTestProtocolCachedWithDep, container);
  169. XCTAssertNotNil(instanceWithDep);
  170. XCTAssertNotNil(instanceWithDep.testProperty);
  171. // Both `instanceWithDep` and `testProperty` should be cached now.
  172. XCTAssertTrue(container.cachedInstances.count == 2);
  173. // Remove the instances and verify cachedInstances is empty, and doesn't block the queue.
  174. [container removeAllCachedInstances];
  175. XCTAssertTrue(container.cachedInstances.count == 0);
  176. }
  177. #pragma mark - Convenience Methods
  178. /// Create a container that has registered the test class.
  179. - (FIRComponentContainer *)containerWithRegistrants:(NSArray<Class> *)registrants {
  180. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  181. GCMSenderID:kGCMSenderID];
  182. _hostApp = [[FIRApp alloc] initInstanceWithName:@"fake_app" options:options];
  183. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  184. // Initialize the container with the test classes.
  185. for (Class c in registrants) {
  186. [FIRComponentContainer registerAsComponentRegistrant:c inSet:allRegistrants];
  187. }
  188. // Override the app's container with the newly instantiated container.
  189. FIRComponentContainer *container = [[FIRComponentContainer alloc] initWithApp:_hostApp
  190. registrants:allRegistrants];
  191. _hostApp.container = container;
  192. // Instantiate all the components that were eagerly registered now that all other properties are
  193. // configured.
  194. [container instantiateEagerComponents];
  195. return container;
  196. }
  197. @end