FIRRemoteConfigComponentTest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  18. #import "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
  19. #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
  20. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  21. @import FirebaseRemoteConfigInterop;
  22. @interface FIRRemoteConfigComponentTest : XCTestCase
  23. @end
  24. @implementation FIRRemoteConfigComponentTest
  25. - (void)tearDown {
  26. [super tearDown];
  27. // Clear out any apps that were called with `configure`.
  28. [FIRApp resetApps];
  29. [FIRRemoteConfigComponent clearAllComponentInstances];
  30. }
  31. - (void)testRCInstanceCreationAndCaching {
  32. // Create the provider to vend Remote Config instances.
  33. FIRRemoteConfigComponent *provider = [self providerForTest];
  34. // Create a Remote Config instance from the provider.
  35. NSString *sharedNamespace = @"some_namespace";
  36. FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
  37. XCTAssertNotNil(config);
  38. // Fetch an instance with the same namespace - should be the same instance.
  39. FIRRemoteConfig *sameConfig = [provider remoteConfigForNamespace:sharedNamespace];
  40. XCTAssertNotNil(sameConfig);
  41. XCTAssertEqual(config, sameConfig);
  42. }
  43. - (void)testRCSeparateInstancesForDifferentNamespaces {
  44. // Create the provider to vend Remote Config instances.
  45. FIRRemoteConfigComponent *provider = [self providerForTest];
  46. // Create a Remote Config instance from the provider.
  47. FIRRemoteConfig *config = [provider remoteConfigForNamespace:@"namespace1"];
  48. XCTAssertNotNil(config);
  49. // Fetch another instance with a different namespace.
  50. FIRRemoteConfig *config2 = [provider remoteConfigForNamespace:@"namespace2"];
  51. XCTAssertNotNil(config2);
  52. XCTAssertNotEqual(config, config2);
  53. }
  54. - (void)testRCSeparateInstancesForDifferentApps {
  55. FIRRemoteConfigComponent *provider = [self providerForTest];
  56. // Create a Remote Config instance from the provider.
  57. NSString *sharedNamespace = @"some_namespace";
  58. FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
  59. XCTAssertNotNil(config);
  60. // Use a new app and new povider, ensure the instances with the same namespace are different.
  61. NSString *secondAppName = [provider.app.name stringByAppendingString:@"2"];
  62. FIRApp *secondApp = [[FIRApp alloc] initInstanceWithName:secondAppName
  63. options:[self fakeOptions]];
  64. FIRRemoteConfigComponent *separateProvider =
  65. [[FIRRemoteConfigComponent alloc] initWithApp:secondApp];
  66. FIRRemoteConfig *separateConfig = [separateProvider remoteConfigForNamespace:sharedNamespace];
  67. XCTAssertNotNil(separateConfig);
  68. XCTAssertNotEqual(config, separateConfig);
  69. }
  70. - (void)testInitialization {
  71. // Explicitly instantiate the component here in case the providerForTest ever changes to mock
  72. // something.
  73. NSString *appName = [self generatedTestAppName];
  74. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:[self fakeOptions]];
  75. FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  76. XCTAssertNotNil(provider);
  77. XCTAssertNotNil(provider.app);
  78. }
  79. - (void)testRegistersAsLibrary {
  80. // Now component has two register, one is provider and another one is Interop
  81. XCTAssertEqual([FIRRemoteConfigComponent componentsToRegister].count, 2);
  82. // Configure a test FIRApp for fetching an instance of the FIRRemoteConfigProvider.
  83. NSString *appName = [self generatedTestAppName];
  84. [FIRApp configureWithName:appName options:[self fakeOptions]];
  85. FIRApp *app = [FIRApp appNamed:appName];
  86. // Attempt to fetch the component and verify it's a valid instance.
  87. id<FIRRemoteConfigProvider> provider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  88. id<FIRRemoteConfigInterop> interop = FIR_COMPONENT(FIRRemoteConfigInterop, app.container);
  89. XCTAssertNotNil(provider);
  90. XCTAssertNotNil(interop);
  91. // Ensure that the instance that comes from the container is cached.
  92. id<FIRRemoteConfigProvider> sameProvider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  93. id<FIRRemoteConfigInterop> sameInterop = FIR_COMPONENT(FIRRemoteConfigInterop, app.container);
  94. XCTAssertNotNil(sameProvider);
  95. XCTAssertNotNil(sameInterop);
  96. XCTAssertEqual(provider, sameProvider);
  97. XCTAssertEqual(interop, sameInterop);
  98. // Dynamic typing, both prototols are refering to the same component instance
  99. id providerID = provider;
  100. id interopID = interop;
  101. XCTAssertEqualObjects(providerID, interopID);
  102. }
  103. - (void)testTwoAppsCreateTwoComponents {
  104. NSString *appName = [self generatedTestAppName];
  105. [FIRApp configureWithName:appName options:[self fakeOptions]];
  106. FIRApp *app = [FIRApp appNamed:appName];
  107. [FIRApp configureWithOptions:[self fakeOptions]];
  108. FIRApp *defaultApp = [FIRApp defaultApp];
  109. XCTAssertNotNil(defaultApp);
  110. XCTAssertNotEqualObjects(app, defaultApp);
  111. id<FIRRemoteConfigProvider> provider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  112. id<FIRRemoteConfigInterop> interop = FIR_COMPONENT(FIRRemoteConfigInterop, app.container);
  113. id<FIRRemoteConfigProvider> defaultAppProvider =
  114. FIR_COMPONENT(FIRRemoteConfigProvider, defaultApp.container);
  115. id<FIRRemoteConfigInterop> defaultAppInterop =
  116. FIR_COMPONENT(FIRRemoteConfigInterop, defaultApp.container);
  117. id providerID = provider;
  118. id interopID = interop;
  119. id defaultAppProviderID = defaultAppProvider;
  120. id defaultAppInteropID = defaultAppInterop;
  121. XCTAssertEqualObjects(providerID, interopID);
  122. XCTAssertEqualObjects(defaultAppProviderID, defaultAppInteropID);
  123. // Check two apps get their own component to register
  124. XCTAssertNotEqualObjects(interopID, defaultAppInteropID);
  125. }
  126. - (void)testThrowsWithEmptyGoogleAppID {
  127. FIROptions *options = [self fakeOptions];
  128. options.googleAppID = @"";
  129. // Create the provider to vend Remote Config instances.
  130. NSString *appName = [self generatedTestAppName];
  131. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  132. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  133. // Creating a Remote Config instance should fail since the googleAppID is empty.
  134. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  135. }
  136. - (void)testThrowsWithNilGoogleAppID {
  137. FIROptions *options = [self fakeOptions];
  138. #pragma clang diagnostic push
  139. #pragma clang diagnostic ignored "-Wnonnull"
  140. options.googleAppID = nil;
  141. #pragma clang diagnostic pop
  142. // Create the provider to vend Remote Config instances.
  143. NSString *appName = [self generatedTestAppName];
  144. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  145. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  146. // Creating a Remote Config instance should fail since the googleAppID is nil.
  147. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  148. }
  149. - (void)testThrowsWithEmptyGCMSenderID {
  150. FIROptions *options = [self fakeOptions];
  151. options.GCMSenderID = @"";
  152. // Create the provider to vend Remote Config instances.
  153. NSString *appName = [self generatedTestAppName];
  154. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  155. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  156. // Creating a Remote Config instance should fail since the GCMSenderID is empty.
  157. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  158. }
  159. - (void)testThrowsWithNilGCMSenderID {
  160. FIROptions *options = [self fakeOptions];
  161. #pragma clang diagnostic push
  162. #pragma clang diagnostic ignored "-Wnonnull"
  163. options.GCMSenderID = nil;
  164. #pragma clang diagnostic pop
  165. // Create the provider to vend Remote Config instances.
  166. NSString *appName = [self generatedTestAppName];
  167. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  168. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  169. // Creating a Remote Config instance should fail since the GCMSenderID is nil.
  170. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  171. }
  172. - (void)testThrowsWithEmptyProjectID {
  173. FIROptions *options = [self fakeOptions];
  174. options.projectID = @"";
  175. // Create the provider to vend Remote Config instances.
  176. NSString *appName = [self generatedTestAppName];
  177. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  178. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  179. // Creating a Remote Config instance should fail since the projectID is empty.
  180. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  181. }
  182. - (void)testThrowsWithNilProjectID {
  183. FIROptions *options = [self fakeOptions];
  184. #pragma clang diagnostic push
  185. #pragma clang diagnostic ignored "-Wnonnull"
  186. options.projectID = nil;
  187. #pragma clang diagnostic pop
  188. // Create the provider to vend Remote Config instances.
  189. NSString *appName = [self generatedTestAppName];
  190. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  191. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  192. // Creating a Remote Config instance should fail since the projectID is empty.
  193. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  194. }
  195. #pragma mark - Helpers
  196. - (FIROptions *)fakeOptions {
  197. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  198. GCMSenderID:@"correct_gcm_sender_id"];
  199. options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
  200. options.projectID = @"project-id";
  201. return options;
  202. }
  203. - (NSString *)generatedTestAppName {
  204. return [RCNTestUtilities generatedTestAppNameForTest:self.name];
  205. }
  206. - (FIRRemoteConfigComponent *)providerForTest {
  207. // Create the provider to vend Remote Config instances.
  208. NSString *appName = [self generatedTestAppName];
  209. FIROptions *options = [[self fakeOptions] copy];
  210. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  211. FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  212. XCTAssertNotNil(provider);
  213. XCTAssert(provider.app.options.googleAppID.length != 0);
  214. XCTAssert(provider.app.options.GCMSenderID.length != 0);
  215. return provider;
  216. }
  217. @end