FIRRemoteConfigComponentTest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <FirebaseRemoteConfig/FirebaseRemoteConfig-Swift.h>
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  20. @import FirebaseRemoteConfigInterop;
  21. @interface FIRRemoteConfigComponentTest : XCTestCase
  22. @end
  23. @implementation FIRRemoteConfigComponentTest
  24. - (void)tearDown {
  25. [super tearDown];
  26. // Clear out any apps that were called with `configure`.
  27. [FIRApp resetApps];
  28. [FIRRemoteConfigComponent clearAllComponentInstances];
  29. }
  30. - (void)testRCInstanceCreationAndCaching {
  31. // Create the provider to vend Remote Config instances.
  32. FIRRemoteConfigComponent *provider = [self providerForTest];
  33. // Create a Remote Config instance from the provider.
  34. NSString *sharedNamespace = @"some_namespace";
  35. FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
  36. XCTAssertNotNil(config);
  37. // Fetch an instance with the same namespace - should be the same instance.
  38. FIRRemoteConfig *sameConfig = [provider remoteConfigForNamespace:sharedNamespace];
  39. XCTAssertNotNil(sameConfig);
  40. XCTAssertEqual(config, sameConfig);
  41. }
  42. - (void)testRCSeparateInstancesForDifferentNamespaces {
  43. // Create the provider to vend Remote Config instances.
  44. FIRRemoteConfigComponent *provider = [self providerForTest];
  45. // Create a Remote Config instance from the provider.
  46. FIRRemoteConfig *config = [provider remoteConfigForNamespace:@"namespace1"];
  47. XCTAssertNotNil(config);
  48. // Fetch another instance with a different namespace.
  49. FIRRemoteConfig *config2 = [provider remoteConfigForNamespace:@"namespace2"];
  50. XCTAssertNotNil(config2);
  51. XCTAssertNotEqual(config, config2);
  52. }
  53. - (void)testRCSeparateInstancesForDifferentApps {
  54. FIRRemoteConfigComponent *provider = [self providerForTest];
  55. // Create a Remote Config instance from the provider.
  56. NSString *sharedNamespace = @"some_namespace";
  57. FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
  58. XCTAssertNotNil(config);
  59. // Use a new app and new povider, ensure the instances with the same namespace are different.
  60. NSString *secondAppName = [provider.app.name stringByAppendingString:@"2"];
  61. FIRApp *secondApp = [[FIRApp alloc] initInstanceWithName:secondAppName
  62. options:[self fakeOptions]];
  63. FIRRemoteConfigComponent *separateProvider =
  64. [[FIRRemoteConfigComponent alloc] initWithApp:secondApp];
  65. FIRRemoteConfig *separateConfig = [separateProvider remoteConfigForNamespace:sharedNamespace];
  66. XCTAssertNotNil(separateConfig);
  67. XCTAssertNotEqual(config, separateConfig);
  68. }
  69. - (void)testInitialization {
  70. // Explicitly instantiate the component here in case the providerForTest ever changes to mock
  71. // something.
  72. NSString *appName = [self generatedTestAppName];
  73. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:[self fakeOptions]];
  74. FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  75. XCTAssertNotNil(provider);
  76. XCTAssertNotNil(provider.app);
  77. }
  78. - (void)testRegistersAsLibrary {
  79. // Now component has two register, one is provider and another one is Interop
  80. XCTAssertEqual([FIRRemoteConfigComponent componentsToRegister].count, 2);
  81. // Configure a test FIRApp for fetching an instance of the FIRRemoteConfigProvider.
  82. NSString *appName = [self generatedTestAppName];
  83. [FIRApp configureWithName:appName options:[self fakeOptions]];
  84. FIRApp *app = [FIRApp appNamed:appName];
  85. // Attempt to fetch the component and verify it's a valid instance.
  86. id<FIRRemoteConfigProvider> provider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  87. id<FIRRemoteConfigInterop> interop = FIR_COMPONENT(FIRRemoteConfigInterop, app.container);
  88. XCTAssertNotNil(provider);
  89. XCTAssertNotNil(interop);
  90. // Ensure that the instance that comes from the container is cached.
  91. id<FIRRemoteConfigProvider> sameProvider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  92. id<FIRRemoteConfigInterop> sameInterop = FIR_COMPONENT(FIRRemoteConfigInterop, app.container);
  93. XCTAssertNotNil(sameProvider);
  94. XCTAssertNotNil(sameInterop);
  95. XCTAssertEqual(provider, sameProvider);
  96. XCTAssertEqual(interop, sameInterop);
  97. // Dynamic typing, both prototols are referring to the same component instance
  98. id providerID = provider;
  99. id interopID = interop;
  100. XCTAssertEqualObjects(providerID, interopID);
  101. }
  102. - (void)testTwoAppsCreateTwoComponents {
  103. NSString *appName = [self generatedTestAppName];
  104. [FIRApp configureWithName:appName options:[self fakeOptions]];
  105. FIRApp *app = [FIRApp appNamed:appName];
  106. [FIRApp configureWithOptions:[self fakeOptions]];
  107. FIRApp *defaultApp = [FIRApp defaultApp];
  108. XCTAssertNotNil(defaultApp);
  109. XCTAssertNotEqualObjects(app, defaultApp);
  110. id<FIRRemoteConfigProvider> provider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  111. id<FIRRemoteConfigInterop> interop = FIR_COMPONENT(FIRRemoteConfigInterop, app.container);
  112. id<FIRRemoteConfigProvider> defaultAppProvider =
  113. FIR_COMPONENT(FIRRemoteConfigProvider, defaultApp.container);
  114. id<FIRRemoteConfigInterop> defaultAppInterop =
  115. FIR_COMPONENT(FIRRemoteConfigInterop, defaultApp.container);
  116. id providerID = provider;
  117. id interopID = interop;
  118. id defaultAppProviderID = defaultAppProvider;
  119. id defaultAppInteropID = defaultAppInterop;
  120. XCTAssertEqualObjects(providerID, interopID);
  121. XCTAssertEqualObjects(defaultAppProviderID, defaultAppInteropID);
  122. // Check two apps get their own component to register
  123. XCTAssertNotEqualObjects(interopID, defaultAppInteropID);
  124. }
  125. - (void)testThrowsWithEmptyGoogleAppID {
  126. FIROptions *options = [self fakeOptions];
  127. options.googleAppID = @"";
  128. // Create the provider to vend Remote Config instances.
  129. NSString *appName = [self generatedTestAppName];
  130. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  131. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  132. // Creating a Remote Config instance should fail since the googleAppID is empty.
  133. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  134. }
  135. // Note: This cannot be tested in Swift.
  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. // Note: This cannot be tested in Swift.
  160. - (void)testThrowsWithNilGCMSenderID {
  161. FIROptions *options = [self fakeOptions];
  162. #pragma clang diagnostic push
  163. #pragma clang diagnostic ignored "-Wnonnull"
  164. options.GCMSenderID = nil;
  165. #pragma clang diagnostic pop
  166. // Create the provider to vend Remote Config instances.
  167. NSString *appName = [self generatedTestAppName];
  168. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  169. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  170. // Creating a Remote Config instance should fail since the GCMSenderID is nil.
  171. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  172. }
  173. - (void)testThrowsWithEmptyProjectID {
  174. FIROptions *options = [self fakeOptions];
  175. options.projectID = @"";
  176. // Create the provider to vend Remote Config instances.
  177. NSString *appName = [self generatedTestAppName];
  178. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  179. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  180. // Creating a Remote Config instance should fail since the projectID is empty.
  181. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  182. }
  183. // Note: This cannot be tested in Swift.
  184. - (void)testThrowsWithNilProjectID {
  185. FIROptions *options = [self fakeOptions];
  186. #pragma clang diagnostic push
  187. #pragma clang diagnostic ignored "-Wnonnull"
  188. options.projectID = nil;
  189. #pragma clang diagnostic pop
  190. // Create the provider to vend Remote Config instances.
  191. NSString *appName = [self generatedTestAppName];
  192. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  193. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  194. // Creating a Remote Config instance should fail since the projectID is empty.
  195. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  196. }
  197. #pragma mark - Helpers
  198. - (FIROptions *)fakeOptions {
  199. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  200. GCMSenderID:@"correct_gcm_sender_id"];
  201. options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
  202. options.projectID = @"project-id";
  203. return options;
  204. }
  205. - (NSString *)generatedTestAppName {
  206. return [RCNTestUtilities generatedTestAppNameForTest:self.name];
  207. }
  208. - (FIRRemoteConfigComponent *)providerForTest {
  209. // Create the provider to vend Remote Config instances.
  210. NSString *appName = [self generatedTestAppName];
  211. FIROptions *options = [[self fakeOptions] copy];
  212. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  213. FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  214. XCTAssertNotNil(provider);
  215. XCTAssert(provider.app.options.googleAppID.length != 0);
  216. XCTAssert(provider.app.options.GCMSenderID.length != 0);
  217. return provider;
  218. }
  219. @end