FIRTestComponents.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/FIRTestComponents.h"
  15. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  16. #pragma mark - Standard Component
  17. @implementation FIRTestClass
  18. /// FIRTestProtocol conformance.
  19. - (void)doSomething {
  20. }
  21. /// FIRComponentRegistrant conformance.
  22. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  23. FIRComponent *testComponent =
  24. [FIRComponent componentWithProtocol:@protocol(FIRTestProtocol)
  25. creationBlock:^id _Nullable(FIRComponentContainer *_Nonnull container,
  26. BOOL *_Nonnull isCacheable) {
  27. return [[FIRTestClass alloc] init];
  28. }];
  29. return @[ testComponent ];
  30. }
  31. /// FIRComponentLifecycleMaintainer conformance.
  32. - (void)appWillBeDeleted:(FIRApp *)app {
  33. }
  34. @end
  35. /// A test class that is a component registrant, a duplicate of FIRTestClass.
  36. @implementation FIRTestClassDuplicate
  37. - (void)doSomething {
  38. }
  39. /// FIRLibrary conformance.
  40. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  41. FIRComponent *testComponent =
  42. [FIRComponent componentWithProtocol:@protocol(FIRTestProtocol)
  43. creationBlock:^id _Nullable(FIRComponentContainer *_Nonnull container,
  44. BOOL *_Nonnull isCacheable) {
  45. return [[FIRTestClassDuplicate alloc] init];
  46. }];
  47. return @[ testComponent ];
  48. }
  49. /// FIRComponentLifecycleMaintainer conformance.
  50. - (void)appWillBeDeleted:(FIRApp *)app {
  51. }
  52. @end
  53. #pragma mark - Eager Component
  54. @implementation FIRTestClassEagerCached
  55. /// FIRTestProtocolEager conformance.
  56. - (void)doSomethingFaster {
  57. }
  58. /// FIRLibrary conformance.
  59. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  60. FIRComponent *testComponent = [FIRComponent
  61. componentWithProtocol:@protocol(FIRTestProtocolEagerCached)
  62. instantiationTiming:FIRInstantiationTimingAlwaysEager
  63. dependencies:@[]
  64. creationBlock:^id _Nullable(FIRComponentContainer *_Nonnull container,
  65. BOOL *_Nonnull isCacheable) {
  66. FIRTestClassEagerCached *instance = [[FIRTestClassEagerCached alloc] init];
  67. *isCacheable = YES;
  68. [instance doSomethingFaster];
  69. return instance;
  70. }];
  71. return @[ testComponent ];
  72. }
  73. /// FIRComponentLifecycleMaintainer conformance.
  74. - (void)appWillBeDeleted:(FIRApp *)app {
  75. }
  76. @end
  77. #pragma mark - Cached Component
  78. @implementation FIRTestClassCached
  79. /// FIRLibrary conformance.
  80. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  81. FIRComponent *testComponent = [FIRComponent
  82. componentWithProtocol:@protocol(FIRTestProtocolCached)
  83. creationBlock:^id _Nullable(FIRComponentContainer *_Nonnull container,
  84. BOOL *_Nonnull isCacheable) {
  85. FIRTestClassCached *instanceToCache = [[FIRTestClassCached alloc] init];
  86. *isCacheable = YES;
  87. return instanceToCache;
  88. }];
  89. return @[ testComponent ];
  90. }
  91. /// FIRComponentLifecycleMaintainer conformance.
  92. - (void)appWillBeDeleted:(FIRApp *)app {
  93. }
  94. /// FIRTestProtocolCached conformance.
  95. - (void)cacheCow {
  96. }
  97. @end
  98. #pragma mark - Test Component with Dependency
  99. @implementation FIRTestClassCachedWithDep
  100. - (instancetype)initWithTest:(id<FIRTestProtocolCached>)testInstance {
  101. self = [super init];
  102. if (self != nil) {
  103. self.testProperty = testInstance;
  104. }
  105. return self;
  106. }
  107. - (void)appWillBeDeleted:(nonnull FIRApp *)app {
  108. // Do something that depends on the instance from our dependency.
  109. [self.testProperty cacheCow];
  110. // Fetch from the container in the deletion function.
  111. id<FIRTestProtocolCached> anotherInstance = FIR_COMPONENT(FIRTestProtocolCached, app.container);
  112. [anotherInstance cacheCow];
  113. }
  114. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  115. FIRDependency *dep = [FIRDependency dependencyWithProtocol:@protocol(FIRTestProtocolCached)];
  116. FIRComponent *testComponent = [FIRComponent
  117. componentWithProtocol:@protocol(FIRTestProtocolCachedWithDep)
  118. instantiationTiming:FIRInstantiationTimingLazy
  119. dependencies:@[ dep ]
  120. creationBlock:^id _Nullable(FIRComponentContainer *_Nonnull container,
  121. BOOL *_Nonnull isCacheable) {
  122. // Fetch from the container in the instantiation block.
  123. *isCacheable = YES;
  124. id<FIRTestProtocolCached> test = FIR_COMPONENT(FIRTestProtocolCached, container);
  125. FIRTestClassCachedWithDep *instance =
  126. [[FIRTestClassCachedWithDep alloc] initWithTest:test];
  127. return instance;
  128. }];
  129. return @[ testComponent ];
  130. }
  131. @end