FIRRemoteConfigComponentTest.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/FIRAppInternal.h>
  18. #import <FirebaseCore/FIRComponentContainer.h>
  19. #import <FirebaseCore/FIRLibrary.h>
  20. #import <FirebaseCore/FIROptionsInternal.h>
  21. #import "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
  22. #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
  23. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  24. @interface FIRRemoteConfigComponentTest : XCTestCase
  25. @end
  26. @implementation FIRRemoteConfigComponentTest
  27. - (void)tearDown {
  28. [super tearDown];
  29. // Clear out any apps that were called with `configure`.
  30. [FIRApp resetApps];
  31. }
  32. - (void)testRCInstanceCreationAndCaching {
  33. // Create the provider to vend Remote Config instances.
  34. FIRRemoteConfigComponent *provider = [self providerForTest];
  35. // Create a Remote Config instance from the provider.
  36. NSString *sharedNamespace = @"some_namespace";
  37. FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
  38. XCTAssertNotNil(config);
  39. // Fetch an instance with the same namespace - should be the same instance.
  40. FIRRemoteConfig *sameConfig = [provider remoteConfigForNamespace:sharedNamespace];
  41. XCTAssertNotNil(sameConfig);
  42. XCTAssertEqual(config, sameConfig);
  43. }
  44. - (void)testRCSeparateInstancesForDifferentNamespaces {
  45. // Create the provider to vend Remote Config instances.
  46. FIRRemoteConfigComponent *provider = [self providerForTest];
  47. // Create a Remote Config instance from the provider.
  48. FIRRemoteConfig *config = [provider remoteConfigForNamespace:@"namespace1"];
  49. XCTAssertNotNil(config);
  50. // Fetch another instance with a different namespace.
  51. FIRRemoteConfig *config2 = [provider remoteConfigForNamespace:@"namespace2"];
  52. XCTAssertNotNil(config2);
  53. XCTAssertNotEqual(config, config2);
  54. }
  55. - (void)testRCSeparateInstancesForDifferentApps {
  56. FIRRemoteConfigComponent *provider = [self providerForTest];
  57. // Create a Remote Config instance from the provider.
  58. NSString *sharedNamespace = @"some_namespace";
  59. FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
  60. XCTAssertNotNil(config);
  61. // Use a new app and new povider, ensure the instances with the same namespace are different.
  62. NSString *secondAppName = [provider.app.name stringByAppendingString:@"2"];
  63. FIRApp *secondApp = [[FIRApp alloc] initInstanceWithName:secondAppName
  64. options:[self fakeOptions]];
  65. FIRRemoteConfigComponent *separateProvider =
  66. [[FIRRemoteConfigComponent alloc] initWithApp:secondApp];
  67. FIRRemoteConfig *separateConfig = [separateProvider remoteConfigForNamespace:sharedNamespace];
  68. XCTAssertNotNil(separateConfig);
  69. XCTAssertNotEqual(config, separateConfig);
  70. }
  71. - (void)testInitialization {
  72. // Explicitly instantiate the component here in case the providerForTest ever changes to mock
  73. // something.
  74. NSString *appName = [self generatedTestAppName];
  75. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:[self fakeOptions]];
  76. FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  77. XCTAssertNotNil(provider);
  78. XCTAssertNotNil(provider.app);
  79. }
  80. - (void)testRegistersAsLibrary {
  81. XCTAssertEqual([FIRRemoteConfigComponent componentsToRegister].count, 1);
  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. XCTAssertNotNil(provider);
  89. // Ensure that the instance that comes from the container is cached.
  90. id<FIRRemoteConfigProvider> sameProvider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
  91. XCTAssertNotNil(sameProvider);
  92. XCTAssertEqual(provider, sameProvider);
  93. }
  94. - (void)testThrowsWithEmptyGoogleAppID {
  95. FIROptions *options = [self fakeOptions];
  96. options.googleAppID = @"";
  97. // Create the provider to vend Remote Config instances.
  98. NSString *appName = [self generatedTestAppName];
  99. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  100. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  101. // Creating a Remote Config instance should fail since the googleAppID is empty.
  102. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  103. }
  104. - (void)testThrowsWithNilGoogleAppID {
  105. FIROptions *options = [self fakeOptions];
  106. #pragma clang diagnostic push
  107. #pragma clang diagnostic ignored "-Wnonnull"
  108. options.googleAppID = nil;
  109. #pragma clang diagnostic pop
  110. // Create the provider to vend Remote Config instances.
  111. NSString *appName = [self generatedTestAppName];
  112. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  113. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  114. // Creating a Remote Config instance should fail since the googleAppID is nil.
  115. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  116. }
  117. - (void)testThrowsWithEmptyGCMSenderID {
  118. FIROptions *options = [self fakeOptions];
  119. options.GCMSenderID = @"";
  120. // Create the provider to vend Remote Config instances.
  121. NSString *appName = [self generatedTestAppName];
  122. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  123. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  124. // Creating a Remote Config instance should fail since the GCMSenderID is empty.
  125. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  126. }
  127. - (void)testThrowsWithNilGCMSenderID {
  128. FIROptions *options = [self fakeOptions];
  129. #pragma clang diagnostic push
  130. #pragma clang diagnostic ignored "-Wnonnull"
  131. options.GCMSenderID = nil;
  132. #pragma clang diagnostic pop
  133. // Create the provider to vend Remote Config instances.
  134. NSString *appName = [self generatedTestAppName];
  135. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  136. FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  137. // Creating a Remote Config instance should fail since the GCMSenderID is nil.
  138. XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
  139. }
  140. #pragma mark - Helpers
  141. - (FIROptions *)fakeOptions {
  142. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  143. GCMSenderID:@"correct_gcm_sender_id"];
  144. options.APIKey = @"api-key";
  145. options.projectID = @"project-id";
  146. return options;
  147. }
  148. - (NSString *)generatedTestAppName {
  149. return [RCNTestUtilities generatedTestAppNameForTest:self.name];
  150. }
  151. - (FIRRemoteConfigComponent *)providerForTest {
  152. // Create the provider to vend Remote Config instances.
  153. NSString *appName = [self generatedTestAppName];
  154. FIROptions *options = [[self fakeOptions] copy];
  155. FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
  156. FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
  157. XCTAssertNotNil(provider);
  158. XCTAssert(provider.app.options.googleAppID.length != 0);
  159. XCTAssert(provider.app.options.GCMSenderID.length != 0);
  160. return provider;
  161. }
  162. @end