FPRNSURLSessionInstrumentTestDelegates.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2020 Google LLC
  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 "FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTestDelegates.h"
  15. @implementation FPRNSURLSessionTestDelegate
  16. - (void)dealloc {
  17. [self didDealloc];
  18. }
  19. /** Exists to be found be OCMExpect. Does nothing. */
  20. - (void)didDealloc {
  21. }
  22. - (void)didCallNonStandardDelegateMethod {
  23. self.nonStandardDelegateMethodCalled = YES;
  24. }
  25. // Used for a respondsToSelector check.
  26. - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error {
  27. self.URLSessionDidBecomeInvalidWithErrorWasCalled = YES;
  28. }
  29. @end
  30. @implementation FPRNSURLSessionCompleteTestDelegate
  31. #pragma mark NSURLSessionTaskDelegate
  32. - (void)URLSession:(NSURLSession *)session
  33. task:(NSURLSessionTask *)task
  34. didCompleteWithError:(NSError *)error {
  35. self.URLSessionTaskDidCompleteWithErrorCalled = YES;
  36. }
  37. - (void)URLSession:(NSURLSession *)session
  38. task:(NSURLSessionTask *)task
  39. didSendBodyData:(int64_t)bytesSent
  40. totalBytesSent:(int64_t)totalBytesSent
  41. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  42. self.URLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedCalled = YES;
  43. }
  44. - (void)URLSession:(NSURLSession *)session
  45. task:(NSURLSessionTask *)task
  46. willPerformHTTPRedirection:(NSHTTPURLResponse *)response
  47. newRequest:(NSURLRequest *)request
  48. completionHandler:(void (^)(NSURLRequest *))completionHandler {
  49. self.URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandlerCalled = YES;
  50. if (completionHandler) {
  51. completionHandler(nil);
  52. };
  53. }
  54. #pragma mark - NSURLSessionDataDelegate methods
  55. - (void)URLSession:(NSURLSession *)session
  56. dataTask:(NSURLSessionDataTask *)dataTask
  57. didReceiveData:(NSData *)data {
  58. self.URLSessionDataTaskDidReceiveDataCalled = YES;
  59. }
  60. #pragma mark - NSURLSessionDownloadDelegate methods
  61. - (void)URLSession:(NSURLSession *)session
  62. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  63. didFinishDownloadingToURL:(NSURL *)location {
  64. self.URLSessionDownloadTaskDidFinishDownloadingToURLCalled = YES;
  65. }
  66. - (void)URLSession:(NSURLSession *)session
  67. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  68. didResumeAtOffset:(int64_t)fileOffset
  69. expectedTotalBytes:(int64_t)expectedTotalBytes {
  70. self.URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytesCalled = YES;
  71. }
  72. - (void)URLSession:(NSURLSession *)session
  73. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  74. didWriteData:(int64_t)bytesWritten
  75. totalBytesWritten:(int64_t)totalBytesWritten
  76. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  77. self.URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesCalled = YES;
  78. }
  79. @end
  80. @interface FPRNSURLSessionTestDownloadDelegate ()
  81. // Redeclare as readwrite and mutable.
  82. @property(nonatomic, readwrite) NSMutableData *dataWrittenThusFar;
  83. @end
  84. @implementation FPRNSURLSessionTestDownloadDelegate
  85. - (void)URLSession:(NSURLSession *)session
  86. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  87. didFinishDownloadingToURL:(NSURL *)location {
  88. self.URLSessionDownloadTaskDidFinishDownloadingToURLCalled = YES;
  89. }
  90. - (void)URLSession:(NSURLSession *)session
  91. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  92. didResumeAtOffset:(int64_t)fileOffset
  93. expectedTotalBytes:(int64_t)expectedTotalBytes {
  94. self.URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytesCalled = YES;
  95. }
  96. - (void)URLSession:(NSURLSession *)session
  97. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  98. didWriteData:(int64_t)bytesWritten
  99. totalBytesWritten:(int64_t)totalBytesWritten
  100. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  101. if (bytesWritten > 0) {
  102. static dispatch_once_t onceToken;
  103. dispatch_once(&onceToken, ^{
  104. [downloadTask cancelByProducingResumeData:^(NSData *_Nullable resumeData) {
  105. // Create a resumable download task with the cancelled data, and start it.
  106. [[session downloadTaskWithResumeData:resumeData] resume];
  107. }];
  108. });
  109. }
  110. self.URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesCalled = YES;
  111. }
  112. @end