FIRIAMMessageContentDataWithImageURLTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2017 Google
  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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FIRIAMMessageContentDataWithImageURL.h"
  19. static NSString *defaultTitle = @"Message Title";
  20. static NSString *defaultBody = @"Message Body";
  21. static NSString *defaultActionButtonText = @"Take action";
  22. static NSString *defaultActionURL = @"https://foo.com/bar";
  23. static NSString *defaultImageURL = @"http://firebase.com/iam/test.png";
  24. @interface FIRIAMMessageContentDataWithImageURLTests : XCTestCase
  25. @property NSURLSession *mockedNSURLSession;
  26. @property FIRIAMMessageContentDataWithImageURL *defaultContentDataWithImageURL;
  27. @end
  28. @implementation FIRIAMMessageContentDataWithImageURLTests
  29. - (void)setUp {
  30. [super setUp];
  31. _mockedNSURLSession = OCMClassMock([NSURLSession class]);
  32. _defaultContentDataWithImageURL = [[FIRIAMMessageContentDataWithImageURL alloc]
  33. initWithMessageTitle:defaultTitle
  34. messageBody:defaultBody
  35. actionButtonText:defaultActionButtonText
  36. actionURL:[NSURL URLWithString:defaultActionURL]
  37. imageURL:[NSURL URLWithString:defaultImageURL]
  38. usingURLSession:_mockedNSURLSession];
  39. }
  40. - (void)tearDown {
  41. [super tearDown];
  42. }
  43. - (void)testReadingTitleAndBodyBackCorrectly {
  44. XCTAssertEqualObjects(defaultTitle, self.defaultContentDataWithImageURL.titleText);
  45. XCTAssertEqualObjects(defaultBody, self.defaultContentDataWithImageURL.bodyText);
  46. }
  47. - (void)testReadingActionButtonTextCorrectly {
  48. XCTAssertEqualObjects(defaultActionButtonText,
  49. self.defaultContentDataWithImageURL.actionButtonText);
  50. }
  51. - (void)testURLRequestUsingCorrectImageURL {
  52. __block NSURLRequest *capturedNSURLRequest;
  53. OCMStub([self.mockedNSURLSession
  54. dataTaskWithRequest:[OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  55. capturedNSURLRequest = request;
  56. return YES;
  57. }]
  58. completionHandler:[OCMArg any] // second parameter is the callback which we don't care in
  59. // this unit testing
  60. ]);
  61. [_defaultContentDataWithImageURL
  62. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSError *_Nullable error){
  63. }];
  64. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  65. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  66. completionHandler:[OCMArg any]]);
  67. XCTAssertEqualObjects([capturedNSURLRequest URL].absoluteString, defaultImageURL);
  68. }
  69. - (void)testReportErrorOnNonSuccessHTTPStatusCode {
  70. // NSURLSessionDataTask * mockedDataTask = OCMClassMock([NSURLSessionDataTask class]);
  71. __block void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error);
  72. OCMStub([self.mockedNSURLSession
  73. dataTaskWithRequest:[OCMArg any]
  74. completionHandler:[OCMArg checkWithBlock:^BOOL(id completionHandler) {
  75. capturedCompletionHandler = completionHandler;
  76. return YES;
  77. }] // second parameter is the callback which we don't care in this unit testing
  78. ]);
  79. XCTestExpectation *expectation =
  80. [self expectationWithDescription:@"image load callback triggered."];
  81. [_defaultContentDataWithImageURL
  82. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  83. XCTAssertNil(imageData);
  84. XCTAssertNotNil(error); // we should report error due to the unsuccessful http status code
  85. [expectation fulfill];
  86. }];
  87. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  88. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  89. completionHandler:[OCMArg any]]);
  90. // by this time we should have capturedCompletionHandler being the callback block for the
  91. // NSURLSessionDataTask, now supply it with invalid http status code to see how the block from
  92. // loadImageDataWithBlock: would react to it.
  93. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  94. NSHTTPURLResponse *unsuccessfulHTTPResponse = [[NSHTTPURLResponse alloc] initWithURL:url
  95. statusCode:404
  96. HTTPVersion:nil
  97. headerFields:nil];
  98. capturedCompletionHandler(nil, unsuccessfulHTTPResponse, nil);
  99. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  100. }
  101. - (void)testReportErrorOnGeneralNSErrorFromNSURLSession {
  102. NSError *customError = [[NSError alloc] initWithDomain:@"Error Domain" code:100 userInfo:nil];
  103. __block void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error);
  104. OCMStub([self.mockedNSURLSession
  105. dataTaskWithRequest:[OCMArg any]
  106. completionHandler:[OCMArg checkWithBlock:^BOOL(id completionHandler) {
  107. capturedCompletionHandler = completionHandler;
  108. return YES;
  109. }] // second parameter is the callback which we don't care in this unit testing
  110. ]);
  111. XCTestExpectation *expectation =
  112. [self expectationWithDescription:@"image load callback triggered."];
  113. [_defaultContentDataWithImageURL
  114. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  115. XCTAssertNil(imageData);
  116. XCTAssertNotNil(error); // we should report error due to the unsuccessful http status code
  117. XCTAssertEqualObjects(error, customError);
  118. [expectation fulfill];
  119. }];
  120. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  121. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  122. completionHandler:[OCMArg any]]);
  123. // by this time we should have capturedCompletionHandler being the callback block for the
  124. // NSURLSessionDataTask, now feed it with an NSError see how the block from
  125. // loadImageDataWithBlock: would react to it.
  126. capturedCompletionHandler(nil, nil, customError);
  127. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  128. }
  129. - (void)testReportErrorOnNonImageContentTypeResponse {
  130. __block void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error);
  131. OCMStub([self.mockedNSURLSession
  132. dataTaskWithRequest:[OCMArg any]
  133. completionHandler:[OCMArg checkWithBlock:^BOOL(id completionHandler) {
  134. capturedCompletionHandler = completionHandler;
  135. return YES;
  136. }] // second parameter is the callback which we don't care in this unit testing
  137. ]);
  138. XCTestExpectation *expectation =
  139. [self expectationWithDescription:@"image load callback triggered."];
  140. [_defaultContentDataWithImageURL
  141. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  142. XCTAssertNil(imageData);
  143. XCTAssertNotNil(error); // we should report error due to the http response
  144. // content type being invalid
  145. [expectation fulfill];
  146. }];
  147. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  148. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  149. completionHandler:[OCMArg any]]);
  150. // by this time we should have capturedCompletionHandler being the callback block for the
  151. // NSURLSessionDataTask, now feed it with a non-image http response to see how the block from
  152. // loadImageDataWithBlock: would react to it.
  153. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  154. NSHTTPURLResponse *nonImageContentTypeHTTPResponse =
  155. [[NSHTTPURLResponse alloc] initWithURL:url
  156. statusCode:200
  157. HTTPVersion:nil
  158. headerFields:@{@"Content-Type" : @"non-image/jpeg"}];
  159. capturedCompletionHandler(nil, nonImageContentTypeHTTPResponse, nil);
  160. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  161. }
  162. - (void)testGettingImageDataSuccessfully {
  163. NSString *imageDataString = @"test image data";
  164. NSData *imageData = [imageDataString dataUsingEncoding:NSUTF8StringEncoding];
  165. __block void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error);
  166. OCMStub([self.mockedNSURLSession
  167. dataTaskWithRequest:[OCMArg any]
  168. completionHandler:[OCMArg checkWithBlock:^BOOL(id completionHandler) {
  169. capturedCompletionHandler = completionHandler;
  170. return YES;
  171. }] // second parameter is the callback which we don't care in this unit testing
  172. ]);
  173. XCTestExpectation *expectation =
  174. [self expectationWithDescription:@"image load callback triggered."];
  175. [_defaultContentDataWithImageURL
  176. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSError *_Nullable error) {
  177. XCTAssertNil(error); // no error is reported
  178. NSString *fetchedImageDataString = [[NSString alloc] initWithData:imageData
  179. encoding:NSUTF8StringEncoding];
  180. XCTAssertEqualObjects(imageDataString, fetchedImageDataString);
  181. [expectation fulfill];
  182. }];
  183. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  184. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  185. completionHandler:[OCMArg any]]);
  186. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  187. NSHTTPURLResponse *successfulHTTPResponse =
  188. [[NSHTTPURLResponse alloc] initWithURL:url
  189. statusCode:200
  190. HTTPVersion:nil
  191. headerFields:@{@"Content-Type" : @"image/jpeg"}];
  192. // by this time we should have capturedCompletionHandler being the callback block for the
  193. // NSURLSessionDataTask, now feed it with image data to see how the block from
  194. // loadImageDataWithBlock: would react to it.
  195. capturedCompletionHandler(imageData, successfulHTTPResponse, nil);
  196. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  197. }
  198. @end