RCNRemoteConfigTest.m 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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 "OCMStubRecorder.h"
  18. #import "OCMock.h"
  19. #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
  20. #import "FirebaseRemoteConfig/Sources/Private/RCNConfigFetch.h"
  21. #import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
  22. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  23. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  24. #import "FirebaseRemoteConfig/Sources/RCNConfigExperiment.h"
  25. #import "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
  26. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  27. #import <GoogleUtilities/GULNSData+zlib.h>
  28. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  29. @interface RCNConfigFetch (ForTest)
  30. - (instancetype)initWithContent:(RCNConfigContent *)content
  31. DBManager:(RCNConfigDBManager *)DBManager
  32. settings:(RCNConfigSettings *)settings
  33. experiment:(RCNConfigExperiment *)experiment
  34. queue:(dispatch_queue_t)queue
  35. namespace:firebaseNamespace
  36. app:firebaseApp;
  37. /// Skip fetching user properties from analytics because we cannot mock the action here. Instead
  38. /// overriding the method to skip.
  39. - (void)fetchWithUserPropertiesCompletionHandler:(FIRAInteropUserPropertiesCallback)block;
  40. - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
  41. completionHandler:
  42. (RCNConfigFetcherCompletion)fetcherCompletion;
  43. - (void)fetchWithUserProperties:(NSDictionary *)userProperties
  44. completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
  45. - (NSString *)constructServerURL;
  46. - (NSURLSession *)currentNetworkSession;
  47. @end
  48. @interface FIRRemoteConfig (ForTest)
  49. - (void)updateWithNewInstancesForConfigFetch:(RCNConfigFetch *)configFetch
  50. configContent:(RCNConfigContent *)configContent
  51. configSettings:(RCNConfigSettings *)configSettings
  52. configExperiment:(RCNConfigExperiment *)configExperiment;
  53. @end
  54. @implementation FIRRemoteConfig (ForTest)
  55. - (void)updateWithNewInstancesForConfigFetch:(RCNConfigFetch *)configFetch
  56. configContent:(RCNConfigContent *)configContent
  57. configSettings:(RCNConfigSettings *)configSettings
  58. configExperiment:(RCNConfigExperiment *)configExperiment {
  59. [self setValue:configFetch forKey:@"_configFetch"];
  60. [self setValue:configContent forKey:@"_configContent"];
  61. [self setValue:configSettings forKey:@"_settings"];
  62. [self setValue:configExperiment forKey:@"_configExperiment"];
  63. }
  64. @end
  65. @interface RCNConfigDBManager (Test)
  66. - (void)removeDatabaseOnDatabaseQueueAtPath:(NSString *)path;
  67. @end
  68. @interface RCNUserDefaultsManager (Test)
  69. + (NSUserDefaults *)sharedUserDefaultsForBundleIdentifier:(NSString *)bundleIdentifier;
  70. @end
  71. typedef NS_ENUM(NSInteger, RCNTestRCInstance) {
  72. RCNTestRCInstanceDefault,
  73. RCNTestRCNumTotalInstances, // TODO(mandard): Remove once OCMock issue is resolved (#4877).
  74. RCNTestRCInstanceSecondNamespace,
  75. RCNTestRCInstanceSecondApp,
  76. };
  77. @interface RCNRemoteConfigTest : XCTestCase {
  78. NSTimeInterval _expectationTimeout;
  79. NSTimeInterval _checkCompletionTimeout;
  80. NSMutableArray<FIRRemoteConfig *> *_configInstances;
  81. NSMutableArray<NSDictionary<NSString *, NSString *> *> *_entries;
  82. NSMutableArray<NSDictionary<NSString *, id> *> *_response;
  83. NSMutableArray<NSData *> *_responseData;
  84. NSMutableArray<NSURLResponse *> *_URLResponse;
  85. NSMutableArray<RCNConfigFetch *> *_configFetch;
  86. RCNConfigDBManager *_DBManager;
  87. NSUserDefaults *_userDefaults;
  88. NSString *_userDefaultsSuiteName;
  89. NSString *_DBPath;
  90. id _DBManagerMock;
  91. id _experimentMock;
  92. id _userDefaultsMock;
  93. }
  94. @end
  95. @implementation RCNRemoteConfigTest
  96. - (void)setUp {
  97. [super setUp];
  98. FIRSetLoggerLevel(FIRLoggerLevelMax);
  99. _expectationTimeout = 5;
  100. _checkCompletionTimeout = 1.0;
  101. // Always remove the database at the start of testing.
  102. _DBPath = [RCNTestUtilities remoteConfigPathForTestDatabase];
  103. _DBManagerMock = OCMClassMock([RCNConfigDBManager class]);
  104. OCMStub([_DBManagerMock remoteConfigPathForDatabase]).andReturn(_DBPath);
  105. _DBManager = [[RCNConfigDBManager alloc] init];
  106. _userDefaultsSuiteName = [RCNTestUtilities userDefaultsSuiteNameForTestSuite];
  107. _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:_userDefaultsSuiteName];
  108. _userDefaultsMock = OCMClassMock([RCNUserDefaultsManager class]);
  109. OCMStub([_userDefaultsMock sharedUserDefaultsForBundleIdentifier:[OCMArg any]])
  110. .andReturn(_userDefaults);
  111. _experimentMock = OCMClassMock([RCNConfigExperiment class]);
  112. OCMStub([_experimentMock
  113. updateExperimentsWithHandler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
  114. RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
  115. _configInstances = [[NSMutableArray alloc] initWithCapacity:3];
  116. _entries = [[NSMutableArray alloc] initWithCapacity:3];
  117. _response = [[NSMutableArray alloc] initWithCapacity:3];
  118. _responseData = [[NSMutableArray alloc] initWithCapacity:3];
  119. _URLResponse = [[NSMutableArray alloc] initWithCapacity:3];
  120. _configFetch = [[NSMutableArray alloc] initWithCapacity:3];
  121. // Populate the default, second app, second namespace instances.
  122. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  123. // Fake a response for default instance.
  124. NSMutableDictionary<NSString *, NSString *> *valuesDict = [[NSMutableDictionary alloc] init];
  125. for (int count = 1; count <= 100; count++) {
  126. NSString *key = [NSString stringWithFormat:@"key%d-%d", count, i];
  127. NSString *value = [NSString stringWithFormat:@"value%d-%d", count, i];
  128. valuesDict[key] = value;
  129. }
  130. _entries[i] = valuesDict;
  131. NSString *currentAppName = nil;
  132. FIROptions *currentOptions = nil;
  133. NSString *currentNamespace = nil;
  134. switch (i) {
  135. case RCNTestRCInstanceSecondNamespace:
  136. currentAppName = RCNTestsDefaultFIRAppName;
  137. currentOptions = [self firstAppOptions];
  138. currentNamespace = RCNTestsPerfNamespace;
  139. break;
  140. case RCNTestRCInstanceSecondApp:
  141. currentAppName = RCNTestsSecondFIRAppName;
  142. currentOptions = [self secondAppOptions];
  143. currentNamespace = FIRNamespaceGoogleMobilePlatform;
  144. break;
  145. case RCNTestRCInstanceDefault:
  146. default:
  147. currentAppName = RCNTestsDefaultFIRAppName;
  148. currentOptions = [self firstAppOptions];
  149. currentNamespace = RCNTestsFIRNamespace;
  150. break;
  151. }
  152. NSString *fullyQualifiedNamespace =
  153. [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
  154. FIRRemoteConfig *config =
  155. OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
  156. FIROptions:currentOptions
  157. namespace:currentNamespace
  158. DBManager:_DBManager
  159. configContent:configContent
  160. analytics:nil]);
  161. _configInstances[i] = config;
  162. RCNConfigSettings *settings =
  163. [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  164. namespace:fullyQualifiedNamespace
  165. firebaseAppName:currentAppName
  166. googleAppID:currentOptions.googleAppID];
  167. dispatch_queue_t queue = dispatch_queue_create(
  168. [[NSString stringWithFormat:@"testqueue: %d", i] cStringUsingEncoding:NSUTF8StringEncoding],
  169. DISPATCH_QUEUE_SERIAL);
  170. _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
  171. DBManager:_DBManager
  172. settings:settings
  173. analytics:nil
  174. experiment:_experimentMock
  175. queue:queue
  176. namespace:fullyQualifiedNamespace
  177. options:currentOptions]);
  178. OCMStubRecorder *mock = OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:0
  179. completionHandler:OCMOCK_ANY]);
  180. mock = [mock ignoringNonObjectArgs];
  181. mock.andDo(^(NSInvocation *invocation) {
  182. __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
  183. NSError *_Nullable error) = nil;
  184. [invocation getArgument:&handler atIndex:3];
  185. [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  186. completionHandler:handler];
  187. });
  188. _response[i] = @{@"state" : @"UPDATE", @"entries" : _entries[i]};
  189. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  190. _URLResponse[i] = [[NSHTTPURLResponse alloc]
  191. initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  192. statusCode:200
  193. HTTPVersion:nil
  194. headerFields:@{@"etag" : [NSString stringWithFormat:@"etag1-%d", i]}];
  195. id completionBlock =
  196. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  197. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  198. completionHandler:completionBlock])
  199. .andReturn(nil);
  200. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  201. configContent:configContent
  202. configSettings:settings
  203. configExperiment:_experimentMock];
  204. }
  205. }
  206. - (void)tearDown {
  207. [_DBManager removeDatabaseOnDatabaseQueueAtPath:_DBPath];
  208. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:_userDefaultsSuiteName];
  209. [_DBManagerMock stopMocking];
  210. _DBManagerMock = nil;
  211. [_userDefaultsMock stopMocking];
  212. _userDefaultsMock = nil;
  213. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  214. [(id)_configInstances[i] stopMocking];
  215. [(id)_configFetch[i] stopMocking];
  216. }
  217. [_configInstances removeAllObjects];
  218. [_configFetch removeAllObjects];
  219. _configInstances = nil;
  220. _configFetch = nil;
  221. [super tearDown];
  222. }
  223. - (void)testFetchConfigWithNilCallback {
  224. NSMutableArray<XCTestExpectation *> *expectations =
  225. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  226. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  227. expectations[i] = [self
  228. expectationWithDescription:
  229. [NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
  230. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  231. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:nil];
  232. dispatch_after(
  233. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_checkCompletionTimeout * NSEC_PER_SEC)),
  234. dispatch_get_main_queue(), ^{
  235. XCTAssertEqual(self->_configInstances[i].lastFetchStatus,
  236. FIRRemoteConfigFetchStatusSuccess);
  237. [expectations[i] fulfill];
  238. });
  239. }
  240. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  241. }
  242. - (void)testFetchConfigsSuccessfully {
  243. NSMutableArray<XCTestExpectation *> *expectations =
  244. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  245. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  246. expectations[i] =
  247. [self expectationWithDescription:
  248. [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
  249. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  250. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  251. NSError *error) {
  252. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  253. XCTAssertNil(error);
  254. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  255. XCTAssertTrue(changed);
  256. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  257. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  258. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  259. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  260. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  261. XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
  262. OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
  263. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  264. @"Callback of first successful config "
  265. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
  266. XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
  267. XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  268. @"last fetch time interval should be set.");
  269. [expectations[i] fulfill];
  270. }];
  271. };
  272. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  273. }
  274. [self waitForExpectationsWithTimeout:_expectationTimeout
  275. handler:^(NSError *error) {
  276. XCTAssertNil(error);
  277. }];
  278. }
  279. - (void)testFetchAndActivate {
  280. NSMutableArray<XCTestExpectation *> *expectations =
  281. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  282. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  283. expectations[i] =
  284. [self expectationWithDescription:
  285. [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
  286. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  287. FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion = ^void(
  288. FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
  289. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  290. XCTAssertNil(error);
  291. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  292. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  293. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  294. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  295. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  296. XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
  297. OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
  298. XCTAssertEqual(
  299. status, FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote,
  300. @"Callback of first successful config "
  301. @"fetchAndActivate status must equal to FIRRemoteConfigFetchAndStatusSuccessFromRemote.");
  302. XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
  303. XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  304. @"last fetch time interval should be set.");
  305. [expectations[i] fulfill];
  306. };
  307. // Update the minimum fetch interval to 0. This disables the cache and forces a remote fetch
  308. // request.
  309. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  310. settings.minimumFetchInterval = 0;
  311. [_configInstances[i] setConfigSettings:settings];
  312. [_configInstances[i] fetchAndActivateWithCompletionHandler:fetchAndActivateCompletion];
  313. }
  314. [self waitForExpectationsWithTimeout:_expectationTimeout
  315. handler:^(NSError *error) {
  316. XCTAssertNil(error);
  317. }];
  318. }
  319. // TODO: Try splitting into smaller tests.
  320. - (void)testFetchConfigsSuccessfullyWithNewActivateMethodSignature {
  321. NSMutableArray<XCTestExpectation *> *expectations =
  322. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  323. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  324. expectations[i] =
  325. [self expectationWithDescription:
  326. [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
  327. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  328. FIRRemoteConfigFetchCompletion fetchCompletion = ^(FIRRemoteConfigFetchStatus status,
  329. NSError *error) {
  330. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  331. XCTAssertNil(error);
  332. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  333. XCTAssertTrue(changed);
  334. XCTAssertNil(error);
  335. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  336. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  337. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  338. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  339. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  340. XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
  341. OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
  342. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  343. @"Callback of first successful config "
  344. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
  345. XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
  346. XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  347. @"last fetch time interval should be set.");
  348. // A second activate should have no effect.
  349. [self->_configInstances[i]
  350. activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  351. XCTAssertFalse(changed);
  352. XCTAssertNil(error);
  353. }];
  354. [expectations[i] fulfill];
  355. }];
  356. };
  357. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  358. }
  359. [self waitForExpectationsWithTimeout:_expectationTimeout
  360. handler:^(NSError *error) {
  361. XCTAssertNil(error);
  362. }];
  363. }
  364. - (void)testEnumeratingConfigResults {
  365. NSMutableArray<XCTestExpectation *> *expectations =
  366. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  367. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  368. expectations[i] = [self
  369. expectationWithDescription:
  370. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  371. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  372. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  373. NSError *error) {
  374. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  375. XCTAssertNil(error);
  376. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  377. XCTAssertTrue(changed);
  378. NSString *key5 = [NSString stringWithFormat:@"key5-%d", i];
  379. NSString *key19 = [NSString stringWithFormat:@"key19-%d", i];
  380. NSString *value5 = [NSString stringWithFormat:@"value5-%d", i];
  381. NSString *value19 = [NSString stringWithFormat:@"value19-%d", i];
  382. XCTAssertEqualObjects(self->_configInstances[i][key5].stringValue, value5);
  383. XCTAssertEqualObjects(self->_configInstances[i][key19].stringValue, value19);
  384. // Test enumerating config values.
  385. for (NSString *key in self->_configInstances[i]) {
  386. if ([key isEqualToString:key5]) {
  387. XCTAssertEqualObjects(self->_configInstances[i][key5].stringValue, value5);
  388. }
  389. if ([key isEqualToString:key19]) {
  390. XCTAssertEqualObjects(self->_configInstances[i][key19].stringValue, value19);
  391. }
  392. }
  393. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  394. @"Callback of first successful config "
  395. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
  396. XCTAssertNotNil(self->_configInstances[i].lastFetchTime);
  397. XCTAssertGreaterThan(self->_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  398. @"last fetch time interval should be set.");
  399. [expectations[i] fulfill];
  400. }];
  401. };
  402. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  403. }
  404. [self waitForExpectationsWithTimeout:_expectationTimeout
  405. handler:^(NSError *error) {
  406. XCTAssertNil(error);
  407. }];
  408. }
  409. - (void)testFetchConfigsFailed {
  410. // Override the setup values to return back an error status.
  411. RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
  412. // Populate the default, second app, second namespace instances.
  413. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  414. NSString *currentAppName = nil;
  415. FIROptions *currentOptions = nil;
  416. NSString *currentNamespace = nil;
  417. switch (i) {
  418. case RCNTestRCInstanceSecondNamespace:
  419. currentAppName = RCNTestsDefaultFIRAppName;
  420. currentOptions = [self firstAppOptions];
  421. currentNamespace = RCNTestsPerfNamespace;
  422. break;
  423. case RCNTestRCInstanceSecondApp:
  424. currentAppName = RCNTestsSecondFIRAppName;
  425. currentOptions = [self secondAppOptions];
  426. currentNamespace = FIRNamespaceGoogleMobilePlatform;
  427. break;
  428. case RCNTestRCInstanceDefault:
  429. default:
  430. currentAppName = RCNTestsDefaultFIRAppName;
  431. currentOptions = [self firstAppOptions];
  432. currentNamespace = RCNTestsFIRNamespace;
  433. break;
  434. }
  435. NSString *fullyQualifiedNamespace =
  436. [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
  437. RCNUserDefaultsManager *userDefaultsManager =
  438. [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
  439. bundleID:[NSBundle mainBundle].bundleIdentifier
  440. namespace:fullyQualifiedNamespace];
  441. userDefaultsManager.lastFetchTime = 0;
  442. FIRRemoteConfig *config =
  443. OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
  444. FIROptions:currentOptions
  445. namespace:currentNamespace
  446. DBManager:_DBManager
  447. configContent:configContent
  448. analytics:nil]);
  449. _configInstances[i] = config;
  450. RCNConfigSettings *settings =
  451. [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  452. namespace:fullyQualifiedNamespace
  453. firebaseAppName:currentAppName
  454. googleAppID:currentOptions.googleAppID];
  455. dispatch_queue_t queue = dispatch_queue_create(
  456. [[NSString stringWithFormat:@"testqueue: %d", i] cStringUsingEncoding:NSUTF8StringEncoding],
  457. DISPATCH_QUEUE_SERIAL);
  458. _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
  459. DBManager:_DBManager
  460. settings:settings
  461. analytics:nil
  462. experiment:nil
  463. queue:queue
  464. namespace:fullyQualifiedNamespace
  465. options:currentOptions]);
  466. OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
  467. .andDo(^(NSInvocation *invocation) {
  468. __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
  469. NSError *_Nullable error) = nil;
  470. // void (^handler)(FIRRemoteConfigFetchCompletion);
  471. [invocation getArgument:&handler atIndex:3];
  472. [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  473. completionHandler:handler];
  474. });
  475. _response[i] = @{};
  476. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  477. _URLResponse[i] =
  478. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  479. statusCode:500
  480. HTTPVersion:nil
  481. headerFields:@{@"etag" : @"etag1"}];
  482. id completionBlock =
  483. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  484. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  485. completionHandler:completionBlock])
  486. .andReturn(nil);
  487. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  488. configContent:configContent
  489. configSettings:settings
  490. configExperiment:nil];
  491. }
  492. // Make the fetch calls for all instances.
  493. NSMutableArray<XCTestExpectation *> *expectations =
  494. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  495. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  496. expectations[i] = [self
  497. expectationWithDescription:
  498. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  499. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  500. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  501. NSError *error) {
  502. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
  503. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  504. XCTAssertFalse(changed);
  505. XCTAssertNil(error);
  506. FIRRemoteConfigValue *value = self->_configInstances[RCNTestRCInstanceDefault][@"key1"];
  507. XCTAssertEqual((int)value.source, (int)FIRRemoteConfigSourceStatic);
  508. XCTAssertEqualObjects(value.stringValue, @"");
  509. XCTAssertEqual(value.boolValue, NO);
  510. [expectations[i] fulfill];
  511. }];
  512. };
  513. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  514. }
  515. [self waitForExpectationsWithTimeout:_expectationTimeout
  516. handler:^(NSError *error) {
  517. XCTAssertNil(error);
  518. }];
  519. }
  520. // TODO(mandard): Break up test with helper methods.
  521. - (void)testFetchConfigsFailedErrorNoNetwork {
  522. // Override the setup values to return back an error status.
  523. RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
  524. // Populate the default, second app, second namespace instances.
  525. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  526. NSString *currentAppName = nil;
  527. FIROptions *currentOptions = nil;
  528. NSString *currentNamespace = nil;
  529. switch (i) {
  530. case RCNTestRCInstanceSecondNamespace:
  531. currentAppName = RCNTestsDefaultFIRAppName;
  532. currentOptions = [self firstAppOptions];
  533. currentNamespace = RCNTestsPerfNamespace;
  534. break;
  535. case RCNTestRCInstanceSecondApp:
  536. currentAppName = RCNTestsSecondFIRAppName;
  537. currentOptions = [self secondAppOptions];
  538. currentNamespace = FIRNamespaceGoogleMobilePlatform;
  539. break;
  540. case RCNTestRCInstanceDefault:
  541. default:
  542. currentAppName = RCNTestsDefaultFIRAppName;
  543. currentOptions = [self firstAppOptions];
  544. currentNamespace = RCNTestsFIRNamespace;
  545. break;
  546. }
  547. NSString *fullyQualifiedNamespace =
  548. [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
  549. RCNUserDefaultsManager *userDefaultsManager =
  550. [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
  551. bundleID:[NSBundle mainBundle].bundleIdentifier
  552. namespace:fullyQualifiedNamespace];
  553. userDefaultsManager.lastFetchTime = 0;
  554. FIRRemoteConfig *config =
  555. OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
  556. FIROptions:currentOptions
  557. namespace:currentNamespace
  558. DBManager:_DBManager
  559. configContent:configContent
  560. analytics:nil]);
  561. _configInstances[i] = config;
  562. RCNConfigSettings *settings =
  563. [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  564. namespace:fullyQualifiedNamespace
  565. firebaseAppName:currentAppName
  566. googleAppID:currentOptions.googleAppID];
  567. dispatch_queue_t queue = dispatch_queue_create(
  568. [[NSString stringWithFormat:@"testqueue: %d", i] cStringUsingEncoding:NSUTF8StringEncoding],
  569. DISPATCH_QUEUE_SERIAL);
  570. _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
  571. DBManager:_DBManager
  572. settings:settings
  573. analytics:nil
  574. experiment:nil
  575. queue:queue
  576. namespace:fullyQualifiedNamespace
  577. options:currentOptions]);
  578. OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
  579. .andDo(^(NSInvocation *invocation) {
  580. __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
  581. NSError *_Nullable error) = nil;
  582. // void (^handler)(FIRRemoteConfigFetchCompletion);
  583. [invocation getArgument:&handler atIndex:3];
  584. [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  585. completionHandler:handler];
  586. });
  587. _response[i] = @{};
  588. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  589. // A no network error is accompanied with an HTTP status code of 0.
  590. _URLResponse[i] =
  591. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  592. statusCode:0
  593. HTTPVersion:nil
  594. headerFields:@{@"etag" : @"etag1"}];
  595. id completionBlock =
  596. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  597. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  598. completionHandler:completionBlock])
  599. .andReturn(nil);
  600. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  601. configContent:configContent
  602. configSettings:settings
  603. configExperiment:nil];
  604. }
  605. // Make the fetch calls for all instances.
  606. NSMutableArray<XCTestExpectation *> *expectations =
  607. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  608. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  609. expectations[i] = [self
  610. expectationWithDescription:
  611. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  612. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  613. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  614. NSError *error) {
  615. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
  616. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  617. XCTAssertFalse(changed);
  618. XCTAssertNil(error);
  619. // No such key, still return a static value.
  620. FIRRemoteConfigValue *value = self->_configInstances[RCNTestRCInstanceDefault][@"key1"];
  621. XCTAssertEqual((int)value.source, (int)FIRRemoteConfigSourceStatic);
  622. XCTAssertEqualObjects(value.stringValue, @"");
  623. XCTAssertEqual(value.boolValue, NO);
  624. [expectations[i] fulfill];
  625. }];
  626. };
  627. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  628. }
  629. [self waitForExpectationsWithTimeout:_expectationTimeout
  630. handler:^(NSError *error) {
  631. XCTAssertNil(error);
  632. }];
  633. }
  634. // Activate should return false if a fetch response returns 200 with NO_CHANGE as the response body.
  635. - (void)testActivateOnFetchNoChangeStatus {
  636. // Override the setup values to return back an error status.
  637. RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
  638. // Populate the default, second app, second namespace instances.
  639. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  640. NSString *currentAppName = nil;
  641. FIROptions *currentOptions = nil;
  642. NSString *currentNamespace = nil;
  643. switch (i) {
  644. case RCNTestRCInstanceSecondNamespace:
  645. currentAppName = RCNTestsDefaultFIRAppName;
  646. currentOptions = [self firstAppOptions];
  647. currentNamespace = RCNTestsPerfNamespace;
  648. break;
  649. case RCNTestRCInstanceSecondApp:
  650. currentAppName = RCNTestsSecondFIRAppName;
  651. currentOptions = [self secondAppOptions];
  652. currentNamespace = FIRNamespaceGoogleMobilePlatform;
  653. break;
  654. case RCNTestRCInstanceDefault:
  655. default:
  656. currentAppName = RCNTestsDefaultFIRAppName;
  657. currentOptions = [self firstAppOptions];
  658. currentNamespace = RCNTestsFIRNamespace;
  659. break;
  660. }
  661. NSString *fullyQualifiedNamespace =
  662. [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
  663. RCNUserDefaultsManager *userDefaultsManager =
  664. [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
  665. bundleID:[NSBundle mainBundle].bundleIdentifier
  666. namespace:fullyQualifiedNamespace];
  667. userDefaultsManager.lastFetchTime = 10;
  668. FIRRemoteConfig *config =
  669. OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
  670. FIROptions:currentOptions
  671. namespace:currentNamespace
  672. DBManager:_DBManager
  673. configContent:configContent
  674. analytics:nil]);
  675. _configInstances[i] = config;
  676. RCNConfigSettings *settings =
  677. [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  678. namespace:fullyQualifiedNamespace
  679. firebaseAppName:currentAppName
  680. googleAppID:currentOptions.googleAppID];
  681. // Start the test with the assumption that we have some data that was fetched and activated.
  682. settings.lastETag = @"etag1";
  683. settings.lastETagUpdateTime = 100;
  684. settings.lastApplyTimeInterval = 101;
  685. dispatch_queue_t queue =
  686. dispatch_queue_create([[NSString stringWithFormat:@"testNoStatusFetchQueue: %d", i]
  687. cStringUsingEncoding:NSUTF8StringEncoding],
  688. DISPATCH_QUEUE_SERIAL);
  689. _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
  690. DBManager:_DBManager
  691. settings:settings
  692. analytics:nil
  693. experiment:nil
  694. queue:queue
  695. namespace:fullyQualifiedNamespace
  696. options:currentOptions]);
  697. OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
  698. .andDo(^(NSInvocation *invocation) {
  699. __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
  700. NSError *_Nullable error) = nil;
  701. [invocation getArgument:&handler atIndex:3];
  702. [self->_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  703. completionHandler:handler];
  704. });
  705. _response[i] = @{@"state" : @"NO_CHANGE"};
  706. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  707. _URLResponse[i] =
  708. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  709. statusCode:200
  710. HTTPVersion:nil
  711. headerFields:@{@"etag" : @"etag1"}];
  712. id completionBlock =
  713. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  714. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  715. completionHandler:completionBlock])
  716. .andReturn(nil);
  717. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  718. configContent:configContent
  719. configSettings:settings
  720. configExperiment:nil];
  721. }
  722. // Make the fetch calls for all instances.
  723. NSMutableArray<XCTestExpectation *> *expectations =
  724. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  725. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  726. expectations[i] = [self
  727. expectationWithDescription:
  728. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  729. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  730. // Make sure activate returns false in fetch completion.
  731. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  732. NSError *error) {
  733. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  734. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  735. XCTAssertFalse(changed);
  736. XCTAssertNil(error);
  737. [expectations[i] fulfill];
  738. }];
  739. };
  740. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  741. }
  742. [self waitForExpectationsWithTimeout:_expectationTimeout
  743. handler:^(NSError *error) {
  744. XCTAssertNil(error);
  745. }];
  746. }
  747. - (void)testConfigValueForKey {
  748. NSMutableArray<XCTestExpectation *> *expectations =
  749. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  750. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  751. expectations[i] =
  752. [self expectationWithDescription:
  753. [NSString stringWithFormat:@"Test configValueForKey: method - instance %d", i]];
  754. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  755. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  756. NSError *error) {
  757. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  758. XCTAssertNil(error);
  759. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  760. XCTAssertTrue(changed);
  761. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  762. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  763. NSString *key3 = [NSString stringWithFormat:@"key3-%d", i];
  764. NSString *key7 = [NSString stringWithFormat:@"key7-%d", i];
  765. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  766. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  767. NSString *value3 = [NSString stringWithFormat:@"value3-%d", i];
  768. NSString *value7 = [NSString stringWithFormat:@"value7-%d", i];
  769. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  770. XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
  771. OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
  772. XCTAssertEqualObjects([self->_configInstances[i] configValueForKey:key3].stringValue,
  773. value3);
  774. if (i == RCNTestRCInstanceDefault) {
  775. XCTAssertEqualObjects([self->_configInstances[i] configValueForKey:key7].stringValue,
  776. value7);
  777. }
  778. XCTAssertEqualObjects([self->_configInstances[i] configValueForKey:key7].stringValue,
  779. value7);
  780. XCTAssertNotNil([self->_configInstances[i] configValueForKey:nil]);
  781. XCTAssertEqual([self->_configInstances[i] configValueForKey:nil].source,
  782. FIRRemoteConfigSourceStatic);
  783. XCTAssertEqual([self->_configInstances[i] configValueForKey:nil].source,
  784. FIRRemoteConfigSourceStatic);
  785. XCTAssertEqual([self->_configInstances[i] configValueForKey:nil source:-1].source,
  786. FIRRemoteConfigSourceStatic);
  787. [expectations[i] fulfill];
  788. }];
  789. };
  790. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  791. }
  792. [self waitForExpectationsWithTimeout:_expectationTimeout
  793. handler:^(NSError *error) {
  794. XCTAssertNil(error);
  795. }];
  796. }
  797. - (void)testFetchConfigWithDefaultSets {
  798. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  799. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  800. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  801. fetchConfigsExpectation[i] = [self
  802. expectationWithDescription:
  803. [NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
  804. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  805. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  806. NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
  807. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  808. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  809. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
  810. [_configInstances[i] setDefaults:defaults];
  811. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  812. NSError *error) {
  813. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  814. XCTAssertNil(error);
  815. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, @"default key1");
  816. XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceDefault);
  817. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  818. XCTAssertTrue(changed);
  819. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  820. XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
  821. XCTAssertEqualObjects([self->_configInstances[i] defaultValueForKey:key1].stringValue,
  822. @"default key1");
  823. XCTAssertEqualObjects(self->_configInstances[i][key2].stringValue, value2);
  824. XCTAssertEqualObjects(self->_configInstances[i][key0].stringValue, @"value0-0");
  825. XCTAssertNil([self->_configInstances[i] defaultValueForKey:nil]);
  826. OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
  827. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  828. @"Callback of first successful config "
  829. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
  830. [fetchConfigsExpectation[i] fulfill];
  831. }];
  832. };
  833. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  834. }
  835. [self waitForExpectationsWithTimeout:_expectationTimeout
  836. handler:^(NSError *error) {
  837. XCTAssertNil(error);
  838. }];
  839. }
  840. - (void)testDefaultsSetsOnly {
  841. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  842. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  843. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  844. NSString *key3 = [NSString stringWithFormat:@"key3-%d", i];
  845. NSString *key4 = [NSString stringWithFormat:@"key4-%d", i];
  846. NSString *key5 = [NSString stringWithFormat:@"key5-%d", i];
  847. NSString *defaultValue1 = @"default value1";
  848. NSData *defaultValue2 = [defaultValue1 dataUsingEncoding:NSUTF8StringEncoding];
  849. NSNumber *defaultValue3 = [NSNumber numberWithFloat:3.1415926];
  850. NSDate *defaultValue4 = [NSDate date];
  851. BOOL defaultValue5 = NO;
  852. NSMutableDictionary<NSString *, id> *defaults = [NSMutableDictionary dictionaryWithDictionary:@{
  853. key1 : defaultValue1,
  854. key2 : defaultValue2,
  855. key3 : defaultValue3,
  856. key4 : defaultValue4,
  857. key5 : @(defaultValue5),
  858. }];
  859. [_configInstances[i] setDefaults:defaults];
  860. if (i == RCNTestRCInstanceSecondNamespace) {
  861. [defaults setObject:@"2860" forKey:@"experience"];
  862. [_configInstances[i] setDefaults:defaults];
  863. }
  864. // Remove objects right away to make sure dispatch_async gets the copy.
  865. [defaults removeAllObjects];
  866. FIRRemoteConfig *config = _configInstances[i];
  867. XCTAssertEqualObjects(config[key1].stringValue, defaultValue1, @"Should support string format");
  868. XCTAssertEqualObjects(config[key2].dataValue, defaultValue2, @"Should support data format");
  869. XCTAssertEqual(config[key3].numberValue.longValue, defaultValue3.longValue,
  870. @"Should support number format");
  871. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  872. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  873. NSString *strValueOfDate = [dateFormatter stringFromDate:(NSDate *)defaultValue4];
  874. XCTAssertEqualObjects(
  875. config[key4].stringValue, strValueOfDate,
  876. @"Date format can be set as an input from plist, but output coming out of "
  877. @"defaultConfig as string.");
  878. XCTAssertEqual(config[key5].boolValue, defaultValue5, @"Should support bool format");
  879. if (i == RCNTestRCInstanceSecondNamespace) {
  880. XCTAssertEqualObjects(
  881. [_configInstances[i] configValueForKey:@"experience"].stringValue, @"2860",
  882. @"Only default config has the key, must equal to default config value.");
  883. }
  884. // Reset default sets
  885. [_configInstances[i] setDefaults:nil];
  886. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 0);
  887. }
  888. }
  889. - (void)testSetDefaultsWithNilParams {
  890. NSMutableArray<XCTestExpectation *> *expectations =
  891. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  892. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  893. expectations[i] = [self
  894. expectationWithDescription:
  895. [NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
  896. // Should work when passing nil.
  897. [_configInstances[i] setDefaults:nil];
  898. [_configInstances[i] setDefaults:nil];
  899. dispatch_after(
  900. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_checkCompletionTimeout * NSEC_PER_SEC)),
  901. dispatch_get_main_queue(), ^{
  902. XCTAssertEqual(
  903. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 0);
  904. [expectations[i] fulfill];
  905. });
  906. }
  907. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  908. }
  909. - (void)testFetchConfigOverwriteDefaultSet {
  910. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  911. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  912. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  913. fetchConfigsExpectation[i] = [self
  914. expectationWithDescription:
  915. [NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
  916. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  917. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  918. [_configInstances[i] setDefaults:@{key1 : @"default key1"}];
  919. FIRRemoteConfigValue *value = _configInstances[i][key1];
  920. XCTAssertEqualObjects(value.stringValue, @"default key1");
  921. XCTAssertEqual(value.source, FIRRemoteConfigSourceDefault);
  922. value = _configInstances[i][@"A key doesn't exist"];
  923. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  924. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  925. NSError *error) {
  926. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  927. XCTAssertNil(error);
  928. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  929. XCTAssertTrue(changed);
  930. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  931. XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
  932. XCTAssertEqualObjects([self->_configInstances[i] defaultValueForKey:key1].stringValue,
  933. @"default key1");
  934. OCMVerify([self->_configInstances[i] objectForKeyedSubscript:key1]);
  935. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  936. @"Callback of first successful config "
  937. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
  938. [fetchConfigsExpectation[i] fulfill];
  939. }];
  940. };
  941. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  942. }
  943. [self waitForExpectationsWithTimeout:_expectationTimeout
  944. handler:^(NSError *error) {
  945. XCTAssertNil(error);
  946. }];
  947. }
  948. - (void)testGetConfigValueBySource {
  949. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  950. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  951. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  952. fetchConfigsExpectation[i] =
  953. [self expectationWithDescription:
  954. [NSString stringWithFormat:@"Test get config value by source - instance %d", i]];
  955. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  956. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  957. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default value1"};
  958. [_configInstances[i] setDefaults:defaults];
  959. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  960. NSError *error) {
  961. XCTAssertEqual(self->_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  962. XCTAssertNil(error);
  963. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, @"default value1");
  964. XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceDefault);
  965. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  966. XCTAssertTrue(changed);
  967. XCTAssertEqualObjects(self->_configInstances[i][key1].stringValue, value1);
  968. XCTAssertEqual(self->_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
  969. FIRRemoteConfigValue *value;
  970. if (i == RCNTestRCInstanceDefault) {
  971. value = [self->_configInstances[i] configValueForKey:key1
  972. source:FIRRemoteConfigSourceRemote];
  973. XCTAssertEqualObjects(value.stringValue, value1);
  974. value = [self->_configInstances[i] configValueForKey:key1
  975. source:FIRRemoteConfigSourceDefault];
  976. XCTAssertEqualObjects(value.stringValue, @"default value1");
  977. value = [self->_configInstances[i] configValueForKey:key1
  978. source:FIRRemoteConfigSourceStatic];
  979. } else {
  980. value = [self->_configInstances[i] configValueForKey:key1
  981. source:FIRRemoteConfigSourceRemote];
  982. XCTAssertEqualObjects(value.stringValue, value1);
  983. value = [self->_configInstances[i] configValueForKey:key1
  984. source:FIRRemoteConfigSourceDefault];
  985. XCTAssertEqualObjects(value.stringValue, @"default value1");
  986. value = [self->_configInstances[i] configValueForKey:key1
  987. source:FIRRemoteConfigSourceStatic];
  988. }
  989. XCTAssertEqualObjects(value.stringValue, @"");
  990. XCTAssertEqualObjects(value.numberValue, @(0));
  991. XCTAssertEqual(value.boolValue, NO);
  992. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  993. @"Callback of first successful config "
  994. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
  995. [fetchConfigsExpectation[i] fulfill];
  996. }];
  997. };
  998. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  999. }
  1000. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  1001. }
  1002. - (void)testInvalidKeyOrNamespace {
  1003. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1004. FIRRemoteConfigValue *value = [_configInstances[i] configValueForKey:nil];
  1005. XCTAssertNotNil(value);
  1006. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  1007. value = [_configInstances[i] configValueForKey:nil];
  1008. XCTAssertNotNil(value);
  1009. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  1010. value = [_configInstances[i] configValueForKey:nil source:5];
  1011. XCTAssertNotNil(value);
  1012. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  1013. }
  1014. }
  1015. // Remote Config converts UTC times in the plists to local times. This utility function makes it
  1016. // possible to check the times when running the tests in any timezone.
  1017. static NSString *UTCToLocal(NSString *utcTime) {
  1018. // Create a UTC dateFormatter.
  1019. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1020. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  1021. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  1022. NSDate *date = [dateFormatter dateFromString:utcTime];
  1023. [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
  1024. return [dateFormatter stringFromDate:date];
  1025. }
  1026. // Manage different bundle locations for Swift Package Manager, CocoaPods static, CocoaPods dynamic.
  1027. - (void)setDefaultsFor:(FIRRemoteConfig *)config {
  1028. #if SWIFT_PACKAGE
  1029. NSBundle *bundle = Firebase_RemoteConfigUnit_SWIFTPM_MODULE_BUNDLE();
  1030. NSString *plistFile = [bundle pathForResource:@"Defaults-testInfo" ofType:@"plist"];
  1031. #else
  1032. NSBundle *bundle = [NSBundle mainBundle];
  1033. NSString *plistFile = [bundle pathForResource:@"Defaults-testInfo" ofType:@"plist"];
  1034. if (plistFile != nil) {
  1035. [config setDefaultsFromPlistFileName:@"Defaults-testInfo"];
  1036. return;
  1037. }
  1038. // We've linked dynamically and the plist file is in the test's bundle.
  1039. for (bundle in [NSBundle allBundles]) {
  1040. plistFile = [bundle pathForResource:@"Defaults-testInfo" ofType:@"plist"];
  1041. if (plistFile != nil) {
  1042. break;
  1043. }
  1044. }
  1045. #endif
  1046. NSDictionary *defaults = [[NSDictionary alloc] initWithContentsOfFile:plistFile];
  1047. [config setDefaults:defaults];
  1048. }
  1049. - (void)testSetDefaultsFromPlist {
  1050. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1051. FIRRemoteConfig *config = _configInstances[i];
  1052. [self setDefaultsFor:config];
  1053. XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
  1054. UTCToLocal(@"2016-02-28 18:33:31"));
  1055. XCTAssertEqual(_configInstances[i][@"isPaidUser"].boolValue, YES);
  1056. XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
  1057. XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
  1058. XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
  1059. XCTAssertEqualObjects(_configInstances[i][@"FileInfo"].stringValue,
  1060. @"To setup default config.");
  1061. XCTAssertEqualObjects(_configInstances[i][@"format"].stringValue, @"key to value.");
  1062. // If given a wrong file name, the default will not be set and kept as previous results.
  1063. [_configInstances[i] setDefaultsFromPlistFileName:@""];
  1064. XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
  1065. UTCToLocal(@"2016-02-28 18:33:31"));
  1066. [_configInstances[i] setDefaultsFromPlistFileName:@"non-existed_file"];
  1067. XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
  1068. [_configInstances[i] setDefaultsFromPlistFileName:nil];
  1069. XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
  1070. [_configInstances[i] setDefaultsFromPlistFileName:@""];
  1071. XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
  1072. }
  1073. }
  1074. - (void)testAllKeysFromSource {
  1075. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1076. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1077. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1078. fetchConfigsExpectation[i] = [self
  1079. expectationWithDescription:[NSString
  1080. stringWithFormat:@"Test allKeys methods - instance %d", i]];
  1081. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  1082. NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
  1083. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
  1084. [_configInstances[i] setDefaults:defaults];
  1085. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  1086. NSError *error) {
  1087. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  1088. XCTAssertNil(error);
  1089. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  1090. XCTAssertTrue(changed);
  1091. XCTAssertEqual(
  1092. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote].count, 100);
  1093. XCTAssertEqual(
  1094. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 2);
  1095. XCTAssertEqual(
  1096. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceStatic].count, 0);
  1097. [fetchConfigsExpectation[i] fulfill];
  1098. }];
  1099. };
  1100. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1101. }
  1102. [self waitForExpectationsWithTimeout:_expectationTimeout
  1103. handler:^(NSError *error) {
  1104. XCTAssertNil(error);
  1105. }];
  1106. }
  1107. - (void)testAllKeysWithPrefix {
  1108. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1109. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1110. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1111. fetchConfigsExpectation[i] = [self
  1112. expectationWithDescription:[NSString
  1113. stringWithFormat:@"Test allKeys methods - instance %d", i]];
  1114. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  1115. NSError *error) {
  1116. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  1117. XCTAssertNil(error);
  1118. NSLog(@"Testing _configInstances %d", i);
  1119. [self->_configInstances[i] activateWithCompletion:^(BOOL changed, NSError *_Nullable error) {
  1120. XCTAssertTrue(changed);
  1121. // Test keysWithPrefix: method.
  1122. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key1"].count, 12);
  1123. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key"].count, 100);
  1124. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"invalid key"].count, 0);
  1125. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:nil].count, 100);
  1126. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@""].count, 100);
  1127. [fetchConfigsExpectation[i] fulfill];
  1128. }];
  1129. };
  1130. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1131. }
  1132. [self waitForExpectationsWithTimeout:_expectationTimeout
  1133. handler:^(NSError *error) {
  1134. XCTAssertNil(error);
  1135. }];
  1136. }
  1137. /// Test the minimum fetch interval is applied and read back correctly.
  1138. - (void)testSetMinimumFetchIntervalConfigSetting {
  1139. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1140. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1141. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1142. fetchConfigsExpectation[i] = [self
  1143. expectationWithDescription:
  1144. [NSString stringWithFormat:@"Test minimumFetchInterval expectation - instance %d", i]];
  1145. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1146. settings.minimumFetchInterval = 123;
  1147. [_configInstances[i] setConfigSettings:settings];
  1148. XCTAssertEqual([_configInstances[i] configSettings].minimumFetchInterval, 123);
  1149. FIRRemoteConfigFetchCompletion fetchCompletion =
  1150. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  1151. XCTAssertFalse([self->_configInstances[i].settings hasMinimumFetchIntervalElapsed:43200]);
  1152. // Update minimum fetch interval.
  1153. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1154. settings.minimumFetchInterval = 0;
  1155. [self->_configInstances[i] setConfigSettings:settings];
  1156. XCTAssertEqual([self->_configInstances[i] configSettings].minimumFetchInterval, 0);
  1157. XCTAssertTrue([self->_configInstances[i].settings hasMinimumFetchIntervalElapsed:0]);
  1158. [fetchConfigsExpectation[i] fulfill];
  1159. };
  1160. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1161. }
  1162. [self waitForExpectationsWithTimeout:_expectationTimeout
  1163. handler:^(NSError *error) {
  1164. XCTAssertNil(error);
  1165. }];
  1166. }
  1167. /// Test the fetch timeout is properly set and read back.
  1168. - (void)testSetFetchTimeoutConfigSetting {
  1169. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1170. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1171. settings.fetchTimeout = 1;
  1172. [_configInstances[i] setConfigSettings:settings];
  1173. XCTAssertEqual([_configInstances[i] configSettings].fetchTimeout, 1);
  1174. NSURLSession *networkSession = [_configFetch[i] currentNetworkSession];
  1175. XCTAssertNotNil(networkSession);
  1176. XCTAssertEqual(networkSession.configuration.timeoutIntervalForResource, 1);
  1177. XCTAssertEqual(networkSession.configuration.timeoutIntervalForRequest, 1);
  1178. }
  1179. }
  1180. #pragma mark - Public Factory Methods
  1181. - (void)testConfigureConfigWithValidInput {
  1182. // Configure the default app with our options and ensure the Remote Config instance is set up
  1183. // properly.
  1184. if (![FIRApp isDefaultAppConfigured]) {
  1185. XCTAssertNoThrow([FIRApp configureWithOptions:[self firstAppOptions]]);
  1186. }
  1187. XCTAssertNoThrow([FIRRemoteConfig remoteConfig]);
  1188. FIRRemoteConfig *config = [FIRRemoteConfig remoteConfig];
  1189. XCTAssertNotNil(config);
  1190. // Ensure the same instance is returned from the singleton.
  1191. FIRRemoteConfig *sameConfig = [FIRRemoteConfig remoteConfig];
  1192. XCTAssertNotNil(sameConfig);
  1193. XCTAssertEqual(config, sameConfig);
  1194. // Ensure the app name is stored properly.
  1195. XCTAssertEqual([config valueForKey:@"_appName"], kFIRDefaultAppName);
  1196. }
  1197. #pragma mark - Test Helpers
  1198. - (FIROptions *)firstAppOptions {
  1199. // TODO: Evaluate if we want to hardcode things here instead.
  1200. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  1201. GCMSenderID:@"correct_gcm_sender_id"];
  1202. options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
  1203. options.projectID = @"abc-xyz-123";
  1204. return options;
  1205. }
  1206. - (FIROptions *)secondAppOptions {
  1207. FIROptions *options =
  1208. [[FIROptions alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]]
  1209. pathForResource:@"SecondApp-GoogleService-Info"
  1210. ofType:@"plist"]];
  1211. XCTAssertNotNil(options);
  1212. return options;
  1213. }
  1214. @end