FIRDatabaseTests.mm 67 KB

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