RCNRemoteConfigTest.m 73 KB

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