FIRStorageIntegrationTests.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <math.h>
  15. #import <XCTest/XCTest.h>
  16. #import "FirebaseStorage.h"
  17. #import "FIRApp.h"
  18. #import "FIROptions.h"
  19. NSTimeInterval kFIRStorageIntegrationTestTimeout = 30;
  20. /**
  21. * Firebase Storage Integration tests
  22. *
  23. * To run these tests, you need to define the following access rights for your Firebase App:
  24. * - unauthentication read/write access to /ios/public
  25. * - authentication read/write access to /ios/private
  26. *
  27. * A sample configuration may look like:
  28. *
  29. * service firebase.storage {
  30. * match /b/{YOUR_PROJECT_ID}.appspot.com/o {
  31. * ...
  32. * match /ios {
  33. * match /public/{allPaths=**} {
  34. * allow read, write;
  35. * }
  36. * match /private/{allPaths=**} {
  37. * allow none;
  38. * }
  39. * }
  40. * }
  41. * }
  42. *
  43. * You can define these access rights in the Firebase Console of your project.
  44. */
  45. @interface FIRStorageIntegrationTests : XCTestCase
  46. @property(strong, nonatomic) FIRApp *app;
  47. @property(strong, nonatomic) FIRStorage *storage;
  48. @end
  49. @implementation FIRStorageIntegrationTests
  50. + (void)setUp {
  51. [FIRApp configure];
  52. }
  53. - (void)setUp {
  54. [super setUp];
  55. self.app = [FIRApp defaultApp];
  56. self.storage = [FIRStorage storageForApp:self.app];
  57. static dispatch_once_t once;
  58. dispatch_once(&once, ^{
  59. XCTestExpectation *expectation = [self expectationWithDescription:@"setup"];
  60. FIRStorageReference *ref = [[FIRStorage storage].reference child:@"ios/public/1mb"];
  61. NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1mb" ofType:@"dat"]];
  62. XCTAssertNotNil(data, "Could not load bundled file");
  63. [ref putData:data metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
  64. XCTAssertNil(error);
  65. [expectation fulfill];
  66. }];
  67. [self waitForExpectations];
  68. });
  69. }
  70. - (void)tearDown {
  71. self.app = nil;
  72. self.storage = nil;
  73. [super tearDown];
  74. }
  75. - (void)testName {
  76. NSString *aGSURI = [NSString stringWithFormat:@"gs://%@.appspot.com/path/to", [[FIRApp defaultApp] options].projectID];
  77. FIRStorageReference *ref = [self.storage referenceForURL:aGSURI];
  78. XCTAssertEqualObjects(ref.description, aGSURI);
  79. }
  80. - (void)testUnauthenticatedGetMetadata {
  81. XCTestExpectation *expectation =
  82. [self expectationWithDescription:@"testUnauthenticatedGetMetadata"];
  83. FIRStorageReference *ref = [self.storage.reference child:@"ios/public/1mb"];
  84. [ref metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  85. XCTAssertNotNil(metadata, "Metadata should not be nil");
  86. XCTAssertNil(error, "Error should be nil");
  87. [expectation fulfill];
  88. }];
  89. [self waitForExpectations];
  90. }
  91. - (void)testUnauthenticatedUpdateMetadata {
  92. XCTestExpectation *expectation =
  93. [self expectationWithDescription:@"testUnauthenticatedUpdateMetadata"];
  94. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  95. FIRStorageMetadata *meta = [[FIRStorageMetadata alloc] init];
  96. [meta setContentType:@"lol/custom"];
  97. [meta setCustomMetadata:@{
  98. @"lol" : @"custom metadata is neat",
  99. @"ちかてつ" : @"🚇",
  100. @"shinkansen" : @"新幹線"
  101. }];
  102. [ref updateMetadata:meta
  103. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  104. XCTAssertEqualObjects(meta.contentType, metadata.contentType);
  105. XCTAssertEqualObjects(meta.customMetadata[@"lol"],
  106. metadata.customMetadata[@"lol"]);
  107. XCTAssertEqualObjects(meta.customMetadata[@"ちかてつ"],
  108. metadata.customMetadata[@"ちかてつ"]);
  109. XCTAssertEqualObjects(meta.customMetadata[@"shinkansen"],
  110. metadata.customMetadata[@"shinkansen"]);
  111. XCTAssertNil(error, "Error should be nil");
  112. [expectation fulfill];
  113. }];
  114. [self waitForExpectations];
  115. }
  116. - (void)testUnauthenticatedDelete {
  117. XCTestExpectation *expectation = [self expectationWithDescription:@"testUnauthenticatedDelete"];
  118. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
  119. NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
  120. [ref putData:data
  121. metadata:nil
  122. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  123. XCTAssertNotNil(metadata, "Metadata should not be nil");
  124. XCTAssertNil(error, "Error should be nil");
  125. [ref deleteWithCompletion:^(NSError *error) {
  126. XCTAssertNil(error, "Error should be nil");
  127. [expectation fulfill];
  128. }];
  129. }];
  130. [self waitForExpectations];
  131. }
  132. - (void)testDeleteWithNilCompletion {
  133. XCTestExpectation *expectation = [self expectationWithDescription:@"testDeleteWithNilCompletion"];
  134. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"];
  135. NSData *data = [@"Delete me!!!!!!!" dataUsingEncoding:NSUTF8StringEncoding];
  136. [ref putData:data
  137. metadata:nil
  138. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  139. XCTAssertNotNil(metadata, "Metadata should not be nil");
  140. XCTAssertNil(error, "Error should be nil");
  141. [ref deleteWithCompletion:nil];
  142. [expectation fulfill];
  143. }];
  144. [self waitForExpectations];
  145. }
  146. - (void)testUnauthenticatedSimplePutData {
  147. XCTestExpectation *expectation =
  148. [self expectationWithDescription:@"testUnauthenticatedSimplePutData"];
  149. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"];
  150. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  151. [ref putData:data
  152. metadata:nil
  153. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  154. XCTAssertNotNil(metadata, "Metadata should not be nil");
  155. XCTAssertNil(error, "Error should be nil");
  156. [expectation fulfill];
  157. }];
  158. [self waitForExpectations];
  159. }
  160. - (void)testUnauthenticatedSimplePutEmptyData {
  161. XCTestExpectation *expectation =
  162. [self expectationWithDescription:@"testUnauthenticatedSimplePutEmptyData"];
  163. FIRStorageReference *ref =
  164. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutEmptyData"];
  165. NSData *data = [[NSData alloc] init];
  166. [ref putData:data
  167. metadata:nil
  168. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  169. XCTAssertNotNil(metadata, "Metadata should not be nil");
  170. XCTAssertNil(error, "Error should be nil");
  171. [expectation fulfill];
  172. }];
  173. [self waitForExpectations];
  174. }
  175. - (void)testUnauthenticatedSimplePutDataUnauthorized {
  176. XCTestExpectation *expectation =
  177. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataUnauthorized"];
  178. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/private/secretfile.txt"];
  179. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  180. [ref putData:data
  181. metadata:nil
  182. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  183. XCTAssertNil(metadata, "Metadata should be nil");
  184. XCTAssertNotNil(error, "Error should not be nil");
  185. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  186. [expectation fulfill];
  187. }];
  188. [self waitForExpectations];
  189. }
  190. - (void)testUnauthenticatedSimplePutFile {
  191. XCTestExpectation *expectation =
  192. [self expectationWithDescription:@"testUnauthenticatedSimplePutFile"];
  193. FIRStorageReference *ref =
  194. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFile"];
  195. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  196. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  197. NSURL *fileURL =
  198. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  199. [data writeToURL:fileURL atomically:YES];
  200. FIRStorageUploadTask *task = [ref putFile:fileURL
  201. metadata:nil
  202. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  203. XCTAssertNotNil(metadata, "Metadata should not be nil");
  204. XCTAssertNil(error, "Error should be nil");
  205. }];
  206. __block long uploadedBytes = -1;
  207. [task observeStatus:FIRStorageTaskStatusSuccess
  208. handler:^(FIRStorageTaskSnapshot *snapshot) {
  209. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  210. [expectation fulfill];
  211. }];
  212. [task observeStatus:FIRStorageTaskStatusProgress
  213. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  214. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  215. [[snapshot description] containsString:@"State: Resume"]);
  216. NSProgress *progress = snapshot.progress;
  217. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes);
  218. uploadedBytes = progress.completedUnitCount;
  219. }];
  220. [self waitForExpectations];
  221. }
  222. - (void)testPutFileWithSpecialCharacters {
  223. XCTestExpectation *expectation =
  224. [self expectationWithDescription:@"testPutFileWithSpecialCharacters"];
  225. NSString *fileName = @"hello&+@_ .txt";
  226. FIRStorageReference *ref =
  227. [self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
  228. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  229. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  230. NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:fileName];
  231. [data writeToURL:fileURL atomically:YES];
  232. [ref putFile:fileURL
  233. metadata:nil
  234. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  235. XCTAssertNotNil(metadata, "Metadata should not be nil");
  236. XCTAssertNil(error, "Error should be nil");
  237. XCTAssertEqualObjects(fileName, metadata.name);
  238. FIRStorageReference *download =
  239. [self.storage referenceWithPath:[@"ios/public/" stringByAppendingString:fileName]];
  240. [download metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  241. XCTAssertNotNil(metadata, "Metadata should not be nil");
  242. XCTAssertNil(error, "Error should be nil");
  243. XCTAssertEqualObjects(fileName, metadata.name);
  244. [expectation fulfill];
  245. }];
  246. }];
  247. [self waitForExpectations];
  248. }
  249. - (void)testUnauthenticatedSimplePutDataNoMetadata {
  250. XCTestExpectation *expectation =
  251. [self expectationWithDescription:@"testUnauthenticatedSimplePutDataNoMetadata"];
  252. FIRStorageReference *ref =
  253. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutDataNoMetadata"];
  254. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  255. [ref putData:data
  256. metadata:nil
  257. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  258. XCTAssertNotNil(metadata, "Metadata should not be nil");
  259. XCTAssertNil(error, "Error should be nil");
  260. [expectation fulfill];
  261. }];
  262. [self waitForExpectations];
  263. }
  264. - (void)testUnauthenticatedSimplePutFileNoMetadata {
  265. XCTestExpectation *expectation =
  266. [self expectationWithDescription:@"testUnauthenticatedSimplePutFileNoMetadata"];
  267. FIRStorageReference *ref =
  268. [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFileNoMetadata"];
  269. NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
  270. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  271. NSURL *fileURL =
  272. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  273. [data writeToURL:fileURL atomically:YES];
  274. [ref putFile:fileURL
  275. metadata:nil
  276. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  277. XCTAssertNotNil(metadata, "Metadata should not be nil");
  278. XCTAssertNil(error, "Error should be nil");
  279. [expectation fulfill];
  280. }];
  281. [self waitForExpectations];
  282. }
  283. - (void)testUnauthenticatedSimpleGetData {
  284. XCTestExpectation *expectation =
  285. [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
  286. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  287. [ref dataWithMaxSize:1 * 1024 * 1024
  288. completion:^(NSData *data, NSError *error) {
  289. XCTAssertNotNil(data, "Data should not be nil");
  290. XCTAssertNil(error, "Error should be nil");
  291. [expectation fulfill];
  292. }];
  293. [self waitForExpectations];
  294. }
  295. - (void)testUnauthenticatedSimpleGetDataTooSmall {
  296. XCTestExpectation *expectation =
  297. [self expectationWithDescription:@"testUnauthenticatedSimpleGetDataTooSmall"];
  298. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  299. /// Only allow 1kB size, which is smaller than our file
  300. [ref dataWithMaxSize:1 * 1024
  301. completion:^(NSData *data, NSError *error) {
  302. XCTAssertEqual(data, nil);
  303. XCTAssertEqual(error.code, FIRStorageErrorCodeDownloadSizeExceeded);
  304. [expectation fulfill];
  305. }];
  306. [self waitForExpectations];
  307. }
  308. - (void)testUnauthenticatedSimpleGetFile {
  309. XCTestExpectation *expectation =
  310. [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"];
  311. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/helloworld"];
  312. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  313. NSURL *fileURL =
  314. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  315. [ref putData:[@"Hello World" dataUsingEncoding:NSUTF8StringEncoding] metadata:nil
  316. completion:^(FIRStorageMetadata *metadata, NSError *error)
  317. {
  318. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  319. [task observeStatus:FIRStorageTaskStatusSuccess
  320. handler:^(FIRStorageTaskSnapshot *snapshot) {
  321. NSString *data = [NSString stringWithContentsOfURL:fileURL
  322. encoding:NSUTF8StringEncoding
  323. error:NULL];
  324. NSString *expectedData = @"Hello World";
  325. XCTAssertEqualObjects(data, expectedData);
  326. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  327. [expectation fulfill];
  328. }];
  329. [task observeStatus:FIRStorageTaskStatusProgress
  330. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  331. NSProgress *progress = snapshot.progress;
  332. NSLog(@"%lld of %lld", progress.completedUnitCount, progress.totalUnitCount);
  333. }];
  334. [task observeStatus:FIRStorageTaskStatusFailure
  335. handler:^(FIRStorageTaskSnapshot *snapshot) {
  336. XCTAssertNil(snapshot.error);
  337. [expectation fulfill];
  338. }];
  339. }];
  340. [self waitForExpectations];
  341. }
  342. - (void)testCancelDownload {
  343. XCTestExpectation *expectation =
  344. [self expectationWithDescription:@"testCancelDownload"];
  345. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  346. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  347. NSURL *fileURL =
  348. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"dat"];
  349. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  350. [task observeStatus:FIRStorageTaskStatusFailure
  351. handler:^(FIRStorageTaskSnapshot *snapshot) {
  352. XCTAssertTrue([[snapshot description] containsString:@"State: Failed"]);
  353. [expectation fulfill];
  354. }];
  355. [task observeStatus:FIRStorageTaskStatusProgress
  356. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  357. [task cancel];
  358. }];
  359. [self waitForExpectations];
  360. }
  361. - (void)assertMetadata:(FIRStorageMetadata *) actualMetadata
  362. contentType:(NSString *) expectedContentType
  363. customMetadata:(NSDictionary *) expectedCustomMetadata {
  364. XCTAssertEqualObjects(actualMetadata.cacheControl, @"cache-control");
  365. XCTAssertEqualObjects(actualMetadata.contentDisposition, @"content-disposition");
  366. XCTAssertEqualObjects(actualMetadata.contentEncoding, @"gzip");
  367. XCTAssertEqualObjects(actualMetadata.contentLanguage, @"de");
  368. XCTAssertEqualObjects(actualMetadata.contentType, expectedContentType);
  369. for (NSString* key in expectedCustomMetadata) {
  370. XCTAssertEqualObjects([actualMetadata.customMetadata objectForKey:key],
  371. [expectedCustomMetadata objectForKey:key]);
  372. }
  373. }
  374. - (void)assertMetadataNil:(FIRStorageMetadata *) actualMetadata {
  375. XCTAssertNil(actualMetadata.cacheControl);
  376. XCTAssertNil(actualMetadata.contentDisposition);
  377. XCTAssertEqualObjects(actualMetadata.contentEncoding, @"identity");
  378. XCTAssertNil(actualMetadata.contentLanguage);
  379. XCTAssertNil(actualMetadata.contentType);
  380. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"a"]);
  381. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"c"]);
  382. XCTAssertNil([actualMetadata.customMetadata objectForKey:@"f"]);
  383. }
  384. - (void)testUpdateMetadata {
  385. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateMetadata"];
  386. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  387. // Update all available metadata
  388. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
  389. metadata.cacheControl = @"cache-control";
  390. metadata.contentDisposition = @"content-disposition";
  391. metadata.contentEncoding = @"gzip";
  392. metadata.contentLanguage = @"de";
  393. metadata.contentType = @"content-type-a";
  394. metadata.customMetadata = @{@"a" : @"b"};
  395. [ref updateMetadata:metadata completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  396. XCTAssertNil(error);
  397. [self assertMetadata:updatedMetadata contentType:@"content-type-a" customMetadata:@{@"a" : @"b"}];
  398. // Update a subset of the metadata using the existing object.
  399. FIRStorageMetadata *metadata = updatedMetadata;
  400. metadata.contentType = @"content-type-b";
  401. metadata.customMetadata = @{@"a" : @"b", @"c" : @"d"};
  402. [ref updateMetadata:metadata completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  403. XCTAssertNil(error);
  404. [self assertMetadata:updatedMetadata contentType:@"content-type-b" customMetadata: @{@"a" : @"b", @"c" : @"d"}];
  405. // Clear all metadata.
  406. FIRStorageMetadata *metadata = updatedMetadata;
  407. metadata.cacheControl = nil;
  408. metadata.contentDisposition = nil;
  409. metadata.contentEncoding = nil;
  410. metadata.contentLanguage = nil;
  411. metadata.contentType = nil;
  412. metadata.customMetadata = [NSDictionary dictionary];
  413. [ref updateMetadata:metadata completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
  414. XCTAssertNil(error);
  415. [self assertMetadataNil:updatedMetadata];
  416. [expectation fulfill];
  417. }];
  418. }];
  419. }];
  420. [self waitForExpectations];
  421. }
  422. - (void)testUnauthenticatedResumeGetFile {
  423. XCTestExpectation *expectation =
  424. [self expectationWithDescription:@"testUnauthenticatedResumeGetFile"];
  425. FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
  426. NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
  427. NSURL *fileURL =
  428. [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
  429. __block long resumeAtBytes = 256 * 1024;
  430. __block long downloadedBytes = 0;
  431. __block double computationResult = 0;
  432. FIRStorageDownloadTask *task = [ref writeToFile:fileURL];
  433. [task observeStatus:FIRStorageTaskStatusSuccess
  434. handler:^(FIRStorageTaskSnapshot *snapshot) {
  435. XCTAssertEqualObjects([snapshot description], @"<State: Success>");
  436. [expectation fulfill];
  437. }];
  438. [task observeStatus:FIRStorageTaskStatusProgress
  439. handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
  440. XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
  441. [[snapshot description] containsString:@"State: Resume"]);
  442. NSProgress *progress = snapshot.progress;
  443. XCTAssertGreaterThanOrEqual(progress.completedUnitCount, downloadedBytes);
  444. downloadedBytes = progress.completedUnitCount;
  445. if (progress.completedUnitCount > resumeAtBytes) {
  446. // Making sure the main run loop is busy.
  447. for (int i = 0; i < 500; ++i) {
  448. dispatch_async(dispatch_get_main_queue(), ^ {
  449. computationResult = sqrt(INT_MAX - i);
  450. });
  451. }
  452. NSLog(@"Pausing");
  453. [task pause];
  454. resumeAtBytes = INT_MAX;
  455. }
  456. }];
  457. [task observeStatus:FIRStorageTaskStatusPause
  458. handler:^(FIRStorageTaskSnapshot *snapshot) {
  459. XCTAssertEqualObjects([snapshot description], @"<State: Paused>");
  460. NSLog(@"Resuming");
  461. [task resume];
  462. }];
  463. [self waitForExpectations];
  464. XCTAssertEqual(INT_MAX, resumeAtBytes);
  465. XCTAssertEqualWithAccuracy(sqrt(INT_MAX - 499), computationResult, 0.1);
  466. }
  467. - (void)waitForExpectations {
  468. [self waitForExpectationsWithTimeout:kFIRStorageIntegrationTestTimeout
  469. handler:^(NSError *_Nullable error) {
  470. if (error) {
  471. NSLog(@"%@", error);
  472. }
  473. }];
  474. }
  475. @end