FIRComponentContainerTest.m 9.9 KB

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