FIRComponentContainerTest.m 10.0 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/FIRAppInternal.h>
  16. #import <FirebaseCore/FIRComponent.h>
  17. #import <FirebaseCore/FIRComponentContainerInternal.h>
  18. #import <FirebaseCore/FIROptions.h>
  19. #import "FirebaseCore/Tests/Unit/FIRTestComponents.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 = [self containerWithRegistrants:@ [[FIRTestClassCached class]]];
  68. // Fetch an instance for `FIRTestProtocolCached`, then fetch it again to assert it's cached.
  69. id<FIRTestProtocolCached> instance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  70. XCTAssertNotNil(instance1);
  71. id<FIRTestProtocolCached> instance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  72. XCTAssertNotNil(instance2);
  73. XCTAssertEqual(instance1, instance2);
  74. }
  75. - (void)testInstanceNotCached {
  76. FIRComponentContainer *container = [self containerWithRegistrants:@ [[FIRTestClass class]]];
  77. // Retrieve an instance from the container, then fetch it again and ensure it's not the same
  78. // instance.
  79. id<FIRTestProtocol> instance1 = FIR_COMPONENT(FIRTestProtocol, container);
  80. XCTAssertNotNil(instance1);
  81. id<FIRTestProtocol> instance2 = FIR_COMPONENT(FIRTestProtocol, container);
  82. XCTAssertNotNil(instance2);
  83. XCTAssertNotEqual(instance1, instance2);
  84. }
  85. - (void)testRemoveAllCachedInstances {
  86. FIRComponentContainer *container =
  87. [self containerWithRegistrants:@ [[FIRTestClass class], [FIRTestClassCached class],
  88. [FIRTestClassEagerCached class],
  89. [FIRTestClassCachedWithDep class]]];
  90. // Retrieve an instance of FIRTestClassCached to ensure it's cached.
  91. id<FIRTestProtocolCached> cachedInstance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  92. id<FIRTestProtocolEagerCached> eagerInstance1 =
  93. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  94. // FIRTestClassEagerCached and FIRTestClassCached instances should be cached at this point.
  95. XCTAssertTrue(container.cachedInstances.count == 2);
  96. // Remove the instances and verify cachedInstances is empty, and that new instances returned from
  97. // the container don't match the old ones.
  98. [container removeAllCachedInstances];
  99. XCTAssertTrue(container.cachedInstances.count == 0);
  100. id<FIRTestProtocolCached> cachedInstance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  101. XCTAssertNotEqual(cachedInstance1, cachedInstance2);
  102. id<FIRTestProtocolEagerCached> eagerInstance2 =
  103. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  104. XCTAssertNotEqual(eagerInstance1, eagerInstance2);
  105. }
  106. - (void)testRemoveAllComponents {
  107. FIRComponentContainer *container =
  108. [self containerWithRegistrants:@ [[FIRTestClass class], [FIRTestClassCached class],
  109. [FIRTestClassEagerCached class],
  110. [FIRTestClassCachedWithDep class]]];
  111. // Retrieve an instance of FIRTestClassCached to ensure it's cached.
  112. id<FIRTestProtocolCached> cachedInstance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  113. XCTAssertNotNil(cachedInstance1);
  114. id<FIRTestProtocolEagerCached> eagerInstance1 =
  115. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  116. XCTAssertNotNil(eagerInstance1);
  117. // FIRTestClassEagerCached and FIRTestClassCached instances should be cached at this point.
  118. XCTAssertTrue(container.cachedInstances.count == 2);
  119. // Remove all components.
  120. [container removeAllComponents];
  121. // Remove the instances.
  122. [container removeAllCachedInstances];
  123. // Verify that no new instances are created.
  124. id<FIRTestProtocolCached> cachedInstance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  125. XCTAssertNil(cachedInstance2);
  126. id<FIRTestProtocolEagerCached> eagerInstance2 =
  127. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  128. XCTAssertNil(eagerInstance2);
  129. }
  130. #pragma mark - Instantiation Tests
  131. - (void)testEagerInstantiation {
  132. // Create a container with `FIRTestClassEagerCached` as a registrant, which provides the
  133. // implementation for `FIRTestProtocolEagerCached` and requires eager instantiation as well as
  134. // caching so the test can verify it was eagerly instantiated.
  135. FIRComponentContainer *container =
  136. [self containerWithRegistrants:@ [[FIRTestClassEagerCached class]]];
  137. NSString *protocolName = NSStringFromProtocol(@protocol(FIRTestProtocolEagerCached));
  138. XCTAssertNotNil(container.cachedInstances[protocolName]);
  139. }
  140. #pragma mark - Input Validation Tests
  141. - (void)testProtocolAlreadyRegistered {
  142. // Register two classes that provide the same protocol. Only one should be stored, and there
  143. // should be a log stating that the protocol has already been registered. Right now there's no
  144. // guarantee which one will be registered first since it's an NSSet under the hood, but that could
  145. // change in the future.
  146. // TODO(wilsonryan): Assert that the log gets called warning that it's already been registered.
  147. FIRComponentContainer *container =
  148. [self containerWithRegistrants:@ [[FIRTestClass class], [FIRTestClassDuplicate class]]];
  149. XCTAssert(container.components.count == 1);
  150. }
  151. #pragma mark - Dependency Tests
  152. - (void)testDependencyDoesntBlock {
  153. /// Test a class that has a dependency, and fetching doesn't block the internal queue.
  154. FIRComponentContainer *container = [self
  155. containerWithRegistrants:@ [[FIRTestClassCached class], [FIRTestClassCachedWithDep class]]];
  156. XCTAssert(container.components.count == 2);
  157. id<FIRTestProtocolCachedWithDep> instanceWithDep =
  158. FIR_COMPONENT(FIRTestProtocolCachedWithDep, container);
  159. XCTAssertNotNil(instanceWithDep);
  160. }
  161. - (void)testDependencyRemoveAllCachedInstancesDoesntBlock {
  162. /// Test a class that has a dependency, and fetching doesn't block the internal queue.
  163. FIRComponentContainer *container = [self
  164. containerWithRegistrants:@ [[FIRTestClassCached class], [FIRTestClassCachedWithDep class]]];
  165. XCTAssert(container.components.count == 2);
  166. id<FIRTestProtocolCachedWithDep> instanceWithDep =
  167. FIR_COMPONENT(FIRTestProtocolCachedWithDep, container);
  168. XCTAssertNotNil(instanceWithDep);
  169. XCTAssertNotNil(instanceWithDep.testProperty);
  170. // Both `instanceWithDep` and `testProperty` should be cached now.
  171. XCTAssertTrue(container.cachedInstances.count == 2);
  172. // Remove the instances and verify cachedInstances is empty, and doesn't block the queue.
  173. [container removeAllCachedInstances];
  174. XCTAssertTrue(container.cachedInstances.count == 0);
  175. }
  176. #pragma mark - Convenience Methods
  177. /// Create a container that has registered the test class.
  178. - (FIRComponentContainer *)containerWithRegistrants:(NSArray<Class> *)registrants {
  179. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  180. GCMSenderID:kGCMSenderID];
  181. _hostApp = [[FIRApp alloc] initInstanceWithName:@"fake_app" options:options];
  182. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  183. // Initialize the container with the test classes.
  184. for (Class c in registrants) {
  185. [FIRComponentContainer registerAsComponentRegistrant:c inSet:allRegistrants];
  186. }
  187. // Override the app's container with the newly instantiated container.
  188. FIRComponentContainer *container = [[FIRComponentContainer alloc] initWithApp:_hostApp
  189. registrants:allRegistrants];
  190. _hostApp.container = container;
  191. // Instantiate all the components that were eagerly registered now that all other properties are
  192. // configured.
  193. [container instantiateEagerComponents];
  194. return container;
  195. }
  196. @end