GULCCComponentContainerTest.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <XCTest/XCTest.h>
  15. #import <GoogleUtilitiesComponents/GULCCComponent.h>
  16. #import <GoogleUtilitiesComponents/GULCCComponentContainerInternal.h>
  17. #import "GULCCTestComponents.h"
  18. /// Internally exposed methods and properties for testing.
  19. @interface GULCCComponentContainer (TestInternal)
  20. @property(nonatomic, strong)
  21. NSMutableDictionary<NSString *, GULCCComponentCreationBlock> *components;
  22. @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
  23. + (void)registerAsComponentRegistrant:(Class<GULCCLibrary>)klass
  24. inSet:(NSMutableSet<Class> *)allRegistrants;
  25. @end
  26. @interface GULCCComponentContainer (TestInternalImplementations)
  27. - (instancetype)initWithContext:(id)context
  28. components:(NSDictionary<NSString *, GULCCComponentCreationBlock> *)components;
  29. @end
  30. @implementation GULCCComponentContainer (TestInternalImplementations)
  31. - (instancetype)initWithContext:(id)context
  32. components:
  33. (NSDictionary<NSString *, GULCCComponentCreationBlock> *)components {
  34. self = [self initWithContext:context registrants:[[NSMutableSet alloc] init]];
  35. if (self) {
  36. self.components = [components mutableCopy];
  37. }
  38. return self;
  39. }
  40. @end
  41. @interface GULCCComponentContainerTest : XCTestCase {
  42. /// Stored context, since the container has a `weak` reference to it.
  43. id _context;
  44. }
  45. @end
  46. @implementation GULCCComponentContainerTest
  47. - (void)tearDown {
  48. _context = nil;
  49. [super tearDown];
  50. }
  51. #pragma mark - Registration Tests
  52. - (void)testRegisteringConformingClass {
  53. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  54. Class testClass = [GULCCTestClass class];
  55. [GULCCComponentContainer registerAsComponentRegistrant:testClass inSet:allRegistrants];
  56. XCTAssertTrue([allRegistrants containsObject:testClass]);
  57. }
  58. - (void)testComponentsPopulatedOnInit {
  59. GULCCComponentContainer *container = [self containerWithRegistrants:@[ [GULCCTestClass class] ]];
  60. // Verify that the block is stored.
  61. NSString *protocolName = NSStringFromProtocol(@protocol(GULCCTestProtocol));
  62. GULCCComponentCreationBlock creationBlock = container.components[protocolName];
  63. XCTAssertNotNil(creationBlock);
  64. }
  65. #pragma mark - Caching Tests
  66. - (void)testInstanceCached {
  67. GULCCComponentContainer *container =
  68. [self containerWithRegistrants:@[ [GULCCTestClassCached class] ]];
  69. // Fetch an instance for `GULCCTestProtocolCached`, then fetch it again to assert it's cached.
  70. id<GULCCTestProtocolCached> instance1 = GUL_COMPONENT(GULCCTestProtocolCached, container);
  71. XCTAssertNotNil(instance1);
  72. id<GULCCTestProtocolCached> instance2 = GUL_COMPONENT(GULCCTestProtocolCached, container);
  73. XCTAssertNotNil(instance2);
  74. XCTAssertEqual(instance1, instance2);
  75. }
  76. - (void)testInstanceNotCached {
  77. GULCCComponentContainer *container = [self containerWithRegistrants:@[ [GULCCTestClass class] ]];
  78. // Retrieve an instance from the container, then fetch it again and ensure it's not the same
  79. // instance.
  80. id<GULCCTestProtocol> instance1 = GUL_COMPONENT(GULCCTestProtocol, container);
  81. XCTAssertNotNil(instance1);
  82. id<GULCCTestProtocol> instance2 = GUL_COMPONENT(GULCCTestProtocol, container);
  83. XCTAssertNotNil(instance2);
  84. XCTAssertNotEqual(instance1, instance2);
  85. }
  86. - (void)testRemoveAllCachedInstances {
  87. GULCCComponentContainer *container = [self containerWithRegistrants:@[
  88. [GULCCTestClass class], [GULCCTestClassCached class], [GULCCTestClassEagerCached class],
  89. [GULCCTestClassCachedWithDep class]
  90. ]];
  91. // Retrieve an instance of GULCCTestClassCached to ensure it's cached.
  92. id<GULCCTestProtocolCached> cachedInstance1 = GUL_COMPONENT(GULCCTestProtocolCached, container);
  93. id<GULCCTestProtocolEagerCached> eagerInstance1 =
  94. GUL_COMPONENT(GULCCTestProtocolEagerCached, container);
  95. // GULCCTestClassEagerCached and GULCCTestClassCached 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<GULCCTestProtocolCached> cachedInstance2 = GUL_COMPONENT(GULCCTestProtocolCached, container);
  102. XCTAssertNotEqual(cachedInstance1, cachedInstance2);
  103. id<GULCCTestProtocolEagerCached> eagerInstance2 =
  104. GUL_COMPONENT(GULCCTestProtocolEagerCached, container);
  105. XCTAssertNotEqual(eagerInstance1, eagerInstance2);
  106. }
  107. #pragma mark - Instantiation Tests
  108. - (void)testEagerInstantiation {
  109. // Create a container with `GULCCTestClassEagerCached` as a registrant, which provides the
  110. // implementation for `GULCCTestProtocolEagerCached` and requires eager instantiation as well as
  111. // caching so the test can verify it was eagerly instantiated.
  112. GULCCComponentContainer *container =
  113. [self containerWithRegistrants:@[ [GULCCTestClassEagerCached class] ]];
  114. NSString *protocolName = NSStringFromProtocol(@protocol(GULCCTestProtocolEagerCached));
  115. XCTAssertNotNil(container.cachedInstances[protocolName]);
  116. }
  117. #pragma mark - Input Validation Tests
  118. - (void)testProtocolAlreadyRegistered {
  119. // Register two classes that provide the same protocol. Only one should be stored, and there
  120. // should be a log stating that the protocol has already been registered. Right now there's no
  121. // guarantee which one will be registered first since it's an NSSet under the hood, but that could
  122. // change in the future.
  123. // TODO(wilsonryan): Assert that the log gets called warning that it's already been registered.
  124. GULCCComponentContainer *container =
  125. [self containerWithRegistrants:@[ [GULCCTestClass class], [GULCCTestClassDuplicate class] ]];
  126. XCTAssert(container.components.count == 1);
  127. }
  128. #pragma mark - Dependency Tests
  129. - (void)testDependencyDoesntBlock {
  130. /// Test a class that has a dependency, and fetching doesn't block the internal queue.
  131. GULCCComponentContainer *container = [self containerWithRegistrants:@[
  132. [GULCCTestClassCached class], [GULCCTestClassCachedWithDep class]
  133. ]];
  134. XCTAssert(container.components.count == 2);
  135. id<GULCCTestProtocolCachedWithDep> instanceWithDep =
  136. GUL_COMPONENT(GULCCTestProtocolCachedWithDep, container);
  137. XCTAssertNotNil(instanceWithDep);
  138. }
  139. - (void)testDependencyRemoveAllCachedInstancesDoesntBlock {
  140. /// Test a class that has a dependency, and fetching doesn't block the internal queue.
  141. GULCCComponentContainer *container = [self containerWithRegistrants:@[
  142. [GULCCTestClassCached class], [GULCCTestClassCachedWithDep class]
  143. ]];
  144. XCTAssert(container.components.count == 2);
  145. id<GULCCTestProtocolCachedWithDep> instanceWithDep =
  146. GUL_COMPONENT(GULCCTestProtocolCachedWithDep, container);
  147. XCTAssertNotNil(instanceWithDep);
  148. XCTAssertNotNil(instanceWithDep.testProperty);
  149. // Both `instanceWithDep` and `testProperty` should be cached now.
  150. XCTAssertTrue(container.cachedInstances.count == 2);
  151. // Remove the instances and verify cachedInstances is empty, and doesn't block the queue.
  152. [container removeAllCachedInstances];
  153. XCTAssertTrue(container.cachedInstances.count == 0);
  154. }
  155. #pragma mark - Convenience Methods
  156. /// Create a container that has registered the test class.
  157. - (GULCCComponentContainer *)containerWithRegistrants:(NSArray<Class> *)registrants {
  158. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  159. // Initialize the container with the test classes.
  160. for (Class c in registrants) {
  161. [GULCCComponentContainer registerAsComponentRegistrant:c inSet:allRegistrants];
  162. }
  163. GULCCComponentContainer *container =
  164. [[GULCCComponentContainer alloc] initWithContext:nil registrants:allRegistrants];
  165. // Instantiate all the components that were eagerly registered now that all other properties are
  166. // configured.
  167. [container instantiateEagerComponents];
  168. return container;
  169. }
  170. @end