FIRRemoteConfigComponentTest.m 7.7 KB

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