FIRDatabaseTests.mm 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  1. /*
  2. * Copyright 2017 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. // TODO(wuandy): Delete this once isPersistenceEnabled and cacheSizeBytes are removed.
  17. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  18. #import <FirebaseFirestore/FirebaseFirestore.h>
  19. #import <XCTest/XCTest.h>
  20. #import "FirebaseCore/Extension/FIRAppInternal.h"
  21. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  22. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  23. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  24. #import "Firestore/Source/API/FIRLocalCacheSettings+Internal.h"
  25. #include "Firestore/core/src/api/query_snapshot.h"
  26. #include "Firestore/core/src/core/firestore_client.h"
  27. #include "Firestore/core/src/model/database_id.h"
  28. #include "Firestore/core/src/util/string_apple.h"
  29. #include "Firestore/core/test/unit/testutil/app_testing.h"
  30. using firebase::firestore::model::DatabaseId;
  31. using firebase::firestore::testutil::AppForUnitTesting;
  32. using firebase::firestore::util::MakeNSString;
  33. using firebase::firestore::util::MakeString;
  34. using firebase::firestore::util::TimerId;
  35. @interface FIRDatabaseTests : FSTIntegrationTestCase
  36. @end
  37. @implementation FIRDatabaseTests
  38. - (void)tearDown {
  39. [FIRApp resetApps];
  40. [super tearDown];
  41. }
  42. - (void)testCanUpdateAnExistingDocument {
  43. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  44. NSDictionary<NSString *, id> *initialData =
  45. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  46. NSDictionary<NSString *, id> *updateData =
  47. @{@"desc" : @"NewDescription", @"owner.email" : @"new@xyz.com"};
  48. NSDictionary<NSString *, id> *finalData =
  49. @{@"desc" : @"NewDescription", @"owner" : @{@"name" : @"Jonny", @"email" : @"new@xyz.com"}};
  50. [self writeDocumentRef:doc data:initialData];
  51. XCTestExpectation *updateCompletion = [self expectationWithDescription:@"updateData"];
  52. [doc updateData:updateData
  53. completion:^(NSError *_Nullable error) {
  54. XCTAssertNil(error);
  55. [updateCompletion fulfill];
  56. }];
  57. [self awaitExpectations];
  58. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  59. XCTAssertTrue(result.exists);
  60. XCTAssertEqualObjects(result.data, finalData);
  61. }
  62. - (void)testEqualityComparison {
  63. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  64. NSDictionary<NSString *, id> *initialData =
  65. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  66. [self writeDocumentRef:doc data:initialData];
  67. FIRDocumentSnapshot *snap1 = [self readDocumentForRef:doc];
  68. FIRDocumentSnapshot *snap2 = [self readDocumentForRef:doc];
  69. FIRDocumentSnapshot *snap3 = [self readDocumentForRef:doc];
  70. XCTAssertTrue([snap1.metadata isEqual:snap2.metadata]);
  71. XCTAssertTrue([snap2.metadata isEqual:snap3.metadata]);
  72. XCTAssertTrue([snap1.documentID isEqual:snap2.documentID]);
  73. XCTAssertTrue([snap2.documentID isEqual:snap3.documentID]);
  74. XCTAssertTrue(snap1.exists == snap2.exists);
  75. XCTAssertTrue(snap2.exists == snap3.exists);
  76. XCTAssertTrue([snap1.reference isEqual:snap2.reference]);
  77. XCTAssertTrue([snap2.reference isEqual:snap3.reference]);
  78. XCTAssertTrue([[snap1 data] isEqual:[snap2 data]]);
  79. XCTAssertTrue([[snap2 data] isEqual:[snap3 data]]);
  80. XCTAssertTrue([snap1 isEqual:snap2]);
  81. XCTAssertTrue([snap2 isEqual:snap3]);
  82. }
  83. - (void)testCanUpdateAnUnknownDocument {
  84. [self readerAndWriterOnDocumentRef:^(FIRDocumentReference *readerRef,
  85. FIRDocumentReference *writerRef) {
  86. [self writeDocumentRef:writerRef data:@{@"a" : @"a"}];
  87. [self updateDocumentRef:readerRef data:@{@"b" : @"b"}];
  88. FIRDocumentSnapshot *writerSnap = [self readDocumentForRef:writerRef
  89. source:FIRFirestoreSourceCache];
  90. XCTAssertTrue(writerSnap.exists);
  91. XCTestExpectation *expectation =
  92. [self expectationWithDescription:@"testCanUpdateAnUnknownDocument"];
  93. [readerRef getDocumentWithSource:FIRFirestoreSourceCache
  94. completion:^(FIRDocumentSnapshot *, NSError *_Nullable error) {
  95. XCTAssertNotNil(error);
  96. [expectation fulfill];
  97. }];
  98. [self awaitExpectations];
  99. writerSnap = [self readDocumentForRef:writerRef];
  100. XCTAssertEqualObjects(writerSnap.data, (@{@"a" : @"a", @"b" : @"b"}));
  101. FIRDocumentSnapshot *readerSnap = [self readDocumentForRef:writerRef];
  102. XCTAssertEqualObjects(readerSnap.data, (@{@"a" : @"a", @"b" : @"b"}));
  103. }];
  104. }
  105. - (void)testCanDeleteAFieldWithAnUpdate {
  106. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  107. NSDictionary<NSString *, id> *initialData =
  108. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  109. NSDictionary<NSString *, id> *updateData =
  110. @{@"owner.email" : [FIRFieldValue fieldValueForDelete]};
  111. NSDictionary<NSString *, id> *finalData =
  112. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny"}};
  113. [self writeDocumentRef:doc data:initialData];
  114. [self updateDocumentRef:doc data:updateData];
  115. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  116. XCTAssertTrue(result.exists);
  117. XCTAssertEqualObjects(result.data, finalData);
  118. }
  119. - (void)testDeleteDocument {
  120. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  121. NSDictionary<NSString *, id> *data = @{@"value" : @"foo"};
  122. [self writeDocumentRef:doc data:data];
  123. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  124. XCTAssertEqualObjects(result.data, data);
  125. [self deleteDocumentRef:doc];
  126. result = [self readDocumentForRef:doc];
  127. XCTAssertFalse(result.exists);
  128. }
  129. - (void)testCanRetrieveDocumentThatDoesNotExist {
  130. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  131. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  132. XCTAssertNil(result.data);
  133. XCTAssertNil(result[@"foo"]);
  134. }
  135. - (void)testCannotUpdateNonexistentDocument {
  136. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  137. XCTestExpectation *setCompletion = [self expectationWithDescription:@"setData"];
  138. [doc updateData:@{@"owner" : @"abc"}
  139. completion:^(NSError *_Nullable error) {
  140. XCTAssertNotNil(error);
  141. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  142. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  143. [setCompletion fulfill];
  144. }];
  145. [self awaitExpectations];
  146. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  147. XCTAssertFalse(result.exists);
  148. }
  149. - (void)testCanOverwriteDataAnExistingDocumentUsingSet {
  150. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  151. NSDictionary<NSString *, id> *initialData =
  152. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  153. NSDictionary<NSString *, id> *udpateData = @{@"desc" : @"NewDescription"};
  154. [self writeDocumentRef:doc data:initialData];
  155. [self writeDocumentRef:doc data:udpateData];
  156. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  157. XCTAssertEqualObjects(document.data, udpateData);
  158. }
  159. - (void)testCanMergeDataWithAnExistingDocumentUsingSet {
  160. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  161. NSDictionary<NSString *, id> *initialData =
  162. @{@"desc" : @"Description", @"owner.data" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  163. NSDictionary<NSString *, id> *mergeData =
  164. @{@"updated" : @YES, @"owner.data" : @{@"name" : @"Sebastian"}};
  165. NSDictionary<NSString *, id> *finalData = @{
  166. @"desc" : @"Description",
  167. @"updated" : @YES,
  168. @"owner.data" : @{@"name" : @"Sebastian", @"email" : @"abc@xyz.com"}
  169. };
  170. [self writeDocumentRef:doc data:initialData];
  171. XCTestExpectation *completed =
  172. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  173. [doc setData:mergeData
  174. merge:YES
  175. completion:^(NSError *error) {
  176. XCTAssertNil(error);
  177. [completed fulfill];
  178. }];
  179. [self awaitExpectations];
  180. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  181. XCTAssertEqualObjects(document.data, finalData);
  182. }
  183. - (void)testCanMergeEmptyObject {
  184. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  185. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  186. id<FIRListenerRegistration> listenerRegistration =
  187. [doc addSnapshotListener:[accumulator valueEventHandler]];
  188. [self writeDocumentRef:doc data:@{}];
  189. FIRDocumentSnapshot *snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  190. XCTAssertEqualObjects(snapshot.data, @{});
  191. [self mergeDocumentRef:doc data:@{@"a" : @{}} fields:@[ @"a" ]];
  192. snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  193. XCTAssertEqualObjects(snapshot.data, @{@"a" : @{}});
  194. [self mergeDocumentRef:doc data:@{@"b" : @{}}];
  195. snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  196. XCTAssertEqualObjects(snapshot.data, (@{@"a" : @{}, @"b" : @{}}));
  197. snapshot = [self readDocumentForRef:doc source:FIRFirestoreSourceServer];
  198. XCTAssertEqualObjects(snapshot.data, (@{@"a" : @{}, @"b" : @{}}));
  199. [listenerRegistration remove];
  200. }
  201. - (void)testCanMergeServerTimestamps {
  202. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  203. NSDictionary<NSString *, id> *initialData = @{
  204. @"updated" : @NO,
  205. };
  206. NSDictionary<NSString *, id> *mergeData = @{
  207. @"time" : [FIRFieldValue fieldValueForServerTimestamp],
  208. @"nested" : @{@"time" : [FIRFieldValue fieldValueForServerTimestamp]}
  209. };
  210. [self writeDocumentRef:doc data:initialData];
  211. XCTestExpectation *completed =
  212. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  213. [doc setData:mergeData
  214. merge:YES
  215. completion:^(NSError *error) {
  216. XCTAssertNil(error);
  217. [completed fulfill];
  218. }];
  219. [self awaitExpectations];
  220. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  221. XCTAssertEqual(document[@"updated"], @NO);
  222. XCTAssertTrue([document[@"time"] isKindOfClass:[FIRTimestamp class]]);
  223. XCTAssertTrue([document[@"nested.time"] isKindOfClass:[FIRTimestamp class]]);
  224. }
  225. - (void)testCanDeleteFieldUsingMerge {
  226. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  227. NSDictionary<NSString *, id> *initialData =
  228. @{@"untouched" : @YES, @"foo" : @"bar", @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}};
  229. NSDictionary<NSString *, id> *mergeData = @{
  230. @"foo" : [FIRFieldValue fieldValueForDelete],
  231. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForDelete]}
  232. };
  233. [self writeDocumentRef:doc data:initialData];
  234. XCTestExpectation *completed =
  235. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  236. [doc setData:mergeData
  237. merge:YES
  238. completion:^(NSError *error) {
  239. XCTAssertNil(error);
  240. [completed fulfill];
  241. }];
  242. [self awaitExpectations];
  243. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  244. XCTAssertEqual(document[@"untouched"], @YES);
  245. XCTAssertNil(document[@"foo"]);
  246. XCTAssertEqual(document[@"nested.untouched"], @YES);
  247. XCTAssertNil(document[@"nested.foo"]);
  248. }
  249. - (void)testCanDeleteFieldUsingMergeFields {
  250. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  251. NSDictionary<NSString *, id> *initialData = @{
  252. @"untouched" : @YES,
  253. @"foo" : @"bar",
  254. @"inner" : @{@"removed" : @YES, @"foo" : @"bar"},
  255. @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}
  256. };
  257. NSDictionary<NSString *, id> *mergeData = @{
  258. @"foo" : [FIRFieldValue fieldValueForDelete],
  259. @"inner" : @{@"foo" : [FIRFieldValue fieldValueForDelete]},
  260. @"nested" : @{
  261. @"untouched" : [FIRFieldValue fieldValueForDelete],
  262. @"foo" : [FIRFieldValue fieldValueForDelete]
  263. }
  264. };
  265. NSDictionary<NSString *, id> *finalData =
  266. @{@"untouched" : @YES, @"inner" : @{}, @"nested" : @{@"untouched" : @YES}};
  267. [self writeDocumentRef:doc data:initialData];
  268. XCTestExpectation *completed =
  269. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  270. [doc setData:mergeData
  271. mergeFields:@[ @"foo", @"inner", @"nested.foo" ]
  272. completion:^(NSError *error) {
  273. XCTAssertNil(error);
  274. [completed fulfill];
  275. }];
  276. [self awaitExpectations];
  277. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  278. XCTAssertEqualObjects([document data], finalData);
  279. }
  280. - (void)testCanSetServerTimestampsUsingMergeFields {
  281. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  282. NSDictionary<NSString *, id> *initialData =
  283. @{@"untouched" : @YES, @"foo" : @"bar", @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}};
  284. NSDictionary<NSString *, id> *mergeData = @{
  285. @"foo" : [FIRFieldValue fieldValueForServerTimestamp],
  286. @"inner" : @{@"foo" : [FIRFieldValue fieldValueForServerTimestamp]},
  287. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForServerTimestamp]}
  288. };
  289. [self writeDocumentRef:doc data:initialData];
  290. XCTestExpectation *completed =
  291. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  292. [doc setData:mergeData
  293. mergeFields:@[ @"foo", @"inner", @"nested.foo" ]
  294. completion:^(NSError *error) {
  295. XCTAssertNil(error);
  296. [completed fulfill];
  297. }];
  298. [self awaitExpectations];
  299. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  300. XCTAssertTrue([document exists]);
  301. XCTAssertTrue([document[@"foo"] isKindOfClass:[FIRTimestamp class]]);
  302. XCTAssertTrue([document[@"inner.foo"] isKindOfClass:[FIRTimestamp class]]);
  303. XCTAssertTrue([document[@"nested.foo"] isKindOfClass:[FIRTimestamp class]]);
  304. }
  305. - (void)testMergeReplacesArrays {
  306. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  307. NSDictionary<NSString *, id> *initialData = @{
  308. @"untouched" : @YES,
  309. @"data" : @"old",
  310. @"topLevel" : @[ @"old", @"old" ],
  311. @"mapInArray" : @[ @{@"data" : @"old"} ]
  312. };
  313. NSDictionary<NSString *, id> *mergeData =
  314. @{@"data" : @"new", @"topLevel" : @[ @"new" ], @"mapInArray" : @[ @{@"data" : @"new"} ]};
  315. NSDictionary<NSString *, id> *finalData = @{
  316. @"untouched" : @YES,
  317. @"data" : @"new",
  318. @"topLevel" : @[ @"new" ],
  319. @"mapInArray" : @[ @{@"data" : @"new"} ]
  320. };
  321. [self writeDocumentRef:doc data:initialData];
  322. XCTestExpectation *completed =
  323. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  324. [doc setData:mergeData
  325. merge:YES
  326. completion:^(NSError *error) {
  327. XCTAssertNil(error);
  328. [completed fulfill];
  329. }];
  330. [self awaitExpectations];
  331. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  332. XCTAssertEqualObjects(document.data, finalData);
  333. }
  334. - (void)testCannotSpecifyFieldMaskForMissingField {
  335. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  336. XCTAssertThrowsSpecific(
  337. [doc setData:@{} mergeFields:@[ @"foo" ]], NSException,
  338. @"Field 'foo' is specified in your field mask but missing from your input data.");
  339. }
  340. - (void)testCanSetASubsetOfFieldsUsingMask {
  341. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  342. NSDictionary<NSString *, id> *initialData =
  343. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  344. NSDictionary<NSString *, id> *finalData = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  345. [self writeDocumentRef:doc data:initialData];
  346. XCTestExpectation *completed =
  347. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  348. [doc setData:@{@"desc" : @"NewDescription", @"owner" : @"Sebastian"}
  349. mergeFields:@[ @"owner" ]
  350. completion:^(NSError *error) {
  351. XCTAssertNil(error);
  352. [completed fulfill];
  353. }];
  354. [self awaitExpectations];
  355. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  356. XCTAssertEqualObjects(document.data, finalData);
  357. }
  358. - (void)testDoesNotApplyFieldDeleteOutsideOfMask {
  359. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  360. NSDictionary<NSString *, id> *initialData =
  361. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  362. NSDictionary<NSString *, id> *finalData = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  363. [self writeDocumentRef:doc data:initialData];
  364. XCTestExpectation *completed =
  365. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  366. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForDelete], @"owner" : @"Sebastian"}
  367. mergeFields:@[ @"owner" ]
  368. completion:^(NSError *error) {
  369. XCTAssertNil(error);
  370. [completed fulfill];
  371. }];
  372. [self awaitExpectations];
  373. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  374. XCTAssertEqualObjects(document.data, finalData);
  375. }
  376. - (void)testDoesNotApplyFieldTransformOutsideOfMask {
  377. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  378. NSDictionary<NSString *, id> *initialData =
  379. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  380. NSDictionary<NSString *, id> *finalData = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  381. [self writeDocumentRef:doc data:initialData];
  382. XCTestExpectation *completed =
  383. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  384. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForServerTimestamp], @"owner" : @"Sebastian"}
  385. mergeFields:@[ @"owner" ]
  386. completion:^(NSError *error) {
  387. XCTAssertNil(error);
  388. [completed fulfill];
  389. }];
  390. [self awaitExpectations];
  391. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  392. XCTAssertEqualObjects(document.data, finalData);
  393. }
  394. - (void)testCanSetEmptyFieldMask {
  395. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  396. NSDictionary<NSString *, id> *initialData =
  397. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  398. NSDictionary<NSString *, id> *finalData = initialData;
  399. [self writeDocumentRef:doc data:initialData];
  400. XCTestExpectation *completed =
  401. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  402. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForServerTimestamp], @"owner" : @"Sebastian"}
  403. mergeFields:@[]
  404. completion:^(NSError *error) {
  405. XCTAssertNil(error);
  406. [completed fulfill];
  407. }];
  408. [self awaitExpectations];
  409. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  410. XCTAssertEqualObjects(document.data, finalData);
  411. }
  412. - (void)testCanSpecifyFieldsMultipleTimesInFieldMask {
  413. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  414. NSDictionary<NSString *, id> *initialData =
  415. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  416. NSDictionary<NSString *, id> *finalData =
  417. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Sebastian", @"email" : @"new@xyz.com"}};
  418. [self writeDocumentRef:doc data:initialData];
  419. XCTestExpectation *completed =
  420. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  421. [doc setData:@{
  422. @"desc" : @"NewDescription",
  423. @"owner" : @{@"name" : @"Sebastian", @"email" : @"new@xyz.com"}
  424. }
  425. mergeFields:@[ @"owner.name", @"owner", @"owner" ]
  426. completion:^(NSError *error) {
  427. XCTAssertNil(error);
  428. [completed fulfill];
  429. }];
  430. [self awaitExpectations];
  431. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  432. XCTAssertEqualObjects(document.data, finalData);
  433. }
  434. - (void)testAddingToACollectionYieldsTheCorrectDocumentReference {
  435. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  436. FIRDocumentReference *ref = [coll addDocumentWithData:@{@"foo" : @1}];
  437. XCTestExpectation *getCompletion = [self expectationWithDescription:@"getData"];
  438. [ref getDocumentWithCompletion:^(FIRDocumentSnapshot *_Nullable document,
  439. NSError *_Nullable error) {
  440. XCTAssertNil(error);
  441. XCTAssertEqualObjects(document.data, (@{@"foo" : @1}));
  442. [getCompletion fulfill];
  443. }];
  444. [self awaitExpectations];
  445. }
  446. - (void)testSnapshotsInSyncListenerFiresAfterListenersInSync {
  447. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  448. FIRDocumentReference *ref = [coll addDocumentWithData:@{@"foo" : @1}];
  449. NSMutableArray<NSString *> *events = [NSMutableArray array];
  450. XCTestExpectation *gotInitialSnapshot = [self expectationWithDescription:@"gotInitialSnapshot"];
  451. __block bool setupComplete = false;
  452. [ref addSnapshotListener:^(FIRDocumentSnapshot *, NSError *error) {
  453. XCTAssertNil(error);
  454. [events addObject:@"doc"];
  455. // Wait for the initial event from the backend so that we know we'll get exactly one snapshot
  456. // event for our local write below.
  457. if (!setupComplete) {
  458. setupComplete = true;
  459. [gotInitialSnapshot fulfill];
  460. }
  461. }];
  462. [self awaitExpectations];
  463. [events removeAllObjects];
  464. XCTestExpectation *done = [self expectationWithDescription:@"SnapshotsInSyncListenerDone"];
  465. [ref.firestore addSnapshotsInSyncListener:^() {
  466. [events addObject:@"snapshots-in-sync"];
  467. if ([events count] == 3) {
  468. // We should have an initial snapshots-in-sync event, then a snapshot event
  469. // for set(), then another event to indicate we're in sync again.
  470. NSArray<NSString *> *expected = @[ @"snapshots-in-sync", @"doc", @"snapshots-in-sync" ];
  471. XCTAssertEqualObjects(events, expected);
  472. [done fulfill];
  473. }
  474. }];
  475. [self writeDocumentRef:ref data:@{@"foo" : @3}];
  476. [self awaitExpectation:done];
  477. }
  478. - (void)testSnapshotsInSyncRemoveIsIdempotent {
  479. // This test merely verifies that calling remove multiple times doesn't
  480. // explode.
  481. auto listener = [self.db addSnapshotsInSyncListener:^(){
  482. }];
  483. [listener remove];
  484. [listener remove];
  485. }
  486. - (void)testListenCanBeCalledMultipleTimes {
  487. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  488. FIRDocumentReference *doc = [coll documentWithAutoID];
  489. XCTestExpectation *completed = [self expectationWithDescription:@"multiple addSnapshotListeners"];
  490. __block NSDictionary<NSString *, id> *resultingData;
  491. // Shut the compiler up about strong references to doc.
  492. FIRDocumentReference *__weak weakDoc = doc;
  493. [doc setData:@{@"foo" : @"bar"}
  494. completion:^(NSError *error1) {
  495. XCTAssertNil(error1);
  496. FIRDocumentReference *strongDoc = weakDoc;
  497. [strongDoc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *error2) {
  498. XCTAssertNil(error2);
  499. FIRDocumentReference *strongDoc2 = weakDoc;
  500. [strongDoc2 addSnapshotListener:^(FIRDocumentSnapshot *snapshot3, NSError *error3) {
  501. XCTAssertNil(error3);
  502. resultingData = snapshot3.data;
  503. [completed fulfill];
  504. }];
  505. }];
  506. }];
  507. [self awaitExpectations];
  508. XCTAssertEqualObjects(resultingData, @{@"foo" : @"bar"});
  509. }
  510. - (void)testDocumentSnapshotEvents_nonExistent {
  511. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  512. XCTestExpectation *snapshotCompletion = [self expectationWithDescription:@"snapshot"];
  513. __block int callbacks = 0;
  514. id<FIRListenerRegistration> listenerRegistration =
  515. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  516. callbacks++;
  517. if (callbacks == 1) {
  518. XCTAssertNotNil(doc);
  519. XCTAssertFalse(doc.exists);
  520. [snapshotCompletion fulfill];
  521. } else {
  522. XCTFail("Should not have received this callback");
  523. }
  524. }];
  525. [self awaitExpectations];
  526. [listenerRegistration remove];
  527. }
  528. - (void)testDocumentSnapshotEvents_forAdd {
  529. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  530. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  531. __block XCTestExpectation *dataCompletion;
  532. __block int callbacks = 0;
  533. id<FIRListenerRegistration> listenerRegistration =
  534. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  535. callbacks++;
  536. if (callbacks == 1) {
  537. XCTAssertNotNil(doc);
  538. XCTAssertFalse(doc.exists);
  539. [emptyCompletion fulfill];
  540. } else if (callbacks == 2) {
  541. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  542. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  543. [dataCompletion fulfill];
  544. } else {
  545. XCTFail("Should not have received this callback");
  546. }
  547. }];
  548. [self awaitExpectations];
  549. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  550. [docRef setData:@{@"a" : @1}];
  551. [self awaitExpectations];
  552. [listenerRegistration remove];
  553. }
  554. - (void)testDocumentSnapshotEvents_forAddIncludingMetadata {
  555. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  556. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  557. __block XCTestExpectation *dataCompletion;
  558. __block int callbacks = 0;
  559. id<FIRListenerRegistration> listenerRegistration = [docRef
  560. addSnapshotListenerWithIncludeMetadataChanges:YES
  561. listener:^(FIRDocumentSnapshot *_Nullable doc,
  562. NSError *) {
  563. callbacks++;
  564. if (callbacks == 1) {
  565. XCTAssertNotNil(doc);
  566. XCTAssertFalse(doc.exists);
  567. [emptyCompletion fulfill];
  568. } else if (callbacks == 2) {
  569. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  570. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  571. } else if (callbacks == 3) {
  572. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  573. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  574. [dataCompletion fulfill];
  575. } else {
  576. XCTFail("Should not have received this callback");
  577. }
  578. }];
  579. [self awaitExpectations];
  580. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  581. [docRef setData:@{@"a" : @1}];
  582. [self awaitExpectations];
  583. [listenerRegistration remove];
  584. }
  585. - (void)testDocumentSnapshotEvents_forChange {
  586. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  587. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  588. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  589. [self writeDocumentRef:docRef data:initialData];
  590. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  591. __block XCTestExpectation *changeCompletion;
  592. __block int callbacks = 0;
  593. id<FIRListenerRegistration> listenerRegistration =
  594. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  595. callbacks++;
  596. if (callbacks == 1) {
  597. XCTAssertEqualObjects(doc.data, initialData);
  598. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  599. [initialCompletion fulfill];
  600. } else if (callbacks == 2) {
  601. XCTAssertEqualObjects(doc.data, changedData);
  602. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  603. [changeCompletion fulfill];
  604. } else {
  605. XCTFail("Should not have received this callback");
  606. }
  607. }];
  608. [self awaitExpectations];
  609. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  610. [docRef setData:changedData];
  611. [self awaitExpectations];
  612. [listenerRegistration remove];
  613. }
  614. - (void)testDocumentSnapshotEvents_forChangeIncludingMetadata {
  615. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  616. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  617. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  618. [self writeDocumentRef:docRef data:initialData];
  619. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  620. __block XCTestExpectation *changeCompletion;
  621. __block int callbacks = 0;
  622. id<FIRListenerRegistration> listenerRegistration = [docRef
  623. addSnapshotListenerWithIncludeMetadataChanges:YES
  624. listener:^(FIRDocumentSnapshot *_Nullable doc,
  625. NSError *) {
  626. callbacks++;
  627. if (callbacks == 1) {
  628. XCTAssertEqualObjects(doc.data, initialData);
  629. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  630. XCTAssertEqual(doc.metadata.isFromCache, YES);
  631. } else if (callbacks == 2) {
  632. XCTAssertEqualObjects(doc.data, initialData);
  633. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  634. XCTAssertEqual(doc.metadata.isFromCache, NO);
  635. [initialCompletion fulfill];
  636. } else if (callbacks == 3) {
  637. XCTAssertEqualObjects(doc.data, changedData);
  638. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  639. XCTAssertEqual(doc.metadata.isFromCache, NO);
  640. } else if (callbacks == 4) {
  641. XCTAssertEqualObjects(doc.data, changedData);
  642. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  643. XCTAssertEqual(doc.metadata.isFromCache, NO);
  644. [changeCompletion fulfill];
  645. } else {
  646. XCTFail("Should not have received this callback");
  647. }
  648. }];
  649. [self awaitExpectations];
  650. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  651. [docRef setData:changedData];
  652. [self awaitExpectations];
  653. [listenerRegistration remove];
  654. }
  655. - (void)testDocumentSnapshotEvents_forDelete {
  656. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  657. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  658. [self writeDocumentRef:docRef data:initialData];
  659. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  660. __block XCTestExpectation *changeCompletion;
  661. __block int callbacks = 0;
  662. id<FIRListenerRegistration> listenerRegistration =
  663. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  664. callbacks++;
  665. if (callbacks == 1) {
  666. XCTAssertEqualObjects(doc.data, initialData);
  667. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  668. XCTAssertEqual(doc.metadata.isFromCache, YES);
  669. [initialCompletion fulfill];
  670. } else if (callbacks == 2) {
  671. XCTAssertFalse(doc.exists);
  672. [changeCompletion fulfill];
  673. } else {
  674. XCTFail("Should not have received this callback");
  675. }
  676. }];
  677. [self awaitExpectations];
  678. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  679. [docRef deleteDocument];
  680. [self awaitExpectations];
  681. [listenerRegistration remove];
  682. }
  683. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  684. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  685. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  686. [self writeDocumentRef:docRef data:initialData];
  687. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  688. __block XCTestExpectation *changeCompletion;
  689. __block int callbacks = 0;
  690. id<FIRListenerRegistration> listenerRegistration = [docRef
  691. addSnapshotListenerWithIncludeMetadataChanges:YES
  692. listener:^(FIRDocumentSnapshot *_Nullable doc,
  693. NSError *) {
  694. callbacks++;
  695. if (callbacks == 1) {
  696. XCTAssertEqualObjects(doc.data, initialData);
  697. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  698. XCTAssertEqual(doc.metadata.isFromCache, YES);
  699. } else if (callbacks == 2) {
  700. XCTAssertEqualObjects(doc.data, initialData);
  701. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  702. XCTAssertEqual(doc.metadata.isFromCache, NO);
  703. [initialCompletion fulfill];
  704. } else if (callbacks == 3) {
  705. XCTAssertFalse(doc.exists);
  706. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  707. XCTAssertEqual(doc.metadata.isFromCache, NO);
  708. [changeCompletion fulfill];
  709. } else {
  710. XCTFail("Should not have received this callback");
  711. }
  712. }];
  713. [self awaitExpectations];
  714. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  715. [docRef deleteDocument];
  716. [self awaitExpectations];
  717. [listenerRegistration remove];
  718. }
  719. - (void)testQuerySnapshotEvents_forAdd {
  720. FIRCollectionReference *roomsRef = [self collectionRef];
  721. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  722. NSDictionary<NSString *, id> *newData = @{@"a" : @1};
  723. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  724. __block XCTestExpectation *changeCompletion;
  725. __block int callbacks = 0;
  726. id<FIRListenerRegistration> listenerRegistration =
  727. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *) {
  728. callbacks++;
  729. if (callbacks == 1) {
  730. XCTAssertEqual(docSet.count, 0);
  731. [emptyCompletion fulfill];
  732. } else if (callbacks == 2) {
  733. XCTAssertEqual(docSet.count, 1);
  734. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  735. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  736. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  737. [changeCompletion fulfill];
  738. } else {
  739. XCTFail("Should not have received a third callback");
  740. }
  741. }];
  742. [self awaitExpectations];
  743. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  744. [docRef setData:newData];
  745. [self awaitExpectations];
  746. [listenerRegistration remove];
  747. }
  748. - (void)testQuerySnapshotEvents_forChange {
  749. FIRCollectionReference *roomsRef = [self collectionRef];
  750. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  751. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  752. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  753. [self writeDocumentRef:docRef data:initialData];
  754. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  755. __block XCTestExpectation *changeCompletion;
  756. __block int callbacks = 0;
  757. id<FIRListenerRegistration> listenerRegistration =
  758. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *) {
  759. callbacks++;
  760. if (callbacks == 1) {
  761. XCTAssertEqual(docSet.count, 1);
  762. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  763. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  764. [initialCompletion fulfill];
  765. } else if (callbacks == 2) {
  766. XCTAssertEqual(docSet.count, 1);
  767. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  768. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  769. [changeCompletion fulfill];
  770. } else {
  771. XCTFail("Should not have received a third callback");
  772. }
  773. }];
  774. [self awaitExpectations];
  775. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  776. [docRef setData:changedData];
  777. [self awaitExpectations];
  778. [listenerRegistration remove];
  779. }
  780. - (void)testQuerySnapshotEvents_forDelete {
  781. FIRCollectionReference *roomsRef = [self collectionRef];
  782. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  783. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  784. [self writeDocumentRef:docRef data:initialData];
  785. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  786. __block XCTestExpectation *changeCompletion;
  787. __block int callbacks = 0;
  788. id<FIRListenerRegistration> listenerRegistration =
  789. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *) {
  790. callbacks++;
  791. if (callbacks == 1) {
  792. XCTAssertEqual(docSet.count, 1);
  793. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  794. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  795. [initialCompletion fulfill];
  796. } else if (callbacks == 2) {
  797. XCTAssertEqual(docSet.count, 0);
  798. [changeCompletion fulfill];
  799. } else {
  800. XCTFail("Should not have received a third callback");
  801. }
  802. }];
  803. [self awaitExpectations];
  804. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  805. [docRef deleteDocument];
  806. [self awaitExpectations];
  807. [listenerRegistration remove];
  808. }
  809. - (void)testExposesFirestoreOnDocumentReferences {
  810. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  811. XCTAssertEqual(doc.firestore, self.db);
  812. }
  813. - (void)testExposesFirestoreOnQueries {
  814. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  815. XCTAssertEqual(q.firestore, self.db);
  816. }
  817. - (void)testDocumentReferenceEquality {
  818. FIRFirestore *firestore = self.db;
  819. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  820. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  821. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  822. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  823. FIRFirestore *otherFirestore = [self firestore];
  824. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  825. }
  826. - (void)testQueryReferenceEquality {
  827. FIRFirestore *firestore = self.db;
  828. FIRQuery *query =
  829. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  830. isEqualTo:@42];
  831. FIRQuery *query2 =
  832. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  833. isEqualTo:@42];
  834. XCTAssertEqualObjects(query, query2);
  835. FIRQuery *query3 =
  836. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  837. isEqualTo:@42];
  838. XCTAssertNotEqualObjects(query, query3);
  839. FIRFirestore *otherFirestore = [self firestore];
  840. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  841. queryWhereField:@"baz"
  842. isEqualTo:@42];
  843. XCTAssertNotEqualObjects(query, query4);
  844. }
  845. - (void)testCanTraverseCollectionsAndDocuments {
  846. NSString *expected = @"a/b/c/d";
  847. // doc path from root Firestore.
  848. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  849. // collection path from root Firestore.
  850. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  851. expected);
  852. // doc path from CollectionReference.
  853. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  854. expected);
  855. // collection path from DocumentReference.
  856. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  857. @"a/b/c/d/e");
  858. }
  859. - (void)testCanTraverseCollectionAndDocumentParents {
  860. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  861. XCTAssertEqualObjects(collection.path, @"a/b/c");
  862. FIRDocumentReference *doc = collection.parent;
  863. XCTAssertEqualObjects(doc.path, @"a/b");
  864. collection = doc.parent;
  865. XCTAssertEqualObjects(collection.path, @"a");
  866. FIRDocumentReference *nilDoc = collection.parent;
  867. XCTAssertNil(nilDoc);
  868. }
  869. - (void)testUpdateFieldsWithDots {
  870. FIRDocumentReference *doc = [self documentRef];
  871. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  872. [self updateDocumentRef:doc
  873. data:@{(id)[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"}];
  874. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  875. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  876. XCTAssertNil(error);
  877. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  878. [expectation fulfill];
  879. }];
  880. [self awaitExpectations];
  881. }
  882. - (void)testUpdateNestedFields {
  883. FIRDocumentReference *doc = [self documentRef];
  884. [self writeDocumentRef:doc
  885. data:@{
  886. @"a" : @{@"b" : @"old"},
  887. @"c" : @{@"d" : @"old"},
  888. @"e" : @{@"f" : @"old"}
  889. }];
  890. [self updateDocumentRef:doc
  891. data:@{
  892. (id) @"a.b" : @"new",
  893. (id)[[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  894. }];
  895. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  896. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  897. XCTAssertNil(error);
  898. XCTAssertEqualObjects(snapshot.data, (@{
  899. @"a" : @{@"b" : @"new"},
  900. @"c" : @{@"d" : @"new"},
  901. @"e" : @{@"f" : @"old"}
  902. }));
  903. [expectation fulfill];
  904. }];
  905. [self awaitExpectations];
  906. }
  907. - (void)testCollectionID {
  908. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  909. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  910. }
  911. - (void)testDocumentID {
  912. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  913. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  914. }
  915. - (void)testCanQueueWritesWhileOffline {
  916. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  917. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  918. FIRDocumentReference *doc = [self documentRef];
  919. FIRFirestore *firestore = doc.firestore;
  920. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  921. [firestore disableNetworkWithCompletion:^(NSError *error) {
  922. XCTAssertNil(error);
  923. [doc setData:data
  924. completion:^(NSError *error) {
  925. XCTAssertNil(error);
  926. [writeEpectation fulfill];
  927. }];
  928. [firestore enableNetworkWithCompletion:^(NSError *error) {
  929. XCTAssertNil(error);
  930. [networkExpectation fulfill];
  931. }];
  932. }];
  933. [self awaitExpectations];
  934. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  935. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  936. XCTAssertNil(error);
  937. XCTAssertEqualObjects(snapshot.data, data);
  938. XCTAssertFalse(snapshot.metadata.isFromCache);
  939. [getExpectation fulfill];
  940. }];
  941. [self awaitExpectations];
  942. }
  943. - (void)testCanGetDocumentsWhileOffline {
  944. FIRDocumentReference *doc = [self documentRef];
  945. FIRFirestore *firestore = doc.firestore;
  946. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  947. XCTestExpectation *failExpectation =
  948. [self expectationWithDescription:@"offline read with no cached data"];
  949. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  950. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  951. __weak FIRDocumentReference *weakDoc = doc;
  952. [firestore disableNetworkWithCompletion:^(NSError *error) {
  953. XCTAssertNil(error);
  954. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *, NSError *error) {
  955. XCTAssertNotNil(error);
  956. [failExpectation fulfill];
  957. }];
  958. [doc setData:data
  959. completion:^(NSError *_Nullable error) {
  960. XCTAssertNil(error);
  961. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  962. XCTAssertNil(error);
  963. // Verify that we are not reading from cache.
  964. XCTAssertFalse(snapshot.metadata.isFromCache);
  965. [onlineExpectation fulfill];
  966. }];
  967. }];
  968. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  969. XCTAssertNil(error);
  970. // Verify that we are reading from cache.
  971. XCTAssertTrue(snapshot.metadata.fromCache);
  972. XCTAssertEqualObjects(snapshot.data, data);
  973. [firestore enableNetworkWithCompletion:^(NSError *) {
  974. [networkExpectation fulfill];
  975. }];
  976. }];
  977. }];
  978. [self awaitExpectations];
  979. }
  980. - (void)testWriteStreamReconnectsAfterIdle {
  981. FIRDocumentReference *doc = [self documentRef];
  982. FIRFirestore *firestore = doc.firestore;
  983. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  984. [firestore workerQueue]->RunScheduledOperationsUntil(TimerId::WriteStreamIdle);
  985. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  986. }
  987. - (void)testWatchStreamReconnectsAfterIdle {
  988. FIRDocumentReference *doc = [self documentRef];
  989. FIRFirestore *firestore = doc.firestore;
  990. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  991. [firestore workerQueue]->RunScheduledOperationsUntil(TimerId::ListenStreamIdle);
  992. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  993. }
  994. - (void)testCanDisableNetwork {
  995. FIRDocumentReference *doc = [self documentRef];
  996. FIRFirestore *firestore = doc.firestore;
  997. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  998. [self awaitExpectations];
  999. [firestore
  1000. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  1001. [self awaitExpectations];
  1002. [firestore
  1003. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  1004. [self awaitExpectations];
  1005. [firestore
  1006. disableNetworkWithCompletion:[self
  1007. completionForExpectationWithName:@"Disable network again"]];
  1008. [self awaitExpectations];
  1009. [firestore
  1010. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  1011. [self awaitExpectations];
  1012. }
  1013. - (void)testClientCallsAfterTerminationFail {
  1014. FIRDocumentReference *doc = [self documentRef];
  1015. FIRFirestore *firestore = doc.firestore;
  1016. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1017. [self awaitExpectations];
  1018. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate"]];
  1019. [self awaitExpectations];
  1020. XCTAssertThrowsSpecific([firestore disableNetworkWithCompletion:^(NSError *){
  1021. }],
  1022. NSException, @"The client has already been terminated.");
  1023. }
  1024. - (void)testTransactionGetAfterTerminationFail {
  1025. FIRDocumentReference *doc = [self documentRef];
  1026. FIRFirestore *firestore = doc.firestore;
  1027. XCTestExpectation *expectation1 = [self expectationWithDescription:@"TransactionBlockStart"];
  1028. XCTestExpectation *expectation2 = [self expectationWithDescription:@"FirestoreTerminated"];
  1029. XCTestExpectation *expectation3 = [self expectationWithDescription:@"TransactionBlockDone"];
  1030. __block NSError *capturedError = nil;
  1031. [firestore
  1032. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  1033. // Tell the test thread that it can now call `terminateWithCompletion`.
  1034. [expectation1 fulfill];
  1035. // Wait for `terminateWithCompletion` to complete.
  1036. [self awaitExpectation:expectation2];
  1037. // Call `[transaction getDocument]` to make sure that it correctly reports the error.
  1038. [transaction getDocument:doc error:error];
  1039. // Save the `NSError` into a variable on which the test thread will perform assertions.
  1040. capturedError = *error;
  1041. // Tell the test thread that it can now perform assertions on the captured `NSError` object.
  1042. [expectation3 fulfill];
  1043. return @"I should have failed since the transaction was terminated";
  1044. }
  1045. completion:^(id, NSError *){
  1046. }];
  1047. // Wait for the transaction callback to start.
  1048. [self awaitExpectation:expectation1];
  1049. // Terminate the Firestore instance.
  1050. [firestore terminateWithCompletion:[self completionForExpectation:expectation2]];
  1051. // Wait for the transaction callback to "publish" the `NSError` from `[transaction getDocument]`.
  1052. [self awaitExpectation:expectation3];
  1053. // Verify that `[transaction getDocument]` set its `NSError` argument to a non-nil error.
  1054. XCTAssertNotNil(capturedError);
  1055. XCTAssertEqual(capturedError.code, FIRFirestoreErrorCodeFailedPrecondition);
  1056. XCTAssertEqualObjects(capturedError.userInfo[NSLocalizedDescriptionKey],
  1057. @"The client has already been terminated.");
  1058. }
  1059. - (void)testMaintainsPersistenceAfterRestarting {
  1060. FIRDocumentReference *doc = [self documentRef];
  1061. FIRFirestore *firestore = doc.firestore;
  1062. FIRApp *app = firestore.app;
  1063. NSString *appName = app.name;
  1064. FIROptions *options = app.options;
  1065. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1066. [self writeDocumentRef:doc data:initialData];
  1067. // -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
  1068. // firestore instance to emulate the way an end user would do this.
  1069. [self terminateFirestore:firestore];
  1070. [self.firestores removeObject:firestore];
  1071. [self deleteApp:app];
  1072. // We restart the app with the same name and options to check that the previous instance's
  1073. // persistent storage persists its data after restarting. Calling [self firestore] here would
  1074. // create a new instance of firestore, which defeats the purpose of this test.
  1075. [FIRApp configureWithName:appName options:options];
  1076. FIRApp *app2 = [FIRApp appNamed:appName];
  1077. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1078. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1079. FIRDocumentSnapshot *snap = [self readDocumentForRef:docRef2 source:FIRFirestoreSourceCache];
  1080. XCTAssertTrue(snap.exists);
  1081. }
  1082. - (void)testCanClearPersistenceAfterRestarting {
  1083. FIRDocumentReference *doc = [self documentRef];
  1084. FIRFirestore *firestore = doc.firestore;
  1085. FIRApp *app = firestore.app;
  1086. NSString *appName = app.name;
  1087. FIROptions *options = app.options;
  1088. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1089. [self writeDocumentRef:doc data:initialData];
  1090. // -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
  1091. // firestore instance to emulate the way an end user would do this.
  1092. [self terminateFirestore:firestore];
  1093. [self.firestores removeObject:firestore];
  1094. [firestore
  1095. clearPersistenceWithCompletion:[self completionForExpectationWithName:@"ClearPersistence"]];
  1096. [self awaitExpectations];
  1097. [self deleteApp:app];
  1098. // We restart the app with the same name and options to check that the previous instance's
  1099. // persistent storage is actually cleared after the restart. Calling [self firestore] here would
  1100. // create a new instance of firestore, which defeats the purpose of this test.
  1101. [FIRApp configureWithName:appName options:options];
  1102. FIRApp *app2 = [FIRApp appNamed:appName];
  1103. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1104. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1105. XCTestExpectation *expectation2 = [self expectationWithDescription:@"getData"];
  1106. [docRef2 getDocumentWithSource:FIRFirestoreSourceCache
  1107. completion:^(FIRDocumentSnapshot *, NSError *_Nullable error) {
  1108. XCTAssertNotNil(error);
  1109. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1110. XCTAssertEqual(error.code, FIRFirestoreErrorCodeUnavailable);
  1111. [expectation2 fulfill];
  1112. }];
  1113. [self awaitExpectations];
  1114. }
  1115. - (void)testCanClearPersistenceOnANewFirestoreInstance {
  1116. FIRDocumentReference *doc = [self documentRef];
  1117. FIRFirestore *firestore = doc.firestore;
  1118. FIRApp *app = firestore.app;
  1119. NSString *appName = app.name;
  1120. FIROptions *options = app.options;
  1121. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1122. [self writeDocumentRef:doc data:initialData];
  1123. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate"]];
  1124. [self.firestores removeObject:firestore];
  1125. [self awaitExpectations];
  1126. [self deleteApp:app];
  1127. // We restart the app with the same name and options to check that the previous instance's
  1128. // persistent storage is actually cleared after the restart. Calling [self firestore] here would
  1129. // create a new instance of firestore, which defeats the purpose of this test.
  1130. [FIRApp configureWithName:appName options:options];
  1131. FIRApp *app2 = [FIRApp appNamed:appName];
  1132. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1133. [firestore2
  1134. clearPersistenceWithCompletion:[self completionForExpectationWithName:@"ClearPersistence"]];
  1135. [self awaitExpectations];
  1136. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1137. XCTestExpectation *expectation2 = [self expectationWithDescription:@"getData"];
  1138. [docRef2 getDocumentWithSource:FIRFirestoreSourceCache
  1139. completion:^(FIRDocumentSnapshot *, NSError *_Nullable error) {
  1140. XCTAssertNotNil(error);
  1141. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1142. XCTAssertEqual(error.code, FIRFirestoreErrorCodeUnavailable);
  1143. [expectation2 fulfill];
  1144. }];
  1145. [self awaitExpectations];
  1146. }
  1147. - (void)testClearPersistenceWhileRunningFails {
  1148. FIRDocumentReference *doc = [self documentRef];
  1149. FIRFirestore *firestore = doc.firestore;
  1150. [self enableNetwork];
  1151. XCTestExpectation *expectation = [self expectationWithDescription:@"clearPersistence"];
  1152. [firestore clearPersistenceWithCompletion:^(NSError *_Nullable error) {
  1153. XCTAssertNotNil(error);
  1154. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1155. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  1156. [expectation fulfill];
  1157. }];
  1158. [self awaitExpectations];
  1159. }
  1160. - (void)testRestartFirestoreLeadsToNewInstance {
  1161. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1162. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1163. FIRFirestore *sameInstance = [FIRFirestore firestoreForApp:app];
  1164. firestore.settings = [FSTIntegrationTestCase settings];
  1165. XCTAssertEqual(firestore, sameInstance);
  1166. NSDictionary<NSString *, id> *data =
  1167. @{@"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  1168. [self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
  1169. [self terminateFirestore:firestore];
  1170. // Create a new instance, check it's a different instance.
  1171. FIRFirestore *newInstance = [FIRFirestore firestoreForApp:app];
  1172. newInstance.settings = [FSTIntegrationTestCase settings];
  1173. XCTAssertNotEqual(firestore, newInstance);
  1174. // New instance still functions.
  1175. FIRDocumentSnapshot *snapshot =
  1176. [self readDocumentForRef:[newInstance documentWithPath:@"abc/123"]];
  1177. XCTAssertTrue([data isEqualToDictionary:[snapshot data]]);
  1178. }
  1179. - (void)testAppDeleteLeadsToFirestoreTermination {
  1180. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1181. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1182. firestore.settings = [FSTIntegrationTestCase settings];
  1183. NSDictionary<NSString *, id> *data =
  1184. @{@"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  1185. [self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
  1186. [self deleteApp:app];
  1187. XCTAssertTrue(firestore.wrapped->client()->is_terminated());
  1188. }
  1189. // Ensures b/172958106 doesn't regress.
  1190. - (void)testDeleteAppWorksWhenLastReferenceToFirestoreIsInListener {
  1191. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1192. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1193. FIRDocumentReference *doc = [firestore documentWithPath:@"abc/123"];
  1194. // Make sure there is a listener.
  1195. [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *){
  1196. }];
  1197. XCTestExpectation *expectation = [self expectationWithDescription:@"App is deleted"];
  1198. [app deleteApp:^(BOOL) {
  1199. [expectation fulfill];
  1200. }];
  1201. // Let go of the last app reference.
  1202. app = nil;
  1203. [self awaitExpectations];
  1204. }
  1205. - (void)testTerminateCanBeCalledMultipleTimes {
  1206. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1207. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1208. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate1"]];
  1209. [self awaitExpectations];
  1210. XCTAssertThrowsSpecific([firestore disableNetworkWithCompletion:^(NSError *){
  1211. }],
  1212. NSException, @"The client has already been terminated.");
  1213. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate2"]];
  1214. [self awaitExpectations];
  1215. XCTAssertThrowsSpecific([firestore enableNetworkWithCompletion:^(NSError *){
  1216. }],
  1217. NSException, @"The client has already been terminated.");
  1218. }
  1219. - (void)testCanRemoveListenerAfterTermination {
  1220. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1221. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1222. firestore.settings = [FSTIntegrationTestCase settings];
  1223. FIRDocumentReference *doc = [[firestore collectionWithPath:@"rooms"] documentWithAutoID];
  1224. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  1225. [self writeDocumentRef:doc data:@{}];
  1226. id<FIRListenerRegistration> listenerRegistration =
  1227. [doc addSnapshotListener:[accumulator valueEventHandler]];
  1228. [accumulator awaitEventWithName:@"Snapshot"];
  1229. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"terminate"]];
  1230. [self awaitExpectations];
  1231. // This should proceed without error.
  1232. [listenerRegistration remove];
  1233. // Multiple calls should proceed as well.
  1234. [listenerRegistration remove];
  1235. }
  1236. - (void)testListenerCallbackBlocksRemove {
  1237. // This tests a guarantee required for C++ that doesn't strictly matter for Objective-C and has no
  1238. // equivalent on other platforms.
  1239. //
  1240. // The problem for C++ is that users can register a listener that refers to some state, then call
  1241. // `ListenerRegistration::Remove()` and expect to be able to immediately delete that state. The
  1242. // trouble is that there may be a callback in progress against that listener so the implementation
  1243. // now blocks the remove call until the callback is complete.
  1244. //
  1245. // To make this work, user callbacks can't be on the main thread because the main thread is
  1246. // blocked waiting for the test to complete (that is, you can't await expectations on the main
  1247. // thread and then have the user callback additionally await expectations).
  1248. dispatch_queue_t userQueue = dispatch_queue_create("firestore.test.user", DISPATCH_QUEUE_SERIAL);
  1249. FIRFirestoreSettings *settings = self.db.settings;
  1250. settings.dispatchQueue = userQueue;
  1251. self.db.settings = settings;
  1252. XCTestExpectation *running = [self expectationWithDescription:@"listener running"];
  1253. XCTestExpectation *allowCompletion =
  1254. [self expectationWithDescription:@"allow listener to complete"];
  1255. XCTestExpectation *removing = [self expectationWithDescription:@"attempting to remove listener"];
  1256. XCTestExpectation *removed = [self expectationWithDescription:@"listener removed"];
  1257. NSMutableString *steps = [NSMutableString string];
  1258. FIRDocumentReference *doc = [self documentRef];
  1259. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  1260. __block bool firstTime = true;
  1261. id<FIRListenerRegistration> listener =
  1262. [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *) {
  1263. @synchronized(self) {
  1264. if (!firstTime) {
  1265. return;
  1266. }
  1267. firstTime = false;
  1268. }
  1269. [steps appendString:@"1"];
  1270. [running fulfill];
  1271. [self awaitExpectation:allowCompletion];
  1272. [steps appendString:@"3"];
  1273. }];
  1274. // Call remove asynchronously to avoid blocking the main test thread.
  1275. dispatch_queue_t async = dispatch_queue_create("firestore.async", DISPATCH_QUEUE_SERIAL);
  1276. dispatch_async(async, ^{
  1277. [self awaitExpectation:running];
  1278. [steps appendString:@"2"];
  1279. [removing fulfill];
  1280. [listener remove];
  1281. [steps appendString:@"4"];
  1282. [removed fulfill];
  1283. });
  1284. // Perform a write to `doc` which will trigger the listener callback. Don't wait for completion
  1285. // though because that completion handler is in line behind the listener callback that the test
  1286. // is blocking.
  1287. XCTestExpectation *setData = [self expectationWithDescription:@"setData"];
  1288. [doc setData:@{@"foo" : @"bar"} completion:[self completionForExpectation:setData]];
  1289. [self awaitExpectation:removing];
  1290. [allowCompletion fulfill];
  1291. [self awaitExpectation:removed];
  1292. XCTAssertEqualObjects(steps, @"1234");
  1293. [self awaitExpectation:setData];
  1294. }
  1295. - (void)testListenerCallbackCanCallRemoveWithoutBlocking {
  1296. // This tests a guarantee required for C++ that doesn't strictly matter for Objective-C and has no
  1297. // equivalent on other platforms. See `testListenerCallbackBlocksRemove` for background.
  1298. XCTestExpectation *removed = [self expectationWithDescription:@"listener removed"];
  1299. NSMutableString *steps = [NSMutableString string];
  1300. FIRDocumentReference *doc = [self documentRef];
  1301. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  1302. __block id<FIRListenerRegistration> listener = nil;
  1303. @synchronized(self) {
  1304. listener = [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *) {
  1305. [steps appendString:@"1"];
  1306. @synchronized(self) {
  1307. // This test is successful if this method does not block.
  1308. [listener remove];
  1309. }
  1310. [steps appendString:@"2"];
  1311. [removed fulfill];
  1312. }];
  1313. }
  1314. // Perform a write to `doc` which will trigger the listener callback.
  1315. [self writeDocumentRef:doc data:@{@"foo" : @"bar2"}];
  1316. [self awaitExpectation:removed];
  1317. XCTAssertEqualObjects(steps, @"12");
  1318. }
  1319. - (void)testListenerCallbacksHappenOnMainThread {
  1320. // Verify that callbacks occur on the main thread if settings.dispatchQueue is not specified.
  1321. XCTestExpectation *invoked = [self expectationWithDescription:@"listener invoked"];
  1322. invoked.assertForOverFulfill = false;
  1323. FIRDocumentReference *doc = [self documentRef];
  1324. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  1325. __block bool callbackThreadIsMainThread;
  1326. __block NSString *callbackThreadDescription;
  1327. [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *) {
  1328. callbackThreadIsMainThread = NSThread.isMainThread;
  1329. callbackThreadDescription = [NSString stringWithFormat:@"%@", NSThread.currentThread];
  1330. [invoked fulfill];
  1331. }];
  1332. [self awaitExpectation:invoked];
  1333. XCTAssertTrue(callbackThreadIsMainThread,
  1334. @"The listener callback was expected to occur on the main thread, but instead it "
  1335. @"occurred on the thread %@",
  1336. callbackThreadDescription);
  1337. }
  1338. - (void)testWaitForPendingWritesCompletes {
  1339. FIRDocumentReference *doc = [self documentRef];
  1340. FIRFirestore *firestore = doc.firestore;
  1341. [self disableNetwork];
  1342. [doc setData:@{@"foo" : @"bar"}];
  1343. [firestore waitForPendingWritesWithCompletion:
  1344. [self completionForExpectationWithName:@"Wait for pending writes"]];
  1345. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1346. [self awaitExpectations];
  1347. }
  1348. - (void)testWaitForPendingWritesFailsWhenUserChanges {
  1349. FIRFirestore *firestore = self.db;
  1350. [self disableNetwork];
  1351. // Writes to local to prevent immediate call to the completion of waitForPendingWrites.
  1352. NSDictionary<NSString *, id> *data =
  1353. @{@"owner" : @{@"name" : @"Andy", @"email" : @"abc@example.com"}};
  1354. [[self documentRef] setData:data];
  1355. XCTestExpectation *expectation = [self expectationWithDescription:@"waitForPendingWrites"];
  1356. [firestore waitForPendingWritesWithCompletion:^(NSError *_Nullable error) {
  1357. XCTAssertNotNil(error);
  1358. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1359. XCTAssertEqual(error.code, FIRFirestoreErrorCodeCancelled);
  1360. [expectation fulfill];
  1361. }];
  1362. [self triggerUserChangeWithUid:@"user-to-fail-pending-writes"];
  1363. [self awaitExpectations];
  1364. }
  1365. - (void)testWaitForPendingWritesCompletesWhenOfflineIfNoPending {
  1366. FIRFirestore *firestore = self.db;
  1367. [self disableNetwork];
  1368. [firestore waitForPendingWritesWithCompletion:
  1369. [self completionForExpectationWithName:@"Wait for pending writes"]];
  1370. [self awaitExpectations];
  1371. }
  1372. - (void)testDefaultNamedDbIsSame {
  1373. [FIRApp configure];
  1374. FIRApp *app = [FIRApp defaultApp];
  1375. FIRFirestore *db1 = [FIRFirestore firestore];
  1376. FIRFirestore *db2 = [FIRFirestore firestoreForApp:app];
  1377. FIRFirestore *db3 = [FIRFirestore firestoreForApp:app database:@"(default)"];
  1378. FIRFirestore *db4 = [FIRFirestore firestoreForDatabase:@"(default)"];
  1379. XCTAssertIdentical(db1, db2);
  1380. XCTAssertIdentical(db1, db3);
  1381. XCTAssertIdentical(db1, db4);
  1382. }
  1383. - (void)testSameNamedDbIsSame {
  1384. [FIRApp configure];
  1385. FIRApp *app = [FIRApp defaultApp];
  1386. FIRFirestore *db1 = [FIRFirestore firestoreForApp:app database:@"myDb"];
  1387. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"myDb"];
  1388. XCTAssertIdentical(db1, db2);
  1389. }
  1390. - (void)testNamedDbHaveDifferentInstance {
  1391. [FIRApp configure];
  1392. FIRFirestore *db1 = [FIRFirestore firestore];
  1393. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"db1"];
  1394. FIRFirestore *db3 = [FIRFirestore firestoreForDatabase:@"db2"];
  1395. XCTAssertNotIdentical(db1, db2);
  1396. XCTAssertNotIdentical(db1, db3);
  1397. XCTAssertNotIdentical(db2, db3);
  1398. }
  1399. - (void)testCannotMixCacheConfigAPIs {
  1400. [FIRApp configure];
  1401. FIRFirestore *db1 = [FIRFirestore firestore];
  1402. FIRFirestoreSettings *settings = db1.settings;
  1403. settings.cacheSizeBytes = 10000000;
  1404. settings.cacheSettings = [[FIRPersistentCacheSettings alloc] init];
  1405. XCTAssertThrowsSpecific(db1.settings = settings, NSException);
  1406. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"db2"];
  1407. settings = db2.settings;
  1408. settings.cacheSettings = [[FIRMemoryCacheSettings alloc] init];
  1409. settings.persistenceEnabled = NO;
  1410. XCTAssertThrowsSpecific(db2.settings = settings, NSException);
  1411. }
  1412. - (void)testMinimumCacheSize {
  1413. XCTAssertThrowsSpecific([[FIRPersistentCacheSettings alloc] initWithSizeBytes:@(1024 * 1024 - 1)],
  1414. NSException);
  1415. }
  1416. @end