RCNRemoteConfigTest.m 67 KB

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