FIRComponentContainerTest.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "FIRTestCase.h"
  15. #import <FirebaseCore/FIRAppInternal.h>
  16. #import <FirebaseCore/FIRComponent.h>
  17. #import <FirebaseCore/FIRComponentContainerInternal.h>
  18. #import "FIRTestComponents.h"
  19. /// Internally exposed methods and properties for testing.
  20. @interface FIRComponentContainer (TestInternal)
  21. @property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
  22. @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
  23. + (void)registerAsComponentRegistrant:(Class)klass inSet:(NSMutableSet<Class> *)allRegistrants;
  24. - (instancetype)initWithApp:(FIRApp *)app registrants:(NSMutableSet<Class> *)allRegistrants;
  25. @end
  26. @interface FIRComponentContainerTest : FIRTestCase
  27. @end
  28. @implementation FIRComponentContainerTest
  29. #pragma mark - Registration Tests
  30. - (void)testRegisteringConformingClass {
  31. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  32. Class testClass = [FIRTestClass class];
  33. [FIRComponentContainer registerAsComponentRegistrant:testClass inSet:allRegistrants];
  34. XCTAssertTrue([allRegistrants containsObject:testClass]);
  35. }
  36. - (void)testRegisteringNonConformingClass {
  37. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  38. XCTAssertThrows(
  39. [FIRComponentContainer registerAsComponentRegistrant:[NSString class] inSet:allRegistrants]);
  40. XCTAssertTrue(allRegistrants.count == 0);
  41. }
  42. - (void)testComponentsPopulatedOnInit {
  43. FIRComponentContainer *container = [self containerWithRegistrants:@[ [FIRTestClass class] ]];
  44. // Verify that the block is stored.
  45. NSString *protocolName = NSStringFromProtocol(@protocol(FIRTestProtocol));
  46. FIRComponentCreationBlock creationBlock = container.components[protocolName];
  47. OCMExpect(creationBlock);
  48. }
  49. #pragma mark - Caching Tests
  50. - (void)testInstanceCached {
  51. FIRComponentContainer *container =
  52. [self containerWithRegistrants:@[ [FIRTestClassCached class] ]];
  53. // Fetch an instance for `FIRTestProtocolCached`, then fetch it again to assert it's cached.
  54. id<FIRTestProtocolCached> instance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  55. XCTAssertNotNil(instance1);
  56. id<FIRTestProtocolCached> instance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  57. XCTAssertNotNil(instance2);
  58. XCTAssertEqual(instance1, instance2);
  59. }
  60. - (void)testInstanceNotCached {
  61. FIRComponentContainer *container = [self containerWithRegistrants:@[ [FIRTestClass class] ]];
  62. // Retrieve an instance from the container, then fetch it again and ensure it's not the same
  63. // instance.
  64. id<FIRTestProtocol> instance1 = FIR_COMPONENT(FIRTestProtocol, container);
  65. XCTAssertNotNil(instance1);
  66. id<FIRTestProtocol> instance2 = FIR_COMPONENT(FIRTestProtocol, container);
  67. XCTAssertNotNil(instance2);
  68. XCTAssertNotEqual(instance1, instance2);
  69. }
  70. - (void)testRemoveAllCachedInstances {
  71. FIRComponentContainer *container = [self containerWithRegistrants:@[
  72. [FIRTestClass class], [FIRTestClassCached class], [FIRTestClassEagerCached class]
  73. ]];
  74. // Retrieve an instance of FIRTestClassCached to ensure it's cached.
  75. id<FIRTestProtocolCached> cachedInstance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  76. id<FIRTestProtocolEagerCached> eagerInstance1 =
  77. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  78. // FIRTestClassEagerCached and FIRTestClassCached instances should be cached at this point.
  79. XCTAssertTrue(container.cachedInstances.count == 2);
  80. // Remove the instances and verify cachedInstances is empty, and that new instances returned from
  81. // the container don't match the old ones.
  82. [container removeAllCachedInstances];
  83. XCTAssertTrue(container.cachedInstances.count == 0);
  84. id<FIRTestProtocolCached> cachedInstance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  85. XCTAssertNotEqual(cachedInstance1, cachedInstance2);
  86. id<FIRTestProtocolEagerCached> eagerInstance2 =
  87. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  88. XCTAssertNotEqual(eagerInstance1, eagerInstance2);
  89. }
  90. #pragma mark - Instantiation Tests
  91. - (void)testEagerInstantiation {
  92. // Create a container with `FIRTestClassEagerCached` as a registrant, which provides the
  93. // implementation for `FIRTestProtocolEagerCached` and requires eager instantiation as well as
  94. // caching so the test can verify it was eagerly instantiated.
  95. FIRComponentContainer *container =
  96. [self containerWithRegistrants:@[ [FIRTestClassEagerCached class] ]];
  97. NSString *protocolName = NSStringFromProtocol(@protocol(FIRTestProtocolEagerCached));
  98. XCTAssertNotNil(container.cachedInstances[protocolName]);
  99. }
  100. #pragma mark - Input Validation Tests
  101. - (void)testProtocolAlreadyRegistered {
  102. // Register two classes that provide the same protocol. Only one should be stored, and there
  103. // should be a log stating that the protocol has already been registered. Right now there's no
  104. // guarantee which one will be registered first since it's an NSSet under the hood, but that could
  105. // change in the future.
  106. // TODO(wilsonryan): Assert that the log gets called warning that it's already been registered.
  107. FIRComponentContainer *container =
  108. [self containerWithRegistrants:@[ [FIRTestClass class], [FIRTestClassDuplicate class] ]];
  109. XCTAssert(container.components.count == 1);
  110. }
  111. #pragma mark - Convenience Methods
  112. /// Create a container that has registered the test class.
  113. - (FIRComponentContainer *)containerWithRegistrants:(NSArray<Class> *)registrants {
  114. id appMock = OCMClassMock([FIRApp class]);
  115. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  116. // Initialize the container with the test classes.
  117. for (Class c in registrants) {
  118. [FIRComponentContainer registerAsComponentRegistrant:c inSet:allRegistrants];
  119. }
  120. return [[FIRComponentContainer alloc] initWithApp:appMock registrants:allRegistrants];
  121. }
  122. @end