RCNRemoteConfigTest.m 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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/Private/RCNConfigFetch.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  21. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  22. #import "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
  23. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  24. #import <OCMock/OCMStubRecorder.h>
  25. #import <OCMock/OCMock.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. - (void)testSetDefaultsFromPlist {
  1030. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1031. FIRRemoteConfig *config = _configInstances[i];
  1032. [config setDefaultsFromPlistFileName:@"Defaults-testInfo"];
  1033. XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
  1034. UTCToLocal(@"2016-02-28 18:33:31"));
  1035. XCTAssertEqual(_configInstances[i][@"isPaidUser"].boolValue, YES);
  1036. XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
  1037. XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
  1038. XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
  1039. XCTAssertEqualObjects(_configInstances[i][@"FileInfo"].stringValue,
  1040. @"To setup default config.");
  1041. XCTAssertEqualObjects(_configInstances[i][@"format"].stringValue, @"key to value.");
  1042. // If given a wrong file name, the default will not be set and kept as previous results.
  1043. [_configInstances[i] setDefaultsFromPlistFileName:@""];
  1044. XCTAssertEqualObjects(_configInstances[i][@"lastCheckTime"].stringValue,
  1045. UTCToLocal(@"2016-02-28 18:33:31"));
  1046. [_configInstances[i] setDefaultsFromPlistFileName:@"non-existed_file"];
  1047. XCTAssertEqualObjects(_configInstances[i][@"dataValue"].stringValue, @"2.4");
  1048. [_configInstances[i] setDefaultsFromPlistFileName:nil namespace:nil];
  1049. XCTAssertEqualObjects(_configInstances[i][@"New item"].numberValue, @(2.4));
  1050. [_configInstances[i] setDefaultsFromPlistFileName:@"" namespace:@""];
  1051. XCTAssertEqualObjects(_configInstances[i][@"Languages"].stringValue, @"English");
  1052. }
  1053. }
  1054. - (void)testSetDefaultsAndNamespaceFromPlist {
  1055. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1056. if (i == RCNTestRCInstanceDefault) {
  1057. [_configInstances[i] setDefaultsFromPlistFileName:@"Defaults-testInfo"
  1058. namespace:RCNTestsPerfNamespace];
  1059. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"lastCheckTime"
  1060. namespace:RCNTestsPerfNamespace]
  1061. .stringValue,
  1062. UTCToLocal(@"2016-02-28 18:33:31"));
  1063. XCTAssertEqual([_configInstances[i] configValueForKey:@"isPaidUser"
  1064. namespace:RCNTestsPerfNamespace]
  1065. .boolValue,
  1066. YES);
  1067. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"dataValue"
  1068. namespace:RCNTestsPerfNamespace]
  1069. .stringValue,
  1070. @"2.4");
  1071. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"New item"
  1072. namespace:RCNTestsPerfNamespace]
  1073. .numberValue,
  1074. @(2.4));
  1075. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"Languages"
  1076. namespace:RCNTestsPerfNamespace]
  1077. .stringValue,
  1078. @"English");
  1079. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"FileInfo"
  1080. namespace:RCNTestsPerfNamespace]
  1081. .stringValue,
  1082. @"To setup default config.");
  1083. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"format"
  1084. namespace:RCNTestsPerfNamespace]
  1085. .stringValue,
  1086. @"key to value.");
  1087. } else {
  1088. [_configInstances[i] setDefaultsFromPlistFileName:@"Defaults-testInfo"];
  1089. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"lastCheckTime"].stringValue,
  1090. UTCToLocal(@"2016-02-28 18:33:31"));
  1091. XCTAssertEqual([_configInstances[i] configValueForKey:@"isPaidUser"].boolValue, YES);
  1092. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"dataValue"].stringValue,
  1093. @"2.4");
  1094. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"New item"].numberValue,
  1095. @(2.4));
  1096. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"Languages"].stringValue,
  1097. @"English");
  1098. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"FileInfo"].stringValue,
  1099. @"To setup default config.");
  1100. XCTAssertEqualObjects([_configInstances[i] configValueForKey:@"format"].stringValue,
  1101. @"key to value.");
  1102. }
  1103. }
  1104. }
  1105. - (void)testSetDeveloperMode {
  1106. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1107. XCTAssertFalse(_configInstances[i].configSettings.isDeveloperModeEnabled);
  1108. FIRRemoteConfigSettings *settings =
  1109. [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
  1110. _configInstances[i].configSettings = settings;
  1111. XCTAssertTrue(_configInstances[i].configSettings.isDeveloperModeEnabled);
  1112. }
  1113. }
  1114. - (void)testAllKeysFromSource {
  1115. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1116. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1117. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1118. fetchConfigsExpectation[i] = [self
  1119. expectationWithDescription:[NSString
  1120. stringWithFormat:@"Test allKeys methods - instance %d", i]];
  1121. NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
  1122. NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
  1123. NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
  1124. [_configInstances[i] setDefaults:defaults];
  1125. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  1126. NSError *error) {
  1127. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  1128. XCTAssertNil(error);
  1129. XCTAssertTrue([self->_configInstances[i] activateFetched]);
  1130. if (i == RCNTestRCInstanceDefault) {
  1131. XCTAssertEqual(
  1132. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1133. namespace:FIRNamespaceGoogleMobilePlatform]
  1134. .count,
  1135. 100);
  1136. XCTAssertEqual(
  1137. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault
  1138. namespace:FIRNamespaceGoogleMobilePlatform]
  1139. .count,
  1140. 2);
  1141. XCTAssertEqual(
  1142. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceStatic
  1143. namespace:FIRNamespaceGoogleMobilePlatform]
  1144. .count,
  1145. 0);
  1146. } else {
  1147. XCTAssertEqual(
  1148. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote].count, 100);
  1149. XCTAssertEqual(
  1150. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault].count, 2);
  1151. XCTAssertEqual(
  1152. [self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceStatic].count, 0);
  1153. }
  1154. XCTAssertNotNil([self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1155. namespace:@"invalid namespace"]);
  1156. XCTAssertEqual([self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1157. namespace:@"invalid namespace"]
  1158. .count,
  1159. 0);
  1160. XCTAssertNotNil([self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1161. namespace:nil]);
  1162. XCTAssertEqual([self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceRemote
  1163. namespace:nil]
  1164. .count,
  1165. 0);
  1166. XCTAssertNotNil([self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault
  1167. namespace:nil]);
  1168. XCTAssertEqual([self->_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault
  1169. namespace:nil]
  1170. .count,
  1171. 0);
  1172. [fetchConfigsExpectation[i] fulfill];
  1173. };
  1174. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1175. }
  1176. [self waitForExpectationsWithTimeout:_expectationTimeout
  1177. handler:^(NSError *error) {
  1178. XCTAssertNil(error);
  1179. }];
  1180. }
  1181. - (void)testAllKeysWithPrefix {
  1182. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1183. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1184. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1185. fetchConfigsExpectation[i] = [self
  1186. expectationWithDescription:[NSString
  1187. stringWithFormat:@"Test allKeys methods - instance %d", i]];
  1188. FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
  1189. NSError *error) {
  1190. XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
  1191. XCTAssertNil(error);
  1192. NSLog(@"Testing _configInstances %d", i);
  1193. #pragma clang diagnostic push
  1194. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1195. XCTAssertTrue([self->_configInstances[i] activateFetched]);
  1196. // Test keysWithPrefix:namespace: method.
  1197. if (i == RCNTestRCInstanceDefault) {
  1198. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key"
  1199. namespace:FIRNamespaceGoogleMobilePlatform]
  1200. .count,
  1201. 100);
  1202. } else {
  1203. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key"].count, 100);
  1204. }
  1205. XCTAssertEqual(
  1206. [self->_configInstances[i] keysWithPrefix:@"pl" namespace:@"invalid namespace"].count, 0);
  1207. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"pl" namespace:nil].count, 0);
  1208. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"pl" namespace:@""].count, 0);
  1209. XCTAssertNotNil([self->_configInstances[i] keysWithPrefix:nil namespace:nil]);
  1210. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:nil namespace:nil].count, 0);
  1211. #pragma clang diagnostic pop
  1212. // Test keysWithPrefix: method.
  1213. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key1"].count, 12);
  1214. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"key"].count, 100);
  1215. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@"invalid key"].count, 0);
  1216. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:nil].count, 100);
  1217. XCTAssertEqual([self->_configInstances[i] keysWithPrefix:@""].count, 100);
  1218. [fetchConfigsExpectation[i] fulfill];
  1219. };
  1220. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1221. }
  1222. [self waitForExpectationsWithTimeout:_expectationTimeout
  1223. handler:^(NSError *error) {
  1224. XCTAssertNil(error);
  1225. }];
  1226. }
  1227. - (void)testSetDeveloperModeConfigSetting {
  1228. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1229. #pragma clang diagnostic push
  1230. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1231. FIRRemoteConfigSettings *settings =
  1232. [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
  1233. [_configInstances[i] setConfigSettings:settings];
  1234. XCTAssertTrue([_configInstances[i] configSettings].isDeveloperModeEnabled);
  1235. settings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO];
  1236. [_configInstances[i] setConfigSettings:settings];
  1237. XCTAssertFalse([_configInstances[i] configSettings].isDeveloperModeEnabled);
  1238. #pragma clang diagnostic pop
  1239. }
  1240. }
  1241. /// Test the minimum fetch interval is applied and read back correctly.
  1242. - (void)testSetMinimumFetchIntervalConfigSetting {
  1243. NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
  1244. [[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
  1245. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1246. fetchConfigsExpectation[i] = [self
  1247. expectationWithDescription:
  1248. [NSString stringWithFormat:@"Test minimumFetchInterval expectation - instance %d", i]];
  1249. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1250. settings.minimumFetchInterval = 123;
  1251. [_configInstances[i] setConfigSettings:settings];
  1252. XCTAssertEqual([_configInstances[i] configSettings].minimumFetchInterval, 123);
  1253. FIRRemoteConfigFetchCompletion fetchCompletion =
  1254. ^void(FIRRemoteConfigFetchStatus status, NSError *error) {
  1255. XCTAssertFalse([self->_configInstances[i].settings hasMinimumFetchIntervalElapsed:43200]);
  1256. // Update minimum fetch interval.
  1257. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1258. settings.minimumFetchInterval = 0;
  1259. [self->_configInstances[i] setConfigSettings:settings];
  1260. XCTAssertEqual([self->_configInstances[i] configSettings].minimumFetchInterval, 0);
  1261. XCTAssertTrue([self->_configInstances[i].settings hasMinimumFetchIntervalElapsed:0]);
  1262. [fetchConfigsExpectation[i] fulfill];
  1263. };
  1264. [_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
  1265. }
  1266. [self waitForExpectationsWithTimeout:_expectationTimeout
  1267. handler:^(NSError *error) {
  1268. XCTAssertNil(error);
  1269. }];
  1270. }
  1271. /// Test the fetch timeout is properly set and read back.
  1272. - (void)testSetFetchTimeoutConfigSetting {
  1273. for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
  1274. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  1275. settings.fetchTimeout = 1;
  1276. [_configInstances[i] setConfigSettings:settings];
  1277. XCTAssertEqual([_configInstances[i] configSettings].fetchTimeout, 1);
  1278. NSURLSession *networkSession = [_configFetch[i] currentNetworkSession];
  1279. XCTAssertNotNil(networkSession);
  1280. XCTAssertEqual(networkSession.configuration.timeoutIntervalForResource, 1);
  1281. XCTAssertEqual(networkSession.configuration.timeoutIntervalForRequest, 1);
  1282. }
  1283. }
  1284. #pragma mark - Public Factory Methods
  1285. - (void)testConfigureConfigWithValidInput {
  1286. // Configure the default app with our options and ensure the Remote Config instance is set up
  1287. // properly.
  1288. if (![FIRApp isDefaultAppConfigured]) {
  1289. XCTAssertNoThrow([FIRApp configureWithOptions:[self firstAppOptions]]);
  1290. }
  1291. XCTAssertNoThrow([FIRRemoteConfig remoteConfig]);
  1292. FIRRemoteConfig *config = [FIRRemoteConfig remoteConfig];
  1293. XCTAssertNotNil(config);
  1294. // Ensure the same instance is returned from the singleton.
  1295. FIRRemoteConfig *sameConfig = [FIRRemoteConfig remoteConfig];
  1296. XCTAssertNotNil(sameConfig);
  1297. XCTAssertEqual(config, sameConfig);
  1298. // Ensure the app name is stored properly.
  1299. XCTAssertEqual([config valueForKey:@"_appName"], kFIRDefaultAppName);
  1300. }
  1301. #pragma mark - Test Helpers
  1302. - (FIROptions *)firstAppOptions {
  1303. // TODO: Evaluate if we want to hardcode things here instead.
  1304. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123abc"
  1305. GCMSenderID:@"correct_gcm_sender_id"];
  1306. options.APIKey = @"correct_api_key";
  1307. options.projectID = @"abc-xyz-123";
  1308. return options;
  1309. }
  1310. - (FIROptions *)secondAppOptions {
  1311. FIROptions *options =
  1312. [[FIROptions alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]]
  1313. pathForResource:@"SecondApp-GoogleService-Info"
  1314. ofType:@"plist"]];
  1315. XCTAssertNotNil(options);
  1316. return options;
  1317. }
  1318. @end