FIRIAMMessageContentDataWithImageURLTests.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 *defaultSecondaryActionButtonText = @"Dismiss";
  23. static NSString *defaultActionURL = @"https://foo.com/bar";
  24. static NSString *defaultSecondaryActionURL = @"https://foo.com/baz";
  25. static NSString *defaultImageURL = @"http://firebase.com/iam/test.png";
  26. static NSString *defaultLandscapeImageURL = @"http://firebase.com/iam/test-landscape.png";
  27. @interface FIRIAMMessageContentDataWithImageURLTests : XCTestCase
  28. @property NSURLSession *mockedNSURLSession;
  29. @property FIRIAMMessageContentDataWithImageURL *defaultContentDataWithImageURL;
  30. @property FIRIAMMessageContentDataWithImageURL *defaultContentDataWithBothImageURLs;
  31. @end
  32. typedef void (^ImageFetchExpectationsBlock)(NSData *, NSData *, NSError *);
  33. @implementation FIRIAMMessageContentDataWithImageURLTests
  34. - (void)setUp {
  35. [super setUp];
  36. _mockedNSURLSession = OCMClassMock([NSURLSession class]);
  37. _defaultContentDataWithImageURL = [[FIRIAMMessageContentDataWithImageURL alloc]
  38. initWithMessageTitle:defaultTitle
  39. messageBody:defaultBody
  40. actionButtonText:defaultActionButtonText
  41. secondaryActionButtonText:defaultSecondaryActionButtonText
  42. actionURL:[NSURL URLWithString:defaultActionURL]
  43. secondaryActionURL:[NSURL URLWithString:defaultSecondaryActionURL]
  44. imageURL:[NSURL URLWithString:defaultImageURL]
  45. landscapeImageURL:[NSURL URLWithString:defaultLandscapeImageURL]
  46. usingURLSession:_mockedNSURLSession];
  47. }
  48. - (void)tearDown {
  49. [super tearDown];
  50. }
  51. - (void)testReadingTitleAndBodyBackCorrectly {
  52. XCTAssertEqualObjects(defaultTitle, self.defaultContentDataWithImageURL.titleText);
  53. XCTAssertEqualObjects(defaultBody, self.defaultContentDataWithImageURL.bodyText);
  54. }
  55. - (void)testReadingActionButtonTextCorrectly {
  56. XCTAssertEqualObjects(defaultActionButtonText,
  57. self.defaultContentDataWithImageURL.actionButtonText);
  58. }
  59. - (void)testURLRequestUsingCorrectImageURLWithOnlyPortrait {
  60. __block NSURLRequest *capturedNSURLRequest;
  61. OCMStub([self.mockedNSURLSession
  62. dataTaskWithRequest:[OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  63. capturedNSURLRequest = request;
  64. return YES;
  65. }]
  66. completionHandler:[OCMArg any] // second parameter is the callback which we don't care in
  67. // this unit testing
  68. ]);
  69. FIRIAMMessageContentDataWithImageURL *portraitOnlyContentData =
  70. [[FIRIAMMessageContentDataWithImageURL alloc]
  71. initWithMessageTitle:defaultTitle
  72. messageBody:defaultBody
  73. actionButtonText:defaultActionButtonText
  74. secondaryActionButtonText:defaultSecondaryActionButtonText
  75. actionURL:[NSURL URLWithString:defaultActionURL]
  76. secondaryActionURL:[NSURL URLWithString:defaultSecondaryActionURL]
  77. imageURL:[NSURL URLWithString:defaultImageURL]
  78. landscapeImageURL:nil
  79. usingURLSession:_mockedNSURLSession];
  80. [portraitOnlyContentData
  81. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSData *_Nullable landscapeImageData,
  82. NSError *error){
  83. }];
  84. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  85. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  86. completionHandler:[OCMArg any]]);
  87. XCTAssertEqualObjects([capturedNSURLRequest URL].absoluteString, defaultImageURL);
  88. }
  89. - (void)testURLRequestUsingCorrectImageURLs {
  90. __block NSURLRequest *capturedNSURLRequestForPortraitImage;
  91. __block NSURLRequest *capturedNSURLRequestForLandscapeImage;
  92. OCMStub([self.mockedNSURLSession
  93. dataTaskWithRequest:[OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  94. if ([request.URL.absoluteString isEqualToString:defaultImageURL]) {
  95. capturedNSURLRequestForPortraitImage = request;
  96. } else if ([request.URL.absoluteString isEqualToString:defaultLandscapeImageURL]) {
  97. capturedNSURLRequestForLandscapeImage = request;
  98. }
  99. return YES;
  100. }]
  101. completionHandler:[OCMArg any] // second parameter is the callback which we don't care in
  102. // this unit testing
  103. ]);
  104. [_defaultContentDataWithImageURL
  105. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSData *_Nullable landscapeImageData,
  106. NSError *error){
  107. }];
  108. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  109. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  110. completionHandler:[OCMArg any]]);
  111. XCTAssertNotNil(capturedNSURLRequestForPortraitImage);
  112. XCTAssertNotNil(capturedNSURLRequestForLandscapeImage);
  113. }
  114. - (void)testReportErrorOnNonSuccessHTTPStatusCode {
  115. // Used to capture both portrait and landscape callbacks.
  116. NSMutableArray *completionHandlers = [NSMutableArray array];
  117. OCMStub([self.mockedNSURLSession
  118. dataTaskWithRequest:[OCMArg any]
  119. completionHandler:[OCMArg checkWithBlock:^BOOL(void (^capturedCompletionHandler)(
  120. NSData *data, NSURLResponse *response, NSError *error)) {
  121. [completionHandlers addObject:capturedCompletionHandler];
  122. return YES;
  123. }]]);
  124. XCTestExpectation *expectation =
  125. [self expectationWithDescription:@"image load callback triggered."];
  126. [_defaultContentDataWithImageURL
  127. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSData *_Nullable landscapeImageData,
  128. NSError *error) {
  129. XCTAssertNil(imageData);
  130. XCTAssertNotNil(error); // we should report error due to the unsuccessful http status code
  131. [expectation fulfill];
  132. }];
  133. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  134. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  135. completionHandler:[OCMArg any]]);
  136. // by this time we should have capturedCompletionHandler being the callback block for the
  137. // NSURLSessionDataTask, now supply it with invalid http status code to see how the block from
  138. // loadImageDataWithBlock: would react to it.
  139. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  140. NSHTTPURLResponse *unsuccessfulHTTPResponse = [[NSHTTPURLResponse alloc] initWithURL:url
  141. statusCode:404
  142. HTTPVersion:nil
  143. headerFields:nil];
  144. for (void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error)
  145. in completionHandlers) {
  146. capturedCompletionHandler(nil, unsuccessfulHTTPResponse, nil);
  147. }
  148. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  149. }
  150. - (void)testReportErrorOnGeneralNSErrorFromNSURLSession {
  151. NSError *customError = [[NSError alloc] initWithDomain:@"Error Domain" code:100 userInfo:nil];
  152. // Used to capture both portrait and landscape callbacks.
  153. NSMutableArray *completionHandlers = [NSMutableArray array];
  154. OCMStub([self.mockedNSURLSession
  155. dataTaskWithRequest:[OCMArg any]
  156. completionHandler:[OCMArg checkWithBlock:^BOOL(void (^capturedCompletionHandler)(
  157. NSData *data, NSURLResponse *response, NSError *error)) {
  158. [completionHandlers addObject:capturedCompletionHandler];
  159. return YES;
  160. }]]);
  161. XCTestExpectation *expectation =
  162. [self expectationWithDescription:@"image load callback triggered."];
  163. [_defaultContentDataWithImageURL
  164. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSData *_Nullable landscapeImageData,
  165. NSError *error) {
  166. XCTAssertNil(imageData);
  167. XCTAssertNotNil(error); // we should report error due to the unsuccessful http status code
  168. XCTAssertEqualObjects(error, customError);
  169. [expectation fulfill];
  170. }];
  171. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  172. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  173. completionHandler:[OCMArg any]]);
  174. for (void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error)
  175. in completionHandlers) {
  176. capturedCompletionHandler(nil, nil, customError);
  177. }
  178. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  179. }
  180. - (void)testReportErrorOnNonImageContentTypeResponse {
  181. // Used to capture both portrait and landscape callbacks.
  182. NSMutableArray *completionHandlers = [NSMutableArray array];
  183. OCMStub([self.mockedNSURLSession
  184. dataTaskWithRequest:[OCMArg any]
  185. completionHandler:[OCMArg checkWithBlock:^BOOL(void (^capturedCompletionHandler)(
  186. NSData *data, NSURLResponse *response, NSError *error)) {
  187. [completionHandlers addObject:capturedCompletionHandler];
  188. return YES;
  189. }]]);
  190. XCTestExpectation *expectation =
  191. [self expectationWithDescription:@"image load callback triggered."];
  192. [_defaultContentDataWithImageURL
  193. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSData *_Nullable landscapeImageData,
  194. NSError *error) {
  195. XCTAssertNil(imageData);
  196. // We should report error due to the HTTP response content type being invalid.
  197. XCTAssertNotNil(error);
  198. [expectation fulfill];
  199. }];
  200. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  201. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  202. completionHandler:[OCMArg any]]);
  203. // by this time we should have capturedCompletionHandler being the callback block for the
  204. // NSURLSessionDataTask, now feed it with a non-image http response to see how the block from
  205. // loadImageDataWithBlock: would react to it.
  206. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  207. NSHTTPURLResponse *nonImageContentTypeHTTPResponse =
  208. [[NSHTTPURLResponse alloc] initWithURL:url
  209. statusCode:200
  210. HTTPVersion:nil
  211. headerFields:@{@"Content-Type" : @"non-image/jpeg"}];
  212. for (void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error)
  213. in completionHandlers) {
  214. capturedCompletionHandler(nil, nonImageContentTypeHTTPResponse, nil);
  215. }
  216. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  217. }
  218. - (void)testGettingBothImagesSuccessfully {
  219. NSData *portraitImageData = [@"test portrait image data" dataUsingEncoding:NSUTF8StringEncoding];
  220. NSData *landscapeImageData =
  221. [@"test landscape image data" dataUsingEncoding:NSUTF8StringEncoding];
  222. // Used to capture both portrait and landscape callbacks.
  223. NSMutableArray *completionHandlers = [NSMutableArray array];
  224. OCMStub([self.mockedNSURLSession
  225. dataTaskWithRequest:[OCMArg any]
  226. completionHandler:[OCMArg checkWithBlock:^BOOL(void (^capturedCompletionHandler)(
  227. NSData *data, NSURLResponse *response, NSError *error)) {
  228. [completionHandlers addObject:capturedCompletionHandler];
  229. return YES;
  230. }]]);
  231. XCTestExpectation *expectation =
  232. [self expectationWithDescription:@"image load callback triggered."];
  233. [_defaultContentDataWithImageURL loadImageDataWithBlock:^(NSData *_Nullable imageData,
  234. NSData *_Nullable landscapeImageData,
  235. NSError *error) {
  236. XCTAssertNil(error); // no error is reported
  237. NSString *fetchedPortraitImageDataString = [[NSString alloc] initWithData:imageData
  238. encoding:NSUTF8StringEncoding];
  239. NSString *fetchedLandscapeImageDataString =
  240. [[NSString alloc] initWithData:landscapeImageData encoding:NSUTF8StringEncoding];
  241. XCTAssertEqualObjects(@"test portrait image data", fetchedPortraitImageDataString);
  242. XCTAssertEqualObjects(@"test landscape image data", fetchedLandscapeImageDataString);
  243. [expectation fulfill];
  244. }];
  245. // Verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object.
  246. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  247. completionHandler:[OCMArg any]]);
  248. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  249. NSHTTPURLResponse *successfulHTTPResponse =
  250. [[NSHTTPURLResponse alloc] initWithURL:url
  251. statusCode:200
  252. HTTPVersion:nil
  253. headerFields:@{@"Content-Type" : @"image/jpeg"}];
  254. // By this time we should have capturedCompletionHandler being the callback block for the
  255. // NSURLSessionDataTask, now feed it with image data to see how the block from
  256. // loadImageDataWithBlock: would react to it.
  257. for (int i = 0; i < completionHandlers.count; i++) {
  258. void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error) =
  259. completionHandlers[i];
  260. if (i == 0) {
  261. capturedCompletionHandler(portraitImageData, successfulHTTPResponse, nil);
  262. } else {
  263. capturedCompletionHandler(landscapeImageData, successfulHTTPResponse, nil);
  264. }
  265. }
  266. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  267. }
  268. - (void)testOnlyPortraitImageLoads {
  269. NSError *customError = [[NSError alloc] initWithDomain:@"Error Domain" code:100 userInfo:nil];
  270. NSData *portraitImageData = [@"test portrait image data" dataUsingEncoding:NSUTF8StringEncoding];
  271. // Used to capture both portrait and landscape callbacks.
  272. NSMutableArray *completionHandlers = [NSMutableArray array];
  273. OCMStub([self.mockedNSURLSession
  274. dataTaskWithRequest:[OCMArg any]
  275. completionHandler:[OCMArg checkWithBlock:^BOOL(void (^capturedCompletionHandler)(
  276. NSData *data, NSURLResponse *response, NSError *error)) {
  277. [completionHandlers addObject:capturedCompletionHandler];
  278. return YES;
  279. }]]);
  280. XCTestExpectation *expectation =
  281. [self expectationWithDescription:@"image load callback triggered."];
  282. [_defaultContentDataWithImageURL loadImageDataWithBlock:^(NSData *_Nullable imageData,
  283. NSData *_Nullable landscapeImageData,
  284. NSError *error) {
  285. XCTAssertNil(error); // no error is reported
  286. NSString *fetchedPortraitImageDataString = [[NSString alloc] initWithData:imageData
  287. encoding:NSUTF8StringEncoding];
  288. XCTAssertEqualObjects(@"test portrait image data", fetchedPortraitImageDataString);
  289. XCTAssertNil(landscapeImageData);
  290. [expectation fulfill];
  291. }];
  292. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  293. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  294. completionHandler:[OCMArg any]]);
  295. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  296. NSHTTPURLResponse *successfulHTTPResponse =
  297. [[NSHTTPURLResponse alloc] initWithURL:url
  298. statusCode:200
  299. HTTPVersion:nil
  300. headerFields:@{@"Content-Type" : @"image/jpeg"}];
  301. // by this time we should have capturedCompletionHandler being the callback block for the
  302. // NSURLSessionDataTask, now feed it with image data to see how the block from
  303. // loadImageDataWithBlock: would react to it.
  304. for (int i = 0; i < completionHandlers.count; i++) {
  305. void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error) =
  306. completionHandlers[i];
  307. if (i == 0) {
  308. capturedCompletionHandler(portraitImageData, successfulHTTPResponse, nil);
  309. } else {
  310. capturedCompletionHandler(nil, nil, customError);
  311. }
  312. }
  313. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  314. }
  315. - (void)testOnlyLandscapeLoads {
  316. NSError *customError = [[NSError alloc] initWithDomain:@"Error Domain" code:100 userInfo:nil];
  317. NSData *landscapeImageData =
  318. [@"test landscape image data" dataUsingEncoding:NSUTF8StringEncoding];
  319. // Used to capture both portrait and landscape callbacks.
  320. NSMutableArray *completionHandlers = [NSMutableArray array];
  321. OCMStub([self.mockedNSURLSession
  322. dataTaskWithRequest:[OCMArg any]
  323. completionHandler:[OCMArg checkWithBlock:^BOOL(void (^capturedCompletionHandler)(
  324. NSData *data, NSURLResponse *response, NSError *error)) {
  325. [completionHandlers addObject:capturedCompletionHandler];
  326. return YES;
  327. }]]);
  328. XCTestExpectation *expectation =
  329. [self expectationWithDescription:@"image load callback triggered."];
  330. [_defaultContentDataWithImageURL
  331. loadImageDataWithBlock:^(NSData *_Nullable imageData, NSData *_Nullable landscapeImageData,
  332. NSError *error) {
  333. XCTAssertNotNil(error); // Error is reported, no image data is valid.
  334. XCTAssertNil(imageData);
  335. XCTAssertNil(landscapeImageData);
  336. [expectation fulfill];
  337. }];
  338. // verify that the dataTaskWithRequest:completionHandler: is triggered for NSURLSession object
  339. OCMVerify([self.mockedNSURLSession dataTaskWithRequest:[OCMArg any]
  340. completionHandler:[OCMArg any]]);
  341. NSURL *url = [[NSURL alloc] initWithString:defaultImageURL];
  342. NSHTTPURLResponse *successfulHTTPResponse =
  343. [[NSHTTPURLResponse alloc] initWithURL:url
  344. statusCode:200
  345. HTTPVersion:nil
  346. headerFields:@{@"Content-Type" : @"image/jpeg"}];
  347. // by this time we should have capturedCompletionHandler being the callback block for the
  348. // NSURLSessionDataTask, now feed it with image data to see how the block from
  349. // loadImageDataWithBlock: would react to it.
  350. for (int i = 0; i < completionHandlers.count; i++) {
  351. void (^capturedCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error) =
  352. completionHandlers[i];
  353. if (i == 0) {
  354. capturedCompletionHandler(nil, nil, customError);
  355. } else {
  356. capturedCompletionHandler(landscapeImageData, successfulHTTPResponse, customError);
  357. }
  358. }
  359. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  360. }
  361. @end