From ad0053c40af384867b87acd3791aea4157555b8f Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Sat, 1 Jun 2013 11:55:48 +1000 Subject: [PATCH 1/4] Fix typos in AFImageRequest header file --- AFNetworking/AFImageRequestOperation.h | 4 ++-- AFNetworking/AFImageRequestOperation.m | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/AFNetworking/AFImageRequestOperation.h b/AFNetworking/AFImageRequestOperation.h index 68b28ed..f0bee4b 100644 --- a/AFNetworking/AFImageRequestOperation.h +++ b/AFNetworking/AFImageRequestOperation.h @@ -32,7 +32,7 @@ #endif /** - `AFImageRequestOperation` is a subclass of `AFHTTPRequestOperation` for downloading an processing images. + `AFImageRequestOperation` is a subclass of `AFHTTPRequestOperation` for downloading and processing images. ## Acceptable Content Types @@ -71,7 +71,7 @@ Creates and returns an `AFImageRequestOperation` object and sets the specified success callback. @param urlRequest The request object to be loaded asynchronously during execution of the operation. - @param success A block object to be executed when the request finishes successfully. This block has no return value and takes a single arguments, the image created from the response data of the request. + @param success A block object to be executed when the request finishes successfully. This block has no return value and takes a single argument, the image created from the response data of the request. @return A new image request operation */ diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index d4119d2..2cb4d82 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -230,7 +230,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { }); } } - }); + }); }; #pragma clang diagnostic pop } From 877a5a46fd44582017832836b43bc62b37fc8a98 Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Sat, 1 Jun 2013 13:34:06 +1000 Subject: [PATCH 2/4] Add initial tests for AFImageRequestOperation --- Tests/AFImageRequestOperationTests.m | 59 +++++++++++++++++++ .../project.pbxproj | 4 ++ 2 files changed, 63 insertions(+) create mode 100644 Tests/AFImageRequestOperationTests.m diff --git a/Tests/AFImageRequestOperationTests.m b/Tests/AFImageRequestOperationTests.m new file mode 100644 index 0000000..459cf7f --- /dev/null +++ b/Tests/AFImageRequestOperationTests.m @@ -0,0 +1,59 @@ +// AFHTTPRequestOperationTests.m +// +// Copyright (c) 2013 AFNetworking (http://afnetworking.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "AFNetworkingTests.h" + +@interface AFImageRequestOperationTests : SenTestCase +@property (readwrite, nonatomic, strong) NSURL *baseURL; +@end + +@implementation AFImageRequestOperationTests +@synthesize baseURL = _baseURL; + +- (void)setUp { + self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString]; +} + +#pragma mark - + +- (void)testThatImageRequestOperationAcceptsCorrectFormatTypes { + NSArray *acceptedFormats = @[@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap"]; + for (NSString *format in acceptedFormats) { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"/response-headers?Content-Type=%@", format] relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); + } +} + +- (void)testThatImageRequestOperationDoesNotAcceptInvalidFormatTypes { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/badFormat" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).willNot.beNil(); +} + +@end diff --git a/Tests/AFNetworking Tests.xcodeproj/project.pbxproj b/Tests/AFNetworking Tests.xcodeproj/project.pbxproj index 38a2293..f92b3c6 100644 --- a/Tests/AFNetworking Tests.xcodeproj/project.pbxproj +++ b/Tests/AFNetworking Tests.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 25C4EC42173D86B60083E116 /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 25C4EC30173D7DCA0083E116 /* MobileCoreServices.framework */; }; 29A9CE2117456336002360C8 /* AFJSONRequestOperationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29A9CE2017456336002360C8 /* AFJSONRequestOperationTests.m */; }; 29A9CE2217456336002360C8 /* AFJSONRequestOperationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29A9CE2017456336002360C8 /* AFJSONRequestOperationTests.m */; }; + 667B268117599C5800764906 /* AFImageRequestOperationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 667B268017599C5800764906 /* AFImageRequestOperationTests.m */; }; A7DC62A617592E4200EBEC2F /* AFTestURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */; }; A7DC62A717592E4200EBEC2F /* AFTestURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */; }; A7DC62A917592E4800EBEC2F /* AFURLConnectionOperationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A817592E4800EBEC2F /* AFURLConnectionOperationTests.m */; }; @@ -77,6 +78,7 @@ 29A9CE2017456336002360C8 /* AFJSONRequestOperationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFJSONRequestOperationTests.m; sourceTree = ""; }; 2B6D24F8E1B74E10A269E8B3 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; }; 55E73C267F33406A9F92476C /* libPods-ios.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-ios.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 667B268017599C5800764906 /* AFImageRequestOperationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFImageRequestOperationTests.m; sourceTree = ""; }; 96A923755B00464187DEDBAF /* libPods-osx.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-osx.a"; sourceTree = BUILT_PRODUCTS_DIR; }; A7DC62A417592E4200EBEC2F /* AFTestURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFTestURLProtocol.h; sourceTree = ""; }; A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFTestURLProtocol.m; sourceTree = ""; }; @@ -196,6 +198,7 @@ isa = PBXGroup; children = ( 2580153E173EB3A70026AA6E /* AFNetworkingTests.h */, + 667B268017599C5800764906 /* AFImageRequestOperationTests.m */, 2580153F173EB3A70026AA6E /* AFNetworkingTests.m */, A7DC62A417592E4200EBEC2F /* AFTestURLProtocol.h */, A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */, @@ -340,6 +343,7 @@ 29A9CE2117456336002360C8 /* AFJSONRequestOperationTests.m in Sources */, A7DC62A617592E4200EBEC2F /* AFTestURLProtocol.m in Sources */, A7DC62A917592E4800EBEC2F /* AFURLConnectionOperationTests.m in Sources */, + 667B268117599C5800764906 /* AFImageRequestOperationTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; From 648b4fb77fcf4e876fe6a9182365dba941989b35 Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Sat, 1 Jun 2013 20:49:07 +1000 Subject: [PATCH 3/4] Test responseImage on success and failure --- Tests/AFImageRequestOperationTests.m | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Tests/AFImageRequestOperationTests.m b/Tests/AFImageRequestOperationTests.m index 459cf7f..ba0c6a4 100644 --- a/Tests/AFImageRequestOperationTests.m +++ b/Tests/AFImageRequestOperationTests.m @@ -56,4 +56,22 @@ expect(operation.error).willNot.beNil(); } +- (void)testThatImageResponseIsNotNilWhenRequestSucceeds { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/png" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.responseImage).willNot.beNil(); +} + +- (void)testThatImageResponseIsNilWhenRequestFails { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.responseImage).will.beNil(); +} + @end From 3ecc2afc23d699387ae2784bb729a7a762bb28b9 Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Sat, 1 Jun 2013 20:50:03 +1000 Subject: [PATCH 4/4] Test image processing block --- Tests/AFImageRequestOperationTests.m | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Tests/AFImageRequestOperationTests.m b/Tests/AFImageRequestOperationTests.m index ba0c6a4..52a3649 100644 --- a/Tests/AFImageRequestOperationTests.m +++ b/Tests/AFImageRequestOperationTests.m @@ -74,4 +74,38 @@ expect(operation.responseImage).will.beNil(); } +- (void)testImageProcessingBlockIsRunOnSuccess { + __block UIImage *blockImage = nil; + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/png" relativeToURL:self.baseURL]]; + + AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) { + blockImage = [[UIImage alloc] init]; + return blockImage; + } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { + } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { + }]; + + [operation start]; + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); + expect(blockImage).willNot.beNil(); +} + +- (void)testImageProcessingBlockIsNotRunOnFailure { + __block UIImage *blockImage = nil; + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]]; + + AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) { + blockImage = [[UIImage alloc] init]; + return blockImage; + } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { + } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { + }]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).willNot.beNil(); + expect(blockImage).will.beNil(); +} + @end