FIRBundlesTests.mm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #import "../../../../../../../../../Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental/string"
  23. #include "Firestore/core/src/util/string_apple.h"
  24. #include "Firestore/core/test/unit/testutil/bundle_builder.h"
  25. namespace testutil = firebase::firestore::testutil;
  26. namespace util = firebase::firestore::util;
  27. @interface FIRBundlesTests : FSTIntegrationTestCase
  28. @end
  29. @implementation FIRBundlesTests
  30. // Clears persistence for each test method to have a clean start.
  31. - (void)setUp {
  32. [super setUp];
  33. self.db = [self firestore];
  34. XCTestExpectation* exp = [self expectationWithDescription:@"clear persistence"];
  35. [self.db clearPersistenceWithCompletion:^(NSError*) {
  36. [exp fulfill];
  37. }];
  38. [self awaitExpectation:exp];
  39. }
  40. - (void)verifyProgress:(FIRLoadBundleTaskProgress*)progress hasLoadedDocument:(int32_t)loaded {
  41. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateInProgress);
  42. XCTAssertLessThanOrEqual(progress.bytesLoaded, progress.totalBytes);
  43. XCTAssertLessThanOrEqual(progress.documentsLoaded, progress.totalDocuments);
  44. XCTAssertEqual(progress.documentsLoaded, loaded);
  45. }
  46. - (void)verifySuccessProgress:(FIRLoadBundleTaskProgress*)progress {
  47. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateSuccess);
  48. XCTAssertGreaterThan(progress.bytesLoaded, 0);
  49. XCTAssertEqual(progress.bytesLoaded, progress.totalBytes);
  50. XCTAssertGreaterThan(progress.documentsLoaded, 0);
  51. XCTAssertEqual(progress.documentsLoaded, progress.totalDocuments);
  52. }
  53. - (void)verifyErrorProgress:(FIRLoadBundleTaskProgress*)progress {
  54. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateError);
  55. XCTAssertEqual(progress.bytesLoaded, 0);
  56. XCTAssertEqual(progress.documentsLoaded, 0);
  57. }
  58. - (std::string)defaultBundle {
  59. return testutil::CreateBundle(util::MakeString([FSTIntegrationTestCase projectID]));
  60. }
  61. - (std::string)bundleForProject:(NSString*)projectID {
  62. return testutil::CreateBundle(util::MakeString(projectID));
  63. }
  64. - (void)verifyQueryResults {
  65. FIRCollectionReference* query = [self.db collectionWithPath:@"coll-1"];
  66. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  67. NSArray* expected = @[ @{@"bar" : @1L, @"k" : @"a"}, @{@"bar" : @2L, @"k" : @"b"} ];
  68. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  69. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @2L, @"k" : @"b"} ]];
  70. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @1L, @"k" : @"a"} ]];
  71. }
  72. - (void)verifyNamedQuery:(NSString*)name hasResult:(NSArray*)expected {
  73. XCTestExpectation* expectation = [self expectationWithDescription:@"namedQuery"];
  74. __block FIRQuery* query;
  75. [self.db getQueryNamed:name
  76. completion:^(FIRQuery* q) {
  77. query = q;
  78. [expectation fulfill];
  79. }];
  80. [self awaitExpectation:expectation];
  81. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  82. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  83. }
  84. - (void)testLoadWithDocumentsThatAreAlreadyPulledFromBackend {
  85. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/a"] data:@{@"bar" : @"newValueA"}];
  86. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/b"] data:@{@"bar" : @"newValueB"}];
  87. // Finishing receiving backend event.
  88. FIRCollectionReference* collection = [self.db collectionWithPath:@"coll-1"];
  89. id<FIRListenerRegistration> registration =
  90. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  91. [self.eventAccumulator awaitRemoteEvent];
  92. // We should see no more snapshots from loading the bundle, because the data there is older.
  93. [self.eventAccumulator assertNoAdditionalEvents];
  94. auto bundle = [self defaultBundle];
  95. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  96. __block FIRLoadBundleTaskProgress* result;
  97. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  98. FIRLoadBundleTask* task =
  99. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  100. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  101. result = progress;
  102. XCTAssertNil(error);
  103. [expectation fulfill];
  104. }];
  105. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  106. [progresses addObject:progress];
  107. }];
  108. [self awaitExpectation:expectation];
  109. XCTAssertEqual(4ul, progresses.count);
  110. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  111. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  112. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  113. [self verifySuccessProgress:progresses[3]];
  114. XCTAssertEqualObjects(progresses[3], result);
  115. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @"newValueB"} ]];
  116. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @"newValueA"} ]];
  117. [registration remove];
  118. }
  119. - (void)testLoadDocumentsWithProgressUpdates {
  120. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  121. auto bundle = [self defaultBundle];
  122. __block FIRLoadBundleTaskProgress* result;
  123. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  124. FIRLoadBundleTask* task =
  125. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  126. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  127. result = progress;
  128. XCTAssertNil(error);
  129. [expectation fulfill];
  130. }];
  131. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  132. [progresses addObject:progress];
  133. }];
  134. [self awaitExpectation:expectation];
  135. XCTAssertEqual(4ul, progresses.count);
  136. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  137. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  138. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  139. [self verifySuccessProgress:progresses[3]];
  140. XCTAssertEqualObjects(progresses[3], result);
  141. [self verifyQueryResults];
  142. }
  143. - (void)testLoadForASecondTimeSkips {
  144. auto bundle = [self defaultBundle];
  145. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]];
  146. // Load for a second time
  147. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  148. __block FIRLoadBundleTaskProgress* result;
  149. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  150. FIRLoadBundleTask* task =
  151. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  152. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  153. result = progress;
  154. XCTAssertNil(error);
  155. [expectation fulfill];
  156. }];
  157. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  158. [progresses addObject:progress];
  159. }];
  160. [self awaitExpectation:expectation];
  161. XCTAssertEqual(1ul, progresses.count);
  162. [self verifySuccessProgress:progresses[0]];
  163. XCTAssertEqualObjects(progresses[0], result);
  164. [self verifyQueryResults];
  165. }
  166. - (void)testLoadedDocumentsShouldNotBeGarbageCollectedRightAway {
  167. auto settings = [self.db settings];
  168. [settings setPersistenceEnabled:FALSE];
  169. [self.db setSettings:settings];
  170. auto bundle = [self defaultBundle];
  171. __block FIRLoadBundleTaskProgress* result;
  172. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  173. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  174. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  175. result = progress;
  176. XCTAssertNil(error);
  177. [expectation fulfill];
  178. }];
  179. [self awaitExpectation:expectation];
  180. [self verifySuccessProgress:result];
  181. // Read a different collection. This will trigger GC.
  182. [self readDocumentSetForRef:[self.db collectionWithPath:@"coll-other"]];
  183. // Read the loaded documents, expecting them to exist in cache. With memory GC, the documents
  184. // would get GC-ed if we did not hold the document keys in an "umbrella" target. See
  185. // LocalStore for details.
  186. [self verifyQueryResults];
  187. }
  188. - (void)testLoadBundlesFromOtherProjectFails {
  189. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  190. __block FIRLoadBundleTaskProgress* result;
  191. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  192. auto bundle = [self bundleForProject:@"OtherProject"];
  193. FIRLoadBundleTask* task =
  194. [self.db loadBundle:[util::MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  195. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  196. result = progress;
  197. XCTAssertNotNil(error);
  198. [expectation fulfill];
  199. }];
  200. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  201. [progresses addObject:progress];
  202. }];
  203. [self awaitExpectation:expectation];
  204. XCTAssertEqual(2ul, progresses.count);
  205. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  206. [self verifyErrorProgress:progresses[1]];
  207. XCTAssertEqualObjects(progresses[1], result);
  208. }
  209. @end