FIRComponentContainerTest.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = [self containerWithRegistrants:@ [[FIRTestClassCached class]]];
  52. // Fetch an instance for `FIRTestProtocolCached`, then fetch it again to assert it's cached.
  53. id<FIRTestProtocolCached> instance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  54. XCTAssertNotNil(instance1);
  55. id<FIRTestProtocolCached> instance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  56. XCTAssertNotNil(instance2);
  57. XCTAssertEqual(instance1, instance2);
  58. }
  59. - (void)testInstanceNotCached {
  60. FIRComponentContainer *container = [self containerWithRegistrants:@ [[FIRTestClass class]]];
  61. // Retrieve an instance from the container, then fetch it again and ensure it's not the same
  62. // instance.
  63. id<FIRTestProtocol> instance1 = FIR_COMPONENT(FIRTestProtocol, container);
  64. XCTAssertNotNil(instance1);
  65. id<FIRTestProtocol> instance2 = FIR_COMPONENT(FIRTestProtocol, container);
  66. XCTAssertNotNil(instance2);
  67. XCTAssertNotEqual(instance1, instance2);
  68. }
  69. - (void)testRemoveAllCachedInstances {
  70. FIRComponentContainer *container =
  71. [self containerWithRegistrants:@ [[FIRTestClass class], [FIRTestClassCached class],
  72. [FIRTestClassEagerCached class]]];
  73. // Retrieve an instance of FIRTestClassCached to ensure it's cached.
  74. id<FIRTestProtocolCached> cachedInstance1 = FIR_COMPONENT(FIRTestProtocolCached, container);
  75. id<FIRTestProtocolEagerCached> eagerInstance1 =
  76. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  77. // FIRTestClassEagerCached and FIRTestClassCached instances should be cached at this point.
  78. XCTAssertTrue(container.cachedInstances.count == 2);
  79. // Remove the instances and verify cachedInstances is empty, and that new instances returned from
  80. // the container don't match the old ones.
  81. [container removeAllCachedInstances];
  82. XCTAssertTrue(container.cachedInstances.count == 0);
  83. id<FIRTestProtocolCached> cachedInstance2 = FIR_COMPONENT(FIRTestProtocolCached, container);
  84. XCTAssertNotEqual(cachedInstance1, cachedInstance2);
  85. id<FIRTestProtocolEagerCached> eagerInstance2 =
  86. FIR_COMPONENT(FIRTestProtocolEagerCached, container);
  87. XCTAssertNotEqual(eagerInstance1, eagerInstance2);
  88. }
  89. #pragma mark - Instantiation Tests
  90. - (void)testEagerInstantiation {
  91. // Create a container with `FIRTestClassEagerCached` as a registrant, which provides the
  92. // implementation for `FIRTestProtocolEagerCached` and requires eager instantiation as well as
  93. // caching so the test can verify it was eagerly instantiated.
  94. FIRComponentContainer *container =
  95. [self containerWithRegistrants:@ [[FIRTestClassEagerCached class]]];
  96. NSString *protocolName = NSStringFromProtocol(@protocol(FIRTestProtocolEagerCached));
  97. XCTAssertNotNil(container.cachedInstances[protocolName]);
  98. }
  99. #pragma mark - Input Validation Tests
  100. - (void)testProtocolAlreadyRegistered {
  101. // Register two classes that provide the same protocol. Only one should be stored, and there
  102. // should be a log stating that the protocol has already been registered. Right now there's no
  103. // guarantee which one will be registered first since it's an NSSet under the hood, but that could
  104. // change in the future.
  105. // TODO(wilsonryan): Assert that the log gets called warning that it's already been registered.
  106. FIRComponentContainer *container =
  107. [self containerWithRegistrants:@ [[FIRTestClass class], [FIRTestClassDuplicate class]]];
  108. XCTAssert(container.components.count == 1);
  109. }
  110. #pragma mark - Convenience Methods
  111. /// Create a container that has registered the test class.
  112. - (FIRComponentContainer *)containerWithRegistrants:(NSArray<Class> *)registrants {
  113. id appMock = OCMClassMock([FIRApp class]);
  114. NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
  115. // Initialize the container with the test classes.
  116. for (Class c in registrants) {
  117. [FIRComponentContainer registerAsComponentRegistrant:c inSet:allRegistrants];
  118. }
  119. return [[FIRComponentContainer alloc] initWithApp:appMock registrants:allRegistrants];
  120. }
  121. @end