RCNRemoteConfigTest.m 71 KB

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