FIRDatabaseTests.mm 66 KB

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