FIRTestComponents.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/Extension/FIRComponentType.h"
  16. #import "FirebaseCore/Extension/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. 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. FIRComponent *testComponent = [FIRComponent
  116. componentWithProtocol:@protocol(FIRTestProtocolCachedWithDep)
  117. instantiationTiming:FIRInstantiationTimingLazy
  118. creationBlock:^id _Nullable(FIRComponentContainer *_Nonnull container,
  119. BOOL *_Nonnull isCacheable) {
  120. // Fetch from the container in the instantiation block.
  121. *isCacheable = YES;
  122. id<FIRTestProtocolCached> test = FIR_COMPONENT(FIRTestProtocolCached, container);
  123. FIRTestClassCachedWithDep *instance =
  124. [[FIRTestClassCachedWithDep alloc] initWithTest:test];
  125. return instance;
  126. }];
  127. return @[ testComponent ];
  128. }
  129. @end