FIRTestComponents.m 5.4 KB

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