FPRNSURLSessionInstrumentTestDelegates.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. - (void)URLSession:(NSURLSession *)session
  61. dataTask:(NSURLSessionDataTask *)dataTask
  62. didReceiveResponse:(NSURLResponse *)response
  63. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
  64. self.URLSessionDataTaskDidReceiveResponseCompletionHandlerCalled = YES;
  65. completionHandler(NSURLSessionResponseAllow);
  66. }
  67. #pragma mark - NSURLSessionDownloadDelegate methods
  68. - (void)URLSession:(NSURLSession *)session
  69. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  70. didFinishDownloadingToURL:(NSURL *)location {
  71. self.URLSessionDownloadTaskDidFinishDownloadingToURLCalled = YES;
  72. }
  73. - (void)URLSession:(NSURLSession *)session
  74. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  75. didResumeAtOffset:(int64_t)fileOffset
  76. expectedTotalBytes:(int64_t)expectedTotalBytes {
  77. self.URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytesCalled = YES;
  78. }
  79. - (void)URLSession:(NSURLSession *)session
  80. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  81. didWriteData:(int64_t)bytesWritten
  82. totalBytesWritten:(int64_t)totalBytesWritten
  83. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  84. self.URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesCalled = YES;
  85. }
  86. @end
  87. @interface FPRNSURLSessionTestDownloadDelegate ()
  88. // Redeclare as readwrite and mutable.
  89. @property(nonatomic, readwrite) NSMutableData *dataWrittenThusFar;
  90. @end
  91. @implementation FPRNSURLSessionTestDownloadDelegate
  92. - (void)URLSession:(NSURLSession *)session
  93. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  94. didFinishDownloadingToURL:(NSURL *)location {
  95. self.URLSessionDownloadTaskDidFinishDownloadingToURLCalled = YES;
  96. }
  97. - (void)URLSession:(NSURLSession *)session
  98. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  99. didResumeAtOffset:(int64_t)fileOffset
  100. expectedTotalBytes:(int64_t)expectedTotalBytes {
  101. self.URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytesCalled = YES;
  102. }
  103. - (void)URLSession:(NSURLSession *)session
  104. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  105. didWriteData:(int64_t)bytesWritten
  106. totalBytesWritten:(int64_t)totalBytesWritten
  107. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  108. if (bytesWritten > 0) {
  109. static dispatch_once_t onceToken;
  110. dispatch_once(&onceToken, ^{
  111. [downloadTask cancelByProducingResumeData:^(NSData *_Nullable resumeData) {
  112. // Create a resumable download task with the cancelled data, and start it.
  113. [[session downloadTaskWithResumeData:resumeData] resume];
  114. }];
  115. });
  116. }
  117. self.URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesCalled = YES;
  118. }
  119. @end