FIRBundlesTests.mm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRLoadBundleTask+Internal.h"
  19. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  20. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  21. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  22. #include "Firestore/core/src/util/string_apple.h"
  23. #include "Firestore/core/test/unit/testutil/bundle_builder.h"
  24. namespace testutil = firebase::firestore::testutil;
  25. namespace util = firebase::firestore::util;
  26. @interface FIRBundlesTests : FSTIntegrationTestCase
  27. @end
  28. @implementation FIRBundlesTests
  29. // Clears persistence for each test method to have a clean start.
  30. - (void)setUp {
  31. [super setUp];
  32. self.db = [self firestore];
  33. XCTestExpectation* exp = [self expectationWithDescription:@"clear persistence"];
  34. [self.db clearPersistenceWithCompletion:^(NSError*) {
  35. [exp fulfill];
  36. }];
  37. [self awaitExpectation:exp];
  38. }
  39. - (void)verifyProgress:(FIRLoadBundleTaskProgress*)progress hasLoadedDocument:(int32_t)loaded {
  40. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateInProgress);
  41. XCTAssertLessThanOrEqual(progress.bytesLoaded, progress.totalBytes);
  42. XCTAssertLessThanOrEqual(progress.documentsLoaded, progress.totalDocuments);
  43. XCTAssertEqual(progress.documentsLoaded, loaded);
  44. }
  45. - (void)verifySuccessProgress:(FIRLoadBundleTaskProgress*)progress {
  46. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateSuccess);
  47. XCTAssertGreaterThan(progress.bytesLoaded, 0);
  48. XCTAssertEqual(progress.bytesLoaded, progress.totalBytes);
  49. XCTAssertGreaterThan(progress.documentsLoaded, 0);
  50. XCTAssertEqual(progress.documentsLoaded, progress.totalDocuments);
  51. }
  52. - (void)verifyErrorProgress:(FIRLoadBundleTaskProgress*)progress {
  53. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateError);
  54. XCTAssertEqual(progress.bytesLoaded, 0);
  55. XCTAssertEqual(progress.documentsLoaded, 0);
  56. }
  57. - (std::string)defaultBundle {
  58. return testutil::CreateBundle(util::MakeString([FSTIntegrationTestCase projectID]));
  59. }
  60. - (std::string)bundleForProject:(NSString*)projectID {
  61. return testutil::CreateBundle(util::MakeString(projectID));
  62. }
  63. - (void)verifyQueryResults {
  64. FIRCollectionReference* query = [self.db collectionWithPath:@"coll-1"];
  65. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  66. NSArray* expected = @[ @{@"bar" : @1L, @"k" : @"a"}, @{@"bar" : @2L, @"k" : @"b"} ];
  67. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  68. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @2L, @"k" : @"b"} ]];
  69. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @1L, @"k" : @"a"} ]];
  70. }
  71. - (void)verifyNamedQuery:(NSString*)name hasResult:(NSArray*)expected {
  72. XCTestExpectation* expectation = [self expectationWithDescription:@"namedQuery"];
  73. __block FIRQuery* query;
  74. [self.db getQueryNamed:name
  75. completion:^(FIRQuery* q) {
  76. query = q;
  77. [expectation fulfill];
  78. }];
  79. [self awaitExpectation:expectation];
  80. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  81. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  82. }
  83. - (void)testLoadWithDocumentsThatAreAlreadyPulledFromBackend {
  84. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/a"] data:@{@"bar" : @"newValueA"}];
  85. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/b"] data:@{@"bar" : @"newValueB"}];
  86. // Finishing receiving backend event.
  87. FIRCollectionReference* collection = [self.db collectionWithPath:@"coll-1"];
  88. id<FIRListenerRegistration> registration =
  89. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  90. [self.eventAccumulator awaitRemoteEvent];
  91. // We should see no more snapshots from loading the bundle, because the data there is older.
  92. [self.eventAccumulator assertNoAdditionalEvents];
  93. auto bundle = [self defaultBundle];
  94. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  95. __block FIRLoadBundleTaskProgress* result;
  96. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  97. FIRLoadBundleTask* task =
  98. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  99. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  100. result = progress;
  101. XCTAssertNil(error);
  102. [expectation fulfill];
  103. }];
  104. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  105. [progresses addObject:progress];
  106. }];
  107. [self awaitExpectation:expectation];
  108. XCTAssertEqual(4ul, progresses.count);
  109. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  110. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  111. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  112. [self verifySuccessProgress:progresses[3]];
  113. XCTAssertEqualObjects(progresses[3], result);
  114. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @"newValueB"} ]];
  115. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @"newValueA"} ]];
  116. [registration remove];
  117. }
  118. - (void)testLoadDocumentsWithProgressUpdates {
  119. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  120. auto bundle = [self defaultBundle];
  121. __block FIRLoadBundleTaskProgress* result;
  122. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  123. FIRLoadBundleTask* task =
  124. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  125. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  126. result = progress;
  127. XCTAssertNil(error);
  128. [expectation fulfill];
  129. }];
  130. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  131. [progresses addObject:progress];
  132. }];
  133. [self awaitExpectation:expectation];
  134. XCTAssertEqual(4ul, progresses.count);
  135. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  136. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  137. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  138. [self verifySuccessProgress:progresses[3]];
  139. XCTAssertEqualObjects(progresses[3], result);
  140. [self verifyQueryResults];
  141. }
  142. - (void)testLoadForASecondTimeSkips {
  143. auto bundle = [self defaultBundle];
  144. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]];
  145. // Load for a second time
  146. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  147. __block FIRLoadBundleTaskProgress* result;
  148. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  149. FIRLoadBundleTask* task =
  150. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  151. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  152. result = progress;
  153. XCTAssertNil(error);
  154. [expectation fulfill];
  155. }];
  156. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  157. [progresses addObject:progress];
  158. }];
  159. [self awaitExpectation:expectation];
  160. XCTAssertEqual(1ul, progresses.count);
  161. [self verifySuccessProgress:progresses[0]];
  162. XCTAssertEqualObjects(progresses[0], result);
  163. [self verifyQueryResults];
  164. }
  165. - (void)testLoadedDocumentsShouldNotBeGarbageCollectedRightAway {
  166. auto settings = [self.db settings];
  167. [settings setPersistenceEnabled:FALSE];
  168. [self.db setSettings:settings];
  169. auto bundle = [self defaultBundle];
  170. __block FIRLoadBundleTaskProgress* result;
  171. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  172. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  173. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  174. result = progress;
  175. XCTAssertNil(error);
  176. [expectation fulfill];
  177. }];
  178. [self awaitExpectation:expectation];
  179. [self verifySuccessProgress:result];
  180. // Read a different collection. This will trigger GC.
  181. [self readDocumentSetForRef:[self.db collectionWithPath:@"coll-other"]];
  182. // Read the loaded documents, expecting them to exist in cache. With memory GC, the documents
  183. // would get GC-ed if we did not hold the document keys in an "umbrella" target. See
  184. // LocalStore for details.
  185. [self verifyQueryResults];
  186. }
  187. - (void)testLoadBundlesFromOtherProjectFails {
  188. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  189. __block FIRLoadBundleTaskProgress* result;
  190. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  191. auto bundle = [self bundleForProject:@"OtherProject"];
  192. FIRLoadBundleTask* task =
  193. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  194. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  195. result = progress;
  196. XCTAssertNotNil(error);
  197. [expectation fulfill];
  198. }];
  199. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  200. [progresses addObject:progress];
  201. }];
  202. [self awaitExpectation:expectation];
  203. XCTAssertEqual(2ul, progresses.count);
  204. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  205. [self verifyErrorProgress:progresses[1]];
  206. XCTAssertEqualObjects(progresses[1], result);
  207. }
  208. @end