| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /*
- * Copyright 2019 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import <XCTest/XCTest.h>
- #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
- #import "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
- #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
- #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
- @interface FIRRemoteConfigComponentTest : XCTestCase
- @end
- @implementation FIRRemoteConfigComponentTest
- - (void)tearDown {
- [super tearDown];
- // Clear out any apps that were called with `configure`.
- [FIRApp resetApps];
- }
- - (void)testRCInstanceCreationAndCaching {
- // Create the provider to vend Remote Config instances.
- FIRRemoteConfigComponent *provider = [self providerForTest];
- // Create a Remote Config instance from the provider.
- NSString *sharedNamespace = @"some_namespace";
- FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
- XCTAssertNotNil(config);
- // Fetch an instance with the same namespace - should be the same instance.
- FIRRemoteConfig *sameConfig = [provider remoteConfigForNamespace:sharedNamespace];
- XCTAssertNotNil(sameConfig);
- XCTAssertEqual(config, sameConfig);
- }
- - (void)testRCSeparateInstancesForDifferentNamespaces {
- // Create the provider to vend Remote Config instances.
- FIRRemoteConfigComponent *provider = [self providerForTest];
- // Create a Remote Config instance from the provider.
- FIRRemoteConfig *config = [provider remoteConfigForNamespace:@"namespace1"];
- XCTAssertNotNil(config);
- // Fetch another instance with a different namespace.
- FIRRemoteConfig *config2 = [provider remoteConfigForNamespace:@"namespace2"];
- XCTAssertNotNil(config2);
- XCTAssertNotEqual(config, config2);
- }
- - (void)testRCSeparateInstancesForDifferentApps {
- FIRRemoteConfigComponent *provider = [self providerForTest];
- // Create a Remote Config instance from the provider.
- NSString *sharedNamespace = @"some_namespace";
- FIRRemoteConfig *config = [provider remoteConfigForNamespace:sharedNamespace];
- XCTAssertNotNil(config);
- // Use a new app and new povider, ensure the instances with the same namespace are different.
- NSString *secondAppName = [provider.app.name stringByAppendingString:@"2"];
- FIRApp *secondApp = [[FIRApp alloc] initInstanceWithName:secondAppName
- options:[self fakeOptions]];
- FIRRemoteConfigComponent *separateProvider =
- [[FIRRemoteConfigComponent alloc] initWithApp:secondApp];
- FIRRemoteConfig *separateConfig = [separateProvider remoteConfigForNamespace:sharedNamespace];
- XCTAssertNotNil(separateConfig);
- XCTAssertNotEqual(config, separateConfig);
- }
- - (void)testInitialization {
- // Explicitly instantiate the component here in case the providerForTest ever changes to mock
- // something.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:[self fakeOptions]];
- FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- XCTAssertNotNil(provider);
- XCTAssertNotNil(provider.app);
- }
- - (void)testRegistersAsLibrary {
- XCTAssertEqual([FIRRemoteConfigComponent componentsToRegister].count, 1);
- // Configure a test FIRApp for fetching an instance of the FIRRemoteConfigProvider.
- NSString *appName = [self generatedTestAppName];
- [FIRApp configureWithName:appName options:[self fakeOptions]];
- FIRApp *app = [FIRApp appNamed:appName];
- // Attempt to fetch the component and verify it's a valid instance.
- id<FIRRemoteConfigProvider> provider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
- XCTAssertNotNil(provider);
- // Ensure that the instance that comes from the container is cached.
- id<FIRRemoteConfigProvider> sameProvider = FIR_COMPONENT(FIRRemoteConfigProvider, app.container);
- XCTAssertNotNil(sameProvider);
- XCTAssertEqual(provider, sameProvider);
- }
- - (void)testThrowsWithEmptyGoogleAppID {
- FIROptions *options = [self fakeOptions];
- options.googleAppID = @"";
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- // Creating a Remote Config instance should fail since the googleAppID is empty.
- XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
- }
- - (void)testThrowsWithNilGoogleAppID {
- FIROptions *options = [self fakeOptions];
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wnonnull"
- options.googleAppID = nil;
- #pragma clang diagnostic pop
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- // Creating a Remote Config instance should fail since the googleAppID is nil.
- XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
- }
- - (void)testThrowsWithEmptyGCMSenderID {
- FIROptions *options = [self fakeOptions];
- options.GCMSenderID = @"";
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- // Creating a Remote Config instance should fail since the GCMSenderID is empty.
- XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
- }
- - (void)testThrowsWithNilGCMSenderID {
- FIROptions *options = [self fakeOptions];
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wnonnull"
- options.GCMSenderID = nil;
- #pragma clang diagnostic pop
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- // Creating a Remote Config instance should fail since the GCMSenderID is nil.
- XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
- }
- - (void)testThrowsWithEmptyProjectID {
- FIROptions *options = [self fakeOptions];
- options.projectID = @"";
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- // Creating a Remote Config instance should fail since the projectID is empty.
- XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
- }
- - (void)testThrowsWithNilProjectID {
- FIROptions *options = [self fakeOptions];
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wnonnull"
- options.projectID = nil;
- #pragma clang diagnostic pop
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *component = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- // Creating a Remote Config instance should fail since the projectID is empty.
- XCTAssertThrows([component remoteConfigForNamespace:@"some_namespace"]);
- }
- #pragma mark - Helpers
- - (FIROptions *)fakeOptions {
- FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
- GCMSenderID:@"correct_gcm_sender_id"];
- options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
- options.projectID = @"project-id";
- return options;
- }
- - (NSString *)generatedTestAppName {
- return [RCNTestUtilities generatedTestAppNameForTest:self.name];
- }
- - (FIRRemoteConfigComponent *)providerForTest {
- // Create the provider to vend Remote Config instances.
- NSString *appName = [self generatedTestAppName];
- FIROptions *options = [[self fakeOptions] copy];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:appName options:options];
- FIRRemoteConfigComponent *provider = [[FIRRemoteConfigComponent alloc] initWithApp:app];
- XCTAssertNotNil(provider);
- XCTAssert(provider.app.options.googleAppID.length != 0);
- XCTAssert(provider.app.options.GCMSenderID.length != 0);
- return provider;
- }
- @end
|