FIRTestComponents.m 5.4 KB

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