| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508 |
- /*
- * 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 <OCMock/OCMStubRecorder.h>
- #import <OCMock/OCMock.h>
- #import <XCTest/XCTest.h>
- #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
- #import "FirebaseRemoteConfig/Sources/Private/RCNConfigFetch.h"
- #import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
- #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
- #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
- #import "FirebaseRemoteConfig/Sources/RCNConfigExperiment.h"
- #import "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
- #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
- #import <GoogleUtilities/GULNSData+zlib.h>
- #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
- @interface RCNConfigFetch (ForTest)
- - (instancetype)initWithContent:(RCNConfigContent *)content
- DBManager:(RCNConfigDBManager *)DBManager
- settings:(RCNConfigSettings *)settings
- experiment:(RCNConfigExperiment *)experiment
- queue:(dispatch_queue_t)queue
- namespace:firebaseNamespace
- app:firebaseApp;
- /// Skip fetching user properties from analytics because we cannot mock the action here. Instead
- /// overriding the method to skip.
- - (void)fetchWithUserPropertiesCompletionHandler:(FIRAInteropUserPropertiesCallback)block;
- - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
- completionHandler:
- (RCNConfigFetcherCompletion)fetcherCompletion;
- - (void)fetchWithUserProperties:(NSDictionary *)userProperties
- completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
- - (NSString *)constructServerURL;
- - (NSURLSession *)currentNetworkSession;
- @end
- @interface FIRRemoteConfig (ForTest)
- - (void)updateWithNewInstancesForConfigFetch:(RCNConfigFetch *)configFetch
- configContent:(RCNConfigContent *)configContent
- configSettings:(RCNConfigSettings *)configSettings
- configExperiment:(RCNConfigExperiment *)configExperiment;
- @end
- @implementation FIRRemoteConfig (ForTest)
- - (void)updateWithNewInstancesForConfigFetch:(RCNConfigFetch *)configFetch
- configContent:(RCNConfigContent *)configContent
- configSettings:(RCNConfigSettings *)configSettings
- configExperiment:(RCNConfigExperiment *)configExperiment {
- [self setValue:configFetch forKey:@"_configFetch"];
- [self setValue:configContent forKey:@"_configContent"];
- [self setValue:configSettings forKey:@"_settings"];
- [self setValue:configExperiment forKey:@"_configExperiment"];
- }
- @end
- @interface RCNConfigDBManager (Test)
- - (void)removeDatabaseOnDatabaseQueueAtPath:(NSString *)path;
- @end
- @interface RCNUserDefaultsManager (Test)
- + (NSUserDefaults *)sharedUserDefaultsForBundleIdentifier:(NSString *)bundleIdentifier;
- @end
- @interface RCNConfigSettings (Test)
- - (NSString *)nextRequestWithUserProperties:(NSDictionary *)userProperties;
- @end
- typedef NS_ENUM(NSInteger, RCNTestRCInstance) {
- RCNTestRCInstanceDefault,
- RCNTestRCInstanceSecondNamespace,
- RCNTestRCInstanceSecondApp,
- RCNTestRCNumTotalInstances
- };
- @interface RCNRemoteConfigTest : XCTestCase {
- NSTimeInterval _expectationTimeout;
- NSTimeInterval _checkCompletionTimeout;
- NSMutableArray<FIRRemoteConfig *> *_configInstances;
- NSMutableArray<NSDictionary<NSString *, NSString *> *> *_entries;
- NSMutableArray<NSDictionary<NSString *, id> *> *_response;
- NSMutableArray<NSData *> *_responseData;
- NSMutableArray<NSURLResponse *> *_URLResponse;
- NSMutableArray<id> *_configFetch;
- RCNConfigDBManager *_DBManager;
- NSUserDefaults *_userDefaults;
- NSString *_userDefaultsSuiteName;
- NSString *_DBPath;
- id _DBManagerMock;
- id _experimentMock;
- id _userDefaultsMock;
- NSString *_fullyQualifiedNamespace;
- RCNConfigSettings *_settings;
- dispatch_queue_t _queue;
- }
- @end
- @implementation RCNRemoteConfigTest
- - (void)setUp {
- [super setUp];
- FIRSetLoggerLevel(FIRLoggerLevelMax);
- _expectationTimeout = 5;
- _checkCompletionTimeout = 1.0;
- // Always remove the database at the start of testing.
- _DBPath = [RCNTestUtilities remoteConfigPathForTestDatabase];
- _DBManagerMock = OCMClassMock([RCNConfigDBManager class]);
- OCMStub([_DBManagerMock remoteConfigPathForDatabase]).andReturn(_DBPath);
- _DBManager = [[RCNConfigDBManager alloc] init];
- _userDefaultsSuiteName = [RCNTestUtilities userDefaultsSuiteNameForTestSuite];
- _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:_userDefaultsSuiteName];
- _userDefaultsMock = OCMClassMock([RCNUserDefaultsManager class]);
- OCMStub([_userDefaultsMock sharedUserDefaultsForBundleIdentifier:[OCMArg any]])
- .andReturn(_userDefaults);
- _experimentMock = OCMClassMock([RCNConfigExperiment class]);
- OCMStub([_experimentMock
- updateExperimentsWithHandler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
- RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
- _configInstances = [[NSMutableArray alloc] initWithCapacity:3];
- _entries = [[NSMutableArray alloc] initWithCapacity:3];
- _response = [[NSMutableArray alloc] initWithCapacity:3];
- _responseData = [[NSMutableArray alloc] initWithCapacity:3];
- _URLResponse = [[NSMutableArray alloc] initWithCapacity:3];
- _configFetch = [[NSMutableArray alloc] initWithCapacity:3];
- // Populate the default, second app, second namespace instances.
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- // Fake a response for default instance.
- NSMutableDictionary<NSString *, NSString *> *valuesDict = [[NSMutableDictionary alloc] init];
- for (int count = 1; count <= 100; count++) {
- NSString *key = [NSString stringWithFormat:@"key%d-%d", count, i];
- NSString *value = [NSString stringWithFormat:@"value%d-%d", count, i];
- valuesDict[key] = value;
- }
- _entries[i] = valuesDict;
- NSString *currentAppName = nil;
- FIROptions *currentOptions = nil;
- NSString *currentNamespace = nil;
- switch (i) {
- case RCNTestRCInstanceSecondNamespace:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsPerfNamespace;
- break;
- case RCNTestRCInstanceSecondApp:
- currentAppName = RCNTestsSecondFIRAppName;
- currentOptions = [self secondAppOptions];
- currentNamespace = FIRNamespaceGoogleMobilePlatform;
- break;
- case RCNTestRCInstanceDefault:
- default:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsFIRNamespace;
- break;
- }
- _fullyQualifiedNamespace =
- [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
- FIRRemoteConfig *config =
- OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
- FIROptions:currentOptions
- namespace:currentNamespace
- DBManager:_DBManager
- configContent:configContent
- analytics:nil]);
- _configInstances[i] = config;
- _settings = [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
- namespace:_fullyQualifiedNamespace
- firebaseAppName:currentAppName
- googleAppID:currentOptions.googleAppID];
- _queue = dispatch_queue_create(
- [[NSString stringWithFormat:@"testqueue: %d", i] cStringUsingEncoding:NSUTF8StringEncoding],
- DISPATCH_QUEUE_SERIAL);
- _configFetch[i] =
- OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
- DBManager:_DBManager
- settings:_settings
- analytics:nil
- experiment:_experimentMock
- queue:_queue
- namespace:_fullyQualifiedNamespace
- options:currentOptions]);
- OCMStubRecorder *mock = OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:0
- completionHandler:OCMOCK_ANY]);
- mock = [mock ignoringNonObjectArgs];
- mock.andDo(^(NSInvocation *invocation) {
- __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
- NSError *_Nullable error) = nil;
- [invocation getArgument:&handler atIndex:3];
- [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
- completionHandler:handler];
- });
- _response[i] = @{@"state" : @"UPDATE", @"entries" : _entries[i]};
- _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
- _URLResponse[i] = [[NSHTTPURLResponse alloc]
- initWithURL:[NSURL URLWithString:@"https://firebase.com"]
- statusCode:200
- HTTPVersion:nil
- headerFields:@{@"etag" : [NSString stringWithFormat:@"etag1-%d", i]}];
- id completionBlock =
- [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
- OCMStub([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
- completionHandler:completionBlock])
- .andReturn(nil);
- [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
- configContent:configContent
- configSettings:_settings
- configExperiment:_experimentMock];
- }
- }
- - (void)tearDown {
- [_DBManager removeDatabaseOnDatabaseQueueAtPath:_DBPath];
- [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:_userDefaultsSuiteName];
- [_DBManagerMock stopMocking];
- _DBManagerMock = nil;
- [_userDefaultsMock stopMocking];
- _userDefaultsMock = nil;
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- [(id)_configInstances[i] stopMocking];
- [(id)_configFetch[i] stopMocking];
- }
- [_configInstances removeAllObjects];
- [_configFetch removeAllObjects];
- _configInstances = nil;
- _configFetch = nil;
- [super tearDown];
- }
- - (void)testFetchConfigWithNilCallback {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:nil];
- dispatch_after(
- dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_checkCompletionTimeout * NSEC_PER_SEC)),
- dispatch_get_main_queue(), ^{
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus,
- FIRRemoteConfigFetchStatusSuccess);
- [expectations[i] fulfill];
- });
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
- }
- - (void)testFetchConfigsSuccessfully {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] =
- [self expectationWithDescription:
- [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
- OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
- @"Callback of first successful config "
- @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
- XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
- XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
- @"last fetch time interval should be set.");
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testFetchAndActivate {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] =
- [self expectationWithDescription:
- [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion = ^void(
- FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
- OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
- XCTAssertEqual(
- status, FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote,
- @"Callback of first successful config "
- @"fetchAndActivate status must equal to FIRRemoteConfigFetchAndStatusSuccessFromRemote.");
- XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
- XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
- @"last fetch time interval should be set.");
- [expectations[i] fulfill];
- };
- // Update the minimum fetch interval to 0. This disables the cache and forces a remote fetch
- // request.
- FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
- settings.minimumFetchInterval = 0;
- [_configInstances[i] setConfigSettings:settings];
- [_configInstances[i] fetchAndActivateWithCompletionHandler:fetchAndActivateCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- // TODO: Try splitting into smaller tests.
- - (void)testFetchConfigsSuccessfullyWithNewActivateMethodSignature {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] =
- [self expectationWithDescription:
- [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- XCTAssertNil(error);
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
- OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
- @"Callback of first successful config "
- @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
- XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
- XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
- @"last fetch time interval should be set.");
- // A second activate should have no effect.
- [self->_configInstances[i]
- activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertFalse(changed);
- XCTAssertNil(error);
- }];
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testEnumeratingConfigResults {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- NSString *key5 = [NSString stringWithFormat:@"key5-%d", i];
- NSString *key19 = [NSString stringWithFormat:@"key19-%d", i];
- NSString *value5 = [NSString stringWithFormat:@"value5-%d", i];
- NSString *value19 = [NSString stringWithFormat:@"value19-%d", i];
- XCTAssertEqualObjects(self->_configInstances[i][key5].stringValue, value5);
- XCTAssertEqualObjects(self->_configInstances[i][key19].stringValue, value19);
- // Test enumerating config values.
- for (NSString *key in self->_configInstances[i]) {
- if ([key isEqualToString:key5]) {
- XCTAssertEqualObjects(self->_configInstances[i][key5].stringValue, value5);
- }
- if ([key isEqualToString:key19]) {
- XCTAssertEqualObjects(self->_configInstances[i][key19].stringValue, value19);
- }
- }
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
- @"Callback of first successful config "
- @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
- XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
- XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
- @"last fetch time interval should be set.");
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testFetchAndActivate3pNamespaceUpdatesExperiments {
- [[_experimentMock expect] updateExperimentsWithResponse:[OCMArg any]];
- XCTestExpectation *expectation = [self
- expectationWithDescription:[NSString stringWithFormat:@"FetchAndActivate call for 'firebase' "
- @"namespace updates experiment data"]];
- XCTAssertEqual(_configInstances[RCNTestRCInstanceDefault].lastFetchStatus,
- FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion =
- ^void(FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
- XCTAssertEqual(status, FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote);
- XCTAssertNil(error);
- XCTAssertEqual(self->_configInstances[RCNTestRCInstanceDefault].lastFetchStatus,
- FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNotNil(self->_configInstances[RCNTestRCInstanceDefault].lastFetchTime);
- XCTAssertGreaterThan(
- self->_configInstances[RCNTestRCInstanceDefault].lastFetchTime.timeIntervalSince1970, 0,
- @"last fetch time interval should be set.");
- [expectation fulfill];
- };
- [_configInstances[RCNTestRCInstanceDefault]
- fetchAndActivateWithCompletionHandler:fetchAndActivateCompletion];
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testFetchAndActivateOtherNamespaceDoesntUpdateExperiments {
- [[_experimentMock reject] updateExperimentsWithResponse:[OCMArg any]];
- XCTestExpectation *expectation = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"FetchAndActivate call for namespace other than 'firebase' "
- @"doesn't update experiment data"]];
- XCTAssertEqual(_configInstances[RCNTestRCInstanceSecondNamespace].lastFetchStatus,
- FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion =
- ^void(FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
- XCTAssertEqual(status, FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote);
- XCTAssertNil(error);
- XCTAssertEqual(self->_configInstances[RCNTestRCInstanceSecondNamespace].lastFetchStatus,
- FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNotNil(self->_configInstances[RCNTestRCInstanceSecondNamespace].lastFetchTime);
- XCTAssertGreaterThan(self->_configInstances[RCNTestRCInstanceSecondNamespace]
- .lastFetchTime.timeIntervalSince1970,
- 0, @"last fetch time interval should be set.");
- [expectation fulfill];
- };
- [_configInstances[RCNTestRCInstanceSecondNamespace]
- fetchAndActivateWithCompletionHandler:fetchAndActivateCompletion];
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testFetchConfigsFailed {
- // Override the setup values to return back an error status.
- RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
- // Populate the default, second app, second namespace instances.
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- NSString *currentAppName = nil;
- FIROptions *currentOptions = nil;
- NSString *currentNamespace = nil;
- switch (i) {
- case RCNTestRCInstanceSecondNamespace:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsPerfNamespace;
- break;
- case RCNTestRCInstanceSecondApp:
- currentAppName = RCNTestsSecondFIRAppName;
- currentOptions = [self secondAppOptions];
- currentNamespace = FIRNamespaceGoogleMobilePlatform;
- break;
- case RCNTestRCInstanceDefault:
- default:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsFIRNamespace;
- break;
- }
- RCNUserDefaultsManager *userDefaultsManager =
- [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
- bundleID:[NSBundle mainBundle].bundleIdentifier
- namespace:_fullyQualifiedNamespace];
- userDefaultsManager.lastFetchTime = 0;
- FIRRemoteConfig *config =
- OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
- FIROptions:currentOptions
- namespace:currentNamespace
- DBManager:_DBManager
- configContent:configContent
- analytics:nil]);
- _configInstances[i] = config;
- _configFetch[i] =
- OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
- DBManager:_DBManager
- settings:_settings
- analytics:nil
- experiment:nil
- queue:_queue
- namespace:_fullyQualifiedNamespace
- options:currentOptions]);
- OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
- NSError *_Nullable error) = nil;
- [invocation getArgument:&handler atIndex:3];
- [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
- completionHandler:handler];
- });
- _response[i] = @{};
- _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
- _URLResponse[i] =
- [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
- statusCode:500
- HTTPVersion:nil
- headerFields:@{@"etag" : @"etag1"}];
- [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
- configContent:configContent
- configSettings:_settings
- configExperiment:nil];
- }
- // Make the fetch calls for all instances.
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertFalse(changed);
- XCTAssertNil(error);
- FIRRemoteConfigValue *value = self->_configInstances[RCNTestRCInstanceDefault][@"key1"];
- XCTAssertEqual((int)value.source, (int)FIRRemoteConfigSourceStatic);
- XCTAssertEqualObjects(value.stringValue, @"");
- XCTAssertEqual(value.boolValue, NO);
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- // TODO(mandard): Break up test with helper methods.
- - (void)testFetchConfigsFailedErrorNoNetwork {
- // Override the setup values to return back an error status.
- RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
- // Populate the default, second app, second namespace instances.
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- NSString *currentAppName = nil;
- FIROptions *currentOptions = nil;
- NSString *currentNamespace = nil;
- switch (i) {
- case RCNTestRCInstanceSecondNamespace:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsPerfNamespace;
- break;
- case RCNTestRCInstanceSecondApp:
- currentAppName = RCNTestsSecondFIRAppName;
- currentOptions = [self secondAppOptions];
- currentNamespace = FIRNamespaceGoogleMobilePlatform;
- break;
- case RCNTestRCInstanceDefault:
- default:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsFIRNamespace;
- break;
- }
- NSString *fullyQualifiedNamespace =
- [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
- RCNUserDefaultsManager *userDefaultsManager =
- [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
- bundleID:[NSBundle mainBundle].bundleIdentifier
- namespace:fullyQualifiedNamespace];
- userDefaultsManager.lastFetchTime = 0;
- FIRRemoteConfig *config =
- OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
- FIROptions:currentOptions
- namespace:currentNamespace
- DBManager:_DBManager
- configContent:configContent
- analytics:nil]);
- _configInstances[i] = config;
- RCNConfigSettings *settings =
- [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
- namespace:fullyQualifiedNamespace
- firebaseAppName:currentAppName
- googleAppID:currentOptions.googleAppID];
- dispatch_queue_t queue = dispatch_queue_create(
- [[NSString stringWithFormat:@"testqueue: %d", i] cStringUsingEncoding:NSUTF8StringEncoding],
- DISPATCH_QUEUE_SERIAL);
- _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
- DBManager:_DBManager
- settings:settings
- analytics:nil
- experiment:nil
- queue:queue
- namespace:fullyQualifiedNamespace
- options:currentOptions]);
- OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
- NSError *_Nullable error) = nil;
- [invocation getArgument:&handler atIndex:3];
- [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
- completionHandler:handler];
- });
- _response[i] = @{};
- _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
- // A no network error is accompanied with an HTTP status code of 0.
- _URLResponse[i] =
- [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
- statusCode:0
- HTTPVersion:nil
- headerFields:@{@"etag" : @"etag1"}];
- [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
- configContent:configContent
- configSettings:settings
- configExperiment:nil];
- }
- // Make the fetch calls for all instances.
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertFalse(changed);
- XCTAssertNil(error);
- // No such key, still return a static value.
- FIRRemoteConfigValue *value = self->_configInstances[RCNTestRCInstanceDefault][@"key1"];
- XCTAssertEqual((int)value.source, (int)FIRRemoteConfigSourceStatic);
- XCTAssertEqualObjects(value.stringValue, @"");
- XCTAssertEqual(value.boolValue, NO);
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testFetchFailedNoNetworkErrorDoesNotThrottle {
- RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
- NSString *currentAppName = RCNTestsDefaultFIRAppName;
- FIROptions *currentOptions = [self firstAppOptions];
- NSString *currentNamespace = RCNTestsFIRNamespace;
- NSString *fullyQualifiedNamespace =
- [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
- RCNUserDefaultsManager *userDefaultsManager =
- [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
- bundleID:[NSBundle mainBundle].bundleIdentifier
- namespace:fullyQualifiedNamespace];
- userDefaultsManager.lastFetchTime = 0;
- FIRRemoteConfig *config = OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
- FIROptions:currentOptions
- namespace:currentNamespace
- DBManager:_DBManager
- configContent:configContent
- analytics:nil]);
- RCNConfigSettings *settings =
- [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
- namespace:fullyQualifiedNamespace
- firebaseAppName:currentAppName
- googleAppID:currentOptions.googleAppID];
- dispatch_queue_t queue = dispatch_queue_create(
- [[NSString stringWithFormat:@"testqueue"] cStringUsingEncoding:NSUTF8StringEncoding],
- DISPATCH_QUEUE_SERIAL);
- RCNConfigFetch *configFetch =
- OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
- DBManager:_DBManager
- settings:settings
- analytics:nil
- experiment:nil
- queue:queue
- namespace:fullyQualifiedNamespace
- options:currentOptions]);
- OCMStub([configFetch fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
- NSError *_Nullable error) = nil;
- [invocation getArgument:&handler atIndex:3];
- [configFetch fetchWithUserProperties:[[NSDictionary alloc] init] completionHandler:handler];
- });
- _responseData[0] = [NSJSONSerialization dataWithJSONObject:@{} options:0 error:nil];
- // A no network error is accompanied with an HTTP status code of 0.
- _URLResponse[0] =
- [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
- statusCode:0
- HTTPVersion:nil
- headerFields:@{@"etag" : @"etag1"}];
- [config updateWithNewInstancesForConfigFetch:configFetch
- configContent:configContent
- configSettings:settings
- configExperiment:nil];
- XCTestExpectation *expectation =
- [self expectationWithDescription:@"Network error doesn't increase throttle interval"];
- XCTAssertEqual(config.lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion =
- ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
- XCTAssertEqual(config.lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
- XCTAssertEqual(settings.exponentialBackoffRetryInterval, 0);
- [expectation fulfill];
- };
- [config fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- // Activate should return false if a fetch response returns 200 with NO_CHANGE as the response body.
- - (void)testActivateOnFetchNoChangeStatus {
- // Override the setup values to return back an error status.
- RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
- // Populate the default, second app, second namespace instances.
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- NSString *currentAppName = nil;
- FIROptions *currentOptions = nil;
- NSString *currentNamespace = nil;
- switch (i) {
- case RCNTestRCInstanceSecondNamespace:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsPerfNamespace;
- break;
- case RCNTestRCInstanceSecondApp:
- currentAppName = RCNTestsSecondFIRAppName;
- currentOptions = [self secondAppOptions];
- currentNamespace = FIRNamespaceGoogleMobilePlatform;
- break;
- case RCNTestRCInstanceDefault:
- default:
- currentAppName = RCNTestsDefaultFIRAppName;
- currentOptions = [self firstAppOptions];
- currentNamespace = RCNTestsFIRNamespace;
- break;
- }
- NSString *fullyQualifiedNamespace =
- [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
- RCNUserDefaultsManager *userDefaultsManager =
- [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
- bundleID:[NSBundle mainBundle].bundleIdentifier
- namespace:fullyQualifiedNamespace];
- userDefaultsManager.lastFetchTime = 10;
- FIRRemoteConfig *config =
- OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
- FIROptions:currentOptions
- namespace:currentNamespace
- DBManager:_DBManager
- configContent:configContent
- analytics:nil]);
- _configInstances[i] = config;
- RCNConfigSettings *settings =
- [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
- namespace:fullyQualifiedNamespace
- firebaseAppName:currentAppName
- googleAppID:currentOptions.googleAppID];
- // Start the test with the assumption that we have some data that was fetched and activated.
- settings.lastETag = @"etag1";
- settings.lastETagUpdateTime = 100;
- settings.lastApplyTimeInterval = 101;
- dispatch_queue_t queue =
- dispatch_queue_create([[NSString stringWithFormat:@"testNoStatusFetchQueue: %d", i]
- cStringUsingEncoding:NSUTF8StringEncoding],
- DISPATCH_QUEUE_SERIAL);
- _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
- DBManager:_DBManager
- settings:settings
- analytics:nil
- experiment:nil
- queue:queue
- namespace:fullyQualifiedNamespace
- options:currentOptions]);
- OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
- .andDo(^(NSInvocation *invocation) {
- __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
- NSError *_Nullable error) = nil;
- [invocation getArgument:&handler atIndex:3];
- [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
- completionHandler:handler];
- });
- _response[i] = @{@"state" : @"NO_CHANGE"};
- _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
- _URLResponse[i] =
- [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
- statusCode:200
- HTTPVersion:nil
- headerFields:@{@"etag" : @"etag1"}];
- id completionBlock =
- [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
- OCMStub([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
- completionHandler:completionBlock])
- .andReturn(nil);
- [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
- configContent:configContent
- configSettings:settings
- configExperiment:nil];
- }
- // Make the fetch calls for all instances.
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- // Make sure activate returns false in fetch completion.
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertFalse(changed);
- XCTAssertNil(error);
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testConfigValueForKey {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] =
- [self expectationWithDescription:
- [NSString stringWithFormat:@"Test configValueForKey: method - instance %d", i]];
- XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
- NSString *key3 = [NSString stringWithFormat:@"key3-%d", i];
- NSString *key7 = [NSString stringWithFormat:@"key7-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
- NSString *value3 = [NSString stringWithFormat:@"value3-%d", i];
- NSString *value7 = [NSString stringWithFormat:@"value7-%d", i];
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
- OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
- XCTAssertEqualObjects([self->_configInstances[i] configValueForKey:key3].stringValue,
- value3);
- if (i == RCNTestRCInstanceDefault) {
- XCTAssertEqualObjects([self->_configInstances[i] configValueForKey:key7].stringValue,
- value7);
- }
- XCTAssertEqualObjects([self->_configInstances[i] configValueForKey:key7].stringValue,
- value7);
- XCTAssertNotNil([self->_configInstances[i] configValueForKey:nil]);
- XCTAssertEqual([self->_configInstances[i] configValueForKey:nil].source,
- FIRRemoteConfigSourceStatic);
- XCTAssertEqual([self->_configInstances[i] configValueForKey:nil].source,
- FIRRemoteConfigSourceStatic);
- XCTAssertEqual([self->_configInstances[i] configValueForKey:nil source:-1].source,
- FIRRemoteConfigSourceStatic);
- [expectations[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testFetchConfigWithDefaultSets {
- NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- fetchConfigsExpectation[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
- NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
- NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
- [_configInstances[i] setDefaults:defaults];
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, @"default key1");
- XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceDefault);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
- XCTAssertEqualObjects([self->_configInstances[i] defaultValueForKey:key1].stringValue,
- @"default key1");
- XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
- XCTAssertEqualObjects(self->_configInstances[i][key0].stringValue, @"value0-0");
- XCTAssertNil([self->_configInstances[i] defaultValueForKey:nil]);
- OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
- @"Callback of first successful config "
- @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
- [fetchConfigsExpectation[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testDefaultsSetsOnly {
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
- NSString *key3 = [NSString stringWithFormat:@"key3-%d", i];
- NSString *key4 = [NSString stringWithFormat:@"key4-%d", i];
- NSString *key5 = [NSString stringWithFormat:@"key5-%d", i];
- NSString *defaultValue1 = @"default value1";
- NSData *defaultValue2 = [defaultValue1 dataUsingEncoding:NSUTF8StringEncoding];
- NSNumber *defaultValue3 = [NSNumber numberWithFloat:3.1415926];
- NSDate *defaultValue4 = [NSDate date];
- BOOL defaultValue5 = NO;
- NSMutableDictionary<NSString *, id> *defaults = [NSMutableDictionary dictionaryWithDictionary:@{
- key1 : defaultValue1,
- key2 : defaultValue2,
- key3 : defaultValue3,
- key4 : defaultValue4,
- key5 : @(defaultValue5),
- }];
- [_configInstances[i] setDefaults:defaults];
- if (i == RCNTestRCInstanceSecondNamespace) {
- [defaults setObject:@"2860" forKey:@"experience"];
- [_configInstances[i] setDefaults:defaults];
- }
- // Remove objects right away to make sure dispatch_async gets the copy.
- [defaults removeAllObjects];
- FIRRemoteConfig *config = _configInstances[i];
- XCTAssertEqualObjects(config[key1].stringValue, defaultValue1, @"Should support string format");
- XCTAssertEqualObjects(config[key2].dataValue, defaultValue2, @"Should support data format");
- XCTAssertEqual(config[key3].numberValue.longValue, defaultValue3.longValue,
- @"Should support number format");
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
- NSString *strValueOfDate = [dateFormatter stringFromDate:(NSDate *)defaultValue4];
- XCTAssertEqualObjects(
- config[key4].stringValue, strValueOfDate,
- @"Date format can be set as an input from plist, but output coming out of "
- @"defaultConfig as string.");
- XCTAssertEqual(config[key5].boolValue, defaultValue5, @"Should support bool format");
- if (i == RCNTestRCInstanceSecondNamespace) {
- XCTAssertEqualObjects(
- [_configInstances[i] configValueForKey:@"experience"].stringValue, @"2860",
- @"Only default config has the key, must equal to default config value.");
- }
- // Reset default sets
- [_configInstances[i] setDefaults:nil];
- XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 0);
- }
- }
- - (void)testSetDefaultsWithNilParams {
- NSMutableArray<XCTestExpectation *> *expectations =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- expectations[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
- // Should work when passing nil.
- [_configInstances[i] setDefaults:nil];
- [_configInstances[i] setDefaults:nil];
- dispatch_after(
- dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_checkCompletionTimeout * NSEC_PER_SEC)),
- dispatch_get_main_queue(), ^{
- XCTAssertEqual(
- [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 0);
- [expectations[i] fulfill];
- });
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
- }
- - (void)testFetchConfigOverwriteDefaultSet {
- NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- fetchConfigsExpectation[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- [_configInstances[i] setDefaults:@{key1 : @"default key1"}];
- FIRRemoteConfigValue *value = _configInstances[i][key1];
- XCTAssertEqualObjects(value.stringValue, @"default key1");
- XCTAssertEqual(value.source, FIRRemoteConfigSourceDefault);
- value = _configInstances[i][@"A key doesn't exist"];
- XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
- XCTAssertEqualObjects([self->_configInstances[i] defaultValueForKey:key1].stringValue,
- @"default key1");
- OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
- @"Callback of first successful config "
- @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
- [fetchConfigsExpectation[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testGetConfigValueBySource {
- NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- fetchConfigsExpectation[i] =
- [self expectationWithDescription:
- [NSString stringWithFormat:@"Test get config value by source - instance %d", i]];
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
- NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default value1"};
- [_configInstances[i] setDefaults:defaults];
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, @"default value1");
- XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceDefault);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
- XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
- FIRRemoteConfigValue *value;
- if (i == RCNTestRCInstanceDefault) {
- value = [self->_configInstances[i] configValueForKey:key1
- source:FIRRemoteConfigSourceRemote];
- XCTAssertEqualObjects(value.stringValue, value1);
- value = [self->_configInstances[i] configValueForKey:key1
- source:FIRRemoteConfigSourceDefault];
- XCTAssertEqualObjects(value.stringValue, @"default value1");
- value = [self->_configInstances[i] configValueForKey:key1
- source:FIRRemoteConfigSourceStatic];
- } else {
- value = [self->_configInstances[i] configValueForKey:key1
- source:FIRRemoteConfigSourceRemote];
- XCTAssertEqualObjects(value.stringValue, value1);
- value = [self->_configInstances[i] configValueForKey:key1
- source:FIRRemoteConfigSourceDefault];
- XCTAssertEqualObjects(value.stringValue, @"default value1");
- value = [self->_configInstances[i] configValueForKey:key1
- source:FIRRemoteConfigSourceStatic];
- }
- XCTAssertEqualObjects(value.stringValue, @"");
- XCTAssertEqualObjects(value.numberValue, @(0));
- XCTAssertEqual(value.boolValue, NO);
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
- @"Callback of first successful config "
- @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
- [fetchConfigsExpectation[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
- }
- - (void)testInvalidKeyOrNamespace {
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- FIRRemoteConfigValue *value = [_configInstances[i] configValueForKey:nil];
- XCTAssertNotNil(value);
- XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
- value = [_configInstances[i] configValueForKey:nil];
- XCTAssertNotNil(value);
- XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
- value = [_configInstances[i] configValueForKey:nil source:5];
- XCTAssertNotNil(value);
- XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
- }
- }
- // Remote Config converts UTC times in the plists to local times. This utility function makes it
- // possible to check the times when running the tests in any timezone.
- static NSString *UTCToLocal(NSString *utcTime) {
- // Create a UTC dateFormatter.
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
- [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
- NSDate *date = [dateFormatter dateFromString:utcTime];
- [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
- return [dateFormatter stringFromDate:date];
- }
- // Manage different bundle locations for Swift Package Manager, CocoaPods static, CocoaPods dynamic.
- - (void)setDefaultsFor:(FIRRemoteConfig *)config {
- #if SWIFT_PACKAGE
- NSBundle *bundle = Firebase_RemoteConfigUnit_SWIFTPM_MODULE_BUNDLE();
- NSString *plistFile = [bundle pathForResource:@"Defaults-testInfo" ofType:@"plist"];
- #else
- NSBundle *bundle = [NSBundle mainBundle];
- NSString *plistFile = [bundle pathForResource:@"Defaults-testInfo" ofType:@"plist"];
- if (plistFile != nil) {
- [config setDefaultsFromPlistFileName:@"Defaults-testInfo"];
- return;
- }
- // We've linked dynamically and the plist file is in the test's bundle.
- for (bundle in [NSBundle allBundles]) {
- plistFile = [bundle pathForResource:@"Defaults-testInfo" ofType:@"plist"];
- if (plistFile != nil) {
- break;
- }
- }
- #endif
- NSDictionary *defaults = [[NSDictionary alloc] initWithContentsOfFile:plistFile];
- [config setDefaults:defaults];
- }
- - (void)testSetDefaultsFromPlist {
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- FIRRemoteConfig *config = _configInstances[i];
- [self setDefaultsFor:config];
- XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
- UTCToLocal(@"2016-02-28 18:33:31"));
- XCTAssertEqual(_configInstances[i][@"isPaidUser"].boolValue, YES);
- XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
- XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
- XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
- XCTAssertEqualObjects(_configInstances[i][@"FileInfo"].stringValue,
- @"To setup default config.");
- XCTAssertEqualObjects(_configInstances[i][@"format"].stringValue, @"key to value.");
- XCTAssertEqualObjects(_configInstances[i][@"arrayValue"].JSONValue,
- ((id) @[ @"foo", @"bar", @"baz" ]));
- XCTAssertEqualObjects(_configInstances[i][@"dictValue"].JSONValue,
- ((id) @{@"foo" : @"foo", @"bar" : @"bar", @"baz" : @"baz"}));
- // If given a wrong file name, the default will not be set and kept as previous results.
- [_configInstances[i] setDefaultsFromPlistFileName:@""];
- XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
- UTCToLocal(@"2016-02-28 18:33:31"));
- [_configInstances[i] setDefaultsFromPlistFileName:@"non-existed_file"];
- XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
- [_configInstances[i] setDefaultsFromPlistFileName:nil];
- XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
- [_configInstances[i] setDefaultsFromPlistFileName:@""];
- XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
- }
- }
- - (void)testAllKeysFromSource {
- NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- fetchConfigsExpectation[i] = [self
- expectationWithDescription:[NSString
- stringWithFormat:@"Test allKeys methods - instance %d", i]];
- NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
- NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
- NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
- [_configInstances[i] setDefaults:defaults];
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- XCTAssertEqual(
- [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote].count, 100);
- XCTAssertEqual(
- [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 2);
- XCTAssertEqual(
- [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceStatic].count, 0);
- [fetchConfigsExpectation[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- - (void)testAllKeysWithPrefix {
- NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- fetchConfigsExpectation[i] = [self
- expectationWithDescription:[NSString
- stringWithFormat:@"Test allKeys methods - instance %d", i]];
- FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
- NSError *error) {
- XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
- XCTAssertNil(error);
- NSLog(@"Testing _configInstances %d", i);
- [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
- XCTAssertTrue(changed);
- // Test keysWithPrefix: method.
- XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key1"].count, 12);
- XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key"].count, 100);
- XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"invalid key"].count, 0);
- XCTAssertEqual([self->_configInstances[i] keysWithPrefix:nil].count, 100);
- XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@""].count, 100);
- [fetchConfigsExpectation[i] fulfill];
- }];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- /// Test the minimum fetch interval is applied and read back correctly.
- - (void)testSetMinimumFetchIntervalConfigSetting {
- NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
- [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- fetchConfigsExpectation[i] = [self
- expectationWithDescription:
- [NSString stringWithFormat:@"Test minimumFetchInterval expectation - instance %d", i]];
- FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
- settings.minimumFetchInterval = 123;
- [_configInstances[i] setConfigSettings:settings];
- XCTAssertEqual([_configInstances[i] configSettings].minimumFetchInterval, 123);
- FIRRemoteConfigFetchCompletion fetchCompletion =
- ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
- XCTAssertFalse([self->_configInstances[i].settings hasMinimumFetchIntervalElapsed:43200]);
- // Update minimum fetch interval.
- FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
- settings.minimumFetchInterval = 0;
- [self->_configInstances[i] setConfigSettings:settings];
- XCTAssertEqual([self->_configInstances[i] configSettings].minimumFetchInterval, 0);
- XCTAssertTrue([self->_configInstances[i].settings hasMinimumFetchIntervalElapsed:0]);
- [fetchConfigsExpectation[i] fulfill];
- };
- [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
- }
- [self waitForExpectationsWithTimeout:_expectationTimeout
- handler:^(NSError *error) {
- XCTAssertNil(error);
- }];
- }
- /// Test the fetch timeout is properly set and read back.
- - (void)testSetFetchTimeoutConfigSetting {
- for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
- FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
- settings.fetchTimeout = 1;
- [_configInstances[i] setConfigSettings:settings];
- XCTAssertEqual([_configInstances[i] configSettings].fetchTimeout, 1);
- NSURLSession *networkSession = [_configFetch[i] currentNetworkSession];
- XCTAssertNotNil(networkSession);
- XCTAssertEqual(networkSession.configuration.timeoutIntervalForResource, 1);
- XCTAssertEqual(networkSession.configuration.timeoutIntervalForRequest, 1);
- }
- }
- - (void)testFetchRequestWithUserPropertiesOnly {
- NSDictionary *userProperties = @{@"user_key" : @"user_value"};
- NSString *req = [_settings nextRequestWithUserProperties:userProperties];
- XCTAssertTrue([req containsString:@"analytics_user_properties:{\"user_key\":\"user_value\"}"]);
- XCTAssertFalse([req containsString:@"first_open_time"]);
- }
- - (void)testFetchRequestWithFirstOpenTimeAndUserProperties {
- NSDictionary *userProperties = @{@"_fot" : @1649116800000, @"user_key" : @"user_value"};
- NSString *req = [_settings nextRequestWithUserProperties:userProperties];
- XCTAssertTrue([req containsString:@"first_open_time:'2022-04-05T00:00:00Z'"]);
- XCTAssertTrue([req containsString:@"analytics_user_properties:{\"user_key\":\"user_value\"}"]);
- }
- - (void)testFetchRequestFirstOpenTimeOnly {
- NSDictionary *userProperties = @{@"_fot" : @1650315600000};
- NSString *req = [_settings nextRequestWithUserProperties:userProperties];
- XCTAssertTrue([req containsString:@"first_open_time:'2022-04-18T21:00:00Z'"]);
- XCTAssertFalse([req containsString:@"analytics_user_properties"]);
- }
- #pragma mark - Public Factory Methods
- - (void)testConfigureConfigWithValidInput {
- // Configure the default app with our options and ensure the Remote Config instance is set up
- // properly.
- if (![FIRApp isDefaultAppConfigured]) {
- XCTAssertNoThrow([FIRApp configureWithOptions:[self firstAppOptions]]);
- }
- XCTAssertNoThrow([FIRRemoteConfig remoteConfig]);
- FIRRemoteConfig *config = [FIRRemoteConfig remoteConfig];
- XCTAssertNotNil(config);
- // Ensure the same instance is returned from the singleton.
- FIRRemoteConfig *sameConfig = [FIRRemoteConfig remoteConfig];
- XCTAssertNotNil(sameConfig);
- XCTAssertEqual(config, sameConfig);
- // Ensure the app name is stored properly.
- XCTAssertEqual([config valueForKey:@"_appName"], kFIRDefaultAppName);
- }
- #pragma mark - Test Helpers
- - (FIROptions *)firstAppOptions {
- // TODO: Evaluate if we want to hardcode things here instead.
- FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
- GCMSenderID:@"correct_gcm_sender_id"];
- options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
- options.projectID = @"abc-xyz-123";
- return options;
- }
- - (FIROptions *)secondAppOptions {
- NSBundle *bundle = [NSBundle bundleForClass:[self class]];
- #if SWIFT_PACKAGE
- bundle = Firebase_RemoteConfigUnit_SWIFTPM_MODULE_BUNDLE();
- #endif
- NSString *plistPath = [bundle pathForResource:@"SecondApp-GoogleService-Info" ofType:@"plist"];
- FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
- XCTAssertNotNil(options);
- return options;
- }
- @end
|