FIRDatabaseTests.mm 72 KB

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