FSTIntegrationTestCase.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  17. #import <FirebaseFirestore/FIRCollectionReference.h>
  18. #import <FirebaseFirestore/FIRDocumentChange.h>
  19. #import <FirebaseFirestore/FIRDocumentReference.h>
  20. #import <FirebaseFirestore/FIRDocumentSnapshot.h>
  21. #import <FirebaseFirestore/FIRFirestore.h>
  22. #import <FirebaseFirestore/FIRFirestoreSettings.h>
  23. #import <FirebaseFirestore/FIRQuerySnapshot.h>
  24. #import <FirebaseFirestore/FIRSnapshotMetadata.h>
  25. #import <FirebaseFirestore/FIRTransaction.h>
  26. #include <memory>
  27. #include <string>
  28. #include <utility>
  29. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  30. #import "Firestore/Example/Tests/Util/FIRFirestore+Testing.h"
  31. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  32. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  33. #include "Firestore/core/src/auth/credentials_provider.h"
  34. #include "Firestore/core/src/auth/empty_credentials_provider.h"
  35. #include "Firestore/core/src/auth/user.h"
  36. #include "Firestore/core/src/local/leveldb_opener.h"
  37. #include "Firestore/core/src/model/database_id.h"
  38. #include "Firestore/core/src/remote/firebase_metadata_provider_apple.h"
  39. #include "Firestore/core/src/remote/grpc_connection.h"
  40. #include "Firestore/core/src/util/async_queue.h"
  41. #include "Firestore/core/src/util/autoid.h"
  42. #include "Firestore/core/src/util/filesystem.h"
  43. #include "Firestore/core/src/util/path.h"
  44. #include "Firestore/core/src/util/string_apple.h"
  45. #include "Firestore/core/test/unit/testutil/app_testing.h"
  46. #include "Firestore/core/test/unit/testutil/async_testing.h"
  47. #include "Firestore/core/test/unit/testutil/status_testing.h"
  48. #include "absl/memory/memory.h"
  49. namespace testutil = firebase::firestore::testutil;
  50. namespace util = firebase::firestore::util;
  51. using firebase::firestore::auth::CredentialChangeListener;
  52. using firebase::firestore::auth::CredentialsProvider;
  53. using firebase::firestore::auth::EmptyCredentialsProvider;
  54. using firebase::firestore::auth::User;
  55. using firebase::firestore::core::DatabaseInfo;
  56. using firebase::firestore::local::LevelDbOpener;
  57. using firebase::firestore::model::DatabaseId;
  58. using firebase::firestore::remote::GrpcConnection;
  59. using firebase::firestore::remote::FirebaseMetadataProviderApple;
  60. using firebase::firestore::testutil::AppForUnitTesting;
  61. using firebase::firestore::testutil::AsyncQueueForTesting;
  62. using firebase::firestore::util::AsyncQueue;
  63. using firebase::firestore::util::CreateAutoId;
  64. using firebase::firestore::util::Filesystem;
  65. using firebase::firestore::util::Path;
  66. using firebase::firestore::util::Status;
  67. using firebase::firestore::util::StatusOr;
  68. NS_ASSUME_NONNULL_BEGIN
  69. /**
  70. * Firestore databases can be subject to a ~30s "cold start" delay if they have not been used
  71. * recently, so before any tests run we "prime" the backend.
  72. */
  73. static const double kPrimingTimeout = 45.0;
  74. static NSString *defaultProjectId;
  75. static FIRFirestoreSettings *defaultSettings;
  76. static bool runningAgainstEmulator = false;
  77. // Behaves the same as `EmptyCredentialsProvider` except it can also trigger a user
  78. // change.
  79. class FakeCredentialsProvider : public EmptyCredentialsProvider {
  80. public:
  81. void SetCredentialChangeListener(CredentialChangeListener changeListener) override {
  82. if (changeListener) {
  83. listener_ = std::move(changeListener);
  84. listener_(User::Unauthenticated());
  85. }
  86. }
  87. void ChangeUser(NSString *new_id) {
  88. if (listener_) {
  89. listener_(firebase::firestore::auth::User::FromUid(new_id));
  90. }
  91. }
  92. private:
  93. CredentialChangeListener listener_;
  94. };
  95. @implementation FSTIntegrationTestCase {
  96. NSMutableArray<FIRFirestore *> *_firestores;
  97. std::shared_ptr<FakeCredentialsProvider> _fakeCredentialsProvider;
  98. }
  99. - (void)setUp {
  100. [super setUp];
  101. LoadXCTestCaseAwait();
  102. _fakeCredentialsProvider = std::make_shared<FakeCredentialsProvider>();
  103. [self clearPersistenceOnce];
  104. [self primeBackend];
  105. _firestores = [NSMutableArray array];
  106. self.db = [self firestore];
  107. self.eventAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  108. }
  109. - (void)tearDown {
  110. @try {
  111. for (FIRFirestore *firestore in _firestores) {
  112. [self terminateFirestore:firestore];
  113. }
  114. } @finally {
  115. _firestores = nil;
  116. [super tearDown];
  117. }
  118. }
  119. /**
  120. * Clears persistence, but only the first time. This ensures that each test
  121. * run is isolated from the last test run, but doesn't allow tests to interfere
  122. * with each other.
  123. */
  124. - (void)clearPersistenceOnce {
  125. auto *fs = Filesystem::Default();
  126. static bool clearedPersistence = false;
  127. @synchronized([FSTIntegrationTestCase class]) {
  128. if (clearedPersistence) return;
  129. DatabaseInfo dbInfo;
  130. LevelDbOpener opener(dbInfo);
  131. StatusOr<Path> maybeLevelDBDir = opener.FirestoreAppDataDir();
  132. ASSERT_OK(maybeLevelDBDir.status());
  133. Path levelDBDir = std::move(maybeLevelDBDir).ValueOrDie();
  134. Status status = fs->RecursivelyRemove(levelDBDir);
  135. ASSERT_OK(status);
  136. clearedPersistence = true;
  137. }
  138. }
  139. - (FIRFirestore *)firestore {
  140. return [self firestoreWithProjectID:[FSTIntegrationTestCase projectID]];
  141. }
  142. /**
  143. * Figures out what kind of testing environment we're using, and sets up testing defaults to make
  144. * that work.
  145. *
  146. * Several configurations are supported:
  147. * * Mobile Harness, running periocally against prod and nightly, using live SSL certs
  148. * * Hexa built from google3, running on a companion gLinux machine, using self-signed test SSL
  149. * certs
  150. * * Firestore emulator, running on localhost, with SSL disabled
  151. *
  152. * See Firestore/README.md for detailed setup instructions or comments below for which specific
  153. * values trigger which configurations.
  154. */
  155. + (void)setUpDefaults {
  156. if (defaultSettings) return;
  157. defaultSettings = [[FIRFirestoreSettings alloc] init];
  158. defaultSettings.persistenceEnabled = YES;
  159. // Check for a MobileHarness configuration, running against nightly or prod, which have live
  160. // SSL certs.
  161. NSString *project = [[NSProcessInfo processInfo] environment][@"PROJECT_ID"];
  162. NSString *host = [[NSProcessInfo processInfo] environment][@"DATASTORE_HOST"];
  163. if (project && host) {
  164. defaultProjectId = project;
  165. defaultSettings.host = host;
  166. return;
  167. }
  168. // Check for configuration of a prod project via GoogleServices-Info.plist.
  169. FIROptions *options = [FIROptions defaultOptions];
  170. if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
  171. defaultProjectId = options.projectID;
  172. if (host) {
  173. // Allow access to nightly or other hosts via this mechanism too.
  174. defaultSettings.host = host;
  175. }
  176. return;
  177. }
  178. // Otherwise fall back on assuming the emulator or Hexa on localhost.
  179. defaultProjectId = @"test-db";
  180. // Hexa uses a self-signed cert: the first bundle location is used by bazel builds. The second is
  181. // used for github clones.
  182. NSString *certsPath =
  183. [[NSBundle mainBundle] pathForResource:@"PlugIns/IntegrationTests.xctest/CAcert"
  184. ofType:@"pem"];
  185. if (certsPath == nil) {
  186. certsPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"CAcert" ofType:@"pem"];
  187. }
  188. unsigned long long fileSize =
  189. [[[NSFileManager defaultManager] attributesOfItemAtPath:certsPath error:nil] fileSize];
  190. if (fileSize != 0) {
  191. defaultSettings.host = @"localhost:8081";
  192. GrpcConnection::UseTestCertificate(util::MakeString(defaultSettings.host),
  193. Path::FromNSString(certsPath), "test_cert_2");
  194. } else {
  195. // If no cert is set up, configure for the Firestore emulator.
  196. defaultSettings.host = @"localhost:8080";
  197. defaultSettings.sslEnabled = false;
  198. runningAgainstEmulator = true;
  199. // Also issue a warning because the Firestore emulator doesn't completely work yet.
  200. NSLog(@"Please set up a GoogleServices-Info.plist for Firestore in Firestore/Example/App using "
  201. "instructions at <https://github.com/firebase/firebase-ios-sdk#running-sample-apps>. "
  202. "Alternatively, if you're a Googler with a Hexa preproduction environment, run "
  203. "setup_integration_tests.py to properly configure testing SSL certificates.");
  204. }
  205. }
  206. + (NSString *)projectID {
  207. if (!defaultProjectId) {
  208. [self setUpDefaults];
  209. }
  210. return defaultProjectId;
  211. }
  212. + (bool)isRunningAgainstEmulator {
  213. // The only way to determine whether or not we're running against the emulator is to figure out
  214. // which testing environment we're using. Essentially `setUpDefaults` determines
  215. // `runningAgainstEmulator` as a side effect.
  216. if (!defaultProjectId) {
  217. [self setUpDefaults];
  218. }
  219. return runningAgainstEmulator;
  220. }
  221. + (FIRFirestoreSettings *)settings {
  222. [self setUpDefaults];
  223. return defaultSettings;
  224. }
  225. - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
  226. FIRApp *app = AppForUnitTesting(util::MakeString(projectID));
  227. return [self firestoreWithApp:app];
  228. }
  229. - (FIRFirestore *)firestoreWithApp:(FIRApp *)app {
  230. NSString *persistenceKey = [NSString stringWithFormat:@"db%lu", (unsigned long)_firestores.count];
  231. FIRSetLoggerLevel(FIRLoggerLevelDebug);
  232. std::string projectID = util::MakeString(app.options.projectID);
  233. FIRFirestore *firestore =
  234. [[FIRFirestore alloc] initWithDatabaseID:DatabaseId(projectID)
  235. persistenceKey:util::MakeString(persistenceKey)
  236. credentialsProvider:_fakeCredentialsProvider
  237. workerQueue:AsyncQueueForTesting()
  238. firebaseMetadataProvider:absl::make_unique<FirebaseMetadataProviderApple>(app)
  239. firebaseApp:app
  240. instanceRegistry:nil];
  241. firestore.settings = [FSTIntegrationTestCase settings];
  242. [_firestores addObject:firestore];
  243. return firestore;
  244. }
  245. - (void)triggerUserChangeWithUid:(NSString *)uid {
  246. _fakeCredentialsProvider->ChangeUser(uid);
  247. }
  248. - (void)primeBackend {
  249. static dispatch_once_t onceToken;
  250. dispatch_once(&onceToken, ^{
  251. [FSTIntegrationTestCase setUpDefaults];
  252. if (runningAgainstEmulator) {
  253. // Priming not required against the emulator.
  254. return;
  255. }
  256. FIRFirestore *db = [self firestore];
  257. XCTestExpectation *watchInitialized =
  258. [self expectationWithDescription:@"Prime backend: Watch initialized"];
  259. __block XCTestExpectation *watchUpdateReceived;
  260. FIRDocumentReference *docRef = [db documentWithPath:[self documentPath]];
  261. id<FIRListenerRegistration> listenerRegistration =
  262. [docRef addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *) {
  263. if ([snapshot[@"value"] isEqual:@"done"]) {
  264. [watchUpdateReceived fulfill];
  265. } else {
  266. [watchInitialized fulfill];
  267. }
  268. }];
  269. // Wait for watch to initialize and deliver first event.
  270. [self awaitExpectation:watchInitialized];
  271. watchUpdateReceived = [self expectationWithDescription:@"Prime backend: Watch update received"];
  272. // Use a transaction to perform a write without triggering any local events.
  273. [docRef.firestore
  274. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **) {
  275. [transaction setData:@{@"value" : @"done"} forDocument:docRef];
  276. return nil;
  277. }
  278. completion:^(id, NSError *){
  279. }];
  280. // Wait to see the write on the watch stream.
  281. [self waitForExpectationsWithTimeout:kPrimingTimeout
  282. handler:^(NSError *_Nullable expectationError) {
  283. if (expectationError) {
  284. XCTFail(@"Error waiting for prime backend: %@",
  285. expectationError);
  286. }
  287. }];
  288. [listenerRegistration remove];
  289. [self terminateFirestore:db];
  290. });
  291. }
  292. - (void)terminateFirestore:(FIRFirestore *)firestore {
  293. XCTestExpectation *expectation = [self expectationWithDescription:@"shutdown"];
  294. [firestore terminateWithCompletion:[self completionForExpectation:expectation]];
  295. [self awaitExpectation:expectation];
  296. }
  297. - (void)deleteApp:(FIRApp *)app {
  298. XCTestExpectation *expectation = [self expectationWithDescription:@"deleteApp"];
  299. [app deleteApp:^(BOOL completion) {
  300. XCTAssertTrue(completion);
  301. [expectation fulfill];
  302. }];
  303. [self awaitExpectation:expectation];
  304. }
  305. - (NSString *)documentPath {
  306. std::string autoId = CreateAutoId();
  307. return [NSString stringWithFormat:@"test-collection/%s", autoId.c_str()];
  308. }
  309. - (FIRDocumentReference *)documentRef {
  310. return [self.db documentWithPath:[self documentPath]];
  311. }
  312. - (FIRCollectionReference *)collectionRef {
  313. std::string autoId = CreateAutoId();
  314. NSString *collectionName = [NSString stringWithFormat:@"test-collection-%s", autoId.c_str()];
  315. return [self.db collectionWithPath:collectionName];
  316. }
  317. - (FIRCollectionReference *)collectionRefWithDocuments:
  318. (NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents {
  319. FIRCollectionReference *collection = [self collectionRef];
  320. // Use a different instance to write the documents
  321. [self writeAllDocuments:documents
  322. toCollection:[[self firestore] collectionWithPath:collection.path]];
  323. return collection;
  324. }
  325. - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents
  326. toCollection:(FIRCollectionReference *)collection {
  327. [documents enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary<NSString *, id> *value,
  328. BOOL *) {
  329. FIRDocumentReference *ref = [collection documentWithPath:key];
  330. [self writeDocumentRef:ref data:value];
  331. }];
  332. }
  333. - (void)readerAndWriterOnDocumentRef:(void (^)(FIRDocumentReference *readerRef,
  334. FIRDocumentReference *writerRef))action {
  335. FIRFirestore *reader = self.db; // for clarity
  336. FIRFirestore *writer = [self firestore];
  337. NSString *path = [self documentPath];
  338. FIRDocumentReference *readerRef = [reader documentWithPath:path];
  339. FIRDocumentReference *writerRef = [writer documentWithPath:path];
  340. action(readerRef, writerRef);
  341. }
  342. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref {
  343. return [self readDocumentForRef:ref source:FIRFirestoreSourceDefault];
  344. }
  345. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref
  346. source:(FIRFirestoreSource)source {
  347. __block FIRDocumentSnapshot *result;
  348. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  349. [ref getDocumentWithSource:source
  350. completion:^(FIRDocumentSnapshot *doc, NSError *_Nullable error) {
  351. XCTAssertNil(error);
  352. result = doc;
  353. [expectation fulfill];
  354. }];
  355. [self awaitExpectation:expectation];
  356. return result;
  357. }
  358. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query {
  359. return [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  360. }
  361. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query source:(FIRFirestoreSource)source {
  362. __block FIRQuerySnapshot *result;
  363. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  364. [query getDocumentsWithSource:source
  365. completion:^(FIRQuerySnapshot *documentSet, NSError *error) {
  366. XCTAssertNil(error);
  367. result = documentSet;
  368. [expectation fulfill];
  369. }];
  370. [self awaitExpectation:expectation];
  371. return result;
  372. }
  373. - (FIRDocumentSnapshot *)readSnapshotForRef:(FIRDocumentReference *)ref
  374. requireOnline:(BOOL)requireOnline {
  375. __block FIRDocumentSnapshot *result;
  376. XCTestExpectation *expectation = [self expectationWithDescription:@"listener"];
  377. id<FIRListenerRegistration> listener = [ref
  378. addSnapshotListenerWithIncludeMetadataChanges:YES
  379. listener:^(FIRDocumentSnapshot *snapshot,
  380. NSError *error) {
  381. XCTAssertNil(error);
  382. if (!requireOnline || !snapshot.metadata.fromCache) {
  383. result = snapshot;
  384. [expectation fulfill];
  385. }
  386. }];
  387. [self awaitExpectation:expectation];
  388. [listener remove];
  389. return result;
  390. }
  391. - (void)writeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  392. XCTestExpectation *expectation = [self expectationWithDescription:@"setData"];
  393. [ref setData:data completion:[self completionForExpectation:expectation]];
  394. [self awaitExpectation:expectation];
  395. }
  396. - (void)updateDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<id, id> *)data {
  397. XCTestExpectation *expectation = [self expectationWithDescription:@"updateData"];
  398. [ref updateData:data completion:[self completionForExpectation:expectation]];
  399. [self awaitExpectation:expectation];
  400. }
  401. - (void)deleteDocumentRef:(FIRDocumentReference *)ref {
  402. XCTestExpectation *expectation = [self expectationWithDescription:@"deleteDocument"];
  403. [ref deleteDocumentWithCompletion:[self completionForExpectation:expectation]];
  404. [self awaitExpectation:expectation];
  405. }
  406. - (FIRDocumentReference *)addDocumentRef:(FIRCollectionReference *)ref
  407. data:(NSDictionary<NSString *, id> *)data {
  408. XCTestExpectation *expectation = [self expectationWithDescription:@"addDocument"];
  409. FIRDocumentReference *doc = [ref addDocumentWithData:data
  410. completion:[self completionForExpectation:expectation]];
  411. [self awaitExpectation:expectation];
  412. return doc;
  413. }
  414. - (void)mergeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  415. XCTestExpectation *expectation = [self expectationWithDescription:@"setDataWithMerge"];
  416. [ref setData:data merge:YES completion:[self completionForExpectation:expectation]];
  417. [self awaitExpectation:expectation];
  418. }
  419. - (void)mergeDocumentRef:(FIRDocumentReference *)ref
  420. data:(NSDictionary<NSString *, id> *)data
  421. fields:(NSArray<id> *)fields {
  422. XCTestExpectation *expectation = [self expectationWithDescription:@"setDataWithMerge"];
  423. [ref setData:data mergeFields:fields completion:[self completionForExpectation:expectation]];
  424. [self awaitExpectation:expectation];
  425. }
  426. - (void)disableNetwork {
  427. XCTestExpectation *expectation = [self expectationWithDescription:@"disableNetwork"];
  428. [self.db disableNetworkWithCompletion:[self completionForExpectation:expectation]];
  429. [self awaitExpectation:expectation];
  430. }
  431. - (void)enableNetwork {
  432. XCTestExpectation *expectation = [self expectationWithDescription:@"enableNetwork"];
  433. [self.db enableNetworkWithCompletion:[self completionForExpectation:expectation]];
  434. [self awaitExpectation:expectation];
  435. }
  436. - (const std::shared_ptr<util::AsyncQueue> &)queueForFirestore:(FIRFirestore *)firestore {
  437. return [firestore workerQueue];
  438. }
  439. - (void)waitUntil:(BOOL (^)())predicate {
  440. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  441. double waitSeconds = [self defaultExpectationWaitSeconds];
  442. while (!predicate() && ([NSDate timeIntervalSinceReferenceDate] - start < waitSeconds)) {
  443. // This waits for the next event or until the 100ms timeout is reached
  444. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  445. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  446. }
  447. if (!predicate()) {
  448. XCTFail(@"Timeout");
  449. }
  450. }
  451. extern "C" NSArray<NSDictionary<NSString *, id> *> *FIRQuerySnapshotGetData(
  452. FIRQuerySnapshot *docs) {
  453. NSMutableArray<NSDictionary<NSString *, id> *> *result = [NSMutableArray array];
  454. for (FIRDocumentSnapshot *doc in docs.documents) {
  455. [result addObject:doc.data];
  456. }
  457. return result;
  458. }
  459. extern "C" NSArray<NSString *> *FIRQuerySnapshotGetIDs(FIRQuerySnapshot *docs) {
  460. NSMutableArray<NSString *> *result = [NSMutableArray array];
  461. for (FIRDocumentSnapshot *doc in docs.documents) {
  462. [result addObject:doc.documentID];
  463. }
  464. return result;
  465. }
  466. extern "C" NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs) {
  467. NSMutableArray<NSMutableArray<id> *> *result = [NSMutableArray array];
  468. for (FIRDocumentChange *docChange in docs.documentChanges) {
  469. NSMutableArray<id> *docChangeData = [NSMutableArray array];
  470. [docChangeData addObject:@(docChange.type)];
  471. [docChangeData addObject:docChange.document.documentID];
  472. [docChangeData addObject:docChange.document.data];
  473. [result addObject:docChangeData];
  474. }
  475. return result;
  476. }
  477. @end
  478. NS_ASSUME_NONNULL_END