RCNRemoteConfigTest.m 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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/OCMStubRecorder.h>
  29. #import <OCMock/OCMock.h>
  30. @interface RCNConfigFetch (ForTest)
  31. - (instancetype)initWithContent:(RCNConfigContent *)content
  32. DBManager:(RCNConfigDBManager *)DBManager
  33. settings:(RCNConfigSettings *)settings
  34. experiment:(RCNConfigExperiment *)experiment
  35. queue:(dispatch_queue_t)queue
  36. namespace:firebaseNamespace
  37. app:firebaseApp;
  38. /// Skip fetching user properties from analytics because we cannot mock the action here. Instead
  39. /// overriding the method to skip.
  40. - (void)fetchWithUserPropertiesCompletionHandler:(FIRAInteropUserPropertiesCallback)block;
  41. - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
  42. completionHandler:
  43. (RCNConfigFetcherCompletion)fetcherCompletion;
  44. - (void)fetchWithUserProperties:(NSDictionary *)userProperties
  45. completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
  46. - (NSString *)constructServerURL;
  47. - (NSURLSession *)currentNetworkSession;
  48. @end
  49. @interface FIRRemoteConfig (ForTest)
  50. - (void)updateWithNewInstancesForConfigFetch:(RCNConfigFetch *)configFetch
  51. configContent:(RCNConfigContent *)configContent
  52. configSettings:(RCNConfigSettings *)configSettings
  53. configExperiment:(RCNConfigExperiment *)configExperiment;
  54. @end
  55. @implementation FIRRemoteConfig (ForTest)
  56. - (void)updateWithNewInstancesForConfigFetch:(RCNConfigFetch *)configFetch
  57. configContent:(RCNConfigContent *)configContent
  58. configSettings:(RCNConfigSettings *)configSettings
  59. configExperiment:(RCNConfigExperiment *)configExperiment {
  60. [self setValue:configFetch forKey:@"_configFetch"];
  61. [self setValue:configContent forKey:@"_configContent"];
  62. [self setValue:configSettings forKey:@"_settings"];
  63. [self setValue:configExperiment forKey:@"_configExperiment"];
  64. }
  65. @end
  66. @interface RCNConfigDBManager (Test)
  67. - (void)removeDatabaseOnDatabaseQueueAtPath:(NSString *)path;
  68. @end
  69. @interface RCNUserDefaultsManager (Test)
  70. + (NSUserDefaults *)sharedUserDefaultsForBundleIdentifier:(NSString *)bundleIdentifier;
  71. @end
  72. typedef NS_ENUM(NSInteger, RCNTestRCInstance) {
  73. RCNTestRCInstanceDefault,
  74. RCNTestRCNumTotalInstances, // TODO(mandard): Remove once OCMock issue is resolved (#4877).
  75. RCNTestRCInstanceSecondNamespace,
  76. RCNTestRCInstanceSecondApp,
  77. };
  78. @interface RCNRemoteConfigTest : XCTestCase {
  79. NSTimeInterval _expectationTimeout;
  80. NSTimeInterval _checkCompletionTimeout;
  81. NSMutableArray<FIRRemoteConfig *> *_configInstances;
  82. NSMutableArray<NSDictionary<NSString *, NSString *> *> *_entries;
  83. NSMutableArray<NSDictionary<NSString *, id> *> *_response;
  84. NSMutableArray<NSData *> *_responseData;
  85. NSMutableArray<NSURLResponse *> *_URLResponse;
  86. NSMutableArray<RCNConfigFetch *> *_configFetch;
  87. RCNConfigDBManager *_DBManager;
  88. NSUserDefaults *_userDefaults;
  89. NSString *_userDefaultsSuiteName;
  90. NSString *_DBPath;
  91. id _DBManagerMock;
  92. id _userDefaultsMock;
  93. }
  94. @end
  95. @implementation RCNRemoteConfigTest
  96. - (void)setUp {
  97. [super setUp];
  98. FIRSetLoggerLevel(FIRLoggerLevelMax);
  99. _expectationTimeout = 5;
  100. _checkCompletionTimeout = 1.0;
  101. // Always remove the database at the start of testing.
  102. _DBPath = [RCNTestUtilities remoteConfigPathForTestDatabase];
  103. _DBManagerMock = OCMClassMock([RCNConfigDBManager class]);
  104. OCMStub([_DBManagerMock remoteConfigPathForDatabase]).andReturn(_DBPath);
  105. _DBManager = [[RCNConfigDBManager alloc] init];
  106. _userDefaultsSuiteName = [RCNTestUtilities userDefaultsSuiteNameForTestSuite];
  107. _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:_userDefaultsSuiteName];
  108. _userDefaultsMock = OCMClassMock([RCNUserDefaultsManager class]);
  109. OCMStub([_userDefaultsMock sharedUserDefaultsForBundleIdentifier:[OCMArg any]])
  110. .andReturn(_userDefaults);
  111. RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
  112. _configInstances = [[NSMutableArray alloc] initWithCapacity:3];
  113. _entries = [[NSMutableArray alloc] initWithCapacity:3];
  114. _response = [[NSMutableArray alloc] initWithCapacity:3];
  115. _responseData = [[NSMutableArray alloc] initWithCapacity:3];
  116. _URLResponse = [[NSMutableArray alloc] initWithCapacity:3];
  117. _configFetch = [[NSMutableArray alloc] initWithCapacity:3];
  118. // Populate the default, second app, second namespace instances.
  119. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  120. // Fake a response for default instance.
  121. NSMutableDictionary<NSString *, NSString *> *valuesDict = [[NSMutableDictionary alloc] init];
  122. for (int count = 1; count <= 100; count++) {
  123. NSString *key = [NSString stringWithFormat:@"key%d-%d", count, i];
  124. NSString *value = [NSString stringWithFormat:@"value%d-%d", count, i];
  125. valuesDict[key] = value;
  126. }
  127. _entries[i] = valuesDict;
  128. NSString *currentAppName = nil;
  129. FIROptions *currentOptions = nil;
  130. NSString *currentNamespace = nil;
  131. switch (i) {
  132. case RCNTestRCInstanceSecondNamespace:
  133. currentAppName = RCNTestsDefaultFIRAppName;
  134. currentOptions = [self firstAppOptions];
  135. currentNamespace = RCNTestsPerfNamespace;
  136. break;
  137. case RCNTestRCInstanceSecondApp:
  138. currentAppName = RCNTestsSecondFIRAppName;
  139. currentOptions = [self secondAppOptions];
  140. currentNamespace = FIRNamespaceGoogleMobilePlatform;
  141. break;
  142. case RCNTestRCInstanceDefault:
  143. default:
  144. currentAppName = RCNTestsDefaultFIRAppName;
  145. currentOptions = [self firstAppOptions];
  146. currentNamespace = RCNTestsFIRNamespace;
  147. break;
  148. }
  149. NSString *fullyQualifiedNamespace =
  150. [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
  151. FIRRemoteConfig *config =
  152. OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
  153. FIROptions:currentOptions
  154. namespace:currentNamespace
  155. DBManager:_DBManager
  156. configContent:configContent
  157. analytics:nil]);
  158. _configInstances[i] = config;
  159. RCNConfigSettings *settings =
  160. [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  161. namespace:fullyQualifiedNamespace
  162. firebaseAppName:currentAppName
  163. googleAppID:currentOptions.googleAppID];
  164. dispatch_queue_t queue = dispatch_queue_create(
  165. [[NSString stringWithFormat:@"testqueue: %d", i] cStringUsingEncoding:NSUTF8StringEncoding],
  166. DISPATCH_QUEUE_SERIAL);
  167. _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
  168. DBManager:_DBManager
  169. settings:settings
  170. analytics:nil
  171. experiment:nil
  172. queue:queue
  173. namespace:fullyQualifiedNamespace
  174. options:currentOptions]);
  175. OCMStubRecorder *mock = OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:0
  176. completionHandler:OCMOCK_ANY]);
  177. mock = [mock ignoringNonObjectArgs];
  178. mock.andDo(^(NSInvocation *invocation) {
  179. void (^handler)(FIRRemoteConfigFetchStatus status, NSError *_Nullable error) = nil;
  180. [invocation getArgument:&handler atIndex:3];
  181. [_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  182. completionHandler:handler];
  183. });
  184. _response[i] = @{@"state" : @"UPDATE", @"entries" : _entries[i]};
  185. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  186. _URLResponse[i] = [[NSHTTPURLResponse alloc]
  187. initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  188. statusCode:200
  189. HTTPVersion:nil
  190. headerFields:@{@"etag" : [NSString stringWithFormat:@"etag1-%d", i]}];
  191. id completionBlock =
  192. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  193. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  194. completionHandler:completionBlock])
  195. .andReturn(nil);
  196. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  197. configContent:configContent
  198. configSettings:settings
  199. configExperiment:nil];
  200. }
  201. }
  202. - (void)tearDown {
  203. [_DBManager removeDatabaseOnDatabaseQueueAtPath:_DBPath];
  204. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:_userDefaultsSuiteName];
  205. [_DBManagerMock stopMocking];
  206. _DBManagerMock = nil;
  207. [_userDefaultsMock stopMocking];
  208. _userDefaultsMock = nil;
  209. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  210. [(id)_configInstances[i] stopMocking];
  211. [(id)_configFetch[i] stopMocking];
  212. }
  213. [_configInstances removeAllObjects];
  214. [_configFetch removeAllObjects];
  215. _configInstances = nil;
  216. _configFetch = nil;
  217. [super tearDown];
  218. }
  219. - (void)testFetchConfigWithNilCallback {
  220. NSMutableArray<XCTestExpectation *> *expectations =
  221. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  222. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  223. expectations[i] = [self
  224. expectationWithDescription:
  225. [NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
  226. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  227. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:nil];
  228. dispatch_after(
  229. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_checkCompletionTimeout * NSEC_PER_SEC)),
  230. dispatch_get_main_queue(), ^{
  231. XCTAssertEqual(_configInstances[i].lastFetchStatus, 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 =
  246. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  247. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  248. XCTAssertNil(error);
  249. #pragma clang diagnostic push
  250. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  251. XCTAssertTrue([_configInstances[i] activateFetched]);
  252. #pragma clang diagnostic pop
  253. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  254. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  255. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  256. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  257. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, value1);
  258. XCTAssertEqualObjects(_configInstances[i][key2].stringValue, value2);
  259. OCMVerify([_configInstances[i] objectForKeyedSubscript:key1]);
  260. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  261. @"Callback of first successful config "
  262. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
  263. XCTAssertNotNil(_configInstances[i].lastFetchTime);
  264. XCTAssertGreaterThan(_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  265. @"last fetch time interval should be set.");
  266. [expectations[i] fulfill];
  267. };
  268. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  269. }
  270. [self waitForExpectationsWithTimeout:_expectationTimeout
  271. handler:^(NSError *error) {
  272. XCTAssertNil(error);
  273. }];
  274. }
  275. - (void)testFetchAndActivate {
  276. NSMutableArray<XCTestExpectation *> *expectations =
  277. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  278. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  279. expectations[i] =
  280. [self expectationWithDescription:
  281. [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
  282. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  283. FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion = ^void(
  284. FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
  285. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  286. XCTAssertNil(error);
  287. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  288. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  289. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  290. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  291. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, value1);
  292. XCTAssertEqualObjects(_configInstances[i][key2].stringValue, value2);
  293. OCMVerify([_configInstances[i] objectForKeyedSubscript:key1]);
  294. XCTAssertEqual(
  295. status, FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote,
  296. @"Callback of first successful config "
  297. @"fetchAndActivate status must equal to FIRRemoteConfigFetchAndStatusSuccessFromRemote.");
  298. XCTAssertNotNil(_configInstances[i].lastFetchTime);
  299. XCTAssertGreaterThan(_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  300. @"last fetch time interval should be set.");
  301. [expectations[i] fulfill];
  302. };
  303. // Update the minimum fetch interval to 0. This disables the cache and forces a remote fetch
  304. // request.
  305. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  306. settings.minimumFetchInterval = 0;
  307. [_configInstances[i] setConfigSettings:settings];
  308. [_configInstances[i] fetchAndActivateWithCompletionHandler:fetchAndActivateCompletion];
  309. }
  310. [self waitForExpectationsWithTimeout:_expectationTimeout
  311. handler:^(NSError *error) {
  312. XCTAssertNil(error);
  313. }];
  314. }
  315. // TODO: Try splitting into smaller tests.
  316. - (void)testFetchConfigsSuccessfullyWithNewActivateMethodSignature {
  317. NSMutableArray<XCTestExpectation *> *expectations =
  318. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  319. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  320. expectations[i] =
  321. [self expectationWithDescription:
  322. [NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
  323. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  324. FIRRemoteConfigFetchCompletion fetchCompletion =
  325. ^(FIRRemoteConfigFetchStatus status, NSError *error) {
  326. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  327. XCTAssertNil(error);
  328. [_configInstances[i] activateWithCompletionHandler:^(NSError *_Nullable error) {
  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(_configInstances[i][key1].stringValue, value1);
  335. XCTAssertEqualObjects(_configInstances[i][key2].stringValue, value2);
  336. OCMVerify([_configInstances[i] objectForKeyedSubscript:key1]);
  337. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  338. @"Callback of first successful config "
  339. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
  340. XCTAssertNotNil(_configInstances[i].lastFetchTime);
  341. XCTAssertGreaterThan(_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  342. @"last fetch time interval should be set.");
  343. // A second activate should return an error.
  344. [_configInstances[i] activateWithCompletionHandler:^(NSError *_Nullable error) {
  345. XCTAssertNotNil(error);
  346. XCTAssertEqualObjects(error.domain, FIRRemoteConfigErrorDomain);
  347. }];
  348. [expectations[i] fulfill];
  349. }];
  350. };
  351. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  352. }
  353. [self waitForExpectationsWithTimeout:_expectationTimeout
  354. handler:^(NSError *error) {
  355. XCTAssertNil(error);
  356. }];
  357. }
  358. - (void)testEnumeratingConfigResults {
  359. NSMutableArray<XCTestExpectation *> *expectations =
  360. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  361. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  362. expectations[i] = [self
  363. expectationWithDescription:
  364. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  365. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  366. FIRRemoteConfigFetchCompletion fetchCompletion =
  367. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  368. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  369. XCTAssertNil(error);
  370. #pragma clang diagnostic push
  371. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  372. XCTAssertTrue([_configInstances[i] activateFetched]);
  373. #pragma clang diagnostic pop
  374. NSString *key5 = [NSString stringWithFormat:@"key5-%d", i];
  375. NSString *key19 = [NSString stringWithFormat:@"key19-%d", i];
  376. NSString *value5 = [NSString stringWithFormat:@"value5-%d", i];
  377. NSString *value19 = [NSString stringWithFormat:@"value19-%d", i];
  378. XCTAssertEqualObjects(_configInstances[i][key5].stringValue, value5);
  379. XCTAssertEqualObjects(_configInstances[i][key19].stringValue, value19);
  380. // Test enumerating config values.
  381. for (NSString *key in _configInstances[i]) {
  382. if ([key isEqualToString:key5]) {
  383. XCTAssertEqualObjects(_configInstances[i][key5].stringValue, value5);
  384. }
  385. if ([key isEqualToString:key19]) {
  386. XCTAssertEqualObjects(_configInstances[i][key19].stringValue, value19);
  387. }
  388. }
  389. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  390. @"Callback of first successful config "
  391. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccessFresh.");
  392. XCTAssertNotNil(_configInstances[i].lastFetchTime);
  393. XCTAssertGreaterThan(_configInstances[i].lastFetchTime.timeIntervalSince1970, 0,
  394. @"last fetch time interval should be set.");
  395. [expectations[i] fulfill];
  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. void (^handler)(FIRRemoteConfigFetchStatus status, NSError *_Nullable error) = nil;
  464. // void (^handler)(FIRRemoteConfigFetchCompletion);
  465. [invocation getArgument:&handler atIndex:3];
  466. [_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  467. completionHandler:handler];
  468. });
  469. _response[i] = @{};
  470. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  471. _URLResponse[i] =
  472. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  473. statusCode:500
  474. HTTPVersion:nil
  475. headerFields:@{@"etag" : @"etag1"}];
  476. id completionBlock =
  477. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  478. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  479. completionHandler:completionBlock])
  480. .andReturn(nil);
  481. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  482. configContent:configContent
  483. configSettings:settings
  484. configExperiment:nil];
  485. }
  486. #pragma clang diagnostic push
  487. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  488. // Make the fetch calls for all instances.
  489. NSMutableArray<XCTestExpectation *> *expectations =
  490. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  491. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  492. expectations[i] = [self
  493. expectationWithDescription:
  494. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  495. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  496. FIRRemoteConfigFetchCompletion fetchCompletion =
  497. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  498. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
  499. XCTAssertFalse([_configInstances[i] activateFetched]);
  500. XCTAssertNotNil(error);
  501. // No such key, still return a static value.
  502. FIRRemoteConfigValue *value = _configInstances[RCNTestRCInstanceDefault][@"key1"];
  503. XCTAssertEqual((int)value.source, (int)FIRRemoteConfigSourceStatic);
  504. XCTAssertEqualObjects(value.stringValue, @"");
  505. XCTAssertEqual(value.boolValue, NO);
  506. [expectations[i] fulfill];
  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. void (^handler)(FIRRemoteConfigFetchStatus status, NSError *_Nullable error) = nil;
  576. // void (^handler)(FIRRemoteConfigFetchCompletion);
  577. [invocation getArgument:&handler atIndex:3];
  578. [_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  579. completionHandler:handler];
  580. });
  581. _response[i] = @{};
  582. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  583. // A no network error is accompanied with an HTTP status code of 0.
  584. _URLResponse[i] =
  585. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  586. statusCode:0
  587. HTTPVersion:nil
  588. headerFields:@{@"etag" : @"etag1"}];
  589. id completionBlock =
  590. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  591. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  592. completionHandler:completionBlock])
  593. .andReturn(nil);
  594. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  595. configContent:configContent
  596. configSettings:settings
  597. configExperiment:nil];
  598. }
  599. #pragma clang diagnostic push
  600. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  601. // Make the fetch calls for all instances.
  602. NSMutableArray<XCTestExpectation *> *expectations =
  603. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  604. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  605. expectations[i] = [self
  606. expectationWithDescription:
  607. [NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
  608. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  609. FIRRemoteConfigFetchCompletion fetchCompletion =
  610. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  611. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusFailure);
  612. XCTAssertFalse([_configInstances[i] activateFetched]);
  613. XCTAssertNotNil(error);
  614. // No such key, still return a static value.
  615. FIRRemoteConfigValue *value = _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. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  622. }
  623. [self waitForExpectationsWithTimeout:_expectationTimeout
  624. handler:^(NSError *error) {
  625. XCTAssertNil(error);
  626. }];
  627. }
  628. // Activate should return false if a fetch response returns 200 with NO_CHANGE as the response body.
  629. - (void)testActivateOnFetchNoChangeStatus {
  630. // Override the setup values to return back an error status.
  631. RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
  632. // Populate the default, second app, second namespace instances.
  633. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  634. NSString *currentAppName = nil;
  635. FIROptions *currentOptions = nil;
  636. NSString *currentNamespace = nil;
  637. switch (i) {
  638. case RCNTestRCInstanceSecondNamespace:
  639. currentAppName = RCNTestsDefaultFIRAppName;
  640. currentOptions = [self firstAppOptions];
  641. currentNamespace = RCNTestsPerfNamespace;
  642. break;
  643. case RCNTestRCInstanceSecondApp:
  644. currentAppName = RCNTestsSecondFIRAppName;
  645. currentOptions = [self secondAppOptions];
  646. currentNamespace = FIRNamespaceGoogleMobilePlatform;
  647. break;
  648. case RCNTestRCInstanceDefault:
  649. default:
  650. currentAppName = RCNTestsDefaultFIRAppName;
  651. currentOptions = [self firstAppOptions];
  652. currentNamespace = RCNTestsFIRNamespace;
  653. break;
  654. }
  655. NSString *fullyQualifiedNamespace =
  656. [NSString stringWithFormat:@"%@:%@", currentNamespace, currentAppName];
  657. RCNUserDefaultsManager *userDefaultsManager =
  658. [[RCNUserDefaultsManager alloc] initWithAppName:currentAppName
  659. bundleID:[NSBundle mainBundle].bundleIdentifier
  660. namespace:fullyQualifiedNamespace];
  661. userDefaultsManager.lastFetchTime = 10;
  662. FIRRemoteConfig *config =
  663. OCMPartialMock([[FIRRemoteConfig alloc] initWithAppName:currentAppName
  664. FIROptions:currentOptions
  665. namespace:currentNamespace
  666. DBManager:_DBManager
  667. configContent:configContent
  668. analytics:nil]);
  669. _configInstances[i] = config;
  670. RCNConfigSettings *settings =
  671. [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  672. namespace:fullyQualifiedNamespace
  673. firebaseAppName:currentAppName
  674. googleAppID:currentOptions.googleAppID];
  675. // Start the test with the assumption that we have some data that was fetched and activated.
  676. settings.lastETag = @"etag1";
  677. settings.lastETagUpdateTime = 100;
  678. settings.lastApplyTimeInterval = 101;
  679. dispatch_queue_t queue =
  680. dispatch_queue_create([[NSString stringWithFormat:@"testNoStatusFetchQueue: %d", i]
  681. cStringUsingEncoding:NSUTF8StringEncoding],
  682. DISPATCH_QUEUE_SERIAL);
  683. _configFetch[i] = OCMPartialMock([[RCNConfigFetch alloc] initWithContent:configContent
  684. DBManager:_DBManager
  685. settings:settings
  686. analytics:nil
  687. experiment:nil
  688. queue:queue
  689. namespace:fullyQualifiedNamespace
  690. options:currentOptions]);
  691. OCMStub([_configFetch[i] fetchConfigWithExpirationDuration:43200 completionHandler:OCMOCK_ANY])
  692. .andDo(^(NSInvocation *invocation) {
  693. void (^handler)(FIRRemoteConfigFetchStatus status, NSError *_Nullable error) = nil;
  694. [invocation getArgument:&handler atIndex:3];
  695. [_configFetch[i] fetchWithUserProperties:[[NSDictionary alloc] init]
  696. completionHandler:handler];
  697. });
  698. _response[i] = @{@"state" : @"NO_CHANGE"};
  699. _responseData[i] = [NSJSONSerialization dataWithJSONObject:_response[i] options:0 error:nil];
  700. _URLResponse[i] =
  701. [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"https://firebase.com"]
  702. statusCode:200
  703. HTTPVersion:nil
  704. headerFields:@{@"etag" : @"etag1"}];
  705. id completionBlock =
  706. [OCMArg invokeBlockWithArgs:_responseData[i], _URLResponse[i], [NSNull null], nil];
  707. OCMExpect([_configFetch[i] URLSessionDataTaskWithContent:[OCMArg any]
  708. completionHandler:completionBlock])
  709. .andReturn(nil);
  710. [_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
  711. configContent:configContent
  712. configSettings:settings
  713. configExperiment:nil];
  714. }
  715. #pragma clang diagnostic push
  716. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  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 =
  727. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  728. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  729. XCTAssertFalse([_configInstances[i] activateFetched]);
  730. XCTAssertNil(error);
  731. [expectations[i] fulfill];
  732. };
  733. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  734. }
  735. [self waitForExpectationsWithTimeout:_expectationTimeout
  736. handler:^(NSError *error) {
  737. XCTAssertNil(error);
  738. }];
  739. }
  740. - (void)testConfigValueForKey {
  741. NSMutableArray<XCTestExpectation *> *expectations =
  742. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  743. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  744. expectations[i] =
  745. [self expectationWithDescription:
  746. [NSString stringWithFormat:@"Test configValueForKey: method - instance %d", i]];
  747. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
  748. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  749. NSError *error) {
  750. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  751. XCTAssertNil(error);
  752. XCTAssertTrue([_configInstances[i] activateFetched]);
  753. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  754. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  755. NSString *key3 = [NSString stringWithFormat:@"key3-%d", i];
  756. NSString *key7 = [NSString stringWithFormat:@"key7-%d", i];
  757. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  758. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  759. NSString *value3 = [NSString stringWithFormat:@"value3-%d", i];
  760. NSString *value7 = [NSString stringWithFormat:@"value7-%d", i];
  761. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, value1);
  762. XCTAssertEqualObjects(_configInstances[i][key2].stringValue, value2);
  763. OCMVerify([_configInstances[i] objectForKeyedSubscript:key1]);
  764. XCTAssertEqualObjects([_configInstances[i] configValueForKey:key3].stringValue, value3);
  765. if (i == RCNTestRCInstanceDefault) {
  766. XCTAssertEqualObjects(
  767. [_configInstances[i] configValueForKey:key7 namespace:FIRNamespaceGoogleMobilePlatform]
  768. .stringValue,
  769. value7);
  770. }
  771. XCTAssertEqualObjects([_configInstances[i] configValueForKey:key7].stringValue, value7);
  772. XCTAssertNotNil([_configInstances[i] configValueForKey:nil]);
  773. XCTAssertEqual([_configInstances[i] configValueForKey:nil].source,
  774. FIRRemoteConfigSourceStatic);
  775. XCTAssertEqual([_configInstances[i] configValueForKey:nil namespace:nil].source,
  776. FIRRemoteConfigSourceStatic);
  777. XCTAssertEqual([_configInstances[i] configValueForKey:nil namespace:nil source:-1].source,
  778. FIRRemoteConfigSourceStatic);
  779. [expectations[i] fulfill];
  780. };
  781. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  782. }
  783. [self waitForExpectationsWithTimeout:_expectationTimeout
  784. handler:^(NSError *error) {
  785. XCTAssertNil(error);
  786. }];
  787. }
  788. - (void)testFetchConfigWithDefaultSets {
  789. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  790. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  791. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  792. fetchConfigsExpectation[i] = [self
  793. expectationWithDescription:
  794. [NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
  795. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  796. NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
  797. NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
  798. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  799. NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
  800. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
  801. [_configInstances[i] setDefaults:defaults];
  802. FIRRemoteConfigFetchCompletion fetchCompletion =
  803. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  804. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  805. XCTAssertNil(error);
  806. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, @"default key1");
  807. XCTAssertEqual(_configInstances[i][key1].source, FIRRemoteConfigSourceDefault);
  808. #pragma clang diagnostic push
  809. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  810. XCTAssertTrue([_configInstances[i] activateFetched]);
  811. #pragma clang diagnostic pop
  812. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, value1);
  813. XCTAssertEqual(_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
  814. XCTAssertEqualObjects([_configInstances[i] defaultValueForKey:key1].stringValue,
  815. @"default key1");
  816. XCTAssertEqualObjects(_configInstances[i][key2].stringValue, value2);
  817. XCTAssertEqualObjects(_configInstances[i][key0].stringValue, @"value0-0");
  818. #pragma clang diagnostic push
  819. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  820. XCTAssertNil([_configInstances[i] defaultValueForKey:nil namespace:nil]);
  821. #pragma clang diagnostic pop
  822. OCMVerify([_configInstances[i] objectForKeyedSubscript:key1]);
  823. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  824. @"Callback of first successful config "
  825. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
  826. [fetchConfigsExpectation[i] fulfill];
  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 namespace:RCNTestsPerfNamespace];
  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" namespace:RCNTestsPerfNamespace]
  877. .stringValue,
  878. @"2860", @"Only default config has the key, must equal to default config value.");
  879. }
  880. // Reset default sets
  881. [_configInstances[i] setDefaults:nil];
  882. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 0);
  883. }
  884. }
  885. - (void)testSetDefaultsWithNilParams {
  886. NSMutableArray<XCTestExpectation *> *expectations =
  887. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  888. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  889. expectations[i] = [self
  890. expectationWithDescription:
  891. [NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
  892. // Should work when passing nil.
  893. [_configInstances[i] setDefaults:nil];
  894. [_configInstances[i] setDefaults:nil namespace:nil];
  895. dispatch_after(
  896. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_checkCompletionTimeout * NSEC_PER_SEC)),
  897. dispatch_get_main_queue(), ^{
  898. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault
  899. namespace:FIRNamespaceGoogleMobilePlatform]
  900. .count,
  901. 0);
  902. [expectations[i] fulfill];
  903. });
  904. }
  905. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  906. }
  907. - (void)testFetchConfigOverwriteDefaultSet {
  908. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  909. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  910. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  911. fetchConfigsExpectation[i] = [self
  912. expectationWithDescription:
  913. [NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
  914. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  915. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  916. [_configInstances[i] setDefaults:@{key1 : @"default key1"}];
  917. FIRRemoteConfigValue *value = _configInstances[i][key1];
  918. XCTAssertEqualObjects(value.stringValue, @"default key1");
  919. XCTAssertEqual(value.source, FIRRemoteConfigSourceDefault);
  920. value = _configInstances[i][@"A key doesn't exist"];
  921. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  922. FIRRemoteConfigFetchCompletion fetchCompletion =
  923. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  924. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  925. XCTAssertNil(error);
  926. XCTAssertTrue([_configInstances[i] activateFetched]);
  927. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, value1);
  928. XCTAssertEqual(_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
  929. XCTAssertEqualObjects([_configInstances[i] defaultValueForKey:key1].stringValue,
  930. @"default key1");
  931. OCMVerify([_configInstances[i] objectForKeyedSubscript:key1]);
  932. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
  933. @"Callback of first successful config "
  934. @"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
  935. [fetchConfigsExpectation[i] fulfill];
  936. };
  937. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  938. }
  939. [self waitForExpectationsWithTimeout:_expectationTimeout
  940. handler:^(NSError *error) {
  941. XCTAssertNil(error);
  942. }];
  943. }
  944. - (void)testGetConfigValueBySource {
  945. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  946. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  947. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  948. fetchConfigsExpectation[i] =
  949. [self expectationWithDescription:
  950. [NSString stringWithFormat:@"Test get config value by source - instance %d", i]];
  951. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  952. NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
  953. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default value1"};
  954. [_configInstances[i] setDefaults:defaults];
  955. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  956. NSError *error) {
  957. XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusSuccess);
  958. XCTAssertNil(error);
  959. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, @"default value1");
  960. XCTAssertEqual(_configInstances[i][key1].source, FIRRemoteConfigSourceDefault);
  961. XCTAssertTrue([_configInstances[i] activateFetched]);
  962. XCTAssertEqualObjects(_configInstances[i][key1].stringValue, value1);
  963. XCTAssertEqual(_configInstances[i][key1].source, FIRRemoteConfigSourceRemote);
  964. FIRRemoteConfigValue *value;
  965. if (i == RCNTestRCInstanceDefault) {
  966. value = [_configInstances[i] configValueForKey:key1
  967. namespace:FIRNamespaceGoogleMobilePlatform
  968. source:FIRRemoteConfigSourceRemote];
  969. XCTAssertEqualObjects(value.stringValue, value1);
  970. value = [_configInstances[i] configValueForKey:key1
  971. namespace:FIRNamespaceGoogleMobilePlatform
  972. source:FIRRemoteConfigSourceDefault];
  973. XCTAssertEqualObjects(value.stringValue, @"default value1");
  974. value = [_configInstances[i] configValueForKey:key1
  975. namespace:FIRNamespaceGoogleMobilePlatform
  976. source:FIRRemoteConfigSourceStatic];
  977. } else {
  978. value = [_configInstances[i] configValueForKey:key1 source:FIRRemoteConfigSourceRemote];
  979. XCTAssertEqualObjects(value.stringValue, value1);
  980. value = [_configInstances[i] configValueForKey:key1 source:FIRRemoteConfigSourceDefault];
  981. XCTAssertEqualObjects(value.stringValue, @"default value1");
  982. value = [_configInstances[i] configValueForKey:key1 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. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  993. }
  994. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  995. }
  996. - (void)testInvalidKeyOrNamespace {
  997. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  998. FIRRemoteConfigValue *value = [_configInstances[i] configValueForKey:nil];
  999. XCTAssertNotNil(value);
  1000. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  1001. value = [_configInstances[i] configValueForKey:nil namespace:nil];
  1002. XCTAssertNotNil(value);
  1003. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  1004. value = [_configInstances[i] configValueForKey:nil namespace:nil source:5];
  1005. XCTAssertNotNil(value);
  1006. XCTAssertEqual(value.source, FIRRemoteConfigSourceStatic);
  1007. }
  1008. }
  1009. // Remote Config converts UTC times in the plists to local times. This utility function makes it
  1010. // possible to check the times when running the tests in any timezone.
  1011. static NSString *UTCToLocal(NSString *utcTime) {
  1012. // Create a UTC dateFormatter.
  1013. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1014. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  1015. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  1016. NSDate *date = [dateFormatter dateFromString:utcTime];
  1017. [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
  1018. return [dateFormatter stringFromDate:date];
  1019. }
  1020. - (void)testSetDefaultsFromPlist {
  1021. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1022. FIRRemoteConfig *config = _configInstances[i];
  1023. [config setDefaultsFromPlistFileName:@"Defaults-testInfo"];
  1024. XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
  1025. UTCToLocal(@"2016-02-28 18:33:31"));
  1026. XCTAssertEqual(_configInstances[i][@"isPaidUser"].boolValue, YES);
  1027. XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
  1028. XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
  1029. XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
  1030. XCTAssertEqualObjects(_configInstances[i][@"FileInfo"].stringValue,
  1031. @"To setup default config.");
  1032. XCTAssertEqualObjects(_configInstances[i][@"format"].stringValue, @"key to value.");
  1033. // If given a wrong file name, the default will not be set and kept as previous results.
  1034. [_configInstances[i] setDefaultsFromPlistFileName:@""];
  1035. XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
  1036. UTCToLocal(@"2016-02-28 18:33:31"));
  1037. [_configInstances[i] setDefaultsFromPlistFileName:@"non-existed_file"];
  1038. XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
  1039. [_configInstances[i] setDefaultsFromPlistFileName:nil namespace:nil];
  1040. XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
  1041. [_configInstances[i] setDefaultsFromPlistFileName:@"" namespace:@""];
  1042. XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
  1043. }
  1044. }
  1045. - (void)testSetDefaultsAndNamespaceFromPlist {
  1046. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1047. if (i == RCNTestRCInstanceDefault) {
  1048. [_configInstances[i] setDefaultsFromPlistFileName:@"Defaults-testInfo"
  1049. namespace:RCNTestsPerfNamespace];
  1050. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"lastCheckTime"
  1051. namespace:RCNTestsPerfNamespace]
  1052. .stringValue,
  1053. UTCToLocal(@"2016-02-28 18:33:31"));
  1054. XCTAssertEqual([_configInstances[i] configValueForKey:@"isPaidUser"
  1055. namespace:RCNTestsPerfNamespace]
  1056. .boolValue,
  1057. YES);
  1058. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"dataValue"
  1059. namespace:RCNTestsPerfNamespace]
  1060. .stringValue,
  1061. @"2.4");
  1062. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"New item"
  1063. namespace:RCNTestsPerfNamespace]
  1064. .numberValue,
  1065. @(2.4));
  1066. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"Languages"
  1067. namespace:RCNTestsPerfNamespace]
  1068. .stringValue,
  1069. @"English");
  1070. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"FileInfo"
  1071. namespace:RCNTestsPerfNamespace]
  1072. .stringValue,
  1073. @"To setup default config.");
  1074. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"format"
  1075. namespace:RCNTestsPerfNamespace]
  1076. .stringValue,
  1077. @"key to value.");
  1078. } else {
  1079. [_configInstances[i] setDefaultsFromPlistFileName:@"Defaults-testInfo"];
  1080. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"lastCheckTime"].stringValue,
  1081. UTCToLocal(@"2016-02-28 18:33:31"));
  1082. XCTAssertEqual([_configInstances[i] configValueForKey:@"isPaidUser"].boolValue, YES);
  1083. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"dataValue"].stringValue,
  1084. @"2.4");
  1085. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"New item"].numberValue,
  1086. @(2.4));
  1087. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"Languages"].stringValue,
  1088. @"English");
  1089. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"FileInfo"].stringValue,
  1090. @"To setup default config.");
  1091. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"format"].stringValue,
  1092. @"key to value.");
  1093. }
  1094. }
  1095. }
  1096. - (void)testSetDeveloperMode {
  1097. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1098. XCTAssertFalse(_configInstances[i].configSettings.isDeveloperModeEnabled);
  1099. FIRRemoteConfigSettings *settings =
  1100. [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
  1101. _configInstances[i].configSettings = settings;
  1102. XCTAssertTrue(_configInstances[i].configSettings.isDeveloperModeEnabled);
  1103. }
  1104. }
  1105. - (void)testAllKeysFromSource {
  1106. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1107. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1108. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1109. fetchConfigsExpectation[i] = [self
  1110. expectationWithDescription:[NSString
  1111. stringWithFormat:@"Test allKeys methods - instance %d", i]];
  1112. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  1113. NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
  1114. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
  1115. [_configInstances[i] setDefaults:defaults];
  1116. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  1117. NSError *error) {
  1118. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  1119. XCTAssertNil(error);
  1120. XCTAssertTrue([_configInstances[i] activateFetched]);
  1121. if (i == RCNTestRCInstanceDefault) {
  1122. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1123. namespace:FIRNamespaceGoogleMobilePlatform]
  1124. .count,
  1125. 100);
  1126. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault
  1127. namespace:FIRNamespaceGoogleMobilePlatform]
  1128. .count,
  1129. 2);
  1130. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceStatic
  1131. namespace:FIRNamespaceGoogleMobilePlatform]
  1132. .count,
  1133. 0);
  1134. } else {
  1135. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote].count,
  1136. 100);
  1137. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count,
  1138. 2);
  1139. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceStatic].count,
  1140. 0);
  1141. }
  1142. XCTAssertNotNil([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1143. namespace:@"invalid namespace"]);
  1144. XCTAssertEqual([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1145. namespace:@"invalid namespace"]
  1146. .count,
  1147. 0);
  1148. XCTAssertNotNil([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1149. namespace:nil]);
  1150. XCTAssertEqual(
  1151. [_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote namespace:nil].count,
  1152. 0);
  1153. XCTAssertNotNil([_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault
  1154. namespace:nil]);
  1155. XCTAssertEqual(
  1156. [_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault namespace:nil].count,
  1157. 0);
  1158. [fetchConfigsExpectation[i] fulfill];
  1159. };
  1160. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1161. }
  1162. [self waitForExpectationsWithTimeout:_expectationTimeout
  1163. handler:^(NSError *error) {
  1164. XCTAssertNil(error);
  1165. }];
  1166. }
  1167. - (void)testAllKeysWithPrefix {
  1168. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1169. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1170. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1171. fetchConfigsExpectation[i] = [self
  1172. expectationWithDescription:[NSString
  1173. stringWithFormat:@"Test allKeys methods - instance %d", i]];
  1174. FIRRemoteConfigFetchCompletion fetchCompletion =
  1175. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  1176. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  1177. XCTAssertNil(error);
  1178. NSLog(@"Testing _configInstances %d", i);
  1179. #pragma clang diagnostic push
  1180. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1181. XCTAssertTrue([_configInstances[i] activateFetched]);
  1182. // Test keysWithPrefix:namespace: method.
  1183. if (i == RCNTestRCInstanceDefault) {
  1184. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"key"
  1185. namespace:FIRNamespaceGoogleMobilePlatform]
  1186. .count,
  1187. 100);
  1188. } else {
  1189. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"key"].count, 100);
  1190. }
  1191. XCTAssertEqual(
  1192. [_configInstances[i] keysWithPrefix:@"pl" namespace:@"invalid namespace"].count, 0);
  1193. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"pl" namespace:nil].count, 0);
  1194. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"pl" namespace:@""].count, 0);
  1195. XCTAssertNotNil([_configInstances[i] keysWithPrefix:nil namespace:nil]);
  1196. XCTAssertEqual([_configInstances[i] keysWithPrefix:nil namespace:nil].count, 0);
  1197. #pragma clang diagnostic pop
  1198. // Test keysWithPrefix: method.
  1199. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"key1"].count, 12);
  1200. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"key"].count, 100);
  1201. XCTAssertEqual([_configInstances[i] keysWithPrefix:@"invalid key"].count, 0);
  1202. XCTAssertEqual([_configInstances[i] keysWithPrefix:nil].count, 100);
  1203. XCTAssertEqual([_configInstances[i] keysWithPrefix:@""].count, 100);
  1204. [fetchConfigsExpectation[i] fulfill];
  1205. };
  1206. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1207. }
  1208. [self waitForExpectationsWithTimeout:_expectationTimeout
  1209. handler:^(NSError *error) {
  1210. XCTAssertNil(error);
  1211. }];
  1212. }
  1213. - (void)testSetDeveloperModeConfigSetting {
  1214. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1215. #pragma clang diagnostic push
  1216. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1217. FIRRemoteConfigSettings *settings =
  1218. [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
  1219. [_configInstances[i] setConfigSettings:settings];
  1220. XCTAssertTrue([_configInstances[i] configSettings].isDeveloperModeEnabled);
  1221. settings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO];
  1222. [_configInstances[i] setConfigSettings:settings];
  1223. XCTAssertFalse([_configInstances[i] configSettings].isDeveloperModeEnabled);
  1224. #pragma clang diagnostic pop
  1225. }
  1226. }
  1227. /// Test the minimum fetch interval is applied and read back correctly.
  1228. - (void)testSetMinimumFetchIntervalConfigSetting {
  1229. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1230. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1231. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1232. fetchConfigsExpectation[i] = [self
  1233. expectationWithDescription:
  1234. [NSString stringWithFormat:@"Test minimumFetchInterval expectation - instance %d", i]];
  1235. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1236. settings.minimumFetchInterval = 123;
  1237. [_configInstances[i] setConfigSettings:settings];
  1238. XCTAssertEqual([_configInstances[i] configSettings].minimumFetchInterval, 123);
  1239. FIRRemoteConfigFetchCompletion fetchCompletion =
  1240. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  1241. XCTAssertFalse([_configInstances[i].settings hasMinimumFetchIntervalElapsed:43200]);
  1242. // Update minimum fetch interval.
  1243. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1244. settings.minimumFetchInterval = 0;
  1245. [_configInstances[i] setConfigSettings:settings];
  1246. XCTAssertEqual([_configInstances[i] configSettings].minimumFetchInterval, 0);
  1247. XCTAssertTrue([_configInstances[i].settings hasMinimumFetchIntervalElapsed:0]);
  1248. [fetchConfigsExpectation[i] fulfill];
  1249. };
  1250. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1251. }
  1252. [self waitForExpectationsWithTimeout:_expectationTimeout
  1253. handler:^(NSError *error) {
  1254. XCTAssertNil(error);
  1255. }];
  1256. }
  1257. /// Test the fetch timeout is properly set and read back.
  1258. - (void)testSetFetchTimeoutConfigSetting {
  1259. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1260. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1261. settings.fetchTimeout = 1;
  1262. [_configInstances[i] setConfigSettings:settings];
  1263. XCTAssertEqual([_configInstances[i] configSettings].fetchTimeout, 1);
  1264. NSURLSession *networkSession = [_configFetch[i] currentNetworkSession];
  1265. XCTAssertNotNil(networkSession);
  1266. XCTAssertEqual(networkSession.configuration.timeoutIntervalForResource, 1);
  1267. XCTAssertEqual(networkSession.configuration.timeoutIntervalForRequest, 1);
  1268. }
  1269. }
  1270. #pragma mark - Public Factory Methods
  1271. - (void)testConfigureConfigWithValidInput {
  1272. // Configure the default app with our options and ensure the Remote Config instance is set up
  1273. // properly.
  1274. if (![FIRApp isDefaultAppConfigured]) {
  1275. XCTAssertNoThrow([FIRApp configureWithOptions:[self firstAppOptions]]);
  1276. }
  1277. XCTAssertNoThrow([FIRRemoteConfig remoteConfig]);
  1278. FIRRemoteConfig *config = [FIRRemoteConfig remoteConfig];
  1279. XCTAssertNotNil(config);
  1280. // Ensure the same instance is returned from the singleton.
  1281. FIRRemoteConfig *sameConfig = [FIRRemoteConfig remoteConfig];
  1282. XCTAssertNotNil(sameConfig);
  1283. XCTAssertEqual(config, sameConfig);
  1284. // Ensure the app name is stored properly.
  1285. XCTAssertEqual([config valueForKey:@"_appName"], kFIRDefaultAppName);
  1286. }
  1287. #pragma mark - Test Helpers
  1288. - (FIROptions *)firstAppOptions {
  1289. // TODO: Evaluate if we want to hardcode things here instead.
  1290. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  1291. GCMSenderID:@"correct_gcm_sender_id"];
  1292. options.APIKey = @"correct_api_key";
  1293. options.projectID = @"abc-xyz-123";
  1294. return options;
  1295. }
  1296. - (FIROptions *)secondAppOptions {
  1297. FIROptions *options =
  1298. [[FIROptions alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]]
  1299. pathForResource:@"SecondApp-GoogleService-Info"
  1300. ofType:@"plist"]];
  1301. XCTAssertNotNil(options);
  1302. return options;
  1303. }
  1304. @end